[英]python subprocess git clone
I'm using subprocess() on AWS lambda And using this layer: https://github.com/lambci/git-lambda-layer我在 AWS lambda 上使用 subprocess() 并使用这一层: https://github.com/lambci/git-lambda-layer
Here is code:这是代码:
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)
}
And it returns:它返回:
Response:
{
"statusCode": 200,
"body": "\"b'word.txt\\\\n'\""
}
So there is something wrong on subprocess.Popen(["git", "clone", "https:/github.com/hongmingu/requirements"], shell=True)
所以
subprocess.Popen(["git", "clone", "https:/github.com/hongmingu/requirements"], shell=True)
I checked there is git by subprocess.check_output(["git --version"], shell=True)
and it works well.我通过 subprocess.check_output
subprocess.check_output(["git --version"], shell=True)
检查了 git 并且效果很好。
How to solve it?如何解决?
There are a few problems.有几个问题。
First, you need to wait for the git
process to exit.首先需要等待
git
进程退出。 To do this with subprocess.Popen
, call .wait()
on the returned Popen
object.要使用 subprocess.Popen 执行此操作,请在返回的
Popen
subprocess.Popen
上调用.wait()
。 However, I'd recommend using subprocess.check_call()
instead to automatically wait for the process to exit and to raise an error if the process returns a non-zero exit status.但是,我建议使用
subprocess.check_call()
来自动等待进程退出并在进程返回非零退出状态时引发错误。
Second, there's no need to specify shell=True
since you're not using any shell expansions or built-ins.其次,没有必要指定
shell=True
因为您没有使用任何 shell 扩展或内置。 In fact, when passing an argument list when using shell=True
, the first item is the command string, and the remaining items are arguments to the shell itself, not the command.实际上,当使用
shell=True
传递参数列表时,第一项是命令字符串,其余项是 arguments 到 shell 本身,而不是命令。
Lastly, you're missing a slash in your GitHub URL.最后,您的 GitHub URL 中缺少斜线。
Try this instead:试试这个:
subprocess.check_call(["git", "clone", "https://github.com/hongmingu/requirements"])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.