From 66899c5144ac0974095a02c341d7dacc42c4dff8 Mon Sep 17 00:00:00 2001 From: Klaas van Schelven Date: Fri, 9 May 2025 12:49:34 +0200 Subject: [PATCH] Add bugsink-util script to allow settings-independent commands to be run --- bugsink/scripts/util.py | 42 +++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 1 + 2 files changed, 43 insertions(+) create mode 100644 bugsink/scripts/util.py diff --git a/bugsink/scripts/util.py b/bugsink/scripts/util.py new file mode 100644 index 0000000..35927b9 --- /dev/null +++ b/bugsink/scripts/util.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +"""A copy of the Django-generated manage.py, but: + +* in the bugsink.scripts package, such that it can be wrapped by a setuptools-installable script +* with the DJANGO_SETTINGS_MODULE set to `bugsink.settings.default` by default. + +This script can be used to run Django management commands for which the settings _don't matter_. + +Such commands "should probably" be extracted to be Django-independent, but that incurs its own extra work (as well as +future maintenance burden): some utility code is shared, the command utilizes the Django argv parsing, and a separate +repo _always_ brings extra overhead (e.g. for testing, CI, etc.). So this is a pragmatic solution to the problem. +""" +import os +import sys + + +def find_commands(management_dir): + # explicitly enumerate Django (settings)-independent commands here (for --help) + if 'bsmain' in management_dir: + return ["stress_test"] + return [] + + +def main(): + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'bugsink.settings.default') + try: + # we just monkeypatch the find_commands function to return the commands which are actually settings-independent. + import django.core.management + django.core.management.find_commands = find_commands + + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/pyproject.toml b/pyproject.toml index d0e7dbe..7217a0b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,6 +22,7 @@ dynamic = ["version", "dependencies"] [project.scripts] bugsink-show-version = "bugsink.scripts.show_version:main" bugsink-manage = "bugsink.scripts.manage:main" +bugsink-util = "bugsink.scripts.util:main" bugsink-create-conf = "bugsink.scripts.create_conf:main" bugsink-runsnappea = "bugsink.scripts.runsnappea:main"