fabric.api.run 的输出过多

Excess output from fabric.api.run

当通过 fabric.api.run 执行命令时 运行,如何捕获 我正在执行的命令的输出 运行在远程服务器上?

系统 bash_profile 文件产生的任何输出也被结构输出捕获。

不确定在这种情况下该怎么做。

from fabric.api import env, run
env.host_string = "hostname"

def run_a_thing():
    output = run("find /some/files/somewhere")
    return output

the_files_I_want = run_a_thing()

the_files_I_want

[hostname] run: find /some/files/somewhere  
**[hostname] out: Module slurm/15.08.1 loaded**   
**[hostname] out:**   
[hostname] out: file1...  
[hostname] out: file2... 

不需要的输出是上面加星号的行。模块由管理员自动加载(在本例中为 slurm),模块加载命令的输出与我尝试 运行.

的命令的输出一起出现

使用output.stdout,因为输出实际上是一个包装器class,不起作用。它 returns 完全一样,因为这都是标准输出。

那么,解决这个特定问题的想法,解决方法的想法?

谢谢

所以,我将分享我暂时使用的解决方法。如果有人有其他方法,请分享。

发件人:

def do_thing(path):
    command = "find {}".format(path)
    return run(command).stdout.splitlines()

收件人:

def do_thing(path):
    command = "find {}".format(path)
    background = run("").stdout.splitlines()
    output = run(command).stdout.splitlines()
    return [x for x in output if x not in background]

而且,由于随机空白似乎是输出的问题:

def do_thing(path):
    command = "find {}".format(path)
    background = run("").stdout.splitlines()
    output = run(command).stdout.splitlines()
    desired_output = []

    for line in output:
        for unwanted_line in background:
            if not re.search(unwanted_line.strip(), line):
                desired_output += [line]

    return desired_output