【问题标题】:Python call makefile compiling and capture make's outputPython调用makefile编译并捕获make的输出
【发布时间】:2013-11-21 06:57:09
【问题描述】:

Jenkins 中的一个工作调用我的 python 脚本,我想在其中调用 make 来编译我的 UT 代码,如果出现“make: *** [] Error 1”之类的编译错误,则引发 sys.exit(1)

我还需要实时打印输出。

这是我的 python 脚本:

make_process = subprocess.Popen("make clean all;", shell=True, stdout=subprocess.PIPE, stderr=sys.stdout.fileno())
    while True:
        line = make_process.stdout.readline()
        if not line:break
        print line, #output to console in time
        sys.stdout.flush()

但是如何捕获make 错误并让这个python 脚本失败呢?

注意:

更新: 原来是makefile问题。请参阅已接受答案的 cmets。但是对于这个python问题,pentadecagon给出了正确的答案。

【问题讨论】:

    标签: python makefile subprocess


    【解决方案1】:

    可以通过

    查看make过程的返回值
    make_process.poll()
    

    如果进程仍在运行,则返回“None”,如果已完成,则返回错误代码。如果您只希望输出最终出现在控制台上,则无需手动执行此操作 无论如何,输出都会进入控制台,并且可以这样做:

    make_process = subprocess.Popen("make clean all", stderr=subprocess.STDOUT)
    if make_process.wait() != 0:
         something_went_wrong();
    

    【讨论】:

    • “等待”代码对我不起作用。与民意调查相同。 make_process.poll() 为 0 时使错误发生。
    • 这是 GNU make,对吧?检查命令行上的返回值。如果那里也为 0,那么您的 Makefile 有问题。否则...也许尝试删除脚本中的分号?
    • 是的,GNU 制造。删除该分号后它仍然不起作用。如何在命令行上检查返回值?谢谢
    • 您在命令行上运行 make,然后在 Linux 上运行:echo $? 在 Windows 上运行:echo %$ERRORLEVEL%。它应该是 2。
    • 我在 Linux 上。是的,它确实是 0 而不是 1 或 2。我认为 makefile 返回 1 是因为打印“make: *** [xxx.so] Error 1”,如果我让它通过“gcc -g -pg -”返回 2 Werror -fprofile-arcs -ftest-coverage -m32 -fPIC -shared -Wl,--no-undefined -o $@ $(D_CMPFLAG) $(INCLUDE) $(LIBS) || exit 2",它将是"make : *** [xxx.so] 错误 2",但回显 $?还是0。你知道为什么吗?谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 2021-11-23
    相关资源
    最近更新 更多