def ModifyAvailableCommands(): """Removes incompatible commands and installs replacements where possible.""" if have_appserver: # Commands are not used when running from an appserver. return from django.core import management if VERSION < (0, 97, None): RemoveCommands(management.DEFAULT_ACTION_MAPPING) from appengine_django.management.commands import runserver management.DEFAULT_ACTION_MAPPING['runserver'] = runserver.v096_command from appengine_django.management.commands import flush management.DEFAULT_ACTION_MAPPING['flush'] = flush.v096_command from appengine_django.management.commands import reset management.DEFAULT_ACTION_MAPPING['reset'] = reset.v096_command # startapp command for django 0.96 management.PROJECT_TEMPLATE_DIR = os.path.join(__path__[0], 'conf', '%s_template') else: project_directory = os.path.join(__path__[0], "../") management.get_commands(project_directory=project_directory) # Replace startapp command which is set by previous call to get_commands(). from appengine_django.management.commands.startapp import ProjectCommand management._commands['startapp'] = ProjectCommand(project_directory) RemoveCommands(management._commands) # Django 0.97 will install the replacements automatically. logging.debug("Removed incompatible Django manage.py commands")
def setup_test_db(worker_index, fixtures, fn, *args): management.get_commands() management._commands['syncdb'] = 'django.core' old_name = settings.DATABASES['default']['NAME'] if worker_index is not None: test_database_name = 'test_%d_%s' % (worker_index, old_name) else: test_database_name = 'test_%s' % (old_name) create_test_db(test_database_name) if fixtures: load_db_fixtures(fixtures) result = None try: result = fn(*args) except Exception as e: log.err(str(e)) raise finally: destroy_test_db(test_database_name) connection.settings_dict['NAME'] = old_name return result
def main_help_text(self, commands_only=False): """ Returns the script's main help text, as a string. """ if commands_only: usage = sorted(get_commands().keys()) else: usage = [ "", "Type '%s help <subcommand>' for help on a specific subcommand." % self.prog_name, "", "Available subcommands:", ] commands = [] for name, app in get_commands().items(): commands.append(name) style = color_style() for name in sorted(commands): usage.append(" %s" % name) # Output an extra note if settings are not properly configured if self.settings_exception is not None: usage.append( style.NOTICE( "Note that only Django core commands are listed " "as settings are not properly configured (error: %s)." % self.settings_exception ) ) return "\n".join(usage)
def manage(command, args=[], in_background=False): """ Run a django command on the kalite project :param command: The django command string identifier, e.g. 'runserver' :param args: List of options to parse to the django management command :param in_background: Creates a sub-process for the command """ # Ensure that django.core.management's global _command variable is set # before call commands, especially the once that run in the background get_commands() # Import here so other commands can run faster if not in_background: utility = ManagementUtility([os.path.basename(sys.argv[0]), command] + args) # This ensures that 'kalite' is printed in help menus instead of # 'kalitectl.py' (a part from the top most text in `kalite manage help` utility.prog_name = 'kalite manage' utility.execute() else: if os.name != "nt": thread = ManageThread(command, args=args, name=" ".join([command] + args)) thread.start() else: # TODO (aron): for versions > 0.13, see if we can just have everyone spawn another process (Popen vs. ManageThread) Popen([ sys.executable, os.path.abspath(sys.argv[0]), "manage", command ] + args, creationflags=CREATE_NEW_PROCESS_GROUP)
def _handle_south(): from django.conf import settings if 'south' in settings.INSTALLED_APPS: # Handle south. from django.core import management try: # if `south` >= 0.7.1 we can use the test helper from south.management.commands import patch_for_test_db_setup except ImportError: # if `south` < 0.7.1 make sure it's migrations are disabled management.get_commands() management._commands['syncdb'] = 'django.core' else: # Monkey-patch south.hacks.django_1_0.SkipFlushCommand to load # initial data. # Ref: http://south.aeracode.org/ticket/1395#comment:3 import south.hacks.django_1_0 from django.core.management.commands.flush import ( Command as FlushCommand) class SkipFlushCommand(FlushCommand): def handle_noargs(self, **options): # Reinstall the initial_data fixture. from django.core.management import call_command # `load_initial_data` got introduces with Django 1.5. load_initial_data = options.get('load_initial_data', None) if load_initial_data or load_initial_data is None: # Reinstall the initial_data fixture. call_command('loaddata', 'initial_data', **options) # no-op to avoid calling flush return south.hacks.django_1_0.SkipFlushCommand = SkipFlushCommand patch_for_test_db_setup()
def manage(command, args=[], as_thread=False): """ Run a django command on the kalite project :param command: The django command string identifier, e.g. 'runserver' :param args: List of options to parse to the django management command :param as_thread: Runs command in thread and returns immediately """ args = update_default_args(["--traceback"], args) if not as_thread: if PROFILE: profile_memory() utility = ManagementUtility([os.path.basename(sys.argv[0]), command] + args) # This ensures that 'kalite' is printed in help menus instead of # 'kalitectl.py' (a part from the top most text in `kalite manage help` utility.prog_name = 'kalite manage' utility.execute() else: get_commands() # Needed to populate the available commands before issuing one in a thread thread = ManageThread(command, args=args, name=" ".join([command] + args)) thread.start() return thread
def run(self): os.environ['DJANGO_SETTINGS_MODULE'] = 'RestAuth.testsettings' import django from django.core import management # this causes django to use stock syncdb instead of South-version management.get_commands() management._commands['syncdb'] = 'django.core' fixture = 'RestAuth/fixtures/testserver.json' if django.VERSION[:2] == (1, 4): # see https://github.com/django/django/commit/bb4452f212e211bca7b6b57904d59270ffd7a503 from django.db import connection as conn # Create a test database. conn.creation.create_test_db() # Import the fixture data into the test database. management.call_command('loaddata', fixture) use_threading = conn.features.test_db_allows_multiple_connections management.call_command( 'runserver', shutdown_message='Testserver stopped.', use_reloader=False, use_ipv6=True, use_threading=use_threading ) else: management.call_command('testserver', fixture, use_ipv6=True)
def handle(self, *test_labels, **options): # handle south migration in tests management.get_commands() if hasattr(settings, "SOUTH_TESTS_MIGRATE") and not settings.SOUTH_TESTS_MIGRATE: # point at the core syncdb command when creating tests # tests should always be up to date with the most recent model structure management._commands['syncdb'] = 'django.core' elif 'south' in settings.INSTALLED_APPS: try: from south.management.commands import MigrateAndSyncCommand management._commands['syncdb'] = MigrateAndSyncCommand() from south.hacks import hacks if hasattr(hacks, "patch_flush_during_test_db_creation"): hacks.patch_flush_during_test_db_creation() except ImportError: management._commands['syncdb'] = 'django.core' verbosity = int(options.get('verbosity', 1)) interactive = options.get('interactive', True) failfast = options.get('failfast', False) TestRunner = self.get_runner() if not inspect.ismethod(TestRunner): failures = TestRunner(test_labels, verbosity=verbosity, interactive=interactive, failfast=failfast, keepdb='--keepdb' in sys.argv) else: test_runner = TestRunner(verbosity=verbosity, interactive=interactive, failfast=failfast) failures = test_runner.run_tests(test_labels) if failures: sys.exit(bool(failures))
def pytest_sessionstart(self, session): setup_test_environment() settings.DEBUG = False if self.database: settings.DATABASE_NAME = self.database management.get_commands() management._commands["syncdb"] = "django.core" if ( "south" in settings.INSTALLED_APPS and hasattr(settings, "SOUTH_TESTS_MIGRATE") and settings.SOUTH_TESTS_MIGRATE ): try: from south.management.commands.syncdb import Command except ImportError: pass else: class MigrateAndSyncCommand(Command): option_list = Command.option_list for opt in option_list: if "--migrate" == opt.get_opt_string(): opt.default = True break management._commands["syncdb"] = MigrateAndSyncCommand() self._old_database_name = settings.DATABASE_NAME create_test_db(self.verbosity, autoclobber=self.noinput, copy_test_db=self.copy_live_db)
def main_help_text(self, commands_only=False): """Return the script's main help text, as a string.""" if commands_only: usage = sorted(get_commands()) else: usage = [ "", "Type '%s help <subcommand>' for help on a specific subcommand." % self.prog_name, "", "Available subcommands:", ] commands_dict = defaultdict(lambda: []) for name, app in get_commands().items(): if app == 'django.core': app = 'django' else: app = app.rpartition('.')[-1] commands_dict[app].append(name) style = color_style() for app in sorted(commands_dict): usage.append("") usage.append(style.NOTICE("[%s]" % app)) for name in sorted(commands_dict[app]): usage.append(" %s" % name) # Output an extra note if settings are not properly configured if self.settings_exception is not None: usage.append( style.NOTICE( "Note that only Django core commands are listed " "as settings are not properly configured (error: %s)." % self.settings_exception)) return '\n'.join(usage)
def manage(command, args=None, as_thread=False): """ Run a django command on the kalite project :param command: The django command string identifier, e.g. 'runserver' :param args: List of options to parse to the django management command :param as_thread: Runs command in thread and returns immediately """ if not args: args = [] args = update_default_args(["--traceback"], args) if not as_thread: utility = ManagementUtility([os.path.basename(sys.argv[0]), command] + args) # This ensures that 'kalite' is printed in help menus instead of # 'kalite' or 'kalite.__main__' (a part from the top most text in `kalite manage help` utility.prog_name = 'kalite manage' utility.execute() else: get_commands() # Needed to populate the available commands before issuing one in a thread thread = ManageThread(command, args=args, name=" ".join([command] + args)) thread.start() return thread
def manage(command, args=[], in_background=False): """ Run a django command on the kalite project :param command: The django command string identifier, e.g. 'runserver' :param args: List of options to parse to the django management command :param in_background: Creates a sub-process for the command """ # Ensure that django.core.management's global _command variable is set # before call commands, especially the once that run in the background get_commands() # Import here so other commands can run faster if not in_background: utility = ManagementUtility([os.path.basename(sys.argv[0]), command] + args) # This ensures that 'kalite' is printed in help menus instead of # 'kalitectl.py' (a part from the top most text in `kalite manage help` utility.prog_name = 'kalite manage' utility.execute() else: if os.name != "nt": thread = ManageThread(command, args=args, name=" ".join([command]+args)) thread.start() else: # TODO (aron): for versions > 0.13, see if we can just have everyone spawn another process (Popen vs. ManageThread) Popen([sys.executable, os.path.abspath(sys.argv[0]), "manage", command] + args, creationflags=CREATE_NEW_PROCESS_GROUP)
def get_available_commands(): available_commands = getattr(settings, 'WEBCOMMANDS_AVAILBLES', ['*']) if '*' in available_commands: return get_commands() else: return {k: v for k, v in get_commands() if k in available_commands}
def main_help_text(self, commands_only=False): """ Returns the script's main help text, as a string. """ if commands_only: usage = sorted(get_commands().keys()) else: usage = [ "", "Type '%s help <subcommand>' for help on a specific subcommand." % self.prog_name, "", "Available subcommands:", ] commands = [] for name, app in get_commands().items(): commands.append(name) style = color_style() for name in sorted(commands): usage.append(" %s" % name) # Output an extra note if settings are not properly configured if self.settings_exception is not None: usage.append( style.NOTICE( "Note that only Django core commands are listed " "as settings are not properly configured (error: %s)." % self.settings_exception)) return "\n".join(usage)
def handle(self, *args, **kwargs): if not hasattr(settings, "SOUTH_TESTS_MIGRATE") or not settings.SOUTH_TESTS_MIGRATE: # point at the core syncdb command when creating tests # tests should always be up to date with the most recent model structure management.get_commands() management._commands['syncdb'] = 'django.core' super(Command, self).handle(*args, **kwargs)
def handle(self, *test_labels, **options): verbosity = int(options.get('verbosity', 1)) interactive = options.get('interactive', True) # it's quite possible someone, lets say South, might have stolen # the syncdb command from django. For testing purposes we should # probably put it back. Migrations don't really make sense # for tests. Actually the South test runner does this too. management.get_commands() management._commands['syncdb'] = 'django.core' if options.get('coverage'): test_runner_name = 'django_kwalitee.testrunners.codecoverage.run_tests' else: test_runner_name = settings.TEST_RUNNER # hack to run subset of full test suite # just use test_labels to load up non-excluded apps if options.get('local') and not test_labels: local_apps = [] for app in get_apps(): app_label = app.__name__.split('.')[-2] if not app_label in settings.KWALITEE_LOCAL_EXCLUDES: local_apps.append(app_label) test_labels = tuple(local_apps) test_runner = get_runner(test_runner_name) failures = test_runner(test_labels, verbosity=verbosity, interactive=interactive) if failures: sys.exit(failures)
def main_help_text(self): """ Returns the script's main help text, as a string. """ usage = ['', "Type '%s help <subcommand>' " "for help on a specific subcommand." % self.prog_name, '', 'Available subcommands:'] #usage.append() mgm.get_commands() commands = mgm._pure_commands.keys() commands.sort() aliases = mgm._aliases.keys() aliases.sort() for cmd in commands: if cmd in aliases: usage.append(' #%s (overrided by aliases)' % cmd) elif "/c" in cmd: usage.append(' %s (original %s)' % (cmd, cmd.replace("/c", ""))) else: usage.append(' %s' % cmd) if aliases: usage.append('\nAvailable Aliases:') for cmd in aliases: usage.append(' %s' % cmd) return '\n'.join(usage)
def patch_for_test_db_setup(): management.get_commands() if hasattr(settings, "SOUTH_TESTS_MIGRATE") and not settings.SOUTH_TESTS_MIGRATE: # point at the core syncdb command when creating tests # tests should always be up to date with the most recent model structure management._commands['syncdb'] = 'django.core' else: management._commands['syncdb'] = MigrateAndSyncCommand()
def create_test_db(test_database_name): connection.settings_dict['TEST_NAME'] = test_database_name management.get_commands() management._commands['syncdb'] = 'django.core' connection.creation.create_test_db(verbosity, autoclobber=not interactive) connection.settings_dict['NAME'] = test_database_name
def handle(self, *args, **kwargs): management.get_commands() if not hasattr(settings, "SOUTH_TESTS_MIGRATE") or not settings.SOUTH_TESTS_MIGRATE: # point at the core syncdb command when creating tests # tests should always be up to date with the most recent model structure management._commands['syncdb'] = 'django.core' else: management._commands['syncdb'] = MigrateAndSyncCommand() super(Command, self).handle(*args, **kwargs)
def _handle_south_management_command(): try: # if `south` >= 0.7.1 we can use the test helper from south.management.commands import patch_for_test_db_setup except ImportError: # if `south` < 0.7.1 make sure it's migrations are disabled management.get_commands() management._commands['syncdb'] = 'django.core' else: patch_for_test_db_setup()
def pytest_sessionstart(self, session): #capture = py.io.StdCapture() # make sure the normal django syncdb command is run (do not run migrations for tests) # this is faster and less error prone management.get_commands() # load commands dict management._commands['syncdb'] = 'django.core' # make sure `south` migrations are disabled self.suite_runner = DjangoTestSuiteRunner() self.suite_runner.setup_test_environment() self.old_db_config = self.suite_runner.setup_databases() settings.DATABASE_SUPPORTS_TRANSACTIONS = True
def patch_for_test_db_setup(): # Load the commands cache management.get_commands() # Repoint to the correct version of zsyncdb if hasattr(settings, "SOUTH_TESTS_MIGRATE") and not settings.SOUTH_TESTS_MIGRATE: # point at the core zsyncdb command when creating tests # tests should always be up to date with the most recent model structure management._commands["zsyncdb"] = "django.core" else: management._commands["zsyncdb"] = MigrateAndSyncCommand()
def wrapper(*args, **kwargs): # hold onto the original and replace flush command with a no-op from django.core.management import get_commands get_commands() original_flush_command = management._commands['flush'] try: management._commands['flush'] = SkipFlushCommand() # run create_test_db f(*args, **kwargs) finally: # unpatch flush back to the original management._commands['flush'] = original_flush_command
def handle(self, *test_labels, **options): # Limit the tested apps to defined in TEST_APPS, unless user asks # for other apps. if not test_labels and settings.TEST_APPS: test_labels = settings.TEST_APPS verbosity = int(options.get('verbosity', 1)) interactive = options.get('interactive', True) failfast = options.get("failfast", False) # it's quite possible someone, lets say South, might have stolen # the syncdb command from django. For testing purposes we should # probably put it back. Migrations don't really make sense # for tests. Actually the South test runner does this too. management.get_commands() management._commands['syncdb'] = 'django.core' if options.get('nodb'): if options.get('coverage'): test_runner_name = 'test_extensions.testrunners.nodatabase.run_tests_with_coverage' else: test_runner_name = 'test_extensions.testrunners.nodatabase.run_tests' elif options.get('coverage'): test_runner_name = 'test_extensions.testrunners.codecoverage.run_tests' elif options.get('figleaf'): test_runner_name = 'test_extensions.testrunners.figleafcoverage.run_tests' elif options.get('xml'): test_runner_name = 'test_extensions.testrunners.xmloutput.run_tests' else: test_runner_name = settings.TEST_RUNNER test_path = test_runner_name.split('.') # Allow for Python 2.5 relative paths if len(test_path) > 1: test_module_name = '.'.join(test_path[:-1]) else: test_module_name = '.' test_module = __import__(test_module_name, {}, {}, test_path[-1]) test_runner = getattr(test_module, test_path[-1]) test_options = dict(verbosity=verbosity, interactive=interactive) if failfast: test_options["failfast"] = failfast try: failures = test_runner(test_labels, **test_options) except TypeError, exc: if "failfast" in str(exc): raise NotImplementedError( "The --failfast option requires Django 1.2 or newer.") raise
def checkcmd(self): ldapcmd = "ldaptest" try: app_name = get_commands()[ldapcmd] except: app_name = None self.assertIsNotNone(app_name)
def fetch_command(subcommand: str) -> BaseCommand: """ Tries to fetch the given subcommand, printing a message with the appropriate command called from the command line (usually "django-admin" or "manage.py") if it can't be found. override a few django commands in the case where settings not loaded. hard to test this because we need to simulate settings not being configured """ if subcommand in ['startapp', 'startproject', 'unzip', 'zip']: command_module = import_module( 'otree.management.commands.{}'.format(subcommand)) return command_module.Command() commands = get_commands() try: app_name = commands[subcommand] except KeyError: sys.stderr.write( "Unknown command: %r\nType 'otree help' for usage.\n" % subcommand) sys.exit(1) if isinstance(app_name, BaseCommand): # If the command is already loaded, use it directly. klass = app_name else: klass = load_command_class(app_name, subcommand) return klass
def run_from_argv(self, argv): """ Changes the option_list to use the options from the wrapped command. Adds schema parameter to specify which schema will be used when executing the wrapped command. """ # load the command object. if len(argv) <= 2: return try: app_name = get_commands()[argv[2]] except KeyError: raise CommandError("Unknown command: %r" % argv[2]) if isinstance(app_name, BaseCommand): # if the command is already loaded, use it directly. klass = app_name else: klass = load_command_class(app_name, argv[2]) # Ugly, but works. Delete tenant_command from the argv, parse the schema manually # and forward the rest of the arguments to the actual command being wrapped. del argv[1] schema_parser = argparse.ArgumentParser() schema_parser.add_argument("-s", "--schema", dest="schema_name", help="specify tenant schema") schema_namespace, args = schema_parser.parse_known_args(argv) tenant = self.get_tenant_from_options_or_interactive(schema_name=schema_namespace.schema_name) connection.set_tenant(tenant) klass.run_from_argv(args)
def __new__(cls, *args, **kwargs): """ Sets option_list and help dynamically. """ obj = super(BaseTenantCommand, cls).__new__(cls, *args, **kwargs) app_name = get_commands()[obj.COMMAND_NAME] if isinstance(app_name, BaseCommand): # If the command is already loaded, use it directly. cmdclass = app_name else: cmdclass = load_command_class(app_name, obj.COMMAND_NAME) # inherit the options from the original command obj.option_list = cmdclass.option_list obj.option_list += (make_option("-s", "--schema", dest="schema_name"), ) obj.option_list += (make_option("-p", "--skip-public", dest="skip_public", action="store_true", default=False), ) # prepend the command's original help with the info about schemata iteration obj.help = "Calls %s for all registered schemata. You can use regular %s options. "\ "Original help for %s: %s" % (obj.COMMAND_NAME, obj.COMMAND_NAME, obj.COMMAND_NAME, getattr(cmdclass, 'help', 'none')) return obj
def handle(self, *args, **options): test = settings.DEBUG commands = get_commands() organizations = [app_name.split(".")[-1] for app_name in settings.INSTALLED_APPS if app_name.startswith("organizations.")] organization_commands = [command for command in ("setup_%s" % organization for organization in organizations) if command in commands] events = [app_name.split(".")[-1] for app_name in settings.INSTALLED_APPS if app_name.startswith("events.")] event_commands = [command for command in ("setup_%s" % event for event in events) if command in commands] management_commands = [ (('kompassi_i18n', '-acv2'), dict()), (('collectstatic',), dict(interactive=False)), (('migrate',), dict()), (('setup_core',), dict(test=test)), (('setup_labour_common_qualifications',), dict(test=test)), (('setup_api_v2',), dict(test=test)), (('setup_access',), dict(test=test)), ] management_commands.extend(((command,), dict(test=test)) for command in organization_commands) management_commands.extend(((command,), dict(test=test)) for command in event_commands) for pargs, opts in management_commands: print "** Running:", pargs[0] with (atomic() if pargs[0].startswith("setup") else noop_context()): call_command(*pargs, **opts)
def ModifyAvailableCommands(): """Removes incompatible commands and installs replacements where possible.""" if have_appserver: # Commands are not used when running from an appserver. return from django.core import management project_directory = os.path.join(__path__[0], "../") if have_django_zip: FindCommandsInZipfile.orig = management.find_commands management.find_commands = FindCommandsInZipfile management.get_commands() # Replace startapp command which is set by previous call to get_commands(). from appengine_django.management.commands.startapp import ProjectCommand management._commands['startapp'] = ProjectCommand(project_directory) RemoveCommands(management._commands) logging.debug("Removed incompatible Django manage.py commands")
def test_command_call(self): commands_dict = get_commands() self.assertTrue(self.COMMAND in commands_dict) saved_streams = sys.stdout, sys.stderr sys.stdout = self.Output() sys.stderr = self.Output() prefix = "stderr:" has_errors = False try: call_command(self.COMMAND, tee=1, prefix=prefix) except: has_errors = True self.assertEqual(has_errors, False) valid_stdout = "" valid_stderr = "" for k, v in count_instances().items(): row = "%s\t%d" % (k, v) valid_stdout += "%s\n" % row valid_stderr += "%s %s\n" % (prefix, row) self.assertEquals(sys.stdout.text, valid_stdout) self.assertEquals(sys.stderr.text, valid_stderr) sys.stdout, sys.stderr = saved_streams
def _django_db_setup(request, _django_runner, _django_cursor_wrapper): """Session-wide database setup, internal to pytest-django""" skip_if_no_django() from django.core import management # Disable south's syncdb command commands = management.get_commands() if commands['syncdb'] == 'south': management._commands['syncdb'] = 'django.core' with _django_cursor_wrapper: # Monkey patch Django's setup code to support database re-use if request.config.getvalue('reuse_db'): if not request.config.getvalue('create_db'): monkey_patch_creation_for_db_reuse() _django_runner.teardown_databases = lambda db_cfg: None # Create the database db_cfg = _django_runner.setup_databases() def teardown_database(): with _django_cursor_wrapper: _django_runner.teardown_databases(db_cfg) request.addfinalizer(teardown_database)
def getHTML(request): commands = {} for commandname, appname in get_commands().items(): if commandname != "scheduletasks": try: cmd = getattr( import_module("%s.management.commands.%s" % (appname, commandname)), "Command", ) if getattr(cmd, "index", -1) >= 0 and getattr( cmd, "getHTML", None): commands[cmd.index] = commandname except Exception: pass commands = [commands[i] for i in sorted(commands)] return render_to_string( "commands/scheduletasks.html", { "schedules": ScheduledTask.objects.all().using( request.database).order_by("name"), "commands": commands, }, request=request, )
def build_command_choices(): command_by_app = defaultdict(list) for k, v in get_commands().items(): command_by_app[v.rpartition('.')[-1]].append(k) return tuple( sorted([(app, tuple(sorted(((cmd, cmd) for cmd in commands)))) for app, commands in command_by_app.items()]))
def handle(self, *args, **options): """Handle method.""" exclude_core = options.get("excludecore", False) for command, app in get_commands().items(): if not (exclude_core and app.startswith("django")): AppCommand.objects.get_or_create(name=command, app_name=app) self.stdout.write(f"{app}: {command}")
def run_from_argv(self, argv): """ Changes the option_list to use the options from the wrapped command. """ # load the command object. if len(argv) <= 2: return try: app_name = get_commands()[argv[2]] except KeyError: raise CommandError("Unknown command: %r" % argv[2]) if isinstance(app_name, BaseCommand): # if the command is already loaded, use it directly. klass = app_name else: klass = load_command_class(app_name, argv[2]) # Ugly, but works. Delete tenant_command from the argv, parse the schema manually # and forward the rest of the arguments to the actual command being wrapped. del argv[1] schema_parser = argparse.ArgumentParser() schema_namespace, args = schema_parser.parse_known_args(argv) print(args) tenant_model = get_tenant_model() tenants = tenant_model.objects.all() for tenant in tenants: self.stdout.write("Applying command to: %s" % tenant.schema_name) connection.set_tenant(tenant) klass.run_from_argv(args)
def run_from_argv(self, argv): """ Changes the option_list to use the options from the wrapped command. Adds schema parameter to specify which schema will be used when executing the wrapped command. """ # load the command object. try: app_name = get_commands()[argv[2]] except KeyError: raise CommandError("Unknown command: {}".format(argv[2])) if isinstance(app_name, BaseCommand): # if the command is already loaded, use it directly. klass = app_name else: klass = load_command_class(app_name, argv[2]) # Ugly, but works. Delete tenant_command from the argv, parse the schema manually # and forward the rest of the arguments to the actual command being wrapped. del argv[1] schema_parser = argparse.ArgumentParser() schema_parser.add_argument("-s", "--schema", dest="schema_name", help="specify tenant schema") schema_namespace, args = schema_parser.parse_known_args(argv) tenant = self.get_tenant_from_options_or_interactive(schema_name=schema_namespace.schema_name) connection.set_tenant(tenant) klass.run_from_argv(args)
def load(): """ Load ``cron`` modules for applications listed in ``INSTALLED_APPS``. """ paths = ['%s.cron' % PROJECT_MODULE.__name__] if '.' in PROJECT_MODULE.__name__: paths.append('%s.cron' % '.'.join( PROJECT_MODULE.__name__.split('.')[0:-1])) for application in settings.INSTALLED_APPS: paths.append('%s.cron' % application) # load kronostasks for p in paths: try: import_module(p) except ImportError as e: if 'No module named' not in str(e): print(e) # load django tasks for cmd, app in get_commands().items(): try: load_command_class(app, cmd) except django.core.exceptions.ImproperlyConfigured: pass
def __new__(cls, *args, **kwargs): """ Sets option_list and help dynamically. """ # instantiate obj = super(BaseTenantCommand, cls).__new__(cls, *args, **kwargs) app_name = get_commands()[obj.COMMAND_NAME] if isinstance(app_name, BaseCommand): # If the command is already loaded, use it directly. cmdclass = app_name else: cmdclass = load_command_class(app_name, obj.COMMAND_NAME) # inherit the options from the original command obj.option_list = cmdclass.option_list #print obj.option_list obj.option_list += ( make_option("-s", "--schema", dest="schema_name"), ) obj.option_list += ( make_option("-p", "--skip-public", dest="skip_public", action="store_true", default=False), ) # prepend the command's original help with the info about schemata iteration obj.help = "Calls %s for all registered schemata. You can use regular %s options. "\ "Original help for %s: %s"\ % (obj.COMMAND_NAME, obj.COMMAND_NAME, obj.COMMAND_NAME,\ getattr(cmdclass, 'help', 'none')) return obj
def main_help_text() -> str: """ Returns the script's main help text, as a string. """ usage = [ "", "Type 'otree help <subcommand>' for help on a specific subcommand.", "", "Available subcommands:", ] commands_dict = defaultdict(lambda: []) for name, app in six.iteritems(get_commands()): if app == 'django.core': app = 'django' else: app = app.rpartition('.')[-1] commands_dict[app].append(name) style = color_style() for app in sorted(commands_dict.keys()): usage.append("") usage.append(style.NOTICE("[%s]" % app)) for name in sorted(commands_dict[app]): usage.append(" %s" % name) return '\n'.join(usage)
def patch_for_test_db_setup(): # Load the commands cache management.get_commands() # Repoint to the correct version of syncdb if hasattr(settings, "SOUTH_TESTS_MIGRATE") and not settings.SOUTH_TESTS_MIGRATE: # point at the core syncdb command when creating tests # tests should always be up to date with the most recent model structure management._commands['syncdb'] = 'django.core' else: management._commands['syncdb'] = MigrateAndSyncCommand() # Avoid flushing data migrations. # http://code.djangoproject.com/ticket/14661 introduced change that flushed custom # sql during the test database creation (thus flushing the data migrations). # we patch flush to be no-op during create_test_db, but still allow flushing # after each test for non-transactional backends. hacks.patch_flush_during_test_db_creation()
def handle(self, *test_labels, **options): # handle south migration in tests commands = management.get_commands() if hasattr(settings, "SOUTH_TESTS_MIGRATE") and not settings.SOUTH_TESTS_MIGRATE: # point at the core syncdb command when creating tests # tests should always be up to date with the most recent model structure commands['syncdb'] = 'django.core' elif 'south' in settings.INSTALLED_APPS: try: from south.management.commands import MigrateAndSyncCommand commands['syncdb'] = MigrateAndSyncCommand() from south.hacks import hacks if hasattr(hacks, "patch_flush_during_test_db_creation"): hacks.patch_flush_during_test_db_creation() except ImportError: commands['syncdb'] = 'django.core' verbosity = int(options.get('verbosity', 1)) interactive = options.get('interactive', True) failfast = options.get('failfast', False) TestRunner = self.get_runner() if not inspect.ismethod(TestRunner): our_options = {"verbosity" : int(verbosity), "interactive" : interactive, "failfast" : failfast} options.update(our_options) failures = TestRunner(test_labels, **options) else: test_runner = TestRunner(verbosity=verbosity, interactive=interactive, failfast=failfast) failures = test_runner.run_tests(test_labels) if failures: sys.exit(bool(failures))
def report_data(dumper): """ Fetches data from management commands and reports it to dumper. :type dumper _xml.XmlDumper :param dumper: destination to report """ utility = ManagementUtility() for command_name in get_commands().keys(): try: command = utility.fetch_command(command_name) except Exception: continue # TODO: Log somehow. Probably print to output? assert isinstance(command, BaseCommand) use_argparse = False try: use_argparse = command.use_argparse except AttributeError: pass dumper.start_command(command_name=command_name, command_help_text=str( command.usage("").replace( "%prog", command_name))) module_to_use = _argparse if use_argparse else _optparse # Choose appropriate module: argparse, optparse module_to_use.process_command(dumper, command, command.create_parser("", command_name)) dumper.close_command()
def get_command_class(name): app_name = get_commands()[name] if isinstance(app_name, BaseCommand): instance = app_name else: instance = load_command_class(app_name, name) return type(instance)
def report_data(dumper): """ Fetches data from management commands and reports it to dumper. :type dumper _xml.XmlDumper :param dumper: destination to report """ utility = ManagementUtility() for command_name in get_commands().keys(): try: command = utility.fetch_command(command_name) except ImproperlyConfigured: continue # TODO: Log somehow assert isinstance(command, BaseCommand) use_argparse = False try: use_argparse = command.use_argparse except AttributeError: pass dumper.start_command(command_name=command_name, command_help_text=str(command.usage("").replace("%prog", command_name))) module_to_use = _argparse if use_argparse else _optparse # Choose appropriate module: argparse, optparse module_to_use.process_command(dumper, command, command.create_parser("", command_name)) dumper.close_command()
def getHTML(request): commands = [] for commandname, appname in get_commands().items(): if commandname != "scheduletasks": try: cmd = getattr( import_module("%s.management.commands.%s" % (appname, commandname)), "Command", ) if getattr(cmd, "index", -1) >= 0 and getattr( cmd, "getHTML", None): commands.append((cmd.index, commandname)) except Exception: pass commands = [i[1] for i in sorted(commands)] schedules = [ s.adjustForTimezone(GridReport.getTimezoneOffset(request)) for s in ScheduledTask.objects.all().using( request.database).order_by("name") ] schedules.append(ScheduledTask()) # Add an empty template return render_to_string( "commands/scheduletasks.html", { "schedules": schedules, "commands": commands }, request=request, )
def load(): """ Load ``cron`` modules for applications listed in ``INSTALLED_APPS``. """ paths = ['%s.cron' % PROJECT_MODULE.__name__] if '.' in PROJECT_MODULE.__name__: paths.append('%s.cron' % '.'.join(PROJECT_MODULE.__name__.split('.')[0:-1])) for application in settings.INSTALLED_APPS: paths.append('%s.cron' % application) # load kronostasks for p in paths: try: import_module(p) except ImportError as e: if 'No module named' not in str(e): print(e) # load django tasks for cmd, app in get_commands().items(): try: load_command_class(app, cmd) except django.core.exceptions.ImproperlyConfigured: pass
def _django_db_setup(request, _django_runner, _django_cursor_wrapper): """Session-wide database setup, internal to pytest-django""" skip_if_no_django() from django.core import management # xdist if hasattr(request.config, 'slaveinput'): db_suffix = request.config.slaveinput['slaveid'] else: db_suffix = None monkey_patch_creation_for_db_suffix(db_suffix) # Disable south's syncdb command commands = management.get_commands() if commands['syncdb'] == 'south': management._commands['syncdb'] = 'django.core' with _django_cursor_wrapper: # Monkey patch Django's setup code to support database re-use if request.config.getvalue('reuse_db'): if not request.config.getvalue('create_db'): monkey_patch_creation_for_db_reuse() _django_runner.teardown_databases = lambda db_cfg: None # Create the database db_cfg = _django_runner.setup_databases() def teardown_database(): with _django_cursor_wrapper: _django_runner.teardown_databases(db_cfg) request.addfinalizer(teardown_database)
def handle(self, *args, **options): test = settings.DEBUG commands = get_commands() organizations = [app_name.split(".")[-1] for app_name in settings.INSTALLED_APPS if app_name.startswith("organizations.")] organization_commands = [command for command in ("setup_%s" % organization for organization in organizations) if command in commands] events = [app_name.split(".")[-1] for app_name in settings.INSTALLED_APPS if app_name.startswith("events.")] event_commands = [command for command in ("setup_%s" % event for event in events) if command in commands] management_commands = [ # (('kompassi_i18n', '-acv2'), dict()), # (('collectstatic',), dict(interactive=False)), (('migrate',), dict(interactive=False)), (('setup_core',), dict(test=test)), (('setup_labour_common_qualifications',), dict(test=test)), (('setup_api_v2',), dict(test=test)), (('setup_access',), dict(test=test)), ] management_commands.extend(((command,), dict(test=test)) for command in organization_commands) management_commands.extend(((command,), dict(test=test)) for command in event_commands) management_commands.append((('access_create_internal_aliases',), dict())) for pargs, opts in management_commands: print("** Running:", pargs[0]) with (atomic() if pargs[0].startswith("setup") else noop_context()): call_command(*pargs, **opts)
def fetch_command(self, subcommand): """ Try to fetch the given subcommand, printing a message with the appropriate command called from the command line (usually "django-admin" or "manage.py") if it can't be found. """ # Get commands outside of try block to prevent swallowing exceptions commands = get_commands() try: app_name = commands[subcommand] except KeyError: if os.environ.get('DJANGO_SETTINGS_MODULE'): # If `subcommand` is missing due to misconfigured settings, the # following line will retrigger an ImproperlyConfigured exception # (get_commands() swallows the original one) so the user is # informed about it. settings.INSTALLED_APPS else: sys.stderr.write("No Django settings specified.\n") possible_matches = get_close_matches(subcommand, commands) sys.stderr.write('Unknown command: %r' % subcommand) if possible_matches: sys.stderr.write('. Did you mean %s?' % possible_matches[0]) sys.stderr.write("\nType '%s help' for usage.\n" % self.prog_name) sys.exit(1) if isinstance(app_name, BaseCommand): # If the command is already loaded, use it directly. klass = app_name else: klass = load_command_class(app_name, subcommand) return klass
def management_view(self, request, *args, **kwargs): class DummyParser: def __init__(self, cmd): if cmd is None: return self.arguments = [] cmd.add_arguments(self) def add_argument(self, name, nargs='*', type=str, help='', # pylint:disable=redefined-builtin action=None, default=None, metavar=None, dest=None, choices=None, add_mutually_exclusive_group=None): self.arguments.append( {'name': name, 'nargs': nargs, 'type': type.__name__, 'help': help }) def get_arguments(self): return self.arguments raw_commands = get_commands() commands = [] modules = [] for key in raw_commands.keys(): if raw_commands[key] not in self.ignore_modules: module = raw_commands[key] name = key command = load_command_class(module, name) parser = DummyParser(command) if module not in modules: modules.append(module) if name not in self.ignore_commands: commands.append({ 'module': module, 'command': name, 'help': command.help, 'arguments': parser.get_arguments(), 'css_order': len(commands) % 2 + 1 }) context = {'test': 'test', 'opts': self.Meta, 'change': False, 'is_popup': False, 'save_as': False, 'has_delete_permission': False, 'has_add_permission': False, 'has_change_permission': False, 'commands': commands, 'modules': modules, 'results': kwargs.get('results', None), 'errors': kwargs.get('errors', None) } return TemplateResponse(request, template="admin/management_admin.html", context=context)