好吧,我必须承认我是面向织物甚至是python的新手,但是我有兴趣以正确的方式做到这一点,所以...我想用prepare
函数装饰我的一些任务根据已经给出的那些,向env
添加一些变量。看看:
from fabric.api import *
import fabstork.project.base as base
import fabstork.utils.drupal as utils
def prepare(task):
""" Decorator to set some additional environment variables """
def prepared(*args, **kwargs):
env.sites_folder = env.sites_folder if 'sites_folder' in env else 'default'
env.settings_file = "%s/www/sites/%s/settings.php" % (env.build_path, env.sites_folder)
# more to come
return task(*args, **kwargs)
return prepared
@task
@prepare
def push(ref='HEAD'):
"""
Deploy a commit to a host
"""
base.push(ref)
utils.settings_php()
utils.link_files()
utils.set_perms()
上面的示例失败,push
不再是任务,在命令行执行fab --list
时,它不在可用任务列表中。省略装饰器可以完成任务。我究竟做错了什么?
答案 0 :(得分:8)
from fabric.decorators import task
from functools import wraps
def custom_decorator(func):
@wraps(func)
def decorated(*args, **kwargs):
print "this function is decorated."
return func(*args, **kwargs)
return decorated
@task
@custom_decorator
def some_function():
print "this is function"
结果:
# fab -l
>Available commands:
>
> some_function
# fab some_function
>this function is decorated.
>this is function
>
>Done.