我在使用python 2.6的机器上运行但是现在我无法升级。
我需要subrpocess.check_output
函数,但我已经理解这是2.6中定义的注释。
所以我使用了workaround:
try:
import subprocess
if "check_output" not in dir( subprocess ): # duck punch it in!
def check_output(*popenargs, **kwargs):
r"""Run command with arguments and return its output as a byte string.
Backported from Python 2.7 as it's implemented as pure python on stdlib.
>>> check_output(['/usr/bin/python', '--version'])
Python 2.6.2
"""
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
error = subprocess.CalledProcessError(retcode, cmd)
error.output = output
raise error
return output
subprocess.check_output = check_output
# Git Information
git_info= {
"last_tag" : subprocess.check_output(['git', 'describe', '--always']),
"last_commit" : subprocess.check_output(['git', 'log', '-1', '--pretty=format:\'%h (%ci)\'', '--abbrev-commit'])
}
except Exception, e:
raise e
else:
data = git_info
return data
我将此与Django
+ wsgi
结合使用。
上一段代码总是给我Command '['git', 'describe', '--always']' returned non-zero exit status 128
。
现在如果我运行git describe --always
我得到了正确的输出,所以我不认为问题存在。
我不知道是什么原因导致了这个问题。
修改
如果我使用命令subprocess.check_output(['ls', '-l'])
或subprocess.check_output(['pwd'])
工作,从这里我就知道从Django调用的视图实际上是在/var/www
运行,这是Apache中指定的DocumentRoot配置文件。
实际文件不在/var/www
下,实际上一切都在我使用本地django dev服务器的本地机器上运行。因此git命令无法正常工作,因为/var/www
下没有git存储库。如何从原始路径(python文件实际位于哪里)执行原始subprocess.check_output(['git', 'describe', '--always'])
?
答案 0 :(得分:0)
我已经通过将cwd参数传递给check_output解决了问题。
def get_git_info():
git_info = {}
try:
import subprocess
# subprocess.check_output did not exist in 2.6
if "check_output" not in dir(subprocess): # duck punch it in!
# workaround/redefinition for the subprocess.check_output() command
def check_output(*popenargs, **kwargs):
""" Run command with arguments and return its output as a byte string.
Backported from Python 2.7 as it's implemented as pure python on stdlib.
>>> check_output(['/usr/bin/python', '--version'])
Python 2.6.2
"""
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
error = subprocess.CalledProcessError(retcode, cmd)
error.output = output
raise error
# In case we want the error in string format:
# stderr=subprocess.STDOUT
# raise Exception(stderr)
return output
subprocess.check_output = check_output
# Set on which dir the git command should be invoked
if os.path.isdir(r'/my/git/dir'):
cwd = r'/my/git/dir'
# If using the django local dev server, then it will invoke the command from the dir where this script is located
else:
cwd = None
# Check that the directory is a git repo:
# 'git rev-parse' returns a number !=0 if we are in a git repo
if subprocess.check_output(['git', 'rev-parse'], cwd=cwd) != 0:
# Git Information
git_info = {
"last_tag": subprocess.check_output(['git', 'describe', '--always'], cwd=cwd),
"last_commit": subprocess.check_output(['git', 'log', '-1', '--pretty=format:\'%h (%ci)\'', '--abbrev-commit'], cwd=cwd),
}
except Exception, e:
log.exception('Problem getting git information')
pass
# return the git info or an empty dict (defined above)
return git_info