sourcenouveau
sourcenouveau

Reputation: 30534

Dynamic target names in Python doit

I have a Python doit script with task that generates files. The file names change depending on task arguments.

Is there any way to include these files in the targets list?

Unfortunately task arguments do not get populated in target name strings.

'targets': ['targetfile{version}']

Example task:

def task_dynamic():
    return {
        'actions': ['touch targetfile{version}'],
        'params': [
            {
                'name': 'version',
                'short': 'v',
                'default': '0'
            }
        ],
        'targets': []
    }

Example command-line:

$ doit dynamic -v 8

Upvotes: 0

Views: 613

Answers (2)

schettino72
schettino72

Reputation: 3170

You can dynamically modify task attributes on actions (just pass the magic parameter task), or on custom uptodate i.e.

DOIT_CONFIG = {
    'action_string_formatting': 'new',
}

def set_target(task, values):
    version = task.options.get('version', '0')
    task.targets.append(f'targetfile{version}')


def task_dynamic():
    return {
        'actions': ['touch targetfile{version}'],
        'params': [
            {
                'name': 'version',
                'short': 'v',
                'default': '0'
            }
        ],
        'uptodate': [set_target],
    }

There is a proposed change that will allow this to be achieved more easily, see issue #311

Upvotes: 1

sourcenouveau
sourcenouveau

Reputation: 30534

There is a non-ideal resolution: I put my dynamic outputs in a directory with a stable name, and then referred to that directory in my targets list.

Upvotes: 0

Related Questions