Fabric 错误:致命错误:local() 在执行 'git commit -m 'message' 时遇到错误(返回码 2)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1875306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fabric error: Fatal error: local() encountered an error (return code 2) while executing 'git commit -m 'message'
提问by BryanWheelock
I'm trying to setup a fabfile to deploy my Django app.
我正在尝试设置一个 fabfile 来部署我的 Django 应用程序。
I can't figure out why I'm getting this error:
我不明白为什么我会收到这个错误:
Fatal error: local() encountered an error (return code 2) while executing 'git commit -m 'changed settings for prodserver'
致命错误:local() 在执行 'git commit -m 'changed settings for prodserver' 时遇到错误(返回码 2)
$ fab create_branch_deploy_to_prodserver
[localhost] run: git checkout prodserver_server
[localhost] run: git merge master
[localhost] run: cp settings_prodserver.py settings.py
[localhost] run: git add settings.py
[localhost] run: git commit -m 'changed settings for prodserver'
Fatal error: local() encountered an error (return code 1) while executing 'git commit -m 'changed settings for prodserver''
Aborting.
Here if the Fabric function:
这里如果 Fabric 函数:
def create_branch_deploy_to_prodserver():
local("git checkout prodserver_server")
local("git merge master")
local('cp settings_prodserver.py settings.py') #
#local('git rm fabfile.py') #This is also creating error so it's commented out
local('git add settings.py')
local("git commit -m 'changed settings for prodserver'")
Is it possible to make a git commit from Fabric?
是否可以从 Fabric 进行 git commit?
回答by BryanWheelock
I was able to diagnose the issue when I added capture=False to the declaration:
当我在声明中添加 capture=False 时,我能够诊断出问题:
local('git rm fabfile.py', capture=False)
local('git add settings.py', capture=False)
This allowed the error to be displayed more verbosely.
这允许更详细地显示错误。
Apparently, the maintainer of Fabric will to revert local
's behavior back to not capturing by default, in 1.0.
显然,Fabric 的维护者将local
在 1.0中将的行为恢复为默认情况下不捕获。
回答by VonC
Is this a python-related issue like the one described in this thread?
这是一个与 python 相关的问题,就像这个线程中描述的那样吗?
The main problem with this is that the
stdout
/stderr
capturing is per-run/sudo invocation and not per-task.It would be wonderful if you could explain me how I could collect outputand errorby only modifying the file called
fabfile_runner.py
.
Idealy the fabric task itself could be unmodified, this would allow to upload the the factory the same file that you have tested manually.Check out the Fabric source and look in the 'tests' folder, specifically tests/utils.py. It contains a single decorator,
@mock_streams
, which is capable of wrapping a function (any function in any Python code -- it's not Fabric specific, as I mentioned) and redirectingsys.stdout
and/orsys.stderr
for capture/examination.It's designed for use around functions, being a decorator, so you could use it directly by modifying your
fabfile_runner.py
like so:
这样做的主要问题是
stdout
/stderr
捕获是每次运行/sudo 调用而不是每个任务。这将是美好的,如果你能解释我怎么能收集输出和错误只修改称为文件
fabfile_runner.py
。
理想情况下,结构任务本身可以不被修改,这将允许将您手动测试的相同文件上传到工厂。查看 Fabric 源代码并查看“tests”文件夹,特别是tests/utils.py。它包含一个装饰器,
@mock_streams
能够包装一个函数(任何 Python 代码中的任何函数——它不是 Fabric 特定的,正如我提到的那样)和重定向sys.stdout
和/或sys.stderr
用于捕获/检查。它是为围绕函数而设计的,是一个装饰器,因此您可以通过
fabfile_runner.py
像这样修改来直接使用它:
fabfile_runner.py
fabfile_runner.py
from StringIO import StringIO
import sys
from test_fabfile import hello_world
def execute(task):
output = StringIO()
error = StringIO()
sys.stdout = output
sys.stderr = error
task()
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
return (output.getvalue(), error.getvalue())
output, error = execute(hello_world)
print "output : %s" %output
print "error : %s" %error