2013-10-11 48 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

对不起,您的答案没有那么相关。 –