Python doit - 在依赖任务中使用参数

时间:2016-01-22 23:10:14

标签: python doit

我有2个doit任务,一个依赖另一个。例如:

def task_deploy():
    return {
        'actions': ['do some deploy commands'],
        'file_dep': ['dist'],
        'params': [{'name': 'projectName',
                    'short': 'p',
                    'long': 'projectName',
                    'default': 'project',
                    'type': str,
                    'help': 'The project name to deploy.'}]
        }

def task_create_distibution_archive():
    return {
        'actions': ['do something that requires projectName'],
        'doc': 'Creates a zip archive of the application in "dist"',
        'targets': ['dist']
    }

有没有办法将任务的参数分享或传递给另一个?我已经阅读了关于任务创建和依赖pydoit.org的所有内容,但是没有找到类似于我想要的东西。

我知道我可以使用yield同时创建这两个任务,但我想在执行任务时使用参数,而不是在我创建它时。

2 个答案:

答案 0 :(得分:2)

  

有没有办法将任务的参数分享或传递给另一个?

是。使用getargshttp://pydoit.org/dependencies.html#getargs

在您的示例中,您需要向任务deploy添加另一个操作,只是为了保存传递的参数。

答案 1 :(得分:0)

您可以使用像commonCommand这样的全局变量。如果您有更复杂的需求,请创建一个类来处理它。

class ComplexCommonParams(object):
    def __init__(self):
        self.command = 'echo'
params = ComplexCommonParams()
commonCommand='echo'
def task_x():
    global commonCommand
    return {
        'actions': [ commonCommand + ' Hello2 > asdf' ],
        'targets': ['asdf']
        }
def task_y():
    global commonCommand
    return {
        'actions': [ commonCommand+'  World' ],
        'file_dep': ['asdf'],
        'verbosity':2}
相关问题