1

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.

phd
  • 82,685
  • 13
  • 120
  • 165
sabarish s
  • 49
  • 1
  • 6
  • Each `os.system` call invokes a unique shell. The `os.system("source ...` command had no effect on the next shell used with `os.system(" repo ...`. Generally, you won't have luck here unless you reimplement `repo.sh` also. – tdelaney Apr 24 '18 at 04:37
  • Aside from the subshell problem, what is the point of this? If it's an exercise I'm afraid you have misunderstood. Converting to a Python script would involve zero invocations of `os.system` with the original source code, unless absolutely necessary. As in, no library exists to do this in Python, and writing one would be prohibitively expensive. – l0b0 Apr 24 '18 at 04:47
  • i tried with subprocess. subprocess.call(["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], shell=True). It shows same error message – sabarish s Apr 24 '18 at 05:00

2 Answers2

1

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.

phd
  • 82,685
  • 13
  • 120
  • 165
  • When i execute the same command in python interactive mode, sometimes it is working. Sometimes it is not working. I don't know why. – sabarish s Apr 24 '18 at 05:02
  • Depends on the value of `PATH` that you've sent before starting Python. – phd Apr 24 '18 at 05:03
  • when i execute "source poky/fnc-init-build-env build" in bash , directory will changed inside poky build (.i.e./data/users/dependencies/Lseries/poky/build). But when i execute in python,directory is not going inside poky/build. – sabarish s May 08 '18 at 06:20
0

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).

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250