Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get python script return value in Makefile?

Can somebody help me to find a way to capture the return value from a python script in Makefile.I have a scenario where the source code is getting build through the Makefile and it should throw a build error when python script reruns a failure.

myMakefile.mak

python mySample.py <arg1> <arg2> // Invoking python script here, How to get script's return value?

mySample.py

SUCCESS 0
FAIL 1

if(true)
  exit(SUCCESS)
else:
  exit(FAIL)

Thanks in advance.

like image 477
Joe Avatar asked Jul 09 '26 22:07

Joe


1 Answers

If your python script exits with a non-zero code, make will detect this and tell you that an error occurred. So you don't need to do anything more.

Example, if I have a program foo.py that does this:

print 'running foo'
exit(1)

And a Makefile like this:

foo:
    python foo.py

And I run make foo, I get the output:

python foo.py

running foo

make: *** [foo] Error 1

So it detected the error.


If on the other hand, what you want to do is record the return value of a command in a Makefile, you can use the $? variable, which stores the last return value of a command.

A couple things to note:

  • the dollar sign has to be escaped in a Makefile, so it becomes $$? in the command
  • since each command in a Makefile is run in a separate shell, you must capture the output in the same subshell as the command you're running (i.e. make the output capture part of the same line as the command you care about)

Example:

build:
    python mySample.py ; echo $$? > output

After running make, you will have a file named output that contains the return value of mySample.py.

like image 164
xgord Avatar answered Jul 11 '26 13:07

xgord



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!