Calling GNU/make from a Python Script

E

Efrat Regev

Hello,

I need to call GNU/make from within a Python script. This raised some
problems:
1. The script is not in the directory of the makefile, and changing the
locations of either is not an option. Consequently, the makefile fails,
since it can't find the targets/dependencies.
2. After searching around, it seems that os.system(..) should be avoided
if there's an alternative. Is there one in this case?

Many Thanks!

Efrat
 
R

Rob Williscroft

Efrat Regev wrote in in comp.lang.python:
Hello,

I need to call GNU/make from within a Python script. This raised some
problems:
1. The script is not in the directory of the makefile, and changing the
locations of either is not an option. Consequently, the makefile fails,
since it can't find the targets/dependencies.
2. After searching around, it seems that os.system(..) should be avoided
if there's an alternative. Is there one in this case?

USe the subprocess module, in particular see

Popen: http://docs.python.org/lib/node529.html

and call: http://docs.python.org/lib/node530.html

you will need to use the "cwd" argument.

Rob.
 
F

Fredrik Lundh

Efrat said:
1. The script is not in the directory of the makefile, and changing the
locations of either is not an option. Consequently, the makefile fails,
since it can't find the targets/dependencies.

build_dir = "path/to/makefile"

cwd = os.getcwd() # get current directory
try:
os.chdir(build_dir)
os.system("make")
finally:
os.chdir(cwd)
2. After searching around, it seems that os.system(..) should be avoided
if there's an alternative. Is there one in this case?

"make" is not an operating system service, no.

</F>
 
S

skip

Fredrik> build_dir = "path/to/makefile"

Fredrik> cwd = os.getcwd() # get current directory
Fredrik> try:
Fredrik> os.chdir(build_dir)
Fredrik> os.system("make")
Fredrik> finally:
Fredrik> os.chdir(cwd)

Or even:

os.system("make -C %s" % build_dir)

Skip
 
S

sjdevnull

Fredrik> build_dir = "path/to/makefile"

Fredrik> cwd = os.getcwd() # get current directory
Fredrik> try:
Fredrik> os.chdir(build_dir)
Fredrik> os.system("make")
Fredrik> finally:
Fredrik> os.chdir(cwd)

Or even:

os.system("make -C %s" % build_dir)

OP specified GNU make, so that works fine, but make sure you're not
going to need to use it with another make before settling on that
alternative. Frederik's works with more versions of make.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top