“来源"使用shell = True将PATH设置为bitbake在Python中无效 [英] "source" to set PATH to bitbake with shell=True having no effect in Python

查看:239
本文介绍了“来源"使用shell = True将PATH设置为bitbake在Python中无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是shell脚本中的代码

Below is the code in shell script

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脚本转换为python脚本

I am trying to convert shell script into python script

a = subprocess.call("source /proj/common/tools/repo/etc/profile.d/repo.sh", shell=True)
b = subprocess.call("repo project init " + branch, shell=True)
b2 = subprocess.call("repo project sync", shell=True)
c = subprocess.call("source poky/fnc-init-build-env build", shell=True)
os.chdir("poky/build")
d = subprocess.call("bitbake -g " + image, shell=True)

但是我遇到了以下错误

/bin/sh: bitbake: command not found

如何在python中解决此问题?

How to resolve this in python ?

推荐答案

使用shell=True时,第一个列表元素是要运行的脚本,随后的参数将传递给该脚本.

When using shell=True, the first list element is a script to run, and subsequent arguments are passed to that script.

每次subprocess.Popen()调用启动单个shell ;一个状态中配置的状态不会传递给其他状态,因此source命令是​​无用的,除非您在同一调用中运行依赖于该状态的命令.

Each subprocess.Popen() invocation starts a single shell; state configured in one isn't carried through to others, so the source command is useless unless you run the commands that depend on it within the same invocation.

script='''
branch=$1; shift  # pop first argument off the list, assign to variable named branch
source /proj/common/tools/repo/etc/profile.d/repo.sh || exit
repo project init "$branch" || exit
repo project sync || exit
source poky/fnc-init-build-env build || exit
exec "$@"  # use remaining arguments to form our command to run
'''

subprocess.call([
    "bash", "-c", script,    # start bash, explicitly: /bin/sh does not support "source"
    "_",                     # becomes $0 inside the shell
    branch,                  # becomes $1, which is assigned to branch and shifted off
    "bitbake", "-g", image   # these arguments become "$@" after the shift
])

请注意|| exit s-通常,您应该在不想明确打算忽略故障的任何命令上使用那些命令.

Note the || exits -- you should generally have those on any command where you don't explicitly intend to ignore failures.

这篇关于“来源"使用shell = True将PATH设置为bitbake在Python中无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆