6

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.

Community
  • 1
  • 1
liuzw
  • 195
  • 1
  • 1
  • 8

1 Answers1

5

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();
pentadecagon
  • 4,717
  • 2
  • 18
  • 26
  • The "wait" codes won't work for me. same with poll. make_process.poll() is 0 when make error hapeens. – liuzw Nov 21 '13 at 09:09
  • This is GNU make, right? Check the return value on the command line. If it's 0 there as well then something is fishy with your Makefile. Otherwise ... maybe try removing that semicolon in your script? – pentadecagon Nov 21 '13 at 10:08
  • yes, GNU make. it still doesn't work after removing that semicolon. How can i check the return value on the command line? Thanks – liuzw Nov 22 '13 at 05:37
  • You run `make` on the command line and then on Linux: `echo $?` On Windows: `echo %$ERRORLEVEL%`. It's supposed to be 2. – pentadecagon Nov 22 '13 at 11:02
  • I'm on Linux. yes, it's indeed 0 instead of 1 or 2. I think the makefile return 1 because of the print "make: *** [xxx.so] Error 1" and if I let it return 2 by "gcc -g -pg -Werror -fprofile-arcs -ftest-coverage -m32 -fPIC -shared -Wl,--no-undefined -o $@ $(D_CMPFLAG) $(INCLUDE) $(LIBS) || exit 2", it will be "make: *** [xxx.so] Error 2", but echo $? is still 0. Do you know why? Thanks – liuzw Nov 26 '13 at 03:44
  • Maybe there is ".IGNORE" within that Makefile? Or your command line has "-k" in it? – pentadecagon Nov 26 '13 at 08:51
  • I didn't find IGNORE in Makefiles. I don't use -k. It's a big project. But I already understand what am I going to do. You're right about my python question. so I put it as the answer. And I will make a note in the questions that there is a problem in my makefile. thanks – liuzw Nov 27 '13 at 08:24