首页 > 解决方案 > 'CalledProcessError' 退出状态 128. Git 描述错误并在 sh() 中使用 sed 将 '/' 替换为 '__'

问题描述

我正在调用这个命令: GITID=sh( 'git describe --dirty --long --tags --abbrev=10 | sed -e "s:/:__:g"' ).strip() 通过 python subprocess.Popen

try:
    # Check file can be read: throws exception if cannot be read
    f = open(self.path, "r")

    # Calls nyq to process yaml: throws exception if exit code!=0 => check=True

    # TODO : use object, as soon as nyq becomes part of mcook legacy code
    proc = subprocess.Popen(["nyq", self.path, str('regressions.' + self.regressionName)],
                            stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf8')
    stdout, stderr = proc.communicate()

    # TODO : Add check for nyq execution exit code
    if proc.returncode != 0:
        logging.error(
            "Error executing nyq ({})".format(proc.returncode))
        raise Exception(stdout)

    # Creates YAML object from output of nyq: throws exception if content is not a valid YAML stream
    list_files = stdout
    self.parsed_data = yaml.load(list_files)  # load EMC data
except Exception as exc:
    logging.error("An error occured during nested YAML processing:")
    logging.error(exc)
    sys.exit(1)

无法理解为什么我会收到此错误: 在此处输入图像描述

已解决:GITID=sh('git describe --dirty --long --tags --abbrev=10').replace('/', '__').strip()

使用替换而不是 sed!

标签: pythonpython-3.xshell

解决方案


我正在调用这个命令:GITID=sh( 'git describe --dirty --long --tags --abbrev=10 | sed -e "s:/:__:g"' ).strip()通过 python subprocess.Popen

我不认为你是 - 至少,不在你在这里显示的代码中。我想你在nyq这里打电话:

proc = subprocess.Popen(["nyq", ...], ...)

nyq随后,nyq在您显示的代码中,什么取决于它自己,并且该代码几乎肯定对错误更重要。如果该代码包含subprocess.Popen,则可以解释其余的错误文本。请注意,subprocess.Popen默认情况下不通过shell 运行命令;因此,该命令不能包含管道指令和进一步的子进程调用。

如果您非常信任命令行中的字符串,您可以添加shell=Truesubprocess.Popen调用中。 在执行此操作之前请参阅子流程文档的安全注意事项部分。


推荐阅读