在python中捕获bash/git致命错误

在python中捕获bash/git致命错误,python,git,bash,Python,Git,Bash,我想写一个简单的python脚本,它能够将git存储库克隆到所需的目录中。我使用了try…除了构造能够捕获所有异常,但是看起来我无法正确处理“致命”异常 #!/usr/bin/env python import subprocess try: subprocess.check_call(['git', 'clone', 'git clone git@some_repo', '/tmp/some_directory']) except Exception: print "

我想写一个简单的python脚本,它能够将git存储库克隆到所需的目录中。我使用了try…除了构造能够捕获所有异常,但是看起来我无法正确处理“致命”异常

#!/usr/bin/env python
import subprocess

try:
    subprocess.check_call(['git', 'clone', 'git clone git@some_repo', '/tmp/some_directory'])
except Exception:
    print "There was a problem during repository configuration"
上面脚本的输出:

致命:存储库的git克隆git@some_repo"不存在"

存储库配置期间出现问题


更具体地说,我只是希望得到“有一个…”的信息。为什么我也会收到“致命”消息?

您需要捕获
子流程的
STDERR
。检查调用()
执行。看


有关详细信息。

您需要捕获
子流程的
STDERR
。请检查调用()
执行。看


有关详细信息。

您看到的消息是由git命令生成的

如果要防止出现该消息,应通过shell将标准错误或所有输出重定向到
/dev/null
,如:

subprocess.check_call(['git', 'clone', 'git clone git@some_repo', '/tmp/some_directory', '2&>/dev/null'], shell=True)

但是,我建议不要这样做,因为您会丢失有关错误实际原因的信息。

您看到的消息是由
git
命令生成的

如果要防止出现该消息,应通过shell将标准错误或所有输出重定向到
/dev/null
,如:

subprocess.check_call(['git', 'clone', 'git clone git@some_repo', '/tmp/some_directory', '2&>/dev/null'], shell=True)

但是,我建议您不要这样做,因为您会丢失有关错误实际原因的信息。

如前所述,您需要捕获标准错误。另外,正如文档中指定的,
subprocess.check_call()
在返回代码非零时只会引发异常

因此,您可以模仿如下行为:

#!/usr/bin/env python
import subprocess

def clone_repository(): # customize your function parameters
    # prepare the arguments with your function parameters
    arguments = ['git', 'clone', 'git clone git@some_repo', '/tmp/some_directory']
    git_proc = subprocess.Popen(arguments)
    stdout, stderr = git_proc.communicate()
    if git_proc.returncode != 0:
        raise subprocess.CalledProcessError(arguments, git_proc.returncode)
    return stdout, stderr

try:
    stdout, stderr = clone_repository()
except (OSError, ValueError) as e:
    # this errors out when the arguments are invalid (ValueError)
    # or when there is an underlying file missing, etc (OSError)
    # put the print that you require for these errors
    pass 
except subprocess.CalledProcessError: 
    # you could use stderr to determine the underlying error
    print "There was a problem during repository configuration"

如前所述,您需要捕获标准错误。另外,正如文档中指定的,
subprocess.check_call()
在返回代码非零时只会引发异常

因此,您可以模仿如下行为:

#!/usr/bin/env python
import subprocess

def clone_repository(): # customize your function parameters
    # prepare the arguments with your function parameters
    arguments = ['git', 'clone', 'git clone git@some_repo', '/tmp/some_directory']
    git_proc = subprocess.Popen(arguments)
    stdout, stderr = git_proc.communicate()
    if git_proc.returncode != 0:
        raise subprocess.CalledProcessError(arguments, git_proc.returncode)
    return stdout, stderr

try:
    stdout, stderr = clone_repository()
except (OSError, ValueError) as e:
    # this errors out when the arguments are invalid (ValueError)
    # or when there is an underlying file missing, etc (OSError)
    # put the print that you require for these errors
    pass 
except subprocess.CalledProcessError: 
    # you could use stderr to determine the underlying error
    print "There was a problem during repository configuration"

这是来自
git
调用本身的标准错误。这是来自
git
调用本身的标准错误。该示例解决了“致命”的问题,但是是否可以将stdout存储到变量中?该示例解决了“致命”的问题,但是是否可以将stdout存储到变量中?