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
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)
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), ([], []))
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), ([], []))
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), ([], []))
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), ([], []))
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), ([], []))
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), ([], []))
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), ([], []))
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))
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), ([], []))
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 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 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
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
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
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
def test_no_duplications(self): self.assertEqual(len(collect_bears( [os.path.join(self.collectors_test_dir, 'bears', '**')], ['*'], ['kind'], self.log_printer)[0]), 2)
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)
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)
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
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)
def test_no_duplications(self): self.assertEqual(len(collect_bears( [os.path.join(self.collectors_test_dir, "bears", "**")], ["*"], ["kind"], self.log_printer)[0]), 2)
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)
def test_bear_invalid(self): self.assertEqual(collect_bears(["invalid_paths"], ["invalid_name"], ["invalid kind"]), [])
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)
def test_bear_invalid(self): self.assertEqual( collect_bears(["invalid_paths"], ["invalid_name"], ["invalid kind"], self.log_printer), [])
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)
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)
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)
def test_simple_single(self): self.assertEqual(len(collect_bears([os.path.join( self.collectors_test_dir, "bears")], ["bear1"], ["kind"])), 1)
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)
def test_no_duplications(self): self.assertEqual( len( collect_bears( [os.path.join(self.collectors_test_dir, "bears", "**")], ["*"], ["kind"], self.log_printer)[0]), 2)
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