python中output尺寸过小怎么办_python - 2.6中的python check_output解决方法 - 堆栈内存溢出...

我已经通过将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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值