Debian Codegraph @

django_pgschemas/management/commands/runschema.py

raw
1 import argparse
2 import sys
3 from typing import Any
4
5 from django.core.management import get_commands, load_command_class
6 from django.core.management.base import BaseCommand, CommandError, CommandParser, SystemCheckError
7
8 from . import WrappedSchemaOption
9
10
11 class Command(WrappedSchemaOption, BaseCommand):
12 help = "Wrapper around Django commands for use with an individual schema"
13
14 def add_arguments(self, parser: CommandParser) -> None:
15 super().add_arguments(parser)
16 parser.add_argument("command_name", help="The command name you want to run")
17
18 def get_command_from_arg(self, arg: str) -> BaseCommand:
19 *chunks, command = arg.split(".")
20 path = ".".join(chunks)
21 if not path:
22 path = get_commands().get(command)
23 try:
24 cmd = load_command_class(path, command)
25 except Exception:
26 raise CommandError("Unknown command: %s" % arg)
27 if isinstance(cmd, WrappedSchemaOption):
28 raise CommandError("Command '%s' cannot be used in runschema" % arg)
29 return cmd
30
31 def run_from_argv(self, argv: list[str]) -> None: # pragma: no cover
32 """
33 Changes the option_list to use the options from the wrapped command.
34 Adds schema parameter to specify which schema will be used when
35 executing the wrapped command.
36 """
37 try:
38 # load the command object.
39 if len(argv) <= 2:
40 raise CommandError("No command to run")
41 target_class = self.get_command_from_arg(argv[2])
42 # Ugly, but works. Delete command_name from the argv, parse the schemas manually
43 # and forward the rest of the arguments to the actual command being wrapped.
44 del argv[1]
45 schema_parser = argparse.ArgumentParser()
46 super().add_arguments(schema_parser)
47 schema_ns, args = schema_parser.parse_known_args(argv)
48
49 schemas = self.get_schemas_from_options(
50 schemas=schema_ns.schemas,
51 all_schemas=schema_ns.all_schemas,
52 static_schemas=schema_ns.static_schemas,
53 dynamic_schemas=schema_ns.dynamic_schemas,
54 tenant_schemas=schema_ns.tenant_schemas,
55 )
56 executor = self.get_executor_from_options(parallel=schema_ns.parallel)
57 except Exception as e:
58 if not isinstance(e, CommandError):
59 raise
60 # SystemCheckError takes care of its own formatting.
61 if isinstance(e, SystemCheckError):
62 self.stderr.write(str(e), lambda x: x)
63 else:
64 self.stderr.write("%s: %s" % (e.__class__.__name__, e))
65 sys.exit(1)
66
67 executor(schemas, target_class, "special:run_from_argv", args)
68
69 def handle(self, *args: Any, **options: Any) -> None:
70 target = self.get_command_from_arg(options.pop("command_name"))
71 schemas = self.get_schemas_from_options(**options)
72 executor = self.get_executor_from_options(**options)
73 options.pop("schemas")
74 options.pop("excluded_schemas")
75 options.pop("all_schemas")
76 options.pop("static_schemas")
77 options.pop("dynamic_schemas")
78 options.pop("tenant_schemas")
79 options.pop("parallel")
80 options.pop("skip_schema_creation")
81 if self.allow_interactive:
82 options.pop("interactive")
83 executor(schemas, target, "special:call_command", args, options)