Python doit - 在相关任务中使用参数

Python doit - Use arguments in dependent tasks

我有 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 同时创建这两个任务,但我想在执行任务时而不是在创建任务时使用参数。

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

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}

Is there a way to share or pass the arguments of a task to another one?

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

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