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



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

永远不会引发异常,因为 os.system 不会引发异常。请使用子进程代替 Python3 子进程

使用 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 nnt"+ options.clone_url +'n')
sys.exit(0)

我使用以下代码找到了修复程序。

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)

它工作得很好。

相关内容

最新更新