-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdjango_cmd.py
60 lines (47 loc) · 1.82 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
52
53
54
55
56
57
58
59
60
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 locate() -> Path:
"""Locate the pyproject.toml file."""
for path in [cwd := Path.cwd(), *cwd.parents]:
candidate = path / "pyproject.toml"
if candidate.is_file():
return candidate
def configure():
"""Run Django, getting the default from a file if needed."""
settings_module = path = None
# Load from pyproject.toml first
if pyproject := locate():
with pyproject.open("rb") as f:
config = tomllib.load(f)
settings_module = (
config.get("tool", {}).get("django", {}).get("settings_module")
)
path = None if settings_module is None else pyproject.parent
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")
path = None if settings_module is None else Path.cwd()
if settings_module is not None and path is not None:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings_module)
if settings_module == os.environ["DJANGO_SETTINGS_MODULE"]:
sys.path.insert(0, str(path))
@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