def main(): # Note: We parse the args here once to check whether to show bears or not. arg_parser = default_arg_parser() args = arg_parser.parse_args() console_printer = ConsolePrinter() if args.show_bears or args.show_all_bears: log_printer = LogPrinter(console_printer) sections, _ = load_configuration(arg_list=None, log_printer=log_printer) if args.show_all_bears: local_bears, global_bears = collect_all_bears_from_sections( sections, log_printer) else: # We ignore missing settings as it's not important. local_bears, global_bears = fill_settings( sections, acquire_settings=lambda *args, **kwargs: {}, log_printer=log_printer) show_bears(local_bears, global_bears, args.show_all_bears, console_printer) return 0 partial_print_sec_beg = functools.partial( print_section_beginning, console_printer) results, exitcode, _ = run_coala( print_results=print_results, acquire_settings=acquire_settings, print_section_beginning=partial_print_sec_beg, nothing_done=nothing_done) return exitcode
def main(): try: console_printer = ConsolePrinter() log_printer = LogPrinter(console_printer) # Note: We parse the args here once to check whether to show bears or # not. args = default_arg_parser().parse_args() if args.show_bears: sections, _ = load_configuration(arg_list=None, log_printer=log_printer) local_bears, global_bears = collect_all_bears_from_sections( sections, log_printer) if args.filter_by_language: local_bears = filter_section_bears_by_languages( local_bears, args.filter_by_language) global_bears = filter_section_bears_by_languages( global_bears, args.filter_by_language) show_bears(local_bears, global_bears, args.show_description or args.show_details, args.show_details, console_printer) return 0 except BaseException as exception: # pylint: disable=broad-except return get_exitcode(exception, log_printer) partial_print_sec_beg = functools.partial(print_section_beginning, console_printer) results, exitcode, _ = run_coala( print_results=print_results, acquire_settings=acquire_settings, print_section_beginning=partial_print_sec_beg, nothing_done=nothing_done) return exitcode
def test_all_bears_from_sections(self): test_section = Section("test_section") test_section.bear_dirs = lambda: os.path.join( self.collectors_test_dir, "bears_local_global", "**") local_bears, global_bears = collect_all_bears_from_sections( {'test_section': test_section}, self.log_printer) self.assertEqual(len(local_bears['test_section']), 2) self.assertEqual(len(global_bears['test_section']), 2)
def test_all_bears_from_sections(self): test_section = Section('test_section') test_section.bear_dirs = lambda: os.path.join(self.collectors_test_dir, 'bears_local_global', '**') local_bears, global_bears = collect_all_bears_from_sections( {'test_section': test_section}, self.log_printer) self.assertEqual(len(local_bears['test_section']), 2) self.assertEqual(len(global_bears['test_section']), 2)
def get_all_bears(log_printer=None, arg_parser=None, silent=True): """ :param log_printer: The log_printer to handle logging. :param arg_parser: An ``ArgParser`` object. :param silent: Whether or not to display warnings. :return: Tuple containing dictionaries of local bears and global bears. """ sections, _ = load_configuration(arg_list=None, arg_parser=arg_parser, silent=silent) local_bears, global_bears = collect_all_bears_from_sections(sections) return local_bears, global_bears
def get_all_bears(log_printer, arg_parser=None): """ :param log_printer: The log_printer to handle logging. :param arg_parser: An ``ArgParser`` object. :return: Tuple containing dictionaries of local bears and global bears. """ sections, _ = load_configuration(arg_list=None, log_printer=log_printer, arg_parser=arg_parser) local_bears, global_bears = collect_all_bears_from_sections( sections, log_printer) return local_bears, global_bears
def get_bears(): """ Get a dict of bears with the bear class as key. :return: A dict with bear classes as key and the list of sections as value. """ log_printer = LogPrinter(NullPrinter()) sections, _ = load_configuration(None, log_printer) local_bears, global_bears = collect_all_bears_from_sections( sections, log_printer) return inverse_dicts(local_bears, global_bears)
def test_filter_section_bears_by_languages(self): test_section = Section("test_section") test_section.bear_dirs = lambda: os.path.join( self.collectors_test_dir, "bears_local_global", "**") local_bears, global_bears = collect_all_bears_from_sections( {'test_section': test_section}, self.log_printer) local_bears = filter_section_bears_by_languages(local_bears, ['C']) self.assertEqual(len(local_bears['test_section']), 1) self.assertEqual(str(local_bears['test_section'][0]), "<class 'bears2.Test2LocalBear'>") global_bears = filter_section_bears_by_languages( global_bears, ['Java']) self.assertEqual(len(global_bears['test_section']), 1) self.assertEqual(str(global_bears['test_section'][0]), "<class 'bears1.Test1GlobalBear'>")
def test_filter_section_bears_by_languages(self): test_section = Section('test_section') test_section.bear_dirs = lambda: os.path.join(self.collectors_test_dir, 'bears_local_global', '**') local_bears, global_bears = collect_all_bears_from_sections( {'test_section': test_section}, self.log_printer) local_bears = filter_section_bears_by_languages(local_bears, ['C']) self.assertEqual(len(local_bears['test_section']), 1) self.assertEqual(str(local_bears['test_section'][0]), "<class 'bears2.Test2LocalBear'>") global_bears = filter_section_bears_by_languages(global_bears, ['Java']) self.assertEqual(len(global_bears['test_section']), 1) self.assertEqual(str(global_bears['test_section'][0]), "<class 'bears1.Test1GlobalBear'>")
def get_filtered_bears(languages, log_printer): """ Fetch bears and filter them based on given list of languages. :param languages: List of languages. :param log_printer: The log_printer to handle logging. :return: Tuple containing dictionaries of local bears and global bears. """ sections, _ = load_configuration(arg_list=None, log_printer=log_printer) local_bears, global_bears = collect_all_bears_from_sections( sections, log_printer) if languages: local_bears = filter_section_bears_by_languages(local_bears, languages) global_bears = filter_section_bears_by_languages( global_bears, languages) return local_bears, global_bears
def get_all_bears(log_printer=None, arg_parser=None, silent=True, bear_globs=('**',)): """ :param log_printer: The log_printer to handle logging. :param arg_parser: An ``ArgParser`` object. :param silent: Whether or not to display warnings. :param bear_globs: List of glob patterns. :return: Tuple containing dictionaries of local bears and global bears. """ sections, _ = load_configuration(arg_list=None, arg_parser=arg_parser, silent=silent) local_bears, global_bears = collect_all_bears_from_sections( sections, bear_globs=bear_globs) return local_bears, global_bears
def get_filtered_bears(languages, log_printer): """ Fetch bears and filter them based on given list of languages. :param languages: List of languages. :param log_printer: The log_printer to handle logging. :return: Tuple containing dictionaries of local bears and global bears. """ sections, _ = load_configuration(arg_list=None, log_printer=log_printer) local_bears, global_bears = collect_all_bears_from_sections( sections, log_printer) if languages: local_bears = filter_section_bears_by_languages( local_bears, languages) global_bears = filter_section_bears_by_languages( global_bears, languages) return local_bears, global_bears
def main(): try: console_printer = ConsolePrinter() log_printer = LogPrinter(console_printer) # Note: We parse the args here once to check whether to show bears or # not. args = default_arg_parser().parse_args() if args.show_bears: sections, _ = load_configuration(arg_list=None, log_printer=log_printer) local_bears, global_bears = collect_all_bears_from_sections( sections, log_printer) if args.filter_by_language: local_bears = filter_section_bears_by_languages( local_bears, args.filter_by_language) global_bears = filter_section_bears_by_languages( global_bears, args.filter_by_language) show_bears(local_bears, global_bears, args.show_description or args.show_details, args.show_details, console_printer) return 0 except BaseException as exception: # pylint: disable=broad-except return get_exitcode(exception, log_printer) partial_print_sec_beg = functools.partial( print_section_beginning, console_printer) results, exitcode, _ = run_coala( print_results=print_results, acquire_settings=acquire_settings, print_section_beginning=partial_print_sec_beg, nothing_done=nothing_done) return exitcode
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 = yielded_unfixed_results = False did_nothing = True sections, local_bears, global_bears, targets = gather_configuration( acquire_settings, log_printer, autoapply=autoapply) tag = str(sections['default'].get('tag', None)) dtag = str(sections['default'].get('dtag', None)) config_file = os.path.abspath(str(sections["default"].get("config"))) show_all_bears = bool(sections['default'].get('show_all_bears', False)) show_bears_ = bool(sections["default"].get("show_bears", "False")) # Deleting all .orig files, so the latest files are up to date! coala_delete_orig.main(log_printer, sections["default"]) delete_tagged_results(dtag, config_file, log_printer) if show_bears_ or show_all_bears: if show_all_bears: (local_bears, global_bears) = collect_all_bears_from_sections( sections, log_printer) show_bears(local_bears, global_bears, show_all_bears) did_nothing = False else: results = {} for section_name, section in sections.items(): 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, yielded_unfixed, results[section_name] = ( simplify_section_result(section_result)) yielded_results = yielded_results or yielded yielded_unfixed_results = (yielded_unfixed_results or yielded_unfixed) did_nothing = False tag_results(tag, config_file, results, log_printer) if did_nothing: nothing_done(log_printer) elif yielded_unfixed_results: exitcode = 1 elif yielded_results: exitcode = 5 except BaseException as exception: # pylint: disable=broad-except exitcode = exitcode or get_exitcode(exception, log_printer) return results, exitcode
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 = yielded_unfixed_results = False did_nothing = True sections, local_bears, global_bears, targets = gather_configuration( acquire_settings, log_printer, autoapply=autoapply) tag = str(sections['default'].get('tag', None)) dtag = str(sections['default'].get('dtag', None)) config_file = os.path.abspath(str(sections["default"].get("config"))) show_all_bears = bool(sections['default'].get('show_all_bears', False)) show_bears_ = bool(sections["default"].get("show_bears", "False")) # Deleting all .orig files, so the latest files are up to date! coala_delete_orig.main(log_printer, sections["default"]) delete_tagged_results(dtag, config_file, log_printer) if show_bears_ or show_all_bears: if show_all_bears: (local_bears, global_bears) = collect_all_bears_from_sections(sections, log_printer) show_bears(local_bears, global_bears, show_all_bears) did_nothing = False else: results = {} for section_name, section in sections.items(): 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, yielded_unfixed, results[section_name] = ( simplify_section_result(section_result)) yielded_results = yielded_results or yielded yielded_unfixed_results = ( yielded_unfixed_results or yielded_unfixed) did_nothing = False tag_results(tag, config_file, results, log_printer) if did_nothing: nothing_done(log_printer) elif yielded_unfixed_results: exitcode = 1 elif yielded_results: exitcode = 5 except BaseException as exception: # pylint: disable=broad-except exitcode = exitcode or get_exitcode(exception, log_printer) return results, exitcode