| 1 |
|
| 2 |
.. meta:: |
| 3 |
:description: pydoit guide - CLI usage and options |
| 4 |
:keywords: python, doit, documentation, guide, task-runner |
| 5 |
|
| 6 |
.. title:: pydoit CLI usage and options - command run |
| 7 |
|
| 8 |
============================== |
| 9 |
Command line interface - run |
| 10 |
============================== |
| 11 |
|
| 12 |
A general `doit` command goes like this: |
| 13 |
|
| 14 |
.. code-block:: console |
| 15 |
|
| 16 |
$ doit [run] [<options>] [<task|target> <task_options>]* [<variables>] |
| 17 |
|
| 18 |
|
| 19 |
The `doit` command line contains several sub-commands. Most of the time you just |
| 20 |
want to execute your tasks, that's what *run* does. Since it is by far the most |
| 21 |
common operation it is also the default, so if you don't specify any sub-command |
| 22 |
to *doit* it will execute *run*. So ``$ doit`` and ``$ doit run`` do the same |
| 23 |
thing. |
| 24 |
|
| 25 |
The basics of task selection were introduced in :ref:`Task Selection |
| 26 |
<task-selection>`. |
| 27 |
|
| 28 |
|
| 29 |
`python -m doit` |
| 30 |
----------------- |
| 31 |
|
| 32 |
`doit` can also be executed without using the `doit` script. |
| 33 |
|
| 34 |
.. code-block:: console |
| 35 |
|
| 36 |
$ python -m doit |
| 37 |
|
| 38 |
This is specially useful when testing `doit` with different python versions. |
| 39 |
|
| 40 |
|
| 41 |
dodo file |
| 42 |
---------- |
| 43 |
|
| 44 |
By default all commands are relative to ``dodo.py`` in the current folder. |
| 45 |
You can specify a different *dodo* file containing task with the flag ``-f``. |
| 46 |
This flag is valid for all sub-commands. |
| 47 |
|
| 48 |
|
| 49 |
.. code-block:: console |
| 50 |
|
| 51 |
$ doit -f release.py |
| 52 |
|
| 53 |
|
| 54 |
*doit* can seek for the ``dodo.py`` file on parent folders if the option |
| 55 |
``--seek-file`` is specified. |
| 56 |
|
| 57 |
|
| 58 |
as an executable file |
| 59 |
----------------------- |
| 60 |
|
| 61 |
using a hashbang |
| 62 |
^^^^^^^^^^^^^^^^^^^^^ |
| 63 |
|
| 64 |
If you have `doit` installed on ``/usr/bin`` use the following hashbang: |
| 65 |
|
| 66 |
.. code-block:: bash |
| 67 |
|
| 68 |
#! /usr/bin/doit -f |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
using the API |
| 73 |
^^^^^^^^^^^^^^ |
| 74 |
|
| 75 |
It is possible to make a ``dodo`` file become an executable on its own |
| 76 |
by calling the ``doit.run()``, you need to pass the ``globals``: |
| 77 |
|
| 78 |
|
| 79 |
.. literalinclude:: samples/executable.py |
| 80 |
|
| 81 |
.. note:: |
| 82 |
|
| 83 |
The ``doit.run()`` method will call ``sys.exit()`` so any code after it |
| 84 |
will not be executed. |
| 85 |
|
| 86 |
|
| 87 |
``doit.run()`` parameter will be passed to a :ref:`ModuleTaskLoader <ModuleTaskLoader>` to find your tasks. |
| 88 |
|
| 89 |
|
| 90 |
from IPython |
| 91 |
------------------ |
| 92 |
|
| 93 |
You can install and use the `%doit` magic function to load tasks defined |
| 94 |
directly in IPython's global namespace (:ref:`more <tools.IPython>`). |
| 95 |
|
| 96 |
|
| 97 |
returned value |
| 98 |
------------------ |
| 99 |
|
| 100 |
``doit`` process returns: |
| 101 |
|
| 102 |
* 0 => all tasks executed successfully |
| 103 |
* 1 => task failed |
| 104 |
* 2 => error executing task |
| 105 |
* 3 => error before task execution starts |
| 106 |
(in this case the reporter is not used) |
| 107 |
|
| 108 |
.. _db_backends: |
| 109 |
|
| 110 |
DB backend |
| 111 |
-------------- |
| 112 |
|
| 113 |
`doit` saves the results of your tasks runs in a "DB-file", it supports |
| 114 |
different backends: |
| 115 |
|
| 116 |
- `dbm`: (default) It uses `python dbm module <https://docs.python.org/3/library/dbm.html>`_. The actual DBM used depends on what is available on your machine/platform. |
| 117 |
|
| 118 |
- `json`: Plain text using a json structure, it is slow but good for debugging. |
| 119 |
|
| 120 |
- `sqlite3`: Support concurrent access |
| 121 |
(DB is updated only once when process is terminated for better performance). |
| 122 |
|
| 123 |
|
| 124 |
From the command line you can select the backend using the ``--backend`` option. |
| 125 |
|
| 126 |
It is quite easy to add a new backend for any key-value store. |
| 127 |
|
| 128 |
|
| 129 |
.. warning: |
| 130 |
|
| 131 |
`dbm` modules do not support concurrent access through different processes. |
| 132 |
`dbm.dumb` will even cause file corruption! |
| 133 |
|
| 134 |
|
| 135 |
DB-file |
| 136 |
---------- |
| 137 |
|
| 138 |
Option ``--db-file`` sets the name of the file to save the "DB", |
| 139 |
default is ``.doit.db``. |
| 140 |
Note that DBM backends might save more than one file, in this case |
| 141 |
the specified name is used as a base name. |
| 142 |
|
| 143 |
To configure in a `dodo` file the field name is ``dep_file`` |
| 144 |
|
| 145 |
.. code-block:: python |
| 146 |
|
| 147 |
DOIT_CONFIG = { |
| 148 |
'backend': 'json', |
| 149 |
'dep_file': 'doit-db.json', |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
.. _verbosity_option: |
| 154 |
|
| 155 |
verbosity |
| 156 |
----------- |
| 157 |
|
| 158 |
Option to change the default global task :ref:`verbosity<verbosity>` value. |
| 159 |
|
| 160 |
.. code-block:: console |
| 161 |
|
| 162 |
$ doit --verbosity 2 |
| 163 |
|
| 164 |
|
| 165 |
failure-verbosity |
| 166 |
----------------- |
| 167 |
|
| 168 |
Option to control if stdout/stderr should be re-displayed in the end of |
| 169 |
of report. This is useful when used in conjunction with `--continue` option. |
| 170 |
|
| 171 |
.. code-block:: console |
| 172 |
|
| 173 |
$ doit --failure-verbosity 1 |
| 174 |
|
| 175 |
|
| 176 |
output buffering |
| 177 |
---------------- |
| 178 |
|
| 179 |
The output (`stdout` and `stderr`) is by default line-buffered |
| 180 |
for `CmdAction`. You can change that by specifying the `buffering` |
| 181 |
parameter when creating a `CmdAction`. The value zero (the default) |
| 182 |
means line-buffered, positive integers are the number of bytes to |
| 183 |
be read per call. |
| 184 |
|
| 185 |
Note this controls the buffering from the `doit` process and the |
| 186 |
terminal, not to be confused with subprocess.Popen `buffered`. |
| 187 |
|
| 188 |
|
| 189 |
.. code-block:: python |
| 190 |
|
| 191 |
from doit.action import CmdAction |
| 192 |
|
| 193 |
def task_progress(): |
| 194 |
return { |
| 195 |
'actions': [CmdAction("progress_bar", buffering=1)], |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
dir (cwd) |
| 202 |
----------- |
| 203 |
|
| 204 |
By default the directory of the `dodo` file is used as the |
| 205 |
"current working directory" on python execution. |
| 206 |
You can specify a different *cwd* with the *-d*/*--dir* option. |
| 207 |
|
| 208 |
.. code-block:: console |
| 209 |
|
| 210 |
$ doit --dir path/to/another/cwd |
| 211 |
|
| 212 |
.. note:: |
| 213 |
|
| 214 |
It is possible to get a reference to the original initial |
| 215 |
current working directory (location where the command line |
| 216 |
was executed) using :ref:`initial_workdir`. |
| 217 |
|
| 218 |
|
| 219 |
continue |
| 220 |
--------- |
| 221 |
|
| 222 |
By default the execution of tasks is halted on the first task failure or error. You can force it to continue execution with the option --continue/-c |
| 223 |
|
| 224 |
.. code-block:: console |
| 225 |
|
| 226 |
$ doit --continue |
| 227 |
|
| 228 |
|
| 229 |
single task execution |
| 230 |
---------------------- |
| 231 |
|
| 232 |
The option ``-s/--single`` can be used to execute a task without executing |
| 233 |
its task dependencies. |
| 234 |
|
| 235 |
.. code-block:: console |
| 236 |
|
| 237 |
$ doit -s do_something |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
.. _parallel-execution: |
| 242 |
|
| 243 |
parallel execution |
| 244 |
------------------- |
| 245 |
|
| 246 |
`doit` supports parallel execution --process/-n. |
| 247 |
This allows different tasks to be run in parallel, as long any dependencies are met. |
| 248 |
By default the `multiprocessing <http://docs.python.org/library/multiprocessing.html>`_ |
| 249 |
module is used. |
| 250 |
So the same restrictions also apply to the use of multiprocessing in `doit`. |
| 251 |
|
| 252 |
.. code-block:: console |
| 253 |
|
| 254 |
$ doit -n 3 |
| 255 |
|
| 256 |
You can also execute in parallel using threads by specifying the option |
| 257 |
`--parallel-type/-P`. |
| 258 |
|
| 259 |
.. code-block:: console |
| 260 |
|
| 261 |
$ doit -n 3 -P thread |
| 262 |
|
| 263 |
|
| 264 |
.. note:: |
| 265 |
|
| 266 |
The actions of a single task are always run sequentially; |
| 267 |
only tasks and sub-tasks are affected by the parallel execution option. |
| 268 |
|
| 269 |
.. warning:: |
| 270 |
|
| 271 |
On Windows, due to some limitations on how `multiprocess` works, |
| 272 |
there are stricter requirements for task properties being picklable than |
| 273 |
other platforms. |
| 274 |
|
| 275 |
|
| 276 |
.. _reporter: |
| 277 |
|
| 278 |
reporter |
| 279 |
--------- |
| 280 |
|
| 281 |
`doit` provides different "*reporters*" to display running tasks info |
| 282 |
on the console. |
| 283 |
Use the option --reporter/-r to choose a reporter. |
| 284 |
Apart from the default it also includes: |
| 285 |
|
| 286 |
* executed-only: Produces zero output if no task is executed |
| 287 |
* json: Output results in JSON format |
| 288 |
* zero: display only error messages (does not display info on tasks |
| 289 |
being executed/skipped). This is used when you only want to see |
| 290 |
the output generated by the tasks execution. |
| 291 |
|
| 292 |
.. code-block:: console |
| 293 |
|
| 294 |
$ doit --reporter json |
| 295 |
|
| 296 |
.. _custom_reporter: |
| 297 |
|
| 298 |
custom reporter |
| 299 |
----------------- |
| 300 |
|
| 301 |
It is possible to define your own custom reporter. Check the code on |
| 302 |
`doit/reporter.py |
| 303 |
<https://github.com/pydoit/doit/blob/master/doit/reporter.py>`_ ... It is easy |
| 304 |
to get started by sub-classing the default reporter as shown below. The custom |
| 305 |
reporter can be enabled directly on DOIT_CONFIG dict. |
| 306 |
|
| 307 |
.. literalinclude:: samples/custom_reporter.py |
| 308 |
|
| 309 |
It is also possible distribute/use a custom reporter |
| 310 |
as a :ref:`plugin <plugin_reporter>`. |
| 311 |
|
| 312 |
Note that the ``reporter`` have no control over the *real time* output |
| 313 |
from a task while it is being executed, |
| 314 |
this is controlled by the ``verbosity`` param. |
| 315 |
|
| 316 |
|
| 317 |
check_file_uptodate |
| 318 |
------------------- |
| 319 |
|
| 320 |
`doit` provides different options to check if dependency files are up to date |
| 321 |
(see :ref:`file-dep`). Use the option ``--check_file_uptodate`` to choose: |
| 322 |
|
| 323 |
* `md5`: use the md5sum. |
| 324 |
* `timestamp`: use the timestamp. |
| 325 |
|
| 326 |
.. note:: |
| 327 |
|
| 328 |
The `timestamp` checker considers a file is not up-to-date if there is |
| 329 |
**any** change in the the modified time (`mtime`), it does not matter if |
| 330 |
the new time is in the future or past of the original timestamp. |
| 331 |
|
| 332 |
|
| 333 |
You can set this option from command line, but you probably want to set it for |
| 334 |
all commands using `DOIT_CONFIG`. |
| 335 |
|
| 336 |
.. code-block:: console |
| 337 |
|
| 338 |
DOIT_CONFIG = {'check_file_uptodate': 'timestamp'} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
custom check_file_uptodate |
| 344 |
^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 345 |
|
| 346 |
It is possible to define your own custom up to date checker. Check the code on |
| 347 |
`doit/dependency.py |
| 348 |
<https://github.com/pydoit/doit/blob/master/doit/dependency.py>`_ ... |
| 349 |
Sub-class ``FileChangedChecker`` and define the 2 required methods as shown |
| 350 |
below. The custom checker must be configured using DOIT_CONFIG dict. |
| 351 |
|
| 352 |
.. code-block:: python |
| 353 |
|
| 354 |
from doit.dependency import FileChangedChecker |
| 355 |
|
| 356 |
class MyChecker(FileChangedChecker): |
| 357 |
"""With this checker, files are always out of date.""" |
| 358 |
def check_modified(self, file_path, file_stat, state): |
| 359 |
return True |
| 360 |
def get_state(self, dep, current_state): |
| 361 |
pass |
| 362 |
|
| 363 |
DOIT_CONFIG = {'check_file_uptodate': MyChecker} |
| 364 |
|
| 365 |
|
| 366 |
output-file |
| 367 |
------------ |
| 368 |
|
| 369 |
The option --output-file/-o let you output the result to a file. |
| 370 |
|
| 371 |
.. code-block:: console |
| 372 |
|
| 373 |
$ doit --output-file result.txt |
| 374 |
|
| 375 |
|
| 376 |
pdb |
| 377 |
------- |
| 378 |
|
| 379 |
If the option ``--pdb`` is used, a post-mortem debugger will be launched in case |
| 380 |
of a unhandled exception while loading tasks. |
| 381 |
|
| 382 |
|
| 383 |
.. _initial_workdir: |
| 384 |
|
| 385 |
get_initial_workdir() |
| 386 |
--------------------- |
| 387 |
|
| 388 |
When `doit` executes by default it will use the location of `dodo.py` |
| 389 |
as the current working directory (unless --dir is specified). |
| 390 |
The value of `doit.get_initial_workdir()` will contain the path |
| 391 |
from where `doit` was invoked from. |
| 392 |
|
| 393 |
This can be used for example set which tasks will be executed: |
| 394 |
|
| 395 |
.. literalinclude:: samples/initial_workdir.py |
| 396 |
|
| 397 |
|
| 398 |
codec_cls |
| 399 |
--------- |
| 400 |
|
| 401 |
`doit` uses the ``json`` package to serialize and deserialize values returned by |
| 402 |
python-actions. If required an alternate encoder/decoder pair may be specified |
| 403 |
so that return values which are not JSON serializable, such as Python class |
| 404 |
instances, may be provided as return values from python-actions. |
| 405 |
|
| 406 |
Check ``dependency.py`` 's ``JSONCodec`` interface. |
| 407 |
|
| 408 |
|
| 409 |
minversion |
| 410 |
------------- |
| 411 |
|
| 412 |
`minversion` can be used to specify the minimum/oldest `doit` version |
| 413 |
that can be used with a `dodo.py` file. |
| 414 |
|
| 415 |
For example if your `dodo.py` makes use of a feature added at `doit X` |
| 416 |
and distribute it. If another user who tries this `dodo.py` with a version |
| 417 |
older that `X`, doit will display an error warning the user to update `doit`. |
| 418 |
|
| 419 |
`minversion` can be specified as a string or a 3-element tuple with integer |
| 420 |
values. If specified as a string any part that is not a number i.e.(dev0, a2, |
| 421 |
b4) will be converted to -1. |
| 422 |
|
| 423 |
.. code-block:: console |
| 424 |
|
| 425 |
DOIT_CONFIG = { |
| 426 |
'minversion': '0.24.0', |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
.. note:: |
| 431 |
|
| 432 |
This feature was added on `doit` 0.24.0. |
| 433 |
Older Versions will not check or display error messages. |
| 434 |
|
| 435 |
|
| 436 |
.. _auto-delayed-regex: |
| 437 |
|
| 438 |
automatic regex for delayed task loaders |
| 439 |
------------------------------------------ |
| 440 |
|
| 441 |
When specifying a target for `doit run`, *doit* usually only considers usual |
| 442 |
tasks and :ref:`delayed tasks <delayed-task-creation>` which have a target |
| 443 |
regex specified. Any task generated by a delayed task loader which has |
| 444 |
:ref:`no target regex specified <specify-target-regex>` will not be |
| 445 |
considered. |
| 446 |
|
| 447 |
By specifying `--auto-delayed-regex`, every delayed task loader having no |
| 448 |
target regex specified is assumed to have `.*` specified, a regex which |
| 449 |
matches any target. |