| 1 |
.. meta:: |
| 2 |
:description: pydoit guide - re-using task definitions & alternative ways to define tasks. |
| 3 |
:keywords: python, doit, documentation, guide, task, dynamic workflow |
| 4 |
|
| 5 |
.. title:: Alternative way of defining tasks - pydoit guide |
| 6 |
|
| 7 |
|
| 8 |
More on Task creation |
| 9 |
===================== |
| 10 |
|
| 11 |
|
| 12 |
importing tasks |
| 13 |
--------------- |
| 14 |
|
| 15 |
The *doit* loader will look at **all** objects in the namespace of the *dodo*. |
| 16 |
It will look for functions staring with ``task_`` and objects with |
| 17 |
``create_doit_tasks``. |
| 18 |
So it is also possible to load task definitions from other |
| 19 |
modules just by importing them into your *dodo* file. |
| 20 |
|
| 21 |
.. literalinclude:: samples/import_tasks.py |
| 22 |
|
| 23 |
.. code-block:: console |
| 24 |
|
| 25 |
$ doit list |
| 26 |
echo |
| 27 |
hello |
| 28 |
sample |
| 29 |
|
| 30 |
|
| 31 |
.. note:: |
| 32 |
|
| 33 |
Importing tasks from different modules is useful if you want to split |
| 34 |
your task definitions in different modules. |
| 35 |
|
| 36 |
The best way to create re-usable tasks that can be used in several projects |
| 37 |
is to call functions that return task dict's. |
| 38 |
For example take a look at a reusable *pyflakes* |
| 39 |
`task generator <https://github.com/pydoit/doit-py/blob/master/doitpy/pyflakes.py>`_. |
| 40 |
Check the project `doit-py <https://github.com/pydoit/doit-py>`_ |
| 41 |
for more examples. |
| 42 |
|
| 43 |
|
| 44 |
.. _delayed-task-creation: |
| 45 |
|
| 46 |
delayed task creation |
| 47 |
--------------------- |
| 48 |
|
| 49 |
|
| 50 |
`doit` execution model is divided in two phases: |
| 51 |
|
| 52 |
- *task-loading* : search for task-creator functions (that starts with string `task_`) and create task metadata |
| 53 |
|
| 54 |
- *task-execution* : check which tasks are out-of-date and execute them |
| 55 |
|
| 56 |
Normally *task-loading* is completed before the *task-execution* starts. |
| 57 |
`doit` allows some task metadata to be modified during *task-execution* with |
| 58 |
`calc_deps` and on `uptodate`, but those are restricted to modifying |
| 59 |
already created tasks... |
| 60 |
|
| 61 |
Sometimes it is not possible to know all tasks that should be created before |
| 62 |
some tasks are executed. For these cases `doit` supports |
| 63 |
*delayed task creation*, that means *task-execution* starts before |
| 64 |
*task-loading* is completed. |
| 65 |
|
| 66 |
When *task-creator* function is decorated with `doit.create_after`, |
| 67 |
its evaluation to create the tasks will be delayed to happen after the |
| 68 |
execution of the specified task in the `executed` param. |
| 69 |
|
| 70 |
.. literalinclude:: samples/delayed.py |
| 71 |
|
| 72 |
|
| 73 |
.. _specify-target-regex: |
| 74 |
|
| 75 |
.. note:: |
| 76 |
|
| 77 |
To be able to specify targets created by delayed task loaders to `doit run`, |
| 78 |
it is possible to also specify a regular expression (regex) for every |
| 79 |
delayed task loader. If specified, this regex should match any target name |
| 80 |
possibly generated by this delayed task generator. It can be specified via |
| 81 |
the additional *task-generator* argument `target_regex`. In the above example, |
| 82 |
the regex `.*\\.out` matches every target name ending with `.out`. |
| 83 |
|
| 84 |
It is possible to match every possible target name by specifying `.*`. |
| 85 |
Alternatively, one can use the command line option `--auto-delayed-regex` |
| 86 |
to `doit run`; see :ref:`here <auto-delayed-regex>` for more information. |
| 87 |
|
| 88 |
|
| 89 |
Parameter: `creates` |
| 90 |
++++++++++++++++++++ |
| 91 |
|
| 92 |
In case the task created by a `DelayedTask` has a different *basename* than |
| 93 |
then creator function, or creates several tasks with different *basenames*, |
| 94 |
you should pass the parameter `creates`. |
| 95 |
|
| 96 |
Since `doit` will only execute the body of the task-creator function on demand, |
| 97 |
the tasks names must be explicitly specified... Example: |
| 98 |
|
| 99 |
.. literalinclude:: samples/delayed_creates.py |
| 100 |
|
| 101 |
|
| 102 |
.. warning:: |
| 103 |
|
| 104 |
`doit` normally automatically sets `task_dep` between tasks by checking |
| 105 |
the relation of `file_dep` and `targets`. |
| 106 |
Due to performance reasons, these `task_dep` relations are NOT computed |
| 107 |
for delayed-task's `targets`. |
| 108 |
This problem can avoided by ordering the creation of delayed-tasks with |
| 109 |
the expected order of execution. |
| 110 |
|
| 111 |
|
| 112 |
.. _create-doit-tasks: |
| 113 |
|
| 114 |
custom task definition |
| 115 |
------------------------ |
| 116 |
|
| 117 |
Apart from collect functions that start with the name `task_`. |
| 118 |
The *doit* loader will also execute the ``create_doit_tasks`` |
| 119 |
callable from any object that contains this attribute. |
| 120 |
|
| 121 |
|
| 122 |
.. literalinclude:: samples/custom_task_def.py |
| 123 |
|
| 124 |
The `project letsdoit <https://bitbucket.org/takluyver/letsdoit>`_ |
| 125 |
has some real-world implementations. |
| 126 |
|
| 127 |
For simple examples to help you create your own check this |
| 128 |
`blog post <http://blog.schettino72.net/posts/doit-task-creation.html>`_. |
| 129 |
|
| 130 |
|