-
Notifications
You must be signed in to change notification settings - Fork 0
/
django_cmd.py
51 lines (40 loc) · 1.5 KB
/
django_cmd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import configparser
import os
import sys
from functools import wraps
from pathlib import Path
try:
import tomllib
except ImportError:
# Python < 3.11
import tomli as tomllib
import django.core.management
def configure():
"""Run Django, getting the default from a file if needed."""
settings_module = None
# Load from pyproject.toml first
pyproject = Path("pyproject.toml")
if pyproject.is_file():
with pyproject.open("rb") as f:
config = tomllib.load(f)
settings_module = (
config.get("tool", {}).get("django", {}).get("settings_module")
)
if settings_module is None:
# Try loading configuration from setup.cfg next
parser = configparser.RawConfigParser()
parser.read("setup.cfg")
if parser.has_option("django", "settings_module"):
settings_module = parser.get("django", "settings_module")
if settings_module is not None:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings_module)
if settings_module == os.environ["DJANGO_SETTINGS_MODULE"]:
sys.path.insert(0, os.getcwd())
@wraps(django.core.management.ManagementUtility, updated=())
class ConfiguredManagementUtility(django.core.management.ManagementUtility):
@wraps(django.core.management.ManagementUtility.execute)
def execute(self):
configure()
return super().execute()
def patch_django():
django.core.management.ManagementUtility = ConfiguredManagementUtility