BASH CODE:
source /proj/common/tools/repo/etc/profile.d/repo.sh
repo project init $branch
repo project sync
source poky/fnc-init-build-env build
bitbake -g $image
I converted this bash code into python(version 2.7). While executing my python code, I am getting repo command not found message.
PYTHON CODE:
os.system("source /proj/common/tools/repo/etc/profile.d/repo.sh")
os.system("repo project init " + branch)
os.system("repo project sync")
os.system("source poky/fnc-init-build-env build")
os.chdir("poky/build")
os.system("bitbake -g " + image)
ERROR MESSAGE:
sh: repo: command not found
sh: repo: command not found
I tried with subprocess.call(), I am getting the same error message.
2 Answers 2
The problem is in this call:
os.system("source /proj/common/tools/repo/etc/profile.d/repo.sh")
The problem is that it runs source in a separate subshell and when the subshell exits all changes to the environment (cd commands if there are any and environment variables, most notable PATH) are gone.
My advice is to continue using the shell script you've used — just call it from Python with one os.system() call. Inside the shell script you can use source.
3 Comments
PATH that you've sent before starting Python.You could at least use the full path for the repo command.
That would avoid the subshell to have to know where the repo command is (meaning have the right PATH set on each os.system call).
7 Comments
which repo: that will give you the full path to use.which equivalent done in python would be: stackoverflow.com/a/15133367/6309
os.systemcall invokes a unique shell. Theos.system("source ...command had no effect on the next shell used withos.system(" repo .... Generally, you won't have luck here unless you reimplementrepo.shalso.os.systemwith the original source code, unless absolutely necessary. As in, no library exists to do this in Python, and writing one would be prohibitively expensive.