在python中连接git命令字符串时出错

时间:2015-10-22 11:42:01

标签: python string git awk

在此之后:Find out git branch creator

我制作了一个python脚本,它为我提供了一组来自

结果的有序电子邮件

git for-each-ref --format='%(authoremail)%09%(refname)' | sort -k5n -k2M -k3n -k4n | grep remotes | awk -F "\t" '{ printf "%-32s %-27s %s\n", $1, $2, $3 }'

这样我就可以通过电子邮件告诉他们这些是你在远程分支的请删除它们。

但是当我尝试将它放在python中时我收到错误

intitial =  "git for-each-ref --format='%(authoremail)%09%(refname)' | sort -k5n -k2M -k3n -k4n | grep remotes | awk -F "
addTab = "\t"
printf = '{ printf "%-32s %-27s %s\n", $1, $2, $3 }'

gitCommnad = "%s%s %s " % (intitial, addTab, printf)




def _exec_git_command(command, verbose=False):
    """ Function used to get data out of git commads
        and errors in case of failure.
        Args:
            command(string): string of a git command
            verbose(bool): whether to display every command
            and its resulting data.
        Returns:
            (tuple): string of Data and error if present
    """
    # converts multiple spaces to single space
    command = re.sub(' +',' ',command)
    pr = subprocess.Popen(command, shell=True,
        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    msg = pr.stdout.read()
    err = pr.stderr.read()
    if err:
        print err
        if 'Could not resolve host' in err:
            return
    if verbose and msg:
        print "Executing '%s' %s" % (command, msg)

    return msg, err


print _exec_git_command(gitCommnad)

1 个答案:

答案 0 :(得分:0)

问题是您没有将\t{ printf "%-32s %-27s %s\n", $1, $2, $3 }放在引号内,因此awk会报告语法错误。你应该使用 -

intitial =  "git for-each-ref --format='%(authoremail)%09%(refname)' | sort -k5n -k2M -k3n -k4n | grep remotes | awk -F"
addTab = "\t"
printf = '{ printf "%-32s %-27s %s\n", $1, $2, $3 }'

gitCommnad = "%s \"%s\" '%s' " % (intitial, addTab, printf)