def get_commands(): """ Returns a dictionary mapping command names to their callback applications. This works by looking for a management.commands package in django.core, and in each installed application -- if a commands package exists, all commands in that package are registered. Core commands are always included. If a settings module has been specified, user-defined commands will also be included, the startproject command will be disabled, and the startapp command will be modified to use the directory in which the settings module appears. The dictionary is in the format {command_name: app_name}. Key-value pairs from this dictionary can then be used in calls to load_command_class(app_name, command_name) If a specific version of a command must be loaded (e.g., with the startapp command), the instantiated module can be placed in the dictionary in place of the application name. The dictionary is cached on the first call and reused on subsequent calls. """ global _commands if _commands is None: _commands = dict([(name, 'django.core') for name in find_commands(management.__path__[0])]) # Find the installed apps try: from django.conf import settings apps = settings.INSTALLED_APPS except (AttributeError, EnvironmentError, ImportError): apps = [] # Find and load the management module for each installed app. for app_name in apps: try: path = find_management_module(app_name) _commands.update( dict([(name, app_name) for name in find_commands(path)])) except ImportError: pass # No management module - ignore this app if apps: # Remove the "startproject" command from self.commands, because # that's a django-admin.py command, not a manage.py command. del _commands['startproject'] return _commands
def get_commands(): """ Returns a dictionary mapping command names to their callback applications. This works by looking for a management.commands package in django.core, and in each installed application -- if a commands package exists, all commands in that package are registered. Core commands are always included. If a settings module has been specified, user-defined commands will also be included, the startproject command will be disabled, and the startapp command will be modified to use the directory in which the settings module appears. The dictionary is in the format {command_name: app_name}. Key-value pairs from this dictionary can then be used in calls to load_command_class(app_name, command_name) If a specific version of a command must be loaded (e.g., with the startapp command), the instantiated module can be placed in the dictionary in place of the application name. The dictionary is cached on the first call and reused on subsequent calls. """ global _commands if _commands is None: _commands = dict([(name, 'django.core') for name in find_commands(management.__path__[0])]) # Find the installed apps try: from django.conf import settings apps = settings.INSTALLED_APPS except (AttributeError, EnvironmentError, ImportError): apps = [] # Find and load the management module for each installed app. for app_name in apps: try: path = find_management_module(app_name) _commands.update(dict([(name, app_name) for name in find_commands(path)])) except ImportError: pass # No management module - ignore this app if apps: # Remove the "startproject" command from self.commands, because # that's a django-admin.py command, not a manage.py command. del _commands['startproject'] return _commands
def get_commands(self, app_name, exclude): path = os.path.join(app_name, 'management') commands = find_commands(path) return { self.to_module_name(app_name, c): c for c in commands if not exclude or c not in exclude }
def handle(self, *args, **options): if os.access('/tmp/check_status.lock', os.R_OK): t = os.path.getmtime('/tmp/check_status.lock') created = datetime.datetime.fromtimestamp(t) if (datetime.datetime.now() - created).total_seconds() > 6 * 60 * 60: print('check_status: Stale lock - removing...') os.remove('/tmp/check_status.lock') else: return touch('/tmp/check_status.lock') for app in settings.INSTALLED_APPS: if app.startswith('django') == False: try: command_names = find_commands(find_management_module(app)) for command_name in command_names: if command_name.startswith('pr_status_check_'): # print('Running: ' + command_name) call_command(command_name) touch('/tmp/check_status.lock') except ImportError: pass for device in PurpleRobotDevice.objects.filter(mute_alerts=True): for alert in PurpleRobotAlert.objects.filter(user_id=device.hash_key, dismissed=None): alert.dismissed = timezone.now() alert.save() os.remove('/tmp/check_status.lock')
def get_apps_with_commands(self): apps = [] for app_name in self.get_app_names(): management_module = self.find_management_module(app_name) commands = [] if management_module: command_names = find_commands(management_module) for command_name in command_names: command = {'command_name': command_name} try: command_class = load_command_class(app_name, command_name) available_options = self.get_available_options(command_class) command.update({ 'success': True, 'message': command_class.help, 'available_options': available_options }) except Exception, e: command.update({ 'success': False, 'message': force_str(e) }) commands.append(command) if commands: apps.append({'app_name': app_name, 'commands': commands})
def handle(self, *args, **options): # The tools which don't use other tools are loaded first. # keys are the command name, values are the command objects primary_tools = dict() secondary_tools = dict() apps_to_run_admin_commands = ['biotools'] for app_config in list(apps.get_app_configs()): if app_config.name in apps_to_run_admin_commands: path = os.path.join(app_config.path, 'management') command_names = find_commands(path) for command_name in command_names: if command_name == 'load_all_biotools': continue command = load_command_class(app_config.name, command_name) if hasattr(command, 'uses_other_biotools') and command.uses_other_biotools is True: secondary_tools[command_name] = command else: primary_tools[command_name] = command # load the primary tools for name in primary_tools: command = primary_tools[name] self.stdout.write("INFO: {0} - {1}\n".format(name, command.help)) command.handle() # load the secondary tools for name in secondary_tools: command = secondary_tools[name] self.stdout.write("INFO: {0} - {1}\n".format(name, command.help)) command.handle()
def get_commands(): commands = { name: 'pyon.core' for name in management.find_commands(__path__[0]) } django_commands = management.get_commands() django_commands.update(commands) return django_commands
def test_load_commands(self): try: management_dir = os.path.join('django_extensions', 'management') commands = find_commands(management_dir) for command in commands: load_command_class('django_extensions', command) except Exception as e: self.fail("Can't load command class of {0}\n{1}".format(command, e))
def new_get_commands(): commands = {} commands.update(orig_get_commands()) commands.update({ name: 'django_mako_plus' for name in management.find_commands(DMP_MANAGEMENT_PATH) }) return commands
def setUp(self) -> None: super().setUp() self.commands = [ command for app_config in apps.get_app_configs() if os.path.dirname(app_config.path) == settings.DEPLOY_ROOT for command in find_commands( os.path.join(app_config.path, "management")) ] assert self.commands
def test_load_commands(self): try: management_dir = os.path.join('django_extensions', 'management') commands = find_commands(management_dir) for command in commands: load_command_class('django_extensions', command) except Exception as e: self.fail("Can't load command class of {0}\n{1}".format( command, e))
def test_discover_commands_in_eggs(self): """ Management commands can also be loaded from Python eggs. """ egg_dir = '%s/eggs' % os.path.dirname(__file__) egg_name = '%s/basic.egg' % egg_dir with extend_sys_path(egg_name): with self.settings(INSTALLED_APPS=['commandegg']): cmds = find_commands(os.path.join(apps.get_app_config('commandegg').path, 'management')) self.assertEqual(cmds, ['eggcommand'])
def load_management_modules(commands): app_configs = [ app_config for app_config in apps.get_app_configs() if app_config.name != DjangoPdbConfig.name ] for app_config in reversed(app_configs): path = os.path.join(app_config.path, 'management') commands.update( {name: app_config.name for name in find_commands(path)})
def test_discover_commands_in_eggs(self): """ Test that management commands can also be loaded from Python eggs. """ egg_dir = '%s/eggs' % os.path.dirname(upath(__file__)) egg_name = '%s/basic.egg' % egg_dir with extend_sys_path(egg_name): with self.settings(INSTALLED_APPS=['commandegg']): cmds = find_commands(os.path.join(apps.get_app_config('commandegg').path, 'management')) self.assertEqual(cmds, ['eggcommand'])
def get_parent_commands(): """ Returns a dictionary mapping command names to their callback applications. This function returns only callback applications above this application in the INSTALLED_APPS stack. """ global _parent_commands if _parent_commands is None: django_path = management.__path__ _parent_commands = dict([(name, 'django.core') for name in find_commands(django_path[0])]) # Find the installed apps try: from django.conf import settings apps = settings.INSTALLED_APPS except (AttributeError, EnvironmentError, ImportError): apps = [] # Find and load the management module for each installed app above # this one. for app_name in apps: try: path = find_management_module(app_name) if path == __path__[0]: # Found this app break _parent_commands.update( dict([(name, app_name) for name in find_commands(path)]) ) except ImportError: pass # No management module - ignore this app # Reset the Django management cache management._commands = None return _parent_commands
def get_commands(): """ Return a dictionary mapping command names to their callback applications. Look for a management.commands package in django.core, and in each installed application -- if a commands package exists, register all commands in that package. Core commands are always included. If a settings module has been specified, also include user-defined commands. The dictionary is in the format {command_name: app_name}. Key-value pairs from this dictionary can then be used in calls to load_command_class(app_name, command_name) If a specific version of a command must be loaded (e.g., with the startapp command), the instantiated module can be placed in the dictionary in place of the application name. The dictionary is cached on the first call and reused on subsequent calls. """ commands = django.core.management.get_commands() arkfbp_commands = { name: 'arkfbp.common.django' for name in find_commands(__path__[0]) } commands.update(arkfbp_commands) if not settings.configured: return commands for app_config in reversed(list(apps.get_app_configs())): path = os.path.join(app_config.path, 'management') commands.update( {name: app_config.name for name in find_commands(path)}) return commands
def handle(self, *args, **options): app_names = [a for a in settings.INSTALLED_APPS if a.endswith(".biotools")] for app_name in app_names: command_names = find_commands(find_management_module(app_name)) for command_name in command_names: if command_name == 'load_all_biotools': continue command = load_command_class(app_name, command_name) self.stdout.write("INFO: {0} - {1}\n".format(command_name, command.help)) command.handle()
def available_commands(self): """ Returns a dict mappinng command names to their applications. """ # Built-in commands from `biohub.core.plugins` commands = { name: self.BIOHUB_PLUGINS_MODULE for name in find_commands( modpath('.'.join([self.BIOHUB_PLUGINS_MODULE, 'management'])), ) } return commands
def get_commands(): # Logic filched from django.core.management.get_commands(), but expressed in a saner way. apps_to_commands = defaultdict(OrderedDict) for app_config in apps.get_app_configs(): path = os.path.join(app_config.path, 'management') for command_name in find_commands(path): cmd = ManagementCommand( app_config=app_config, name=command_name, ) if not cmd.is_enabled: continue apps_to_commands[app_config][command_name] = cmd return apps_to_commands
def get_commands(): commands = original_get_commands() # Find and load the management module for all panels path = imp.find_module('piplmesh')[1] panels_directory = imp.find_module('panels', [path])[1] for directory in os.listdir(panels_directory): app_name = 'piplmesh.panels.%s' % directory try: path = management.find_management_module(app_name) commands.update(dict([(name, app_name) for name in management.find_commands(path)])) except ImportError: pass return commands
def handle(self, *args, **options): app_names = [ a for a in settings.INSTALLED_APPS if a.endswith(".biotools") ] for app_name in app_names: command_names = find_commands(find_management_module(app_name)) for command_name in command_names: if command_name == 'load_all_biotools': continue command = load_command_class(app_name, command_name) self.stdout.write("INFO: {0} - {1}\n".format( command_name, command.help)) command.handle()
def get_parent_commands(): """ Returns a dictionary mapping command names to their callback applications. This function returns only callback applications above this application in the INSTALLED_APPS stack. """ global _parent_commands if _parent_commands is None: django_path = management.__path__ _parent_commands = dict([(name, "django.core") for name in find_commands(django_path[0])]) load_management_modules(_parent_commands) # Reset the Django management cache management._commands = None return _parent_commands
def get_parent_commands(): """ Returns a dictionary mapping command names to their callback applications. This function returns only callback applications above this application in the INSTALLED_APPS stack. """ global _parent_commands if _parent_commands is None: django_path = management.__path__ _parent_commands = dict([(name, 'django.core') for name in find_commands(django_path[0])]) load_management_modules(_parent_commands) # Reset the Django management cache management._commands = None return _parent_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 app_config in reversed(list(apps.get_app_configs())): path = os.path.join(app_config.path, 'management') if app_config.name.startswith('lico'): commands.update( {name: app_config for name in find_commands(path)}) commands_dict = defaultdict(lambda: []) for name, app_config in six.iteritems(commands): commands_dict[app_config.verbose_name].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) # 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 get_commands(only_merengue_commands=False): """ Like django.core.management.get_commands but filters only merengue management commands (commands extending MerengueCommand) if only_merengue_commands is True """ if not only_merengue_commands: return django_management.get_commands() global _merengue_commands if _merengue_commands is None: _merengue_commands = {} # look for commands into every merengue app merengue_dir = os.path.abspath(os.path.join(__path__[0], '..', '..')) for f in os.listdir(merengue_dir): path = os.path.join(merengue_dir, f, 'management') if os.path.isdir(path): commands = django_management.find_commands(os.path.abspath(path)) for command in commands: _merengue_commands[command] = 'merengue.%s' % f return _merengue_commands
def get_commands(only_merengue_commands=False): """ Like django.core.management.get_commands but filters only merengue management commands (commands extending MerengueCommand) if only_merengue_commands is True """ if not only_merengue_commands: return django_management.get_commands() global _merengue_commands if _merengue_commands is None: _merengue_commands = {} # look for commands into every merengue app merengue_dir = os.path.abspath(os.path.join(__path__[0], '..', '..')) for f in os.listdir(merengue_dir): path = os.path.join(merengue_dir, f, 'management') if os.path.isdir(path): commands = django_management.find_commands( os.path.abspath(path)) for command in commands: _merengue_commands[command] = 'merengue.%s' % f return _merengue_commands
def load_management_modules(commands): # Find the installed apps try: from django.conf import settings _apps = settings.INSTALLED_APPS except (AttributeError, EnvironmentError, ImportError): _apps = [] # Find and load the management module for each installed app above # this one. for app_name in _apps: try: path = find_management_module(app_name) if path == django_pdb.management.__path__[0]: # Found this app break commands.update( dict([(name, app_name) for name in find_commands(path)])) except ImportError: pass # No management module - ignore this app
def load_management_modules(commands): # Find the installed apps try: from django.conf import settings _apps = settings.INSTALLED_APPS except (AttributeError, EnvironmentError, ImportError): _apps = [] # Find and load the management module for each installed app above # this one. for app_name in _apps: try: path = find_management_module(app_name) if path == django_pdb.management.__path__[0]: # Found this app break commands.update( dict([(name, app_name) for name in find_commands(path)]) ) except ImportError: pass # No management module - ignore this app
def commands(request): template = loader.get_template('djangoappengine_rdbms/commands.html') _commands = {} #import debugger; debugger.pdb().set_trace() for app_name in settings.INSTALLED_APPS + ["django.core"]: try: command_names = find_commands(find_management_module(app_name)) for command_name in command_names: if command_name not in EXCLUDED_COMMANDS: if "%s:%s" % (app_name, command_name) not in OVERWRITE_COMMANDS: _commands[command_name] = {'command_name':command_name,'app_name':app_name} except ImportError: pass #import debugger; debugger.pdb().set_trace() _commands = _commands.values() _commands.sort(key=lambda x: x['command_name']) context = {'commands':_commands} return HttpResponse(template.render(RequestContext(request,context)))
def get_commands(): """ Returns a dictionary mapping command names to their callback applications. Copy from site-packages/django/core/management/__init__.py Override it to ignore django commands. """ commands = {} if not settings.configured: return commands for app_config in reversed(list(apps.get_app_configs())): if not app_config.label.startswith('pyqueuer'): continue path = os.path.join(app_config.path, 'management') commands.update( {name: app_config.name for name in find_commands(path)}) return commands
def handle(self, *args, **options): if os.access('/tmp/check_status.lock', os.R_OK): t = os.path.getmtime('/tmp/check_status.lock') created = datetime.datetime.fromtimestamp(t) if (datetime.datetime.now() - created).total_seconds() > 60 * 60: print('check_status: Stale lock - removing...') os.remove('/tmp/check_status.lock') else: return touch('/tmp/check_status.lock') for app in settings.INSTALLED_APPS: if app.startswith('django') == False: command_names = find_commands(find_management_module(app)) for command_name in command_names: if command_name.startswith('pr_status_check_'): # print('Running: ' + command_name) call_command(command_name) os.remove('/tmp/check_status.lock')
# What's the subcommand being run? # This code uses the same logic from django.core.management to handle command args argv = sys.argv[:] parser = LaxOptionParser(option_list=BaseCommand.option_list) parser.parse_args(argv) if len(argv) > 1: prof_id = subcommand = argv[1] # See if this command belongs to a disabled app commands = { } skipped_apps = sum([ app.django_apps for app in appmanager.SKIPPED_APPS ], []) for app_name in skipped_apps: try: path = find_management_module(app_name) if subcommand in find_commands(path): LOG.info("NOT starting the command '%s' from the disabled application '%s'. Exit." % (subcommand, app_name)) sys.exit(0) except ImportError: pass # No management module - ignore this app else: prof_id = str(os.getpid()) # Let django handle the normal execution if os.getenv("DESKTOP_PROFILE"): _profile(prof_id, lambda: execute_manager(settings)) else: execute_manager(settings)
# What's the subcommand being run? # This code uses the same logic from django.core.management to handle command args argv = sys.argv[:] parser = LaxOptionParser(option_list=BaseCommand.option_list) parser.parse_args(argv) if len(argv) > 1: prof_id = subcommand = argv[1] # See if this command belongs to a disabled app commands = {} skipped_apps = sum([app.django_apps for app in appmanager.BROKEN_APPS], []) for app_name in skipped_apps: try: path = find_management_module(app_name) if subcommand in find_commands(path): LOG.info( "NOT starting the command '%s' from the disabled application '%s'. Exit." % (subcommand, app_name)) sys.exit(0) except ImportError: pass # No management module - ignore this app else: prof_id = str(os.getpid()) # Let django handle the normal execution if os.getenv("DESKTOP_PROFILE"): _profile(prof_id, lambda: execute_manager(settings)) else: execute_manager(settings)
def load_management_modules(commands): app_configs = [app_config for app_config in apps.get_app_configs() if app_config.name != DjangoPdbConfig.name] for app_config in reversed(app_configs): path = os.path.join(app_config.path, 'management') commands.update({name: app_config.name for name in find_commands(path)})
def get_commands(): commands = {name: "django_environs" for name in find_commands(__path__[0])} return commands
#!/usr/bin/env python import os # import sys from django.core import management # from coop import management as coop_management import pes.management if __name__ == "__main__": # global management._commands if management._commands is None: management._commands = dict([(name, 'pes') for name in management.find_commands(pes.management.__path__[0])]) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pes.minimal_settings") management.execute_from_command_line()
def setUp(self): management_dir = os.path.join('django_extensions', 'management') self.commands = find_commands(management_dir)
#!/usr/bin/env python import os # import sys from django.core import management # from coop import management as coop_management import coop.management if __name__ == "__main__": # global management._commands if management._commands is None: management._commands = dict([(name, 'coop') for name in management.find_commands(coop.management.__path__[0])]) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "coop.minimal_settings") management.execute_from_command_line()
def main(): """Main entry point for the command line interface The workflow of this function follows a similar pattern as Django's MangementUtility.execute() in that an inital "fake" parser is used to parse a couple preliminary arguments, but we always force the very first argument to be the subcommand, never an option. It also only exports commands from this package. """ argv = sys.argv try: subcommand = argv[1] except IndexError: subcommand = "help" parser = argparse.ArgumentParser( usage="%(prog)s subcommand --config CONFIG [options] [args]", add_help=False, ) parser.add_argument("--config") options, args = parser.parse_known_args(argv) # Set the path to the database from either the command line option or an # env var. It must be set one way or the other. if options.config: os.environ['BACKATHON_CONFIG'] = options.config if not "BACKATHON_CONFIG" in os.environ: if subcommand == "help": # Just going to display some help... set an in-memory database so # we don't run into any errors if something tries to do database # access os.environ['BACKATHON_CONFIG'] = ":memory:" else: parser.error( "You must use --config or set the environment variable " "BACKATHON_CONFIG") dbpath = os.environ['BACKATHON_CONFIG'] # Special exception, all commands except for 'init' require the database # to exist. if (subcommand not in ['init', 'help'] and not os.path.exists(dbpath)): sys.stderr.write("Could not find config database: {}\n".format(dbpath)) sys.stderr.write( "Check the path, or if this is a new config you must run 'init'\n") sys.exit(1) setup() # Now that we've configured Django, we can import the rest of the modules # and configure the real parser specific for the given subcommand backathon_config = apps.app_configs['backathon'] commands = find_commands(os.path.join(backathon_config.path, 'management')) if subcommand == "help": usage = [ parser.usage % { 'prog': parser.prog }, "", "Available subcommands:" ] for command in sorted(commands): usage.append("\t" + command) sys.stdout.write("\n".join(usage) + "\n") sys.exit(1) if subcommand not in commands: sys.stderr.write( "Unknown command: {!r}\tType '{} help' for usage.\n".format( subcommand, os.path.basename(argv[0]))) sys.exit(1) command_class = load_command_class("backathon", subcommand) assert isinstance(command_class, BaseCommand) # Reconfigure the parser and re-parse the arguments parser = argparse.ArgumentParser( prog="{} {}".format(os.path.basename(argv[0]), subcommand), description=command_class.help or None, ) parser.add_argument("-v", "--verbose", action="count", default=0) parser.add_argument("-q", "--quiet", action="store_true") parser.add_argument("--config", help="Path to the config database") command_class.add_arguments(parser) options = parser.parse_args(argv[2:]) # Set log level if options.quiet: level = logging.ERROR elif options.verbose == 0: level = logging.WARNING elif options.verbose == 1: level = logging.INFO else: level = logging.DEBUG logging.getLogger("backathon").setLevel(level) logger.info("Using config database {}".format(dbpath)) try: command_class.handle(**vars(options)) except CommandError as e: command_class.stderr.write(str(e)) sys.exit(1)
def get_commands(self, app_name, exclude): path = os.path.join(app_name, 'management') commands = find_commands(path) return {self.to_module_name(app_name, c): c for c in commands if not exclude or c not in exclude}
def get_commands(): commands = {name: 'pyon.core' for name in management.find_commands(__path__[0])} django_commands = management.get_commands() django_commands.update(commands) return django_commands