简体   繁体   中英

python subprocess git clone

I'm using subprocess() on AWS lambda And using this layer: 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)

I checked there is git by subprocess.check_output(["git --version"], shell=True) and it works well.

How to solve it?

There are a few problems.

First, you need to wait for the git process to exit. To do this with subprocess.Popen , call .wait() on the returned Popen object. 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.

Second, there's no need to specify shell=True since you're not using any shell expansions or built-ins. 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.

Lastly, you're missing a slash in your GitHub URL.

Try this instead:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM