Пример #1
0
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
Пример #2
0
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 test_load_commands(self):
     """Try to load every management command to catch exceptions."""
     try:
         for command in self.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_load_commands(self):
     """Try to load every management command to catch exceptions."""
     try:
         for command in self.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_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 setUp(self):
     self.original_cursor_wrapper = db_backends_util.CursorDebugWrapper
     # Since debugsqlshell monkey-patches django.db.backends.utils, we can
     # test it simply by loading it, without executing it. But we have to
     # undo the monkey-patch on exit.
     command_name = 'debugsqlshell'
     app_name = management.get_commands()[command_name]
     management.load_command_class(app_name, command_name)
Пример #7
0
 def setUp(self):
     self.original_cursor_wrapper = db_backends_util.CursorDebugWrapper
     # Since debugsqlshell monkey-patches django.db.backends.utils, we can
     # test it simply by loading it, without executing it. But we have to
     # undo the monkey-patch on exit.
     command_name = 'debugsqlshell'
     app_name = management.get_commands()[command_name]
     management.load_command_class(app_name, command_name)
Пример #8
0
 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))
Пример #9
0
    def handle(self, *args, **options):
        cmds = [(app, cmd, load_command_class(app, cmd).help)
                for cmd, app in get_commands().items()
                if not app.startswith('django')
                and not hasattr(load_command_class(app, cmd), 'ScheduledJob')]

        for app, g in groupby(sorted(cmds, key=lambda r: r[0]),
                              lambda r: r[0]):
            self.stdout.write("* {}".format(app))
            found = False
            for x, c, h in g:
                self.stdout.write("   {} -- {}".format(c, h))
                found = True
            if found:
                self.stdout.write("")
Пример #10
0
def locate_command(name):
    """
        Apps may override Django commands, what we want to do is
        subclass whichever one had precedence before the gcloudc.commands app and subclass that
    """

    try:
        index = settings.INSTALLED_APPS.index("gcloudc.commands")
    except ValueError:
        raise ImproperlyConfigured(
            "Unable to locate gcloudc.commands in INSTALLED_APPS")

    APPS_TO_CHECK = list(settings.INSTALLED_APPS) + ["django.core"]

    for i in range(index + 1, len(APPS_TO_CHECK)):
        app_label = APPS_TO_CHECK[i]
        try:
            command = load_command_class(app_label, name)
        except ModuleNotFoundError:
            continue

        if command:
            return command.__class__
    else:
        raise ImportError("Unable to locate a base %s Command to subclass" %
                          name)
Пример #11
0
 def run_with_argv_raw(self, argv_raw):
     try:
         argv = [self.app_name, self.command_name] + shlex.split(argv_raw)
         command_class = load_command_class(self.app_name, self.command_name)
         command_class.run_from_argv(argv)
     except SystemExit:
         pass
Пример #12
0
    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})
Пример #13
0
    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()
Пример #14
0
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
Пример #15
0
    def send_reminder(self, request, sdo=None):
        opts = self.model._meta
        app_label = opts.app_label

        output = None
        sdo_pk = sdo and sdo.pk or None
        if request.method == 'POST' and request.POST.get('send', False):
            command = load_command_class('ietf.liaisons',
                                         'remind_update_sdo_list')
            output = command.handle(return_output=True, sdo_pk=sdo_pk)
            output = '\n'.join(output)

        context = {
            'opts': opts,
            'has_change_permission': self.has_change_permission(request),
            'app_label': app_label,
            'output': output,
            'sdo': sdo,
        }
        return render_to_response(
            'admin/group/group/send_sdo_reminder.html',
            context,
            context_instance=template.RequestContext(
                request, current_app=self.admin_site.name),
        )
Пример #16
0
    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 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)
Пример #18
0
    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)
Пример #19
0
def command_details(request, app_name, command_name):

    command = load_command_class(app_name, command_name)
    template = loader.get_template('djangoappengine_rdbms/command_details.html')
    #
    
    stdout = StringIO.StringIO()
    stderr = StringIO.StringIO()
    



    @redirect_stderr_stdout(stdout=stdout,stderr=stderr)            
    def _execute_command(command, command_name, stdout, stderr, argv):

        parser = command.create_parser("manage.py", command_name)
        options, argss = parser.parse_args(argv.split())
        handle_default_options(options)
        options.__dict__["stdout"] = stdout
        options.__dict__["stderr"] = stderr
        options.__dict__['interactive'] = False
        #import debugger; debugger.pdb().set_trace()
        try:
            return command.execute(*argss, **options.__dict__)
        except SystemExit, e:
            pass
        except Exception, e:
            stderr.write(e)
Пример #20
0
def fetch_command(command_name):
    try:
        app_name = get_available_commands()[command_name]
    except KeyError:
        return None
    klass = load_command_class(app_name, command_name)
    return klass
Пример #21
0
 def fetch_command(self, subcommand):
     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
Пример #22
0
    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)
Пример #23
0
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']:
        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
Пример #24
0
    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)
Пример #25
0
    def test_run(self) :		
        command = load_command_class('biotools', 'load_geneid__1_4')
        command.handle()

        biotools_settings = configparser.ConfigParser()
        biotools_settings.read( os.path.join( os.path.abspath(os.path.dirname(__file__)), '../settings.ini') )

        genome_file = LocalFile( label = 'C. cerevisiae S288C genome', \
                                 path  = "{0}/Saccharomyces_cerevisiae_S288C/Saccharomyces_cerevisiae_S288C.genomic.fna".format(settings.EMERGENCE_EXAMPLE_DATA_ROOT) )
        genome_file.save()

        geneid = StandaloneTool.objects.get( name='geneid', version='1.4' )
        flow = geneid.new_flow()
        flow.save()

        command = flow.get_command(name='Run geneid')
        command.set_param(name='<sequence_filename>', val=genome_file.path)
        command.stdout = '/tmp/geneid.test.out'

        geneid_settings = biotools_settings[ "geneid 1.4" ]
        command.set_param(name='-P', val=geneid_settings['test_param_file'] )

        flow.run()

        while flow.is_executing():
            flow = Flow.objects.get(id=flow.id)
            print("Gene prediction state is: {0}".format(flow.get_state_display()) )
            sleep(1)

            self.assertEqual(format(flow.get_state_display()) , 'complete')
Пример #26
0
    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 _dj_call_command(command_name, *args, **options):
    try:
        command = load_command_class('django.core', command_name)
    except ImportError:
        return None
    parser = command.create_parser('', command_name)
    # Use the `dest` option name from the parser option

    if hasattr(parser, '_actions'):
        opt_mapping = {
            min(s_opt.option_strings).lstrip('-').replace('-', '_'): s_opt.dest
            for s_opt in parser._actions if s_opt.option_strings
        }
    else:
        opt_mapping = {}

    arg_options = {
        opt_mapping.get(key, key): value
        for key, value in options.items()
    }
    defaults = parser.parse_args(args=[force_text(a) for a in args])
    if hasattr(defaults, '_get_kwargs'):
        defaults = dict(defaults._get_kwargs(), **arg_options)
    else:
        defaults = dict(**arg_options)
    # Move positional args out of options to mimic legacy optparse
    args = defaults.pop('args', ())
    if 'skip_checks' not in options:
        defaults['skip_checks'] = True

    return command.execute(*args, **defaults)
    def test_run(self) :
        # Load dependent tools.  This is only needed because Django creates a new database for each test
        for tool_name in ( 'load_nucmer__3_23', 'load_show_coords__3_23', 'load_nucmer_genome_coverage__1_0' ):
            command = load_command_class('emergence.apps.biotools', tool_name)
            command.handle()

        print("INFO: Loading E. coli K12")
        k12_genome_file = LocalFile( label = 'E. coli K12 DH10B genome', \
                                     path  = "{0}/Escherichia_coli_K12_DH10B/e_coli_k12_dh10b.fna".format(settings.EMERGENCE_EXAMPLE_DATA_ROOT) )
        k12_genome_file.save()

        print("INFO: Loading E. coli O157 H7")
        o157_genome_file = LocalFile( label = 'E. coli K12 DH10B genome', \
                                      path  = "{0}/Escherichia_coli_O157_H7_1044/NZ_AERP00000000.scaffolds.fna".format(settings.EMERGENCE_EXAMPLE_DATA_ROOT) )
        o157_genome_file.save()
	
        genome_cov = StandaloneTool.objects.get( name='NUCmer genome coverage', version='1.0' )

        ## this results in a built Flow
        flow = genome_cov.new_flow()
        flow.save()

        print("DEBUG: Flow that was just built:")
        for thing in flow.get_children():
            print("step: {0}".format(thing))
        
        nucmer_command = flow.get_command(name='Run NUCmer')
        print("DEBUG: TESTING: Within the test, flow.get_command() for nucmer returned this exec string: {0}".format(nucmer_command.exec_string))
        nucmer_command.set_param(name='<reference_in>', val=k12_genome_file.path)
        nucmer_command.set_param(name='<query_in>', val=o157_genome_file.path)

        #sc_command = flow.get_command(name='Run show-coords')
        # THIS NEEDS TO BE SET TO THE CWD/out.delta but we don't know the CWD at build time.  Issue.
        #sc_command.set_param(name='<deltafile>', val=nuc )
        flow.run()
Пример #29
0
    def test_run(self) :		
        command = load_command_class('biotools', 'load_prodigal__2_60')
        command.handle()

        genome_file = LocalFile( label = 'E. coli K12 DH10B genome', \
                                 path  = "{0}/Escherichia_coli_K12_DH10B/e_coli_k12_dh10b.fna".format(settings.EMERGENCE_EXAMPLE_DATA_ROOT) )
        genome_file.save()

        prodigal = StandaloneTool.objects.get( name='Prodigal', version='2.60' )
        flow = prodigal.new_flow()
        flow.save()

        command = flow.get_command(name='Run prodigal')
        command.set_param(name='-i', val=genome_file.path)
        command.set_param(name='-o', val='/tmp/prodigal.test.out' )
        command.set_param(name='-g', val='10' )

        flow.run()

        while flow.is_executing():
            flow = Flow.objects.get(id=flow.id)
            print("Gene prediction state is: {0}".format(flow.get_state_display()) )
            sleep(1)

            self.assertEqual(format(flow.get_state_display()) , 'complete')
Пример #30
0
    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)
Пример #31
0
    def __init__(self, *args, **kwargs):
        super(CommandExecutionForm, self).__init__(*args, **kwargs)
        self.command_dict = get_commands()
        self.allowed_commands = []
        apps = {}
        for command_name, app_name in self.command_dict.iteritems():
            if self.command_allowed(command_name, app_name):
                self.allowed_commands.append(command_name)
                command_class = load_command_class(app_name, command_name)
                # creating an optionparser
                optparser = command_class.create_parser(
                    './manage.py', command_name)
                # get the docstring from the parser
                docstring = ''
                if optparser.usage is not None:
                    docstring = optparser.usage.replace('%prog', './manage.py')
                # get the options
                options = []
                Option = namedtuple('Option', ['opt_string', 'help'])
                for opt in getattr(optparser, 'option_list', []):
                    if opt.dest is not None:
                        dest = opt.dest.upper()
                    else:
                        dest = ''
                    opt_string = ','.join(opt._long_opts)
                    if dest:
                        opt_string += '={0}'.format(dest)
                    if opt._short_opts:
                        opt_string = '{0} {1}, '.format(
                            ','.join(opt._short_opts), dest) + opt_string
                    options.append(Option(opt_string, opt.help))

                Command = namedtuple('Command', ['command', 'docstring',
                                                 'options', 'log'])
                if command_class.__doc__ is not None:
                    # in case there's a docstring on the class, prepend it to
                    # make sure all description is included.
                    docstring = command_class.__doc__ + '\n\n' + docstring

                log = None
                if app_settings.LOGFILE_PATH is not None:
                    file_name = os.path.join(
                        app_settings.LOGFILE_PATH,
                        'command_interface_log-{0}.log'.format(command_name))
                    try:
                        with open(file_name, 'r') as f:
                            log = f.read()
                    except IOError:
                        pass

                command = Command(command_name, docstring, options, log)
                App = namedtuple(app_name.replace('.', '_'),
                                 ['app_name', 'commands'])
                if not app_name in apps:
                    apps[app_name] = App(app_name, [command])
                else:
                    apps[app_name].commands.append(command)
        self.apps = apps.values()
Пример #32
0
 def test_help_message_contains_the_time(self):
     '''
     Test that when you run the help command on the command inactiveuser
         - That you get the minutes that were set for that command.
     '''
     command_class = management.load_command_class('front', 'changeemail')
     self.assertIn(
         'after {} minutes'.format(
             settings.CHANGE_EMAIL_EXPIRY_MINUTES_TIME), command_class.help)
Пример #33
0
def get_command(name):
    # this is a copy pasted from django.core.management.call_command
    app_name = get_commands()[name]
    if isinstance(app_name, BaseCommand):
        # If the command is already loaded, use it directly.
        klass = app_name
    else:
        klass = load_command_class(app_name, name)
    return klass
Пример #34
0
def get_command(name):
    # this is a copy pasted from django.core.management.call_command
    app_name = get_commands()[name]
    if isinstance(app_name, BaseCommand):
        # If the command is already loaded, use it directly.
        klass = app_name
    else:
        klass = load_command_class(app_name, name)
    return klass
Пример #35
0
def handle_post_migrate(sender, **kwargs):
    ScheduledJob = sender.get_model('ScheduledJob')

    with transaction.atomic():
        # Check that our own app has actually been migrated. If it has not, just don't
        # do anything -- it might be part of an initial migration of just some apps.
        curs = connection.cursor()
        curs.execute("SELECT id FROM django_migrations WHERE app='scheduler'")
        if curs.rowcount == 0:
            print("Job scheduler not yet installed, not triggering migration")
            return

        jobs = []
        for name, app in get_commands().items():
            if app.startswith('django'):
                continue
            cmd = load_command_class(app, name)
            if hasattr(cmd, 'ScheduledJob'):
                job, created = ScheduledJob.objects.get_or_create(app=app,
                                                                  command=name)

                dirty = False

                def _update_field(jobfield,
                                  cmdfield,
                                  obj=cmd.ScheduledJob,
                                  default=None):
                    if getattr(job, jobfield) != getattr(
                            obj, cmdfield, default):
                        setattr(job, jobfield, getattr(obj, cmdfield, default))
                        return True
                    return False

                dirty += _update_field('description', 'help', obj=cmd)
                dirty += _update_field('scheduled_interval',
                                       'scheduled_interval')
                dirty += _update_field('scheduled_times', 'scheduled_times')
                if dirty:
                    job.full_clean()
                    if created:
                        if getattr(cmd.ScheduledJob,
                                   'default_notify_on_success', None):
                            job.notifyonsuccess = True
                        reschedule_job(job, save=False)
                    job.save()
                    if created:
                        print("Created scheduled job for {}".format(
                            job.description))
                    else:
                        print("Updated scheduled job for {}".format(
                            job.description))
                jobs.append(job.pk)

        for dj in ScheduledJob.objects.exclude(pk__in=jobs):
            print("Deleted scheduled job for {}".format(dj.description))
            dj.delete()
Пример #36
0
 def test_loads_all_regiond_commands(self):
     parser = ArgumentParser()
     cli.register_cli_commands(parser)
     for name, app, help_text in cli.regiond_commands:
         subparser = parser.subparsers.choices.get(name)
         klass = management.load_command_class(app, name)
         if help_text is None:
             help_text = klass.help
         self.assertIsNotNone(subparser)
         self.assertEqual(help_text, subparser.description)
Пример #37
0
def index(request):
    all_cmds = management.get_commands()
    cmds = []
    for cmd, app in all_cmds.items():
        if app == 'data':
            cmd_class = management.load_command_class("data", cmd)
            cmd_class.name = cmd
            cmds.append(cmd_class)

    return render(request, 'data/index.html', {"cmds": cmds})
Пример #38
0
def can_ping(fqdn, count=4, timeout=5):
    ping_cls = load_command_class('relops_hardware_controller.api', 'ping')

    try:
        call_command(ping_cls, fqdn, 'ping', '-c', count, '-w', timeout)
        logger.debug('pinging %s succeeded', fqdn)
        return True
    except Exception as error:
        logger.warn('pinging %s failed: %s', fqdn, error)
        return False
Пример #39
0
def get_command_by_name(command_name: str) -> BaseCommand:
    """
    Gets Django management BaseCommand derived command class instance by name.
    """
    all_commands = get_commands()
    app_name = all_commands.get(command_name)
    if app_name is None:
        raise Exception(f"Django management command {command_name} not found")
    command = app_name if isinstance(app_name, BaseCommand) else load_command_class(app_name, command_name)
    assert isinstance(command, BaseCommand)
    return command
Пример #40
0
 def _get_command_class(self, name):
     try:
         app_name = get_commands()[name]
         if isinstance(app_name, BaseCommand):
             # If the command is already loaded, use it directly.
             klass = app_name
         else:
             klass = load_command_class(app_name, name)
     except KeyError:
         raise CommandError("Unknown command: %r" % name)
     return klass
Пример #41
0
def get_management_command_details(command_name):
    """
    Get the help output of the management command.
    """
    app_name = get_management_commands_apps()[command_name]
    command_class = load_command_class(app_name, command_name)
    parser = command_class.create_parser("", command_name)
    with io.StringIO() as output:
        parser.print_help(output)
        cmd_help_output = output.getvalue()
    return cmd_help_output
Пример #42
0
def call_command(name, *args, **options):
    """
    Calls the given command, with the given options and args/kwargs.

    This is the primary API you should use for calling specific commands.

    Some examples:
        call_command('migrate')
        call_command('shell', plain=True)
        call_command('sqlmigrate', 'myapp')

    Copy of the function from django.core.management. In addition to the
    standard functionality, stores the raw args on the command instance.
    """
    # Load the command object.
    try:
        app_name = get_commands()[name]
    except KeyError:
        raise CommandError("Unknown command: %r" % name)

    if isinstance(app_name, BaseCommand):
        # If the command is already loaded, use it directly.
        command = app_name
    else:
        command = load_command_class(app_name, name)

    # Store the raw args on the command so they can be used later if needed
    command.raw_args = args

    # Simulate argument parsing to get the option defaults (see #10080 for details).
    parser = command.create_parser('', name)
    if command.use_argparse:
        # Use the `dest` option name from the parser option
        opt_mapping = {
            sorted(s_opt.option_strings)[0].lstrip('-').replace('-', '_'):
            s_opt.dest
            for s_opt in parser._actions if s_opt.option_strings
        }
        arg_options = {
            opt_mapping.get(key, key): value
            for key, value in options.items()
        }
        defaults = parser.parse_args(args=args)
        defaults = dict(defaults._get_kwargs(), **arg_options)
        # Move positional args out of options to mimic legacy optparse
        args = defaults.pop('args', ())
    else:
        # Legacy optparse method
        defaults, _ = parser.parse_args(args=[])
        defaults = dict(defaults.__dict__, **options)
    if 'skip_checks' not in options:
        defaults['skip_checks'] = True

    return command.execute(*args, **defaults)
Пример #43
0
def load():
    """
    Load ``cron`` modules for applications listed in ``INSTALLED_APPS``.
    """
    autodiscover_modules('cron')

    if '.' in PROJECT_MODULE.__name__:
        try:
            import_module('%s.cron' % '.'.join(
	            PROJECT_MODULE.__name__.split('.')[0:-1]))
        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
Пример #44
0
def load_regiond_commands(management, parser):
    """Load the allowed regiond commands into the MAAS cli."""
    for name, app, help_text in regiond_commands:
        klass = management.load_command_class(app, name)
        if help_text is None:
            help_text = klass.help
        command_parser = parser.subparsers.add_parser(
            safe_name(name), help=help_text, description=help_text)
        klass.add_arguments(command_parser)
        command_parser.set_defaults(
            execute=partial(run_regiond_command, management))
Пример #45
0
 def _get_command_class(self, name):
     try:
         app_name = get_commands()[name]
         if isinstance(app_name, BaseCommand):
             # If the command is already loaded, use it directly.
             klass = app_name
         else:
             klass = load_command_class(app_name, name)
     except KeyError:
         raise CommandError("Unknown command: %r" % name)
     return klass
Пример #46
0
    def setUpTestData(cls):
        cls.import_command = management.load_command_class(
            ConsumptionConfig.name, 'import')
        # change to test data path
        cls.import_command.user_data_path = os.path.join(
            settings.TESTDATA_DIR, settings.IMPORT_USER_DATA_PATH)
        cls.import_command.consumption_path = os.path.join(
            settings.TESTDATA_DIR, settings.IMPORT_CONSUMPTION_PATH)

        # execute
        cls.import_command.handle()
Пример #47
0
def load():
    """
    Load ``cron`` modules for applications listed in ``INSTALLED_APPS``.
    """
    autodiscover_modules('cron')

    if PROJECT_MODULE:
        if '.' in PROJECT_MODULE.__name__:
            try:
                import_module('%s.cron' % '.'.join(
                    PROJECT_MODULE.__name__.split('.')[0:-1]))
            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
Пример #48
0
def monkey_patch_command_execute(name):
    """
    Monkey patches a django management command to give it an execute
    function that does not sys.exit(1) on CommandError. Bug was fixed in
    django 1.5 and hence this should only be used for less than 1.5 django
    version
    """
    from django.core import management
    from django.core.management.base import CommandError, BaseCommand
    import types
    # Populate the cache
    app_name = management.get_commands()[name]
    # Get the command
    if isinstance(app_name, BaseCommand):
        klass = app_name
    else:
        klass = management.load_command_class(app_name, name)

    def execute(self, *args, **options):
        """
        Try to execute this command, performing model validation if
        needed (as controlled by the attribute
        ``self.requires_model_validation``). If the command raises a
        ``CommandError``, intercept it and print it sensibly to
        stderr.
        """
        from django.utils.encoding import smart_str
        import sys
        show_traceback = options.get('traceback', False)

        try:
            self.stdout = options.get('stdout', sys.stdout)
            self.stderr = options.get('stderr', sys.stderr)
            if self.requires_model_validation:
                self.validate()
            output = self.handle(*args, **options)
            if output:
                if self.output_transaction:
                    # This needs to be imported here, because it relies on
                    # settings.
                    from django.db import connections, DEFAULT_DB_ALIAS
                    connection = connections[options.get('database', DEFAULT_DB_ALIAS)]
                    if connection.ops.start_transaction_sql():
                        self.stdout.write(self.style.SQL_KEYWORD(connection.ops.start_transaction_sql()) + '\n')
                self.stdout.write(output)
                if self.output_transaction:
                    self.stdout.write('\n' + self.style.SQL_KEYWORD("COMMIT;") + '\n')
        except CommandError, e:
            if show_traceback:
                traceback.print_exc()
            else:
                self.stderr.write(smart_str(self.style.ERROR('Error: %s\n' % e)))
Пример #49
0
 def get_command_from_arg(self, arg):
     *chunks, command = arg.split(".")
     path = ".".join(chunks)
     if not path:
         path = get_commands().get(command)
     try:
         cmd = load_command_class(path, command)
     except Exception:
         raise CommandError("Unknown command: %s" % arg)
     if isinstance(cmd, WrappedSchemaOption):
         raise CommandError("Command '%s' cannot be used in runschema" %
                            arg)
     return cmd
Пример #50
0
    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()
Пример #51
0
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:
            pass

    # load django tasks
    for cmd, app in get_commands().items():
        load_command_class(app, cmd)
Пример #52
0
 def __new__(cls, *args, **kwargs):
     """
     Sets option_list and help dynamically.
     """
     # instantiate
     obj = super(BaseSchemataCommand, cls).__new__(cls, *args, **kwargs)
     # load the command class
     cmdclass = load_command_class(get_commands()[obj.COMMAND_NAME], obj.COMMAND_NAME) 
     # inherit the options from the original command
     obj.option_list = cmdclass.option_list
     # 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 group_commands():
    grouped = {}
    for command, provider in get_commands().items():
        if isinstance(provider, basestring):
            key = provider
            cls = load_command_class(provider, command)
        elif isinstance(provider, base.BaseCommand):
            cls = provider
            mod = provider.__module__
            idx = mod.find('management') - 1
            key = mod[:idx]
        else:
            key = '?unknown'
        value = (command, getattr(cls, 'help', ''))
        grouped[key] = sorted(grouped.get(key, []) + [value])
    return grouped
Пример #54
0
    def __new__(cls, *args, **kwargs):
        """
        Sets option_list and help dynamically.
        """
        obj = super(BaseTenantCommand, cls).__new__(cls, *args)

        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)

        # prepend the command's original help with the info about schemata iteration
        obj.help = "Calls {0} for all registered schemata. You can use regular {0} options. " \
                   "Original help for {0}: {1}".format(obj.COMMAND_NAME, getattr(cmdclass, 'help', 'none'))
        return obj
Пример #55
0
    def handle_noargs(self, **options):
        from django.conf import settings
        from django.db import models
        from django.core.management.sql import emit_post_sync_signal
        from django.db.utils import DEFAULT_DB_ALIAS
        from django.core.management import call_command

        ignore_reset = options.get('ignore_reset')
        if ignore_reset is None:
            ignore_reset = self.ignore_reset_default

        db = options.get('database', DEFAULT_DB_ALIAS)
        database_config = settings.DATABASES[db]

        if not ignore_reset:
            reset_schema(database_config)

            # hack to avoid using south
            klass = load_command_class('django.core', 'syncdb')
            args = {}
            defaults = {}
            options['interactive'] = False
            for opt in klass.option_list:
                if opt.default is NO_DEFAULT:
                    defaults[opt.dest] = None
                else:
                    defaults[opt.dest] = opt.default
            defaults.update(options)
            klass.execute(*args, **defaults)

            # Emit the post sync signal. This allows individual
            # applications to respond as if the database had been
            # sync'd from scratch.
            emit_post_sync_signal(models.get_models(), 0, 0, db)

            call_command('migrate', fake=True)

        self.log("load Random Dummy data ...")

        try:
            user = get_user_model().objects.create_superuser('admin', '*****@*****.**', DUMMY_PASSWORD)
            user.first_name, user.last_name = name_factory.get_full_name()
            user.save()
            self.log("superuser: admin")
        except Exception, e:
            self.log("WARNING: could not create superuser: %s" % str(e))
Пример #56
0
    def fetch_command(self, subcommand):

        commands = get_commands()

        try:
            app_name = commands[subcommand]
        except KeyError:
            sys.stderr.write("Unknown command: %r\nType '%s help' for usage.\n" %
                             (subcommand, self.prog_name))
            sys.exit(1)

        if isinstance(app_name, management.BaseCommand):
            klass = app_name
        else:
            klass = management.load_command_class(app_name, subcommand)

        return klass
Пример #57
0
        def the_patched(*args, **kw):
            res = get_commands(*args, **kw)
            tester = res.get('test', None)
            if tester is None:
                return res
            if isinstance(tester, basestring):
                tester = management.load_command_class('django.core', 'test')

            new_options = basic[:]

            ignored_opts = ('--unit', '--functional', '--integration')
            for opt in tester.option_list:
                if opt.get_opt_string() not in ignored_opts:
                    new_options.insert(0, opt)

            tester.option_list = tuple(new_options)
            res['test'] = tester
            return res
Пример #58
0
def monkey_patch_command_execute(name):
    """
    Monkey patches a django management command to give it an execute
    function that does not sys.exit(1) on CommandError. Bug was fixed in
    django 1.5 and hence this should only be used for less than 1.5 django
    version
    """
    from django.core import management
    from django.core.management.base import CommandError, BaseCommand
    import types
    # Populate the cache
    app_name = management.get_commands()[name]
    # Get the command
    if isinstance(app_name, BaseCommand):
        klass = app_name
    else:
        klass = management.load_command_class(app_name, name)

    def execute(self, *args, **options):
        """
        Try to execute this command, performing model validation if
        needed (as controlled by the attribute
        ``self.requires_model_validation``). If the command raises a
        ``CommandError``, intercept it and print it sensibly to
        stderr.
        """
        from django.utils.encoding import smart_str
        import sys

        try:
            self.stdout = options.get('stdout', sys.stdout)
            self.stderr = options.get('stderr', sys.stderr)
            if self.requires_model_validation:
                self.validate()
            output = self.handle(*args, **options)
            if output:
                # Since this is only used for error catching an actual body in
                # this is useless. Just pass
                pass
        except CommandError as e:
                self.stderr.write(smart_str(self.style.ERROR('Error: %s\n' % e)))
    setattr(klass, 'execute', types.MethodType(execute, klass))
    # Finalize command manipulation
    management._commands[name] = klass
Пример #59
0
 def fetch_command(self, subcommand):
     """
     Tries to fetch the given subcommand, printing a message with the
     appropriate command called from the command line (usually
     "django-admin.py" or "manage.py") if it can't be found.
     """
     try:
         app_name = get_commands()[subcommand]
     except KeyError:
         sys.stderr.write(("Unknown command: %r\n"
                           "Type '%s help' for usage.\n") %
                          (subcommand, 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