python 子进程 git 克隆
python subprocess git clone
我在 AWS lambda 上使用 subprocess()
并使用这一层:https://github.com/lambci/git-lambda-layer
代码如下:
import json
import os
import subprocess
def lambda_handler(event, context):
os.chdir('/tmp')
subprocess.Popen(["git", "clone", "https:/github.com/hongmingu/requirements"], shell=True)
subprocess.Popen(["touch word.txt"], shell=True)
word = str(subprocess.check_output(["ls"], shell=True))
return {
'statusCode': 200,
'body': json.dumps(word)
}
它 returns:
Response:
{
"statusCode": 200,
"body": "\"b'word.txt\\n'\""
}
所以 subprocess.Popen(["git", "clone", "https:/github.com/hongmingu/requirements"], shell=True)
上有问题
我检查过 subprocess.check_output(["git --version"], shell=True)
有 git,它运行良好。
如何解决?
有一些问题。
首先需要等待git
进程退出。要使用 subprocess.Popen
执行此操作,请在返回的 Popen
对象上调用 .wait()
。但是,我建议使用 subprocess.check_call()
来自动等待进程退出并在进程 returns 非零退出状态时引发错误。
其次,无需指定 shell=True
,因为您没有使用任何 shell 扩展或内置。事实上,当使用 shell=True
传递参数列表时,第一项是命令字符串,其余项是 shell 本身的参数,而不是命令。
最后,您在 GitHub URL 中遗漏了一个斜线。
试试这个:
subprocess.check_call(["git", "clone", "https://github.com/hongmingu/requirements"])
我在 AWS lambda 上使用 subprocess() 并使用这一层:https://github.com/lambci/git-lambda-layer
代码如下:
import json
import os
import subprocess
def lambda_handler(event, context):
os.chdir('/tmp')
subprocess.Popen(["git", "clone", "https:/github.com/hongmingu/requirements"], shell=True)
subprocess.Popen(["touch word.txt"], shell=True)
word = str(subprocess.check_output(["ls"], shell=True))
return {
'statusCode': 200,
'body': json.dumps(word)
}
它 returns:
Response:
{
"statusCode": 200,
"body": "\"b'word.txt\\n'\""
}
所以 subprocess.Popen(["git", "clone", "https:/github.com/hongmingu/requirements"], shell=True)
我检查过 subprocess.check_output(["git --version"], shell=True)
有 git,它运行良好。
如何解决?
有一些问题。
首先需要等待git
进程退出。要使用 subprocess.Popen
执行此操作,请在返回的 Popen
对象上调用 .wait()
。但是,我建议使用 subprocess.check_call()
来自动等待进程退出并在进程 returns 非零退出状态时引发错误。
其次,无需指定 shell=True
,因为您没有使用任何 shell 扩展或内置。事实上,当使用 shell=True
传递参数列表时,第一项是命令字符串,其余项是 shell 本身的参数,而不是命令。
最后,您在 GitHub URL 中遗漏了一个斜线。
试试这个:
subprocess.check_call(["git", "clone", "https://github.com/hongmingu/requirements"])