Basics of the Python task automation tool “doit”#
This is a work memo that explains the basic concept, introduction method, and simple usage examples of the task automation tool “doit” written in Python.
overview#
doit is a tool that allows you to flexibly describe Makefile-like task automation mechanisms using Python syntax. By writing routine tasks such as Sphinx builds, lint tests, etc., only the necessary tasks can be automatically executed using difference detection.
install#
pip install doit
Or if you use Poetry, install it as follows.
poetry add --group dev doit
Minimal task definition#
The file name should basically be dodo.py.
def task_hello():
return {
'actions': ['echo "Hello, world!"'],
}
Execution is as follows.
doit # すべてのタスクを実行
doit list # タスク一覧を表示
doit hello # タスクhelloだけを実行
Example using input/output files#
def task_build_docs():
return {
'actions': ['sphinx-build -b html docs build'],
'file_dep': ['docs/conf.py', 'docs/index.rst'],
'targets': ['build/index.html'],
'clean': True,
}
file_dep: input file (dependent file)targets: Output files (can be skipped if built)clean: Include for deletion when executingdoit clean
Actions with Python functions#
def say_hello(name):
print(f"Hello, {name}!")
def task_greet():
return {
'actions': [(say_hello, ['Alice'])],
}
Common uses#
Purpose |
Content |
|---|---|
document |
Building Sphinx and MkDocs |
test |
Running pytest, unittest, etc. |
lint |
flake8, black, mypy etc. |
multiple steps |
Dependencies between steps can also be described |
automatic monitoring |
Watch file changes with |
supplement#
If your project grows, you can also write separate tasks in the tasks directory and import them from dodo.py.
summary#
doit, which can be defined in Python and enables flexible and highly reusable task automation, is extremely useful for streamlining small to medium-sized projects. In the future, we plan to consider configurations that link with Poetry, Sphinx, CI tools, etc.