意外行为嵌套使用面料CD呼叫时 [英] Unexpected behavior when nesting cd calls using Fabric
问题描述
我经历(在Ubuntu 13.04版本1.7.0)一些麻烦面料。
I am experiencing some troubles with Fabric (version 1.7.0 on Ubuntu 13.04).
考虑一下这个功能:
def does_not_work():
with cd('/absolute/folder/one/'):
with prefix('change_path_command'):
with cd('/absolute/folder/two/'):
run('some_random_command')
我希望它执行相同的命令:
I expect it to execute the same command as:
def works():
run('cd /absolute/folder/one/ && change_path_command && cd /absolute/folder/two/ && some_random_command')
然而,这里是晶圆厂does_not_work
的面料输出:
Requested: some_random_command
Executed: /bin/bash -l -c "cd /absolute/folder/two/ && change_path_command && some_random_command"
似乎嵌套 CD
s的造成我的困扰。
有一个很好的解释?
推荐答案
的 CD
上下文管理和的 preFIX
不实际运行的命令上下文管理当你调用他们,他们只是修改影响运行的任何后续调用()
和/或须藤()$ C一些地方的环境设置$ C>。
The cd
context manager and the prefix
context manager don't actually run commands when you invoke them, they just modify some local environment settings that affect any subsequent invocations of run()
and/or sudo()
.
所以,当你的运行('some_random_command')
被执行,它就会被执行,它运行的背景下( CD = /文件夹/有一个
, preFIX = change_path_command
, CD = /文件夹/两个
),并且由于内 CD
花费precedence在外部 CD
,最终的结果是执行一个命令光盘/文件夹/双功放&;&安培; change_path_command&功放;&安培; some_random_command
。
So when your run('some_random_command')
gets executed, it gets executed, it runs in the context of (cd=/folder/one
, prefix=change_path_command
, cd=/folder/two
), and since the inner cd
takes precedence over the outer cd
, the end result is a single command executed with cd /folder/two && change_path_command && some_random_command
.
看看源$ C $ C为 CD
和的 preFIX
到获得如何工作的一个更好的想法 - 他们都最终要做的就是修改字典 fabric.state.env
当他们进入和退出。这些后来的调用get应用于 _ prefix_commands()
,它会从称为run()
和须藤()
通过 _run_command()
功能。
Take a look at the source code for cd
and prefix
to a get a better idea of how that works -- all they ultimately do is modify the dictionary fabric.state.env
when they enter and exit. These later get applied in the call to _prefix_commands()
, which gets called from run()
and sudo()
via the _run_command()
function.
这篇关于意外行为嵌套使用面料CD呼叫时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!