diff --git a/allianceauth/eveonline/providers.py b/allianceauth/eveonline/providers.py index fb1749dd..d8c30604 100644 --- a/allianceauth/eveonline/providers.py +++ b/allianceauth/eveonline/providers.py @@ -8,6 +8,7 @@ from django.conf import settings from esi.clients import esi_client_factory from allianceauth import __version__ +from allianceauth.utils.django import StartupCommand SWAGGER_SPEC_PATH = os.path.join(os.path.dirname( @@ -175,15 +176,16 @@ class EveProvider: class EveSwaggerProvider(EveProvider): def __init__(self, token=None, adapter=None): - if settings.DEBUG: + if settings.DEBUG or StartupCommand().is_management_command: self._client = None - logger.info( - 'DEBUG mode detected: ESI client will be loaded on-demand.' - ) + logger.info('ESI client will be loaded on-demand') else: + logger.info('Loading ESI client') try: self._client = esi_client_factory( - token=token, spec_file=SWAGGER_SPEC_PATH, app_info_text=("allianceauth v" + __version__) + token=token, + spec_file=SWAGGER_SPEC_PATH, + app_info_text=f"allianceauth v{__version__}" ) except (HTTPError, RefResolutionError): logger.exception( diff --git a/allianceauth/utils/django.py b/allianceauth/utils/django.py new file mode 100644 index 00000000..deb567aa --- /dev/null +++ b/allianceauth/utils/django.py @@ -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" diff --git a/allianceauth/utils/tests/test_django.py b/allianceauth/utils/tests/test_django.py new file mode 100644 index 00000000..a2af2440 --- /dev/null +++ b/allianceauth/utils/tests/test_django.py @@ -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)