Skip to content
manage.py 1.76 KiB
Newer Older
Andreas Klöckner's avatar
Andreas Klöckner committed
#!/usr/bin/env python
from __future__ import annotations

Andreas Klöckner's avatar
Andreas Klöckner committed
import os
import sys

Dong Zhuang's avatar
Dong Zhuang committed

def get_local_test_settings_file(argv):
    assert argv[1] == "test"
    assert "manage.py" in argv[0]

    local_settings_dir = os.path.split(argv[0])[0]
    assert os.path.isfile(os.path.join(local_settings_dir, "manage.py"))

Andreas Klöckner's avatar
Andreas Klöckner committed
    from django.core.management import CommandError, CommandParser
    parser = CommandParser(
            usage="%(prog)s subcommand [options] [args]",
            add_help=False)
    parser.add_argument("--local_test_settings",
Dong Zhuang's avatar
Dong Zhuang committed
                        dest="local_test_settings")
    options, _args = parser.parse_known_args(argv)
Dong Zhuang's avatar
Dong Zhuang committed

    if options.local_test_settings is None:
        local_settings_file = "local_settings_example.py"
Dong Zhuang's avatar
Dong Zhuang committed
    else:
        local_settings_file = options.local_test_settings

    if os.path.split(local_settings_file)[0] == "":
        local_settings_file = os.path.join(
            local_settings_dir, local_settings_file)

    if os.path.abspath(local_settings_file) == os.path.abspath(
            os.path.join(local_settings_dir, "local_settings.py")):
        raise CommandError(
            "Using production local_settings for tests is not "
            "allowed due to security reason."
        )

    if not os.path.isfile(local_settings_file):
        raise CommandError(
            f"file '{local_settings_file}' does not exist"
Dong Zhuang's avatar
Dong Zhuang committed
        )

    return local_settings_file


Andreas Klöckner's avatar
Andreas Klöckner committed
if __name__ == "__main__":
Andreas Klöckner's avatar
Andreas Klöckner committed
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "relate.settings")
Andreas Klöckner's avatar
Andreas Klöckner committed

Josh Asplund's avatar
Josh Asplund committed
    from django.core.management import execute_from_command_line
Dong Zhuang's avatar
Dong Zhuang committed

    if sys.argv[1] == "test":
        local_settings_file = get_local_test_settings_file(sys.argv)
        os.environ["RELATE_LOCAL_TEST_SETTINGS"] = local_settings_file
Andreas Klöckner's avatar
Andreas Klöckner committed

    execute_from_command_line(sys.argv)