我是靠谱客的博主 深情手机,最近开发中收集的这篇文章主要介绍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

最后

以上就是深情手机为你收集整理的python中output尺寸过小怎么办_python - 2.6中的python check_output解决方法 - 堆栈内存溢出...的全部内容,希望文章能够帮你解决python中output尺寸过小怎么办_python - 2.6中的python check_output解决方法 - 堆栈内存溢出...所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部