Python3:无法捕获运行`auto-apt -ysi run`的子进程的stdout

时间:2018-03-26 08:58:04

标签: python debian subprocess apt

我希望将Install:的输出与auto-apt -ysi run cmake ..的输出匹配(在模拟模式下运行cmakeauto-apt)。我可以获得此命令的所有stdoutstderr,除了我想要的行...

示例输出(手动运行):

zhangysh1995@ubuntu-zhangyushao:~/tmp/project/OpenRaider/cbuild$ auto-apt -ysi run cmake ..
Entering auto-apt mode: cmake ..
Exit the command to leave auto-apt mode.
Install:universe/devel/cmake-curses-gui file:/usr/bin/ccmake    by:cmake ..
-- Setting build type to 'Debug' as none was specified.
CMake Error: Could not open file for write in copy operation /home/zhangysh1995/tmp/project/OpenRaider/cbuild/CMakeFiles/git-data/grabRef.cmake.tmp

我得到了什么(subprocess.check_output):

Entering auto-apt mode: cmake ..
Exit the command to leave auto-apt mode.
-- Setting build type to 'Debug' as none was specified.
CMake Error: Could not open file for write in copy operation /home/zhangysh1995/tmp/project/OpenRaider/cbuild/CMakeFiles/git-data/grabRef.cmake.tmp

我的脚本中stdout缺少此行:

  

安装:universe / devel / cmake-curses-gui文件:/ usr / bin / ccmake
  通过:cmake ..

代码:

def runSim():
    output = ''
    results = ''

    try:
        process = subprocess.run("auto-apt -ysi run cmake ..", stdout=subprocess.PIPE,
                                         stderr=subprocess.STDOUT, shell=True)
        # fix: cannot catch `Install:xxx` here
        output = process.stdout.decode('utf-8')
    except subprocess.CalledProcessError as e:
        output = e.output
        # print(output)

    for line in output.splitlines():
        line = line
        if line.startswith("Install:"):
            results += line + ', '

    for pkg in results.split(','):
        print(pkg, end=' ')

我的问题是:可以subprocess.check_output()获取子进程的所有stdout吗?如果是,我的代码是否正确?如果不是,怎么做才能获得我想要的线条? (我认为不是因为这个命令c 应该打开另一个子过程。)

0 个答案:

没有答案