Beispiel #1
0
def fill_settings(sections, acquire_settings, log_printer):
    """
    Retrieves all bears and requests missing settings via the given
    acquire_settings method.

    :param sections:         The sections to fill up, modified in place.
    :param acquire_settings: The method to use for requesting settings. It will
                             get a parameter which is a dictionary with the
                             settings name as key and a list containing a
                             description in [0] and the names of the bears
                             who need this setting in all following indexes.
    :param log_printer:      The log printer to use for logging.
    :return:                 A tuple containing (local_bears, global_bears),
                             each of them being a dictionary with the section
                             name as key and as value the bears as a list.
    """
    local_bears = {}
    global_bears = {}

    for section_name, section in sections.items():
        bear_dirs = section.bear_dirs()
        bears = list(section.get("bears", ""))
        section_local_bears = collect_bears(bear_dirs, bears,
                                            [BEAR_KIND.LOCAL], log_printer)
        section_global_bears = collect_bears(bear_dirs, bears,
                                             [BEAR_KIND.GLOBAL], log_printer)
        all_bears = copy.deepcopy(section_local_bears)
        all_bears.extend(section_global_bears)
        fill_section(section, acquire_settings, log_printer, all_bears)

        local_bears[section_name] = section_local_bears
        global_bears[section_name] = section_global_bears

    return local_bears, global_bears
Beispiel #2
0
 def test_bear_suffix(self):
     self.assertEqual(
         len(collect_bears(os.path.join(self.collectors_test_dir, 'bears'),
                           ['namebear'], ['kind'],
                           self.log_printer)[0]), 1)
     self.assertEqual(
         len(collect_bears(os.path.join(self.collectors_test_dir, 'bears'),
                           ['name'], ['kind'],
                           self.log_printer)[0]), 1)
Beispiel #3
0
 def test_bear_suffix(self):
     self.assertEqual(
         len(collect_bears(os.path.join(self.collectors_test_dir, 'bears'),
                           ['namebear'], ['kind'],
                           self.log_printer)[0]), 1)
     self.assertEqual(
         len(collect_bears(os.path.join(self.collectors_test_dir, 'bears'),
                           ['name'], ['kind'],
                           self.log_printer)[0]), 1)
Beispiel #4
0
    def test_bear_invalid(self):
        self.assertEqual(
            collect_bears(['invalid_paths'], ['invalid_name'],
                          ['invalid kind'], self.log_printer), ([], ))
        self.assertEqual([log.message for log in self.log_printer.logs], [
            'No bears matching \'invalid_name\' were found. Make '
            'sure you have coala-bears installed or you have '
            'typed the name correctly.'
        ])

        self.assertEqual(
            collect_bears(['invalid_paths'], ['invalid_name'],
                          ['invalid kind1', 'invalid kind2'],
                          self.log_printer), ([], []))
Beispiel #5
0
    def test_bear_invalid(self):
        self.assertEqual(
            collect_bears(["invalid_paths"], ["invalid_name"],
                          ["invalid kind"], self.log_printer), ([], ))
        self.assertEqual([log.message for log in self.log_printer.logs], [
            "No bears matching 'invalid_name' were found. Make "
            "sure you have coala-bears installed or you have "
            "typed the name correctly."
        ])

        self.assertEqual(
            collect_bears(["invalid_paths"], ["invalid_name"],
                          ["invalid kind1", "invalid kind2"],
                          self.log_printer), ([], []))
Beispiel #6
0
 def test_bear_invalid(self):
     with LogCapture() as capture:
         self.assertEqual(
             collect_bears(['invalid_paths'], ['invalid_name'],
                           ['invalid kind'], self.log_printer), ([], ))
     capture.check(
         ('root', 'WARNING', 'No bears matching \'invalid_name\' were '
          'found. Make sure you have coala-bears '
          'installed or you have typed the name '
          'correctly.'))
     self.assertEqual(
         collect_bears(['invalid_paths'], ['invalid_name'],
                       ['invalid kind1', 'invalid kind2'],
                       self.log_printer), ([], []))
Beispiel #7
0
    def test_bear_invalid(self):
        self.assertEqual(collect_bears(["invalid_paths"],
                                       ["invalid_name"],
                                       ["invalid kind"],
                                       self.log_printer), ([],))
        self.assertEqual([log.message for log in self.log_printer.logs],
                         ["No bears matching 'invalid_name' were found. Make "
                          "sure you have coala-bears installed or you have "
                          "typed the name correctly."])

        self.assertEqual(collect_bears(["invalid_paths"],
                                       ["invalid_name"],
                                       ["invalid kind1", "invalid kind2"],
                                       self.log_printer), ([], []))
Beispiel #8
0
    def test_bear_invalid(self):
        with retrieve_stdout() as sio:
            self.assertEqual(collect_bears(["invalid_paths"],
                                           ["invalid_name"],
                                           ["invalid kind"],
                                           self.log_printer), ([],))
            self.assertRegex(sio.getvalue(),
                             ".*\\[WARNING\\].*No bears were found matching "
                             "'invalid_name'.\n")

        self.assertEqual(collect_bears(["invalid_paths"],
                                       ["invalid_name"],
                                       ["invalid kind1", "invalid kind2"],
                                       self.log_printer), ([], []))
Beispiel #9
0
    def test_bear_invalid(self):
        self.assertEqual(collect_bears(['invalid_paths'],
                                       ['invalid_name'],
                                       ['invalid kind'],
                                       self.log_printer), ([],))
        self.assertEqual([log.message for log in self.log_printer.logs],
                         ["No bears matching 'invalid_name' were found. Make "
                          'sure you have coala-bears installed or you have '
                          'typed the name correctly.'])

        self.assertEqual(collect_bears(['invalid_paths'],
                                       ['invalid_name'],
                                       ['invalid kind1', 'invalid kind2'],
                                       self.log_printer), ([], []))
Beispiel #10
0
    def test_bear_invalid(self):
        with retrieve_stdout() as sio:
            self.assertEqual(
                collect_bears(["invalid_paths"], ["invalid_name"],
                              ["invalid kind"], self.log_printer), ([], ))
            self.assertRegex(
                sio.getvalue(),
                ".*\\[WARNING\\].*No bears were found matching "
                "'invalid_name'.\n")

        self.assertEqual(
            collect_bears(["invalid_paths"], ["invalid_name"],
                          ["invalid kind1", "invalid kind2"],
                          self.log_printer), ([], []))
Beispiel #11
0
    def test_bear_invalid(self):
        with retrieve_stdout() as sio:
            self.assertEqual(
                collect_bears(["invalid_paths"], ["invalid_name"],
                              ["invalid kind"], self.log_printer), ([], ))
            self.assertRegex(
                sio.getvalue(), ".*\\[WARNING\\].*No bears were found "
                "matching 'invalid_name'. Make sure you "
                "have coala-bears installed or you have "
                "typed the name correctly.\n")

        self.assertEqual(
            collect_bears(["invalid_paths"], ["invalid_name"],
                          ["invalid kind1", "invalid kind2"],
                          self.log_printer), ([], []))
def get_all_bears(bear_dirs):
    local_bears, global_bears = collect_bears(
        bear_dirs,
        ['**'],
        [BEAR_KIND.LOCAL, BEAR_KIND.GLOBAL],
        warn_if_unused_glob=False)
    return list(itertools.chain(local_bears, global_bears))
Beispiel #13
0
 def test_bear_invalid(self):
     with LogCapture() as capture:
         self.assertEqual(collect_bears(['invalid_paths'],
                                        ['invalid_name'],
                                        ['invalid kind'],
                                        self.log_printer), ([],))
     capture.check(
         ('root', 'WARNING', 'No bears matching \'invalid_name\' were '
                             'found. Make sure you have coala-bears '
                             'installed or you have typed the name '
                             'correctly.')
     )
     self.assertEqual(collect_bears(['invalid_paths'],
                                    ['invalid_name'],
                                    ['invalid kind1', 'invalid kind2'],
                                    self.log_printer), ([], []))
Beispiel #14
0
def fill_settings(sections, acquire_settings, log_printer):
    """
    Retrieves all bears and requests missing settings via the given
    acquire_settings method.

    :param sections:         The sections to fill up, modified in place.
    :param acquire_settings: The method to use for requesting settings. It will
                             get a parameter which is a dictionary with the
                             settings name as key and a list containing a
                             description in [0] and the names of the bears
                             who need this setting in all following indexes.
    :param log_printer:      The log printer to use for logging.
    :return:                 A tuple containing (local_bears, global_bears),
                             each of them being a dictionary with the section
                             name as key and as value the bears as a list.
    """
    local_bears = {}
    global_bears = {}

    for section_name, section in sections.items():
        bear_dirs = section.bear_dirs()
        bears = list(section.get("bears", ""))
        section_local_bears, section_global_bears = collect_bears(
            bear_dirs,
            bears,
            [BEAR_KIND.LOCAL, BEAR_KIND.GLOBAL],
            log_printer)
        all_bears = copy.deepcopy(section_local_bears)
        all_bears.extend(section_global_bears)
        fill_section(section, acquire_settings, log_printer, all_bears)

        local_bears[section_name] = section_local_bears
        global_bears[section_name] = section_global_bears

    return local_bears, global_bears
def get_all_bears(bear_dirs):
    local_bears, global_bears = collect_bears(
        bear_dirs,
        ['**'],
        [BEAR_KIND.LOCAL, BEAR_KIND.GLOBAL],
        warn_if_unused_glob=False)
    return list(itertools.chain(local_bears, global_bears))
Beispiel #16
0
    def test_bear_invalid(self):
        with retrieve_stdout() as sio:
            self.assertEqual(collect_bears(["invalid_paths"],
                                           ["invalid_name"],
                                           ["invalid kind"],
                                           self.log_printer), ([],))
            self.assertRegex(sio.getvalue(),
                             ".*\\[WARNING\\].*No bears were found "
                             "matching 'invalid_name'. Make sure you "
                             "have coala-bears installed or you have "
                             "typed the name correctly.\n")

        self.assertEqual(collect_bears(["invalid_paths"],
                                       ["invalid_name"],
                                       ["invalid kind1", "invalid kind2"],
                                       self.log_printer), ([], []))
Beispiel #17
0
def fill_settings(sections,
                  acquire_settings,
                  log_printer,
                  fill_section_method=fill_section,
                  **kwargs):
    """
    Retrieves all bears and requests missing settings via the given
    acquire_settings method.

    This will retrieve all bears and their dependencies.

    :param sections:            The sections to fill up, modified in place.
    :param acquire_settings:    The method to use for requesting settings. It
                                will get a parameter which is a dictionary with
                                the settings name as key and a list containing
                                a description in [0] and the names of the bears
                                who need this setting in all following indexes.
    :param log_printer:         The log printer to use for logging.
    :param fill_section_method: Method to be used to fill the section settings.
    :param kwargs:              Any other arguments for the fill_section_method
                                can be supplied via kwargs, which are passed
                                directly to the fill_section_method.
    :return:                    A tuple containing (local_bears, global_bears),
                                each of them being a dictionary with the
                                section name as key and as value the bears as a
                                list.
    """
    local_bears = {}
    global_bears = {}

    for section_name, section in sections.items():
        bear_dirs = section.bear_dirs()
        if getattr(section, 'aspects', None):
            section_local_bears, section_global_bears = (
                collect_bears_by_aspects(
                    section.aspects,
                    [BEAR_KIND.LOCAL, BEAR_KIND.GLOBAL]))
        else:
            bears = list(section.get('bears', ''))
            section_local_bears, section_global_bears = collect_bears(
                bear_dirs,
                bears,
                [BEAR_KIND.LOCAL, BEAR_KIND.GLOBAL],
                log_printer)
        section_local_bears = Dependencies.resolve(section_local_bears)
        section_global_bears = Dependencies.resolve(section_global_bears)
        all_bears = copy.deepcopy(section_local_bears)
        all_bears.extend(section_global_bears)
        fill_section_method(section,
                            acquire_settings,
                            log_printer,
                            all_bears,
                            **kwargs)

        local_bears[section_name] = section_local_bears
        global_bears[section_name] = section_global_bears

    return local_bears, global_bears
Beispiel #18
0
    def _fill_settings(self):
        for section_name in self.sections:
            section = self.sections[section_name]
            section.retrieve_logging_objects()

            bear_dirs = path_list(section.get("bear_dirs", ""))
            bear_dirs.append(
                os.path.join(StringConstants.coalib_bears_root, "**"))
            bears = list(section.get("bears", ""))
            local_bears = collect_bears(bear_dirs, bears, [BEAR_KIND.LOCAL])
            global_bears = collect_bears(bear_dirs, bears, [BEAR_KIND.GLOBAL])
            filler = SectionFiller(section)
            all_bears = copy.deepcopy(local_bears)
            all_bears.extend(global_bears)
            filler.fill_section(all_bears)

            self.local_bears[section_name] = local_bears
            self.global_bears[section_name] = global_bears
Beispiel #19
0
def fill_settings(sections,
                  acquire_settings,
                  log_printer=None,
                  fill_section_method=fill_section,
                  **kwargs):
    """
    Retrieves all bears and requests missing settings via the given
    acquire_settings method.

    This will retrieve all bears and their dependencies.

    :param sections:            The sections to fill up, modified in place.
    :param acquire_settings:    The method to use for requesting settings. It
                                will get a parameter which is a dictionary with
                                the settings name as key and a list containing
                                a description in [0] and the names of the bears
                                who need this setting in all following indexes.
    :param log_printer:         The log printer to use for logging.
    :param fill_section_method: Method to be used to fill the section settings.
    :param kwargs:              Any other arguments for the fill_section_method
                                can be supplied via kwargs, which are passed
                                directly to the fill_section_method.
    :return:                    A tuple containing (local_bears, global_bears),
                                each of them being a dictionary with the
                                section name as key and as value the bears as a
                                list.
    """
    local_bears = {}
    global_bears = {}

    for section_name, section in sections.items():
        bear_dirs = section.bear_dirs()
        if getattr(section, 'aspects', None):
            section_local_bears, section_global_bears = (
                collect_bears_by_aspects(
                    section.aspects,
                    [BEAR_KIND.LOCAL, BEAR_KIND.GLOBAL]))
        else:
            bears = list(section.get('bears', ''))
            section_local_bears, section_global_bears = collect_bears(
                bear_dirs,
                bears,
                [BEAR_KIND.LOCAL, BEAR_KIND.GLOBAL])
        section_local_bears = Dependencies.resolve(section_local_bears)
        section_global_bears = Dependencies.resolve(section_global_bears)
        all_bears = copy.deepcopy(section_local_bears)
        all_bears.extend(section_global_bears)
        fill_section_method(section,
                            acquire_settings,
                            None,
                            all_bears,
                            **kwargs)

        local_bears[section_name] = section_local_bears
        global_bears[section_name] = section_global_bears

    return local_bears, global_bears
Beispiel #20
0
    def _fill_settings(self):
        for section_name in self.sections:
            section = self.sections[section_name]
            section.retrieve_logging_objects()

            bear_dirs = path_list(section.get("bear_dirs", ""))
            bear_dirs.append(os.path.join(StringConstants.coalib_bears_root,
                                          "**"))
            bears = list(section.get("bears", ""))
            local_bears = collect_bears(bear_dirs,
                                        bears,
                                        [BEAR_KIND.LOCAL])
            global_bears = collect_bears(bear_dirs,
                                         bears,
                                         [BEAR_KIND.GLOBAL])
            filler = SectionFiller(section)
            all_bears = copy.deepcopy(local_bears)
            all_bears.extend(global_bears)
            filler.fill_section(all_bears)

            self.local_bears[section_name] = local_bears
            self.global_bears[section_name] = global_bears
Beispiel #21
0
 def test_no_duplications(self):
     self.assertEqual(len(collect_bears(
         [os.path.join(self.collectors_test_dir, 'bears', '**')],
         ['*'],
         ['kind'],
         self.log_printer)[0]), 2)
Beispiel #22
0
 def test_reference_single(self):
     self.assertEqual(len(collect_bears(
         [os.path.join(self.collectors_test_dir, 'bears')],
         ['metabear'],
         ['kind'],
         self.log_printer)[0]), 1)
Beispiel #23
0
 def test_string_single(self):
     self.assertEqual(len(collect_bears(
         os.path.join(self.collectors_test_dir, 'bears'),
         ['bear1'],
         ['kind'],
         self.log_printer)[0]), 1)
Beispiel #24
0
def run_coala(log_printer=None,
              print_results=do_nothing,
              acquire_settings=fail_acquire_settings,
              print_section_beginning=do_nothing,
              nothing_done=do_nothing,
              show_bears=do_nothing,
              autoapply=True):
    """
    This is a main method that should be usable for almost all purposes and
    reduces executing coala to one function call.

    :param log_printer:             A LogPrinter object to use for logging.
    :param print_results:           A callback that takes a LogPrinter, a
                                    section, a list of results to be printed,
                                    the file dict and the mutable file diff
                                    dict.
    :param acquire_settings:        The method to use for requesting settings.
                                    It will get a parameter which is a
                                    dictionary with the settings name as key
                                    and a list containing a description in [0]
                                    and the names of the bears who need this
                                    setting in all following indexes.
    :param print_section_beginning: A callback that will be called with a
                                    section name string whenever analysis of a
                                    new section is started.
    :param nothing_done:            A callback that will be called with only a
                                    log printer that shall indicate that
                                    nothing was done.
    :param show_bears:              A callback that will be called with first
                                    a list of local bears, second a list of
                                    global bears to output them. A third bool
                                    parameter may be used to indicate if a
                                    compressed output (True) or a normal output
                                    (False) is desired, the former being used
                                    for showing all available bears to the
                                    user.
    :param autoapply:               Set to False to autoapply nothing by
                                    default; this is overridable via any
                                    configuration file/CLI.
    :return:                        A dictionary containing a list of results
                                    for all analyzed sections as key.
    """
    log_printer = log_printer or LogPrinter(ConsolePrinter())

    exitcode = 0
    results = None
    try:
        yielded_results = False
        did_nothing = True
        sections, local_bears, global_bears, targets = (gather_configuration(
            acquire_settings, log_printer))

        tag = str(sections['default'].get('tag', None))
        dtag = str(sections['default'].get('dtag', None))

        if not autoapply and 'autoapply' not in sections['default']:
            sections['default']['autoapply'] = "False"

        show_all_bears = bool(sections['default'].get('show_all_bears', False))
        show_bears_ = bool(sections["default"].get("show_bears", "False"))
        if show_all_bears:
            show_bears_ = True
            for section in sections:
                bear_dirs = sections[section].bear_dirs()
                local_bears[section] = collect_bears(bear_dirs, ["**"],
                                                     [BEAR_KIND.LOCAL],
                                                     log_printer)
                global_bears[section] = collect_bears(bear_dirs, ["**"],
                                                      [BEAR_KIND.GLOBAL],
                                                      log_printer)

        if dtag != "None":
            delete_tagged_results(
                dtag, os.path.abspath(str(sections["default"].get("config"))))

        if show_bears_:
            show_bears(local_bears, global_bears, show_all_bears)
            did_nothing = False
        else:
            results = {}
            for section_name in sections:
                section = sections[section_name]
                if not section.is_enabled(targets):
                    continue

                print_section_beginning(section)
                section_result = execute_section(
                    section=section,
                    global_bear_list=global_bears[section_name],
                    local_bear_list=local_bears[section_name],
                    print_results=print_results,
                    log_printer=log_printer)
                yielded_results = yielded_results or section_result[0]

                results_for_section = []
                for value in chain(section_result[1].values(),
                                   section_result[2].values()):
                    if value is None:
                        continue

                    for result in value:
                        results_for_section.append(result)

                results[section_name] = results_for_section
                did_nothing = False

            if tag != "None":
                tag_results(
                    tag,
                    os.path.abspath(str(sections["default"].get("config"))),
                    results)

        if did_nothing:
            nothing_done(log_printer)

        if yielded_results:
            exitcode = 1
    except BaseException as exception:  # pylint: disable=broad-except
        exitcode = exitcode or get_exitcode(exception, log_printer)

    return results, exitcode
Beispiel #25
0
 def test_wrong_kind(self):
     self.assertEqual(len(collect_bears(
         [os.path.join(self.collectors_test_dir, "bears", "**")],
         ["*"],
         ["other_kind"],
         self.log_printer)[0]), 0)
Beispiel #26
0
 def test_no_duplications(self):
     self.assertEqual(len(collect_bears(
         [os.path.join(self.collectors_test_dir, "bears", "**")],
         ["*"],
         ["kind"],
         self.log_printer)[0]), 2)
Beispiel #27
0
 def test_reference_single(self):
     self.assertEqual(len(collect_bears(
         [os.path.join(self.collectors_test_dir, "bears")],
         ["metabear"],
         ["kind"],
         self.log_printer)[0]), 1)
Beispiel #28
0
 def test_bear_invalid(self):
     self.assertEqual(collect_bears(["invalid_paths"],
                                    ["invalid_name"],
                                    ["invalid kind"]), [])
Beispiel #29
0
 def test_wrong_kind(self):
     self.assertEqual(len(collect_bears(
         [os.path.join(self.collectors_test_dir, 'bears', '**')],
         ['*'],
         ['other_kind'],
         self.log_printer)[0]), 0)
Beispiel #30
0
 def test_bear_invalid(self):
     self.assertEqual(
         collect_bears(["invalid_paths"], ["invalid_name"],
                       ["invalid kind"], self.log_printer), [])
Beispiel #31
0
 def test_wrong_kind(self):
     self.assertEqual(len(collect_bears(
         [os.path.join(self.collectors_test_dir, 'bears', '**')],
         ['*'],
         ['other_kind'],
         self.log_printer)[0]), 0)
Beispiel #32
0
 def test_wrong_kind(self):
     self.assertEqual(
         len(
             collect_bears(
                 [os.path.join(self.collectors_test_dir, "bears", "**")],
                 ["*"], ["other_kind"], self.log_printer)[0]), 0)
Beispiel #33
0
 def test_reference_single(self):
     self.assertEqual(
         len(
             collect_bears(
                 [os.path.join(self.collectors_test_dir, "bears")],
                 ["metabear"], ["kind"], self.log_printer)[0]), 1)
Beispiel #34
0
 def test_string_single(self):
     self.assertEqual(len(collect_bears(
         os.path.join(self.collectors_test_dir, "bears"),
         ["bear1"],
         ["kind"],
         self.log_printer)[0]), 1)
Beispiel #35
0
 def test_simple_single(self):
     self.assertEqual(len(collect_bears([os.path.join(
         self.collectors_test_dir, "bears")],
         ["bear1"],
         ["kind"])), 1)
Beispiel #36
0
 def test_string_single(self):
     self.assertEqual(
         len(
             collect_bears(os.path.join(self.collectors_test_dir, "bears"),
                           ["bear1"], ["kind"], self.log_printer)[0]), 1)
Beispiel #37
0
 def test_string_single(self):
     self.assertEqual(len(collect_bears(
         os.path.join(self.collectors_test_dir, 'bears'),
         ['bear1'],
         ['kind'],
         self.log_printer)[0]), 1)
Beispiel #38
0
 def test_no_duplications(self):
     self.assertEqual(
         len(
             collect_bears(
                 [os.path.join(self.collectors_test_dir, "bears", "**")],
                 ["*"], ["kind"], self.log_printer)[0]), 2)
Beispiel #39
0
 def test_reference_single(self):
     self.assertEqual(len(collect_bears(
         [os.path.join(self.collectors_test_dir, 'bears')],
         ['metabear'],
         ['kind'],
         self.log_printer)[0]), 1)
Beispiel #40
0
def run_coala(log_printer=None,
              print_results=do_nothing,
              acquire_settings=fail_acquire_settings,
              print_section_beginning=do_nothing,
              nothing_done=do_nothing,
              show_bears=do_nothing,
              autoapply=True):
    """
    This is a main method that should be usable for almost all purposes and
    reduces executing coala to one function call.

    :param log_printer:             A LogPrinter object to use for logging.
    :param print_results:           A callback that takes a LogPrinter, a
                                    section, a list of results to be printed,
                                    the file dict and the mutable file diff
                                    dict.
    :param acquire_settings:        The method to use for requesting settings.
                                    It will get a parameter which is a
                                    dictionary with the settings name as key
                                    and a list containing a description in [0]
                                    and the names of the bears who need this
                                    setting in all following indexes.
    :param print_section_beginning: A callback that will be called with a
                                    section name string whenever analysis of a
                                    new section is started.
    :param nothing_done:            A callback that will be called with only a
                                    log printer that shall indicate that
                                    nothing was done.
    :param show_bears:              A callback that will be called with first
                                    a list of local bears, second a list of
                                    global bears to output them. A third bool
                                    parameter may be used to indicate if a
                                    compressed output (True) or a normal output
                                    (False) is desired, the former being used
                                    for showing all available bears to the
                                    user.
    :param autoapply:               Set to False to autoapply nothing by
                                    default; this is overridable via any
                                    configuration file/CLI.
    :return:                        A dictionary containing a list of results
                                    for all analyzed sections as key.
    """
    log_printer = log_printer or LogPrinter(ConsolePrinter())

    exitcode = 0
    results = None
    try:
        yielded_results = False
        did_nothing = True
        sections, local_bears, global_bears, targets = (
            gather_configuration(acquire_settings, log_printer))

        tag = str(sections['default'].get('tag', None))
        dtag = str(sections['default'].get('dtag', None))

        if not autoapply and 'autoapply' not in sections['default']:
            sections['default']['autoapply'] = "False"

        show_all_bears = bool(sections['default'].get('show_all_bears', False))
        show_bears_ = bool(sections["default"].get("show_bears", "False"))
        if show_all_bears:
            show_bears_ = True
            for section in sections:
                bear_dirs = sections[section].bear_dirs()
                local_bears[section] = collect_bears(bear_dirs,
                                                     ["**"],
                                                     [BEAR_KIND.LOCAL],
                                                     log_printer)
                global_bears[section] = collect_bears(bear_dirs,
                                                      ["**"],
                                                      [BEAR_KIND.GLOBAL],
                                                      log_printer)

        if dtag != "None":
            delete_tagged_results(
                dtag,
                os.path.abspath(str(sections["default"].get("config"))))

        if show_bears_:
            show_bears(local_bears,
                       global_bears,
                       show_all_bears)
            did_nothing = False
        else:
            results = {}
            for section_name in sections:
                section = sections[section_name]
                if not section.is_enabled(targets):
                    continue

                print_section_beginning(section)
                section_result = execute_section(
                    section=section,
                    global_bear_list=global_bears[section_name],
                    local_bear_list=local_bears[section_name],
                    print_results=print_results,
                    log_printer=log_printer)
                yielded_results = yielded_results or section_result[0]

                results_for_section = []
                for value in chain(section_result[1].values(),
                                   section_result[2].values()):
                    if value is None:
                        continue

                    for result in value:
                        results_for_section.append(result)

                results[section_name] = results_for_section
                did_nothing = False

            if tag != "None":
                tag_results(
                    tag,
                    os.path.abspath(str(sections["default"].get("config"))),
                    results)

        if did_nothing:
            nothing_done(log_printer)

        if yielded_results:
            exitcode = 1
    except BaseException as exception:  # pylint: disable=broad-except
        exitcode = exitcode or get_exitcode(exception, log_printer)

    return results, exitcode
Beispiel #41
0
 def test_no_duplications(self):
     self.assertEqual(len(collect_bears(
         [os.path.join(self.collectors_test_dir, 'bears', '**')],
         ['*'],
         ['kind'],
         self.log_printer)[0]), 2)