2013-10-11 42 views
0

無法理解如何執行某些操作,我認爲這對Fabric應該很直觀。我想捕獲遠程執行命令產生的stdout,然後在隨後的遠程調用中使用結果。插入結構命令的字符串輸出時出現的奇怪問題

但是,我收到了真正無法理解的錯誤。我感覺到我在做什麼有很多問題,但我不知道從哪裏開始。也許有人可以幫我把這件事分開。

我有什麼:

... 
with cd(env.repo): 
    abbrev_hash = run('git log -1 --pretty="%h"') 
run("rsync -r --exclude '.git/*' %s %s" % (env.repo, abbrev_hash)) 
... 

相關的輸出:

[cookcountyjail.recoveredfactory.net] run: git log -1 --pretty="%h" [cookcountyjail.recoveredfactory.net] out: [cookcountyjail.recoveredfactory.net] out: c6d4ea0 [cookcountyjail.recoveredfactory.net] out: [cookcountyjail.recoveredfactory.net] out: [cookcountyjail.recoveredfactory.net] run: rsync -r --exclude '.git/*' > /home/ubuntu/repos/cookcoc6d4ea0l_2.0-dev

[cookcountyjail.recoveredfactory.net] out: /bin/bash: -c: line 1: syntax error near unexpected token newline' [cookcountyjail.recoveredfactory.net] out: /bin/bash: -c: line 1: [cookcountyjail.recoveredfactory.net] out: ' [cookcountyjail.recoveredfactory.net] out:

Fatal error: run() received nonzero return code 1 while executing!

c6d4ea0ed: rsync -r --exclude '.git/*' /home/ubuntu/repos/cookcountyjail_2.0-dev

Executed: /bin/bash -l -c "cd /home/ubuntu/website/2.0/websites && export PATH=\"\$PATH:\"/home/ubuntu/website/2.0/websites/active\"\" && rsync -r --exclude '.git/*' /home/ubuntu/repos/cookcouc6d4ea0_2.0-dev "

+0

順便說一句,我意識到,我可能只是通過某種手段輸出重定向或另一個在bash shell裏面,但我真的很想理解更好的織物。 –

回答

2

這是否你想要什麼不能做?

def t1(): 
    captured = local('ls -alh') 
    print captured 

http://docs.fabfile.org/en/1.8/api/core/operations.html#fabric.operations.run

run will return the result of the remote program’s stdout as a single (likely multiline) string. This string will exhibit failed and succeeded boolean attributes specifying whether the command failed or succeeded, and will also include the return code as the return_code attribute. Furthermore, it includes a copy of the requested & actual command strings executed, as .command and .real_command, respectively.

也有此位在local文檔:

http://docs.fabfile.org/en/1.8/api/core/operations.html#fabric.operations.local

local is not currently capable of simultaneously printing and capturing output, as run/sudo do. The capture kwarg allows you to switch between printing and capturing as necessary, and defaults to False.

When capture=False, the local subprocess’ stdout and stderr streams are hooked up directly to your terminal, though you may use the global output controls output.stdout and output.stderr to hide one or both if desired. In this mode, the return value’s stdout/stderr values are always empty.

When capture=True, you will not see any output from the subprocess in your terminal, but the return value will contain the captured stdout/stderr.

In either case, as with run and sudo, this return value exhibits the return_code, stderr, failed and succeeded attributes. See run for details.

+0

'captured = local('ls -alh',capture = True);打印捕獲'確實做我想要的。 –

+1

我不知道,我不能聲稱輸出不再被捕獲。用'sudo'命令我可以捕獲輸出並打印出來。也許這是Fabric有點奇怪的字符串插值問題。當我嘗試將其作爲包含常規python sting格式的字符串的一部分包含它時,輸出不會顯示出來。我得到了很多奇怪的輸出。 –

+0

對不起,您的答案沒有那麼相關。 –