Skip to main content
 首页 » 编程设计

python之fabric.api 与 fabric.operations

2024年10月25日15lyj

我是 Python 和 Fabric 的新手,使用 Fabric==1.10.1 和 Python 2.7.6。我不明白 fabric.api 和 fabric.operations 调用之间的区别,两者似乎都在做同样的事情。我应该在 fabfile 中使用哪一个?我注意到的一件事是,当我执行 fabric.api.reboot() 时,它会显示消息,

[127.0.0.1] out: Broadcast message from vagrant@localhost.localdomain 
[127.0.0.1] out: 
[127.0.0.1] out:    (/dev/pts/0) at 17:39 ... 
[127.0.0.1] out: 
[127.0.0.1] out: 
[127.0.0.1] out: 
[127.0.0.1] out: 
[127.0.0.1] out: The system is going down for reboot NOW! 
[127.0.0.1] out: 

但是当我使用 fabric.operations.reboot() 时,它没有显示任何消息。

更新:实际上似乎 fabric.operations.reboot() 和 fabric.api.reboot() 产生了消息。

请您参考如下方法:

您可以使用其中任何一种,但 fabric.api 是更好的选择。这是因为为简单起见,它是导入其他结构模块的地方。看这里:

$ cat fabric/api.py                                                                                                                                                                                                                                          (env: selenium) 
""" 
Non-init module for doing convenient * imports from. 
 
Necessary because if we did this in __init__, one would be unable to import 
anything else inside the package -- like, say, the version number used in 
setup.py -- without triggering loads of most of the code. Which doesn't work so 
well when you're using setup.py to install e.g. ssh! 
""" 
from fabric.context_managers import (cd, hide, settings, show, path, prefix, 
    lcd, quiet, warn_only, remote_tunnel, shell_env) 
from fabric.decorators import (hosts, roles, runs_once, with_settings, task, 
        serial, parallel) 
from fabric.operations import (require, prompt, put, get, run, sudo, local, 
    reboot, open_shell) 
from fabric.state import env, output 
from fabric.utils import abort, warn, puts, fastprint 
from fabric.tasks import execute 

fabric.api 正在为您导入 fabric.operations.reboot。