1

I keep running into an error when I want to compile code written in C++ using Python script to run "make" in x directory. Compiling the code takes about few seconds so I am using time module to have the script sleep for 60 seconds to finish compiling the code.

Here is the code:

from subprocess import call
from time import sleep
def make_ut_adsmain():
        os.system("make ../../ads/main/unittest")
        # call(["ls", "-l"])
        sleep(60)

make_ut_adsmain()

# Run other functions when compiled...

Error

make: *** No rule to make target `../../ads/main/unittest'.  Stop.

The code never really compiles because the rest of the code depends on the log file which never gets created. Please guide me.

2
  • Does running make manually in that directory work? This doesn't seem indicative of python not being able to find the Makefile Commented Oct 4, 2016 at 18:20
  • yup it does. I just able to resolve it. Posted the solution below. Commented Oct 4, 2016 at 18:21

1 Answer 1

3

I actually figured it out and able to run the build process by using the following code:

import subprocess
from time import sleep
def make_ut_adsmain():
        subprocess.Popen(["make"], stdout=subprocess.PIPE, cwd="../../ads/main/unitest")
        sleep(60)

make_ut_adsmain()

Note, the previous solution works only if you need to bypass errors. The correct way to do is the following. Check @tdelaney comment and read more on python docs https://docs.python.org/2/library/subprocess.html

import subprocess
from time import sleep
def make_ut_adsmain():
        subprocess.check_all(["make"], cwd="../../ads/main/unitest")

make_ut_adsmain()
Sign up to request clarification or add additional context in comments.

5 Comments

This has major problems. You fix one problem by changing cwd but you make stdout a pipe but then don't read it (make will deadlock if it prints a bunch of data) and then you just sleep an arbitrary 60 seconds instead of waiting for the call to finish. As a minimum, do subprocess.check_call(["make"], cwd="../../ads/main/unitest")) and get rid of the sleep completely.
Thanks @tdelaney : I read the docs and it makes sense. But your minimum solution gives me error: subprocess.CalledProcessError: Command '['make']' returned non-zero exit status 2.
That's great! It means that the make failed and you need to figure that out. Your original script silently ignored errors.
@tdelaney I now get it. But for my testing purpose on my dev machine, I still need to stick to my original solution because there is an error I need to ignore and execute running the compiled binary file. Thanks for the advice! (Also, it'd be great if you can remove downvote) as this doesn't apply to what I am looking for. Thanks.
I think you also want to remove the sleep... then it looks good and I'll remove the downvote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.