| 1 |
.. meta:: |
| 2 |
:description: pydoit guide - doit has a flexible dependency system, not exclusively based on files |
| 3 |
:keywords: python, doit, documentation, guide, task, dependency, incremental-build, up-to-date |
| 4 |
|
| 5 |
.. title:: pydoit task dependency system support - pydoit guide |
| 6 |
|
| 7 |
===================== |
| 8 |
Non-file dependencies |
| 9 |
===================== |
| 10 |
|
| 11 |
.. _attr-uptodate: |
| 12 |
|
| 13 |
uptodate |
| 14 |
---------- |
| 15 |
|
| 16 |
Apart from file dependencies you can extend `doit` to support other ways |
| 17 |
to determine if a task is up-to-date through the attribute ``uptodate``. |
| 18 |
|
| 19 |
This can be used in cases where you need to some kind of calculation to |
| 20 |
determine if the task is up-to-date or not. |
| 21 |
|
| 22 |
``uptodate`` is a list where each element can be True, False, None, a callable |
| 23 |
or a command(string). |
| 24 |
|
| 25 |
* ``False`` indicates that the task is NOT up-to-date |
| 26 |
* ``True`` indicates that the task is up-to-date |
| 27 |
* ``None`` values will just be ignored. This is used when the value is dynamically calculated |
| 28 |
|
| 29 |
|
| 30 |
.. note:: |
| 31 |
|
| 32 |
An ``uptodate`` value equal to ``True`` does not override others |
| 33 |
up-to-date checks. It is one more way to check if task is **not** up-to-date. |
| 34 |
|
| 35 |
i.e. if uptodate==True but a file_dep changes the task is still |
| 36 |
considered **not** up-to-date. |
| 37 |
|
| 38 |
|
| 39 |
If an ``uptodate`` item is a string it will be executed on the shell. |
| 40 |
If the process exits with the code ``0``, it is considered as up-to-date. |
| 41 |
All other values would be considered as not up-to-date. |
| 42 |
|
| 43 |
``uptodate`` elements can also be a callable that will be executed on runtime |
| 44 |
(not when the task is being created). |
| 45 |
The section ``custom-uptodate`` will explain in details how to extend `doit` |
| 46 |
writing your own callables for ``uptodate``. This callables will typically |
| 47 |
compare a value on the present time with a value calculated on the last |
| 48 |
successful execution. |
| 49 |
|
| 50 |
.. note:: |
| 51 |
|
| 52 |
There is no guarantee ``uptodate`` callables or commands will be executed. |
| 53 |
`doit` short-circuit the checks, if it is already determined that the |
| 54 |
task is no `up-to-date` it will not execute remaining ``uptodate`` checks. |
| 55 |
|
| 56 |
|
| 57 |
`doit` includes several implementations to be used as ``uptodate``. |
| 58 |
They are all included in module `doit.tools` and will be discussed in detail |
| 59 |
:ref:`later <uptodate_api>`: |
| 60 |
|
| 61 |
* :ref:`result_dep <result_dep>`: check if the result of another task |
| 62 |
has changed |
| 63 |
* :ref:`run_once <run_once>`: execute a task only once |
| 64 |
(used for tasks without dependencies) |
| 65 |
* :ref:`timeout <timeout>`: indicate that a task should "expire" after |
| 66 |
a certain time interval |
| 67 |
* :ref:`config_changed <config_changed>`: check for changes in |
| 68 |
a "configuration" string or dictionary |
| 69 |
* :ref:`check_timestamp_unchanged`: check access, |
| 70 |
status change/create or modify timestamp of a given file/directory |
| 71 |
|
| 72 |
|
| 73 |
.. _up-to-date-def: |
| 74 |
|
| 75 |
doit up-to-date definition |
| 76 |
----------------------------- |
| 77 |
|
| 78 |
A task is **not** up-to-date if any of: |
| 79 |
|
| 80 |
* an :ref:`uptodate <attr-uptodate>` item is (or evaluates to) `False` |
| 81 |
* a file is added to or removed from `file_dep` |
| 82 |
* a `file_dep` changed since last successful execution |
| 83 |
* a `target` path does not exist |
| 84 |
* a task has no `file_dep` and `uptodate` item equal to `True` |
| 85 |
|
| 86 |
It means that if a task does not explicitly define any *input* (dependency) |
| 87 |
it will never be considered `up-to-date`. |
| 88 |
|
| 89 |
Note that since a `target` represents an *output* of the task, |
| 90 |
a missing `target` is enough to determine that a task is not `up-to-date`. |
| 91 |
But its existence by itself is not enough to mark a task `up-to-date`. |
| 92 |
|
| 93 |
In some situations, it is useful to define a task with targets but no |
| 94 |
dependencies. If you want to re-execute this task only when targets are missing |
| 95 |
you must explicitly add a dependency: you could add a ``uptodate`` with ``True`` |
| 96 |
value or use :ref:`run_once() <run_once>` to force at least one |
| 97 |
execution managed by `doit`. Example: |
| 98 |
|
| 99 |
.. literalinclude:: samples/touch.py |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
Apart from ``file_dep`` and ``uptodate`` used to determine if a task |
| 104 |
is `up-to-date` or not, |
| 105 |
``doit`` also includes other kind of dependencies (introduced below) |
| 106 |
to help you combine tasks |
| 107 |
so they are executed in appropriate order. |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
.. _uptodate_api: |
| 112 |
|
| 113 |
uptodate API |
| 114 |
-------------- |
| 115 |
|
| 116 |
This section will explain how to extend ``doit`` writing an ``uptodate`` |
| 117 |
implementation. So unless you need to write an ``uptodate`` implementation |
| 118 |
you can skip this. |
| 119 |
|
| 120 |
Let's start with trivial example. `uptodate` is a function that returns |
| 121 |
a boolean value. |
| 122 |
|
| 123 |
.. literalinclude:: samples/uptodate_callable.py |
| 124 |
|
| 125 |
You could also execute this function in the task-creator and pass the value |
| 126 |
to to `uptodate`. The advantage of just passing the callable is that this |
| 127 |
check will not be executed at all if the task was not selected to be executed. |
| 128 |
|
| 129 |
|
| 130 |
Example: run-once implementation |
| 131 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 132 |
|
| 133 |
Most of the time an `uptodate` implementation will compare the current value |
| 134 |
of something with the value it had last time the task was executed. |
| 135 |
|
| 136 |
We already saw how tasks can save values by returning dict on its actions. |
| 137 |
But usually the "value" we want to check is independent from the task actions. |
| 138 |
So the first step is to add a callable to the task so it can save some extra |
| 139 |
values. These values are not used by the task itself, they are only used |
| 140 |
for dependency checking. |
| 141 |
|
| 142 |
The Task has a property called ``value_savers`` that contains a list of |
| 143 |
callables. These callables should return a dict that will be saved together |
| 144 |
with other task values. The ``value_savers`` will be executed after all actions. |
| 145 |
|
| 146 |
The second step is to actually compare the saved value with its "current" value. |
| 147 |
|
| 148 |
The `uptodate` callable can take two positional parameters ``task`` and ``values``. The callable can also be represented by a tuple (callable, args, kwargs). |
| 149 |
|
| 150 |
|
| 151 |
- ``task`` parameter will give you access to task object. So you have access |
| 152 |
to its metadata and opportunity to modify the task itself! |
| 153 |
- ``values`` is a dictionary with the computed values saved in the last |
| 154 |
successful execution of the task. |
| 155 |
|
| 156 |
|
| 157 |
Let's take a look in the ``run_once`` implementation. |
| 158 |
|
| 159 |
.. literalinclude:: samples/run_once.py |
| 160 |
|
| 161 |
The function ``save_executed`` returns a dict. In this case it is not checking |
| 162 |
for any value because it just checks it the task was ever executed. |
| 163 |
|
| 164 |
The next line we use the ``task`` parameter adding |
| 165 |
``save_executed`` to ``task.value_savers``.So whenever this task is executed this |
| 166 |
task value 'run-once' will be saved. |
| 167 |
|
| 168 |
Finally the return value should be a boolean to indicate if the task is |
| 169 |
up-to-date or not. Remember that the 'values' parameter contains the dict with |
| 170 |
the values saved from last successful execution of the task. |
| 171 |
So it just checks if this task was executed before by looking for the |
| 172 |
``run-once`` entry in ```values``. |
| 173 |
|
| 174 |
|
| 175 |
Example: timeout implementation |
| 176 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 177 |
|
| 178 |
Let's look another example, the ``timeout``. The main difference is that |
| 179 |
we actually pass the parameter ``timeout_limit``. Here we present |
| 180 |
a simplified version that only accepts integers (seconds) as a parameter. |
| 181 |
|
| 182 |
|
| 183 |
.. code-block:: python |
| 184 |
|
| 185 |
class timeout(object): |
| 186 |
def __init__(self, timeout_limit): |
| 187 |
self.limit_sec = timeout_limit |
| 188 |
|
| 189 |
def __call__(self, task, values): |
| 190 |
def save_now(): |
| 191 |
return {'success-time': time_module.time()} |
| 192 |
task.value_savers.append(save_now) |
| 193 |
last_success = values.get('success-time', None) |
| 194 |
if last_success is None: |
| 195 |
return False |
| 196 |
return (time_module.time() - last_success) < self.limit_sec |
| 197 |
|
| 198 |
This is a class-based implementation where the objects are made callable |
| 199 |
by implementing a ``__call__`` method. |
| 200 |
|
| 201 |
On ``__init__`` we just save the ``timeout_limit`` as an attribute. |
| 202 |
|
| 203 |
The ``__call__`` is very similar with the ``run-once`` implementation. |
| 204 |
First it defines a function (``save_now``) that is registered |
| 205 |
into ``task.value_savers``. Than it compares the current time |
| 206 |
with the time that was saved on last successful execution. |
| 207 |
|
| 208 |
|
| 209 |
Example: result_dep implementation |
| 210 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 211 |
|
| 212 |
The ``result_dep`` is more complicated due to two factors. It needs to modify |
| 213 |
the task's ``task_dep``. |
| 214 |
And it needs to check the task's saved values and metadata |
| 215 |
from a task different from where it is being applied. |
| 216 |
|
| 217 |
A ``result_dep`` implies that its dependency is also a ``task_dep``. |
| 218 |
We have seen that the callable takes a `task` parameter that we used |
| 219 |
to modify the task object. The problem is that modifying ``task_dep`` |
| 220 |
when the callable gets called would be "too late" according to the |
| 221 |
way `doit` works. When an object is passed ``uptodate`` and this |
| 222 |
object's class has a method named ``configure_task`` it will be called |
| 223 |
during the task creation. |
| 224 |
|
| 225 |
The base class ``dependency.UptodateCalculator`` gives access to |
| 226 |
an attribute named ``tasks_dict`` containing a dictionary with |
| 227 |
all task objects where the ``key`` is the task name (this is used to get all |
| 228 |
sub-tasks from a task-group). And also a method called ``get_val`` to access |
| 229 |
the saved values and results from any task. |
| 230 |
|
| 231 |
See the `result_dep` `source <https://github.com/pydoit/doit/blob/master/doit/task.py>`_. |
| 232 |
|
| 233 |
|
| 234 |
task-dependency |
| 235 |
--------------- |
| 236 |
|
| 237 |
It is used to enforce tasks are executed on the desired order. |
| 238 |
By default tasks are executed on the same order as they were defined in |
| 239 |
the `dodo` file. To define a dependency on another task use the |
| 240 |
task name (whatever comes after ``task_`` on the function name) in the |
| 241 |
"task_dep" attribute. |
| 242 |
|
| 243 |
|
| 244 |
.. note:: |
| 245 |
|
| 246 |
A *task-dependency* **only** indicates that another task should be "executed" |
| 247 |
before itself. The task-dependency might not really be executed if it |
| 248 |
is *up-to-date*. |
| 249 |
|
| 250 |
.. note:: |
| 251 |
|
| 252 |
*task-dependencies* are **not** used to determine if a task is up-to-date or |
| 253 |
not. If a task defines only *task-dependency* it will always be executed. |
| 254 |
|
| 255 |
|
| 256 |
This example we make sure we include a file with the latest revision number of |
| 257 |
the mercurial repository on the tar file. |
| 258 |
|
| 259 |
.. literalinclude:: samples/tar.py |
| 260 |
|
| 261 |
.. code-block:: console |
| 262 |
|
| 263 |
$ doit |
| 264 |
. version |
| 265 |
. tar |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
groups |
| 270 |
^^^^^^^ |
| 271 |
|
| 272 |
You can define a group of tasks by adding tasks as dependencies and setting |
| 273 |
its `actions` to ``None``. |
| 274 |
|
| 275 |
.. literalinclude:: samples/group.py |
| 276 |
|
| 277 |
Note that tasks are never executed twice in the same "run". |
| 278 |
|
| 279 |
|
| 280 |
setup-task |
| 281 |
------------- |
| 282 |
|
| 283 |
Some tasks may require some kind of environment setup. |
| 284 |
In this case they can define a list of "setup" tasks. |
| 285 |
|
| 286 |
* the setup-task will be executed only if the task is to be executed (not up-to-date) |
| 287 |
* setup-tasks are just normal tasks that follow all other task behavior |
| 288 |
|
| 289 |
.. note:: |
| 290 |
|
| 291 |
A *task-dependency* is executed before checking if the task is up-to-date. |
| 292 |
A *setup-task* is executed after the checking if the task is up-to-date and |
| 293 |
it is executed only if the task is not up-to-date and will be executed. |
| 294 |
|
| 295 |
|
| 296 |
teardown |
| 297 |
^^^^^^^^^^^ |
| 298 |
Task may also define 'teardown' actions. |
| 299 |
These actions are executed after all tasks have finished their execution. |
| 300 |
They are executed in reverse order their tasks were executed. |
| 301 |
|
| 302 |
|
| 303 |
Example: |
| 304 |
|
| 305 |
.. literalinclude:: samples/tsetup.py |
| 306 |
|
| 307 |
|
| 308 |
.. code-block:: console |
| 309 |
|
| 310 |
$ doit withenvX |
| 311 |
. setup_sample:setupX |
| 312 |
start setupX |
| 313 |
. withenvX:c |
| 314 |
x c |
| 315 |
. withenvX:b |
| 316 |
x b |
| 317 |
. withenvX:a |
| 318 |
x a |
| 319 |
stop setupX |
| 320 |
$ doit withenvY |
| 321 |
. setup_sample:setupY |
| 322 |
start setupY |
| 323 |
. withenvY |
| 324 |
y |
| 325 |
stop setupY |
| 326 |
|
| 327 |
|
| 328 |
saving computed values |
| 329 |
------------------------ |
| 330 |
|
| 331 |
Tasks can save computed values by returning a dictionary on it's python-actions. |
| 332 |
The values must be JSON encodable. |
| 333 |
|
| 334 |
A cmd-action can also save it's output. |
| 335 |
But for this you will need to explicitly import `CmdAction` and set its `save_out` |
| 336 |
parameter with the *name* used to save the output in *values* |
| 337 |
|
| 338 |
.. literalinclude:: samples/save_out.py |
| 339 |
|
| 340 |
|
| 341 |
These values can be used on uptodate_ and getargs_. |
| 342 |
Check those sections for examples. |
| 343 |
|
| 344 |
|
| 345 |
.. _getargs: |
| 346 |
|
| 347 |
getargs |
| 348 |
-------- |
| 349 |
|
| 350 |
`getargs` provides a way to use values computed from one task in another task. |
| 351 |
The values are taken from "saved computed values" |
| 352 |
(returned dict from a python-action). |
| 353 |
|
| 354 |
For *cmd-action* use dictionary-based string formatting. Formatting style is controlled by ``action_string_formatting`` key in ``DOIT_CONFIG`` (see :ref:`keywords on cmd-action string <keywords-on-cmd-action-string>`). |
| 355 |
|
| 356 |
|
| 357 |
For *python-action* the action callable parameter names must match with keys |
| 358 |
from `getargs`. |
| 359 |
|
| 360 |
`getargs` is a dictionary where the key is the argument name used on actions, |
| 361 |
and the value is a tuple with 2 strings: task name, "value name". |
| 362 |
|
| 363 |
.. literalinclude:: samples/getargs.py |
| 364 |
|
| 365 |
|
| 366 |
The values are being passed on to a python-action you can pass the whole dict |
| 367 |
by specifying the value name as ``None``. |
| 368 |
|
| 369 |
.. literalinclude:: samples/getargs_dict.py |
| 370 |
|
| 371 |
|
| 372 |
If a group-task is used, the values from all its sub-tasks are passed as a dict. |
| 373 |
|
| 374 |
.. literalinclude:: samples/getargs_group.py |
| 375 |
|
| 376 |
|
| 377 |
.. note:: |
| 378 |
``getargs`` creates an implicit setup-task. |
| 379 |
|
| 380 |
|
| 381 |
.. _attr-calc_dep: |
| 382 |
|
| 383 |
calculated-dependencies |
| 384 |
------------------------ |
| 385 |
|
| 386 |
Calculation of dependencies might be an expensive operation, so not suitable |
| 387 |
to be done on load time by task-creators. |
| 388 |
For this situation it is better to delegate |
| 389 |
the calculation of dependencies to another task. |
| 390 |
The task calculating dependencies must have a python-action returning a |
| 391 |
dictionary with `file_dep`, `task_dep`, `uptodate` or another `calc_dep`. |
| 392 |
|
| 393 |
.. note:: |
| 394 |
An alternative way (and often easier) to have task attributes that |
| 395 |
rely on other tasks execution is to use :ref:`delayed tasks <delayed-task-creation>`. |
| 396 |
|
| 397 |
|
| 398 |
On the example below ``mod_deps`` prints on the screen all direct dependencies |
| 399 |
from a module. The dependencies itself are calculated on task ``get_dep`` |
| 400 |
(note: get_dep has a fake implementation where the results are taken from a dict). |
| 401 |
|
| 402 |
|
| 403 |
.. literalinclude:: samples/calc_dep.py |
| 404 |
|
| 405 |
|
| 406 |
|