mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2026-02-06 23:26:19 +01:00
Faster management commands
This commit is contained in:
25
allianceauth/utils/django.py
Normal file
25
allianceauth/utils/django.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import sys
|
||||
from copy import copy
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class StartupCommand:
|
||||
"""Information about the command this Django instance was started with."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._argv = copy(sys.argv)
|
||||
|
||||
@property
|
||||
def argv(self) -> list:
|
||||
"""Return raw list of command line arguments."""
|
||||
return self._argv
|
||||
|
||||
@property
|
||||
def script_name(self) -> str:
|
||||
"""Return the base script name."""
|
||||
path = Path(self._argv[0])
|
||||
return path.name
|
||||
|
||||
@property
|
||||
def is_management_command(self) -> bool:
|
||||
return self.script_name == "manage.py"
|
||||
25
allianceauth/utils/tests/test_django.py
Normal file
25
allianceauth/utils/tests/test_django.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from allianceauth.utils.django import StartupCommand
|
||||
|
||||
MODULE_PATH = "allianceauth.utils.django"
|
||||
|
||||
|
||||
class TestStartupCommand(TestCase):
|
||||
def test_should_detect_management_command(self):
|
||||
# when
|
||||
with patch(MODULE_PATH + ".sys") as m:
|
||||
m.argv = ["manage.py", "check"]
|
||||
info = StartupCommand()
|
||||
# then
|
||||
self.assertTrue(info.is_management_command)
|
||||
|
||||
def test_should_detect_not_a_management_command(self):
|
||||
# when
|
||||
with patch(MODULE_PATH + ".sys") as m:
|
||||
m.argv = ['/home/python/allianceauth-dev/venv/bin/gunicorn', 'myauth.wsgi']
|
||||
info = StartupCommand()
|
||||
# then
|
||||
self.assertFalse(info.is_management_command)
|
||||
Reference in New Issue
Block a user