python check_output 2.6 中的解决方法

python check_output workaround in 2.6

我 运行正在使用 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 调用的视图实际上是 运行ning 在 /var/www 是指定的 DocumentRoot在 Apache 配置文件中。

真正的文件不在 /var/www 下,实际上一切都在我使用本地 django 开发服务器的本地机器上运行。所以 git 命令将不起作用,因为 /var/www 下没有 git 存储库。如何从其原始路径(python 文件实际所在的位置)执行原始 subprocess.check_output(['git', 'describe', '--always'])

我已经按照评论中的建议将 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