| 1 |
.. meta:: |
| 2 |
:description: Customize the up-to-date check for incremental task execution |
| 3 |
:keywords: python, doit, documentation, guide, incremental-build, result-dependency, run_one, config_changed, timeout |
| 4 |
|
| 5 |
.. title:: Customize the up-to-date check for incremental execution |
| 6 |
|
| 7 |
|
| 8 |
================= |
| 9 |
custom up-to-date |
| 10 |
================= |
| 11 |
|
| 12 |
The basics of `uptodate` was already :ref:`introduced <attr-uptodate>`. |
| 13 |
Here we look in more |
| 14 |
detail into some implementations shipped with `doit`. |
| 15 |
|
| 16 |
|
| 17 |
.. _result_dep: |
| 18 |
|
| 19 |
result-dependency |
| 20 |
---------------------- |
| 21 |
|
| 22 |
In some cases you can not determine if a task is "up-to-date" only based on |
| 23 |
input files, the input could come from a database or an external process. |
| 24 |
*doit* defines a "result-dependency" to deal with these cases without need to |
| 25 |
create an intermediate file with the results of the process. |
| 26 |
|
| 27 |
i.e. Suppose you want to send an email every time you run *doit* on a mercurial |
| 28 |
repository that contains a new revision number. |
| 29 |
|
| 30 |
.. literalinclude:: samples/taskresult.py |
| 31 |
|
| 32 |
|
| 33 |
Note the `result_dep` with the name of the task ('version'). `doit` will keep |
| 34 |
track of the output of the task *version* and will execute *send_email* only |
| 35 |
when the mercurial repository has a new version since last |
| 36 |
time *doit* was executed. |
| 37 |
|
| 38 |
The "result" from the dependent task compared between different runs is given |
| 39 |
by its last action. |
| 40 |
The content for python-action is the value of the returned string or dict. |
| 41 |
For cmd-actions it is the output send to stdout plus stderr. |
| 42 |
|
| 43 |
`result_dep` also supports group-tasks. In this case it will check that the |
| 44 |
result of all subtasks did not change. And also the existing sub-tasks are |
| 45 |
the same. |
| 46 |
|
| 47 |
.. _run_once: |
| 48 |
|
| 49 |
|
| 50 |
run_once() |
| 51 |
--------------- |
| 52 |
|
| 53 |
Sometimes there is no dependency for a task but you do not want to execute it |
| 54 |
all the time. With "run_once" the task will not be executed again after the first |
| 55 |
successful run. This is mostly used together with targets. |
| 56 |
|
| 57 |
Suppose you need to download something from internet. |
| 58 |
There is no dependency, but you do not want to download it many times. |
| 59 |
|
| 60 |
|
| 61 |
.. literalinclude:: samples/download.py |
| 62 |
|
| 63 |
Note that even with *run_once* the file will be downloaded again in case the target is removed. |
| 64 |
|
| 65 |
|
| 66 |
.. code-block:: console |
| 67 |
|
| 68 |
$ doit |
| 69 |
. get_pylogo |
| 70 |
$ doit |
| 71 |
-- get_pylogo |
| 72 |
$ rm python-logo.gif |
| 73 |
$ doit |
| 74 |
. get_pylogo |
| 75 |
|
| 76 |
|
| 77 |
.. _timeout: |
| 78 |
|
| 79 |
timeout() |
| 80 |
----------- |
| 81 |
|
| 82 |
``timeout`` is used to expire a task after a certain time interval. |
| 83 |
|
| 84 |
i.e. You want to re-execute a task only if the time elapsed since the last |
| 85 |
time it was executed is bigger than 5 minutes. |
| 86 |
|
| 87 |
.. literalinclude:: samples/timeout.py |
| 88 |
|
| 89 |
|
| 90 |
``timeout`` is function that takes an ``int`` (seconds) or ``timedelta`` as a |
| 91 |
parameter. It returns a callable suitable to be used as an ``uptodate`` callable. |
| 92 |
|
| 93 |
|
| 94 |
.. _config_changed: |
| 95 |
|
| 96 |
config_changed() |
| 97 |
----------------- |
| 98 |
|
| 99 |
``config_changed`` is used to check if any "configuration" value for the task has |
| 100 |
changed. Config values can be a string or dict. |
| 101 |
|
| 102 |
For dict's the values are converted to string (using `json.dumps()` with `sort_key=True`) |
| 103 |
and only a digest/checksum of the dictionaries keys and values are saved. |
| 104 |
|
| 105 |
If converting the values of the dict requires a special encoder this can be |
| 106 |
passed with the argument ``encoder=...``. This will be passed on to `json.dumps()`. |
| 107 |
|
| 108 |
.. literalinclude:: samples/config_params.py |
| 109 |
|
| 110 |
|
| 111 |
.. _check_timestamp_unchanged: |
| 112 |
|
| 113 |
check_timestamp_unchanged() |
| 114 |
----------------------------- |
| 115 |
|
| 116 |
``check_timestamp_unchanged`` is used to check if specified timestamp of a given |
| 117 |
file/dir is unchanged since last run. |
| 118 |
|
| 119 |
The timestamp field to check defaults to ``mtime``, but can be selected by |
| 120 |
passing ``time`` parameter which can be one of: ``atime``, ``ctime``, ``mtime`` |
| 121 |
(or their aliases ``access``, ``status``, ``modify``). |
| 122 |
|
| 123 |
Note that ``ctime`` or ``status`` is platform dependent. |
| 124 |
On Unix it is the time of most recent metadata change, |
| 125 |
on Windows it is the time of creation. |
| 126 |
See `Python library documentation for os.stat`__ and Linux man page for |
| 127 |
stat(2) for details. |
| 128 |
|
| 129 |
__ http://docs.python.org/library/os.html#os.stat |
| 130 |
|
| 131 |
It also accepts an ``cmp_op`` parameter which defaults to ``operator.eq`` (==). |
| 132 |
To use it pass a callable which takes two parameters (prev_time, current_time) |
| 133 |
and returns True if task should be considered up-to-date, False otherwise. |
| 134 |
Here ``prev_time`` is the time from the last successful run and ``current_time`` |
| 135 |
is the time obtained in current run. |
| 136 |
|
| 137 |
If the specified file does not exist, an exception will be raised. |
| 138 |
If a file is a target of another task you should probably add |
| 139 |
``task_dep`` on that task to ensure the file is created before it is checked. |
| 140 |
|
| 141 |
.. literalinclude:: samples/check_timestamp_unchanged.py |