Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • make_process.wait() is 0 when make error happens.
  • this answer doesn't work for me: Running shell command from Python and capturing the output

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.

like image 618
liuzw Avatar asked Oct 02 '22 09:10

liuzw


1 Answers

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();
like image 130
pentadecagon Avatar answered Oct 13 '22 11:10

pentadecagon