Python脚本未在sys.exit()上退出

2024-04-26 05:45:48 发布

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

这里没有sys.exit。我试过,quitexit等等。。 有什么建议吗

    try:
        print ("Cloning Git Repo")
        os.system("git clone " + options.clone_url)
    except Exception as e:
        print ("Git Clone Failed")
        sys.exit(2)

    try:
        print ("Building Docker Image")
        os.system("docker build -t " + options.image_tag + " -f " + repo_name + "/" + DOCKERFILE)
    except Exception as e:
        print ("Docker Build Failed", e)
        sys.exit(2)

    try:
        print ("Tagging Docker Image")
        os.system("docker tag " + options.image_tag + " " + PORTUS_REPO_URL + "/" + options.image_tag)
    except Exception as e:
        print ("Docker Tag Failed", e)
        sys.exit(2)

输出:

Cloning Git Repo
Cloning into 'xxx'...
git@git.hashedin.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Building Docker Image
"docker build" requires exactly 1 argument.
See 'docker build --help'.

Usage:  docker build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile
Tagging Docker Image
Error parsing reference: "http://portus.hashedin.com/nginx" is not a valid repository/tag: invalid reference format
Pushing Docker Image

Tags: dockerimagegitbuildostagsysexit
3条回答

我用下面的代码找到了一个修复程序

    print("Building Docker Image")
    proc = subprocess.Popen(
        ['sudo', 'docker', 'build', '-t', options.image_tag, '.'])
    proc.wait()
    (stdout, stderr) = proc.communicate()
    if proc.returncode != 0:
        print("Docker Build Failed")
        sys.exit(2)

它工作得很好

从不引发异常,因为os.system未引发异常。请改用子进程Python3 subprocess

使用python3子进程使用下面的代码:https://docs.python.org/3/library/subprocess.html


import subprocess

log = subprocess.Popen("git clone " + options.clone_url,stdout=subprocess.PIPE, stderr=subprocess.PIPE)

# GET BOTH THE ERROR LOG AND OUTPUT LOG FOR IT
stdout,stderr = log.communicate()

# FORMAT THE OUTPUT
stdout = stdout.decode('utf-8')
stderr = stderr.decode('utf-8')

if stderr != "" :
    Tlog("ERROR WHILE CLONING GIT URL \n\n\t"+ options.clone_url +'\n')
    sys.exit(0)

相关问题 更多 >