Define named groups of management commands in Django settings files for batched execution.
Project description
django-routines
Configure batches of Django management commands in your settings files and run them all at once. For example, batch together your common database maintenance tasks, deployment routines or any other set of commands you need to run together. This helps single source general site maintenance into your settings files keeping your code base DRY.
Example
Let's define two named routines, "package" and "deploy". The package routine will be a collection of commands that we typically run to generate package artifacts (like migrations and transpiled javascript). The deploy routine will be a collection of commands we typically run when deploying the site for the first time on a new server or when we deploy version updates on the server.
Routine commands are run in the order they are registered, or by priority.
There are two types of commands, management commands and system commands. The management commands will be called in the same process space as routine unless --subprocess is specified in which case they will use the same management script as routine was invoked with or whatever value you supply to --manage-script. System commands are always invoked as subprocesses.
In our settings file we may define these routines like this:
from django_routines import (
ManagementCommand,
SystemCommand,
command,
system,
routine
)
# register routines and their help text
routine(
name="package",
help_text=(
"Generate pre-package artifacts like migrations and transpiled "
"javascript."
)
)
# you may register commands on a routine after defining a routine (or before!)
command("package", "makemigrations")
command("package", "renderstatic")
system("package", "poetry", "build")
routine(
"deploy",
"Deploy the site application into production.",
# you may also specify commands inline using the ManagementCommand dataclass
ManagementCommand(
("routine", "package"), switches=["prepare"]
), # routine commands can be other routines!
ManagementCommand("migrate"),
ManagementCommand("collectstatic"),
ManagementCommand(("shellcompletion", "install"), switches=["initial"]),
ManagementCommand(("loaddata", "./fixtures/demo.json"), switches=["demo"]),
SystemCommand(("touch", "/path/to/wsgi.py")),
# define switches that toggle commands on and off
prepare="Generate artifacts like migrations and transpiled javascript.",
initial="Things to do on the very first deployment on a new server.",
demo="Load the demo data.",
)
The routine command will read our settings file and generate two subcommands, one called deploy and one called package:
Now we can run all of our package routines with one command:
?> ./manage.py routine package
makemigrations
...
renderstatic
...
poetry build
...
The deploy command has several switches that we can enable to run additional commands.
For example to deploy our demo on a new server we would run:
?> ./manage.py routine deploy --initial --demo
migrate
...
collectstatic
...
shellcompletion install
...
loaddata ./fixtures/demo.json
...
touch /path/to/wsgi.py
Settings
The ManagementCommand dataclass, routine and command helper functions in the example above make it easier for us to work with the native configuration format which is a dictionary structure defined in the DJANGO_ROUTINES
setting attribute. For example the above configuration is equivalent to:
DJANGO_ROUTINES = {
"deploy": {
"commands": [
{"command": ("routine", "package"), "switches": ["prepare"]},
{"command": "migrate"},
{"command": "collectstatic"},
{
"command": ("shellcompletion", "install"),
"switches": ["initial"],
},
{
"command": ("loaddata", "./fixtures/demo.json"),
"switches": ["demo"],
},
{"command": ("touch", "/path/to/wsgi.py"), "kind": "system"},
],
"help_text": "Deploy the site application into production.",
"name": "deploy",
"switch_helps": {
"demo": "Load the demo data.",
"initial": "Things to do on the very first deployment on a new "
"server.",
"prepare": "Generate artifacts like migrations and transpiled "
"javascript.",
},
},
"package": {
"commands": [
{"command": "makemigrations"},
{"command": "renderstatic"},
{"command": ("poetry", "build"), "kind": "system"},
],
"help_text": "Generate pre-package artifacts like migrations and "
"transpiled javascript.",
"name": "package",
},
}
Priorities
If you are composing settings from multiple apps or source files using a utility like django-split-settings you may not be able to define all routines at once. You can use priorities to make sure commands defined in a de-coupled way run in the correct order.
command("deploy", "makemigrations", priority=1)
command("deploy", "migrate", priority=2)
Options
When specifying arguments you may add them to the command tuple OR specify them as named options in the style that will be passed to call_command:
# these two are equivalent
command("package", "makemigrations", "--no-header")
command("package", "makemigrations", no_header=True)
Installation
-
Clone django-routines from GitHub or install a release off PyPI :
pip install django-routines
rich is a powerful library for rich text and beautiful formatting in the terminal. It is not required, but highly recommended for the best experience:
pip install "django-routines[rich]"
-
Add
django_routines
to yourINSTALLED_APPS
setting:INSTALLED_APPS = [ ... 'django_routines', 'django_typer', # optional! ]
You only need to install django_typer as an app if you want to use the shellcompletion command to enable tab-completion or if you would like django-typer to install rich traceback rendering for you - which it does by default if rich is also installed.
Rationale
When does it make sense to configure routines in Django settings? Its generally convenient to group common management pathways into easily discoverable and executable aggregations of subroutines. This is usually done in supporting shell scripts or just files and in most cases that is appropriate. If your goal is to keep your Django deployment as tight and self contained as possible and the deployment is not generally very complex, using django-routines can make a lot of sense. It can eliminate extra dependencies on a shell scripting environment or just files and can keep this logic packaged with your installable wheel.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django_routines-1.1.3.tar.gz
.
File metadata
- Download URL: django_routines-1.1.3.tar.gz
- Upload date:
- Size: 15.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.2 Darwin/23.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a9e236f1cd862a018355a8dad56e6bd2ee0f6beb13863865b17be7e7c969cdd |
|
MD5 | c06c114b4c13fa2b14475958d416de1e |
|
BLAKE2b-256 | 8fa9c65979265d811b3169d1601060f2e87317a24f24c958b8b9ccbc305c4848 |
File details
Details for the file django_routines-1.1.3-py3-none-any.whl
.
File metadata
- Download URL: django_routines-1.1.3-py3-none-any.whl
- Upload date:
- Size: 13.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.2 Darwin/23.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e0e2e81f86f9e62230126bf46e0a21c7d7bd8fbe54775124a32db7fa95ae1a1e |
|
MD5 | ff164344ceac3e4180c39e8395cf85d8 |
|
BLAKE2b-256 | 8e6461b3befd524045ea4d0bba4353e654b525f6486e2b98c29e24f4e08fb9e0 |