Python doit - Use arguments in dependent tasks

▼魔方 西西 提交于 2019-12-12 03:18:40

问题


I have 2 doit tasks, one having a dependency on the other. For example:

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']
    }

Is there a way to share or pass the arguments of a task to another one? I have read pretty much everything I could on task creation and dependency on pydoit.org, but haven't found anything similar to what I want.

I am aware that I could use yield to create these two tasks at the same time, but I'd like to use a parameter when executing the task, not when I am creating it.


回答1:


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

Yes. Using getargs: http://pydoit.org/dependencies.html#getargs

In your example, you would need to add another action to the task deploy just to save the passed parameter.




回答2:


You could just use a global variable like commonCommand. If you have more complex needs, create a class to handle it.

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}


来源:https://stackoverflow.com/questions/34957460/python-doit-use-arguments-in-dependent-tasks

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!