【问题标题】:error while concatinating git command string in python在python中连接git命令字符串时出错
【发布时间】:2016-01-21 16:21:30
【问题描述】:

关注这个: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)

【问题讨论】:

  • 你得到什么错误?
  • @AnandSKumar SyntaxError awk: printf ^ grep: write error ('', 'awk: printf\nawk: ^ syntax error\ngrep: write error\n')
  • 来自 awk 的语法错误对吗?

标签: python string git awk


【解决方案1】:

问题是您没有将\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)

【讨论】:

    猜你喜欢
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-13
    • 2012-10-02
    • 1970-01-01
    相关资源
    最近更新 更多