liuzw
liuzw

Reputation: 195

Python call makefile compiling and capture make's output

A job in Jenkins calls my python script, in which I want to call make to compile my UT codes, and raise sys.exit(1) if a compiling error like "make: *** [ ] Error 1" occurs.

I also need to print output in real time.

Here's my python script:

make_process = subprocess.Popen("make clean all;", shell=True, stdout=subprocess.PIPE, stderr=sys.stdout.fileno())
    while True:
        line = make_process.stdout.readline()
        if not line:break
        print line, #output to console in time
        sys.stdout.flush()

But how do I capture the make error and let this python script fail?

Note:

Update: It turned out to be a makefile issue. See the comments on the accepted answer. But for this python question, pentadecagon gave the right answer.

Upvotes: 6

Views: 18842

Answers (1)

pentadecagon
pentadecagon

Reputation: 4847

You can check the return value of the make process by

make_process.poll()

This returns "None" if the process is still running, or the error code if it's finished. If you just want the output to end up on the console there is no need to do this manually The output goes to the console anyway, and can do it like this:

make_process = subprocess.Popen("make clean all", stderr=subprocess.STDOUT)
if make_process.wait() != 0:
     something_went_wrong();

Upvotes: 5

Related Questions