2012-02-26 63 views

回答

31

聽起來像Managing output部分是你要找的。

要隱藏控制檯輸出,嘗試這樣的事情:

from __future__ import with_statement 
from fabric.api import hide, run, get 

with hide('output'): 
    run('mysqldump --no-data test | tee test.create_table') 
    get('~/test.create_table', '~/test.create_table') 

初級講座是抽樣結果:

No hosts found. Please specify (single) host string for connection: 192.168.6.142 
[192.168.6.142] run: mysqldump --no-data test | tee test.create_table 
[192.168.6.142] download: /home/quanta/test.create_table <- /home/quanta/test.create_table 
17

,如果你想隱藏一切從日誌,避免面料試試這個當命令失敗時拋出異常:

from __future__ import with_statement 
from fabric.api import env,run,hide,settings 

env.host_string = '[email protected]' 
env.key_filename = '/path/to/key.pem' 

def exec_remote_cmd(cmd): 
    with hide('output','running','warnings'), settings(warn_only=True): 
     return run(cmd) 

之後,您可以檢查命令resu LT如本例所示:

cmd_list = ['ls', 'lss'] 
for cmd in cmd_list: 
    result = exec_remote_cmd(cmd) 
    if result.succeeded: 
     sys.stdout.write('\n* Command succeeded: '+cmd+'\n') 
     sys.stdout.write(result+"\n") 
    else: 
     sys.stdout.write('\n* Command failed: '+cmd+'\n') 
     sys.stdout.write(result+"\n") 

這將是該程序的控制檯輸出(觀察到有未記錄從織物消息):

 
* Command succeeded: ls 
Desktop espaiorgcats.sql Pictures Public  Videos 
Documents examples.desktop projectes scripts 
Downloads Music   prueba Templates 

* Command failed: lss 
/bin/bash: lss: command not found