I have a python script which prints some strings and updates it's execution progress in console:
if __name__ == '__main__':
    ...
    print 'Hello, world!'
    while page <= pages:
        ...
        done = float(page) / pages
        sys.stdout.write('\r[{0:50s}] {1:.2f}%'.format('#' * int(done * 50), done * 100))
        page += 1
    print ''
When I run it from console like python script.py everything is ok and I can see output and progressbar. I need to run this script as a part of Gradle build, so, I've created a task:
task process(type: Exec) {
    workingDir file('src/main/python')
    commandLine 'python', 'process.py', ...
}
Now, when I use gradle process to execute the script I see no output in console, the last line that is printed is > Building 0% > :process
I've tried to use Java 7's ProcessBuilder with no luck too:
task process << {
    def processBuilder = new ProcessBuilder([
            'python',
            'process.py',
            ...
    ]).directory(file('src/main/python'))
            .redirectInput(ProcessBuilder.Redirect.INHERIT)
            .redirectOutput(ProcessBuilder.Redirect.INHERIT)
            .redirectError(ProcessBuilder.Redirect.INHERIT).start().waitFor()
}
I'm stuck. I really want to see python's output in the same console. How can I achieve it?
UPD: sometimes it somehow prints gibberish: 
I worked around this issue by flushing the system output in the python script. it's not the ideal solution, but it gets the job done.
So I have the following in my python script
import sys
import time
def flush_out(string):
    print(string)
    sys.stdout.flush()
#script does something
flush_out("Waiting for 10 seconds...")
time.sleep(10)
flush_out("Exiting")
sys.exit(0)
                        The only solution for me was to redirect output to the file:
  .redirectErrorStream(true)
  .redirectOutput(ProcessBuilder.Redirect.to(new File(project.buildDir, "my-task.log")))
                        Even though the question is old, this may be relevant to others. My solution is to explicitely start a shell with "-c" parameter to let the shell process the parameters. This even allows to redirect STDERR and STDOUT.
Here is an example:
task(createVersionFile, type: Exec) {
    commandLine 'sh', '-c', "echo ${version} > ${htmlDir}/version.txt"
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With