python子进程git克隆

2024-10-04 01:30:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在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)
    }

它返回:

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,它工作得很好

如何解决


Tags: lambdahttpsimportgitgithubcomjsontrue
1条回答
网友
1楼 · 发布于 2024-10-04 01:30:29

有几个问题

首先,您需要等待git进程退出。要对subprocess.Popen执行此操作,请对返回的Popen对象调用.wait()。但是,我建议使用subprocess.check_call()来自动等待进程退出,并在进程返回非零退出状态时引发错误

其次,没有必要指定shell=True,因为您没有使用任何shell扩展或内置。事实上,当使用shell=True传递参数列表时,第一项是命令字符串,其余项是shell本身的参数,而不是命令

最后,您的GitHub URL中缺少一个斜杠

请尝试以下方法:

subprocess.check_call(["git", "clone", "https://github.com/hongmingu/requirements"])

相关问题 更多 >