Exemple #1
0
    def test_find_matched_conf(self):
        matched_conf = find_matched_conf(self.testapp1, 'sample1.cfg')
        self.assertEqual(
            matched_conf.path,
            'tests/sample_data/configs/testapp1/configs/sample1.cfg')

        matched_conf = find_matched_conf(
            self.testapp1,
            'tests/sample_data/configs/testapp1/configs/sample1.cfg')
        self.assertEqual(
            matched_conf.path,
            'tests/sample_data/configs/testapp1/configs/sample1.cfg')

        matched_conf = find_matched_conf(self.testapp1,
                                         'a:common.name=sample-test1')
        self.assertEqual(
            matched_conf.path,
            'tests/sample_data/configs/testapp1/configs/sample1.cfg')

        matched_conf = find_matched_conf(self.testapp1,
                                         'a:common.name=sample-test1')
        self.assertEqual(
            matched_conf.path,
            'tests/sample_data/configs/testapp1/configs/sample1.cfg')

        matched_conf = find_matched_conf(self.testapp1, 'f:unknown')
        self.assertEqual(matched_conf, None)
Exemple #2
0
    def handle(self, app_label, **options):
        from moose.apps import apps
        try:
            app_config = apps.get_app_config(app_label)
        except (LookupError, ImportError) as e:
            raise CommandError(
                "%s. Are you sure your INSTALLED_APPS setting is correct?" % e)

        if self.multiple_configs_allowed:
            configs = options['config']
        else:
            configs = [
                options['config'],
            ]

        output = []
        for conf_desc in configs:
            config_loader = find_matched_conf(app_config, conf_desc)
            if not config_loader:
                raise CommandError(
                    "No matched config found for the description '%s', aborted."
                    % conf_desc)
            app_output = self.handle_config_loader(app_config, config_loader,
                                                   **options)
            if app_output:
                output.append(app_output)
        return '\n'.join(output)
Exemple #3
0
    def handle_action(self, app_config, action_alias, configs):
        # get user-defined class for the action
        action_klass = app_config.get_action_klass(action_alias)
        if not action_klass:
            raise CommandError("Unknown action alias '%s'." % action_alias)

        output = []
        if configs == None:
            actor = action_klass(app_config,
                                 stdout=self.stdout,
                                 stderr=self.stderr,
                                 style=self.style)
            # when option `-c` was None, run without arguments
            output.append(actor.run())
        else:
            # run with configs and get the output
            for conf_desc in configs:
                # Initialize the class each time to make it reentrancy
                actor = action_klass(app_config,
                                     stdout=self.stdout,
                                     stderr=self.stderr,
                                     style=self.style)

                config_loader = find_matched_conf(app_config, conf_desc)
                if not config_loader:
                    # Instead of raising an exception, makes a report after all done
                    err_msg = "No matched config found for description '%s', aborted." % conf_desc
                    output.append(err_msg)
                    continue
                else:
                    config = config_loader._parse()
                    output.append(actor.run(config=config))

        return '\n'.join(output)
Exemple #4
0
    def handle_app_config(self, app_config, **options):
        self.action_alias = options['action']
        self.configs = options['configs']

        keep_quite = options['quite']
        recipients = options['recipients']
        message = options.get('message', self.comment())

        # start to time
        record = CommandRecord(app_config, self, message)

        # get user-defined class for the action
        action_klass = app_config.get_action_klass(self.action_alias)
        if action_klass:
            actor = action_klass(app_config)
        else:
            raise CommandError("Unknown action alias '%s'." %
                               self.action_alias)

        # run with configs and get the output
        output = []
        for conf_desc in self.configs:
            config_loader = find_matched_conf(app_config, conf_desc)
            if not config_loader:
                # Instead of raising an exception, makes a report after all done
                err_msg = "No matched config found for description '%s', aborted." % conf_desc
                output.append(err_msg)
                continue
            else:
                config = config_loader._parse()
                output.append(actor.run(config=config, app=app_config))

        # to close the timer
        record.done()

        if not keep_quite:
            records.add(record)

        output_str = '\n'.join(output)
        if recipients:
            mail_sender = CommandRunNotifier(recipients)
            context = mail_sender.get_context(record, self, output_str)
            mail_sender.send(context)

        return output_str