Beispiel #1
0
def main(log_printer=None, section: Section=None):
    configure_logging()

    start_path = get_config_directory(section)
    log_printer = (LogPrinter(ConsolePrinter()) if log_printer is None
                   else log_printer)

    if start_path is None:
        return 255

    # start_path may have unintended glob characters
    orig_files = Globbing.glob(os.path.join(
        glob_escape(start_path), '**', '*.orig'))

    not_deleted = 0
    for ofile in orig_files:
        log_printer.info('Deleting old backup file... '
                         + os.path.relpath(ofile))
        try:
            os.remove(ofile)
        except OSError as oserror:
            not_deleted += 1
            log_printer.warn("Couldn't delete {}. {}".format(
                os.path.relpath(ofile), oserror.strerror))

    if not_deleted:
        log_printer.warn(str(not_deleted) + ' .orig backup files could not be'
                         ' deleted, possibly because you lack the permission'
                         ' to do so. coala may not be able to create'
                         ' backup files when patches are applied.')
    return 0
Beispiel #2
0
def main(log_printer=None, section: Section = None):
    configure_logging()

    start_path = get_config_directory(section)
    log_printer = (LogPrinter(ConsolePrinter())
                   if log_printer is None else log_printer)

    if start_path is None:
        return 255

    # start_path may have unintended glob characters
    orig_files = Globbing.glob(
        os.path.join(glob_escape(start_path), '**', '*.orig'))

    not_deleted = 0
    for ofile in orig_files:
        log_printer.info('Deleting old backup file... ' +
                         os.path.relpath(ofile))
        try:
            os.remove(ofile)
        except OSError as oserror:
            not_deleted += 1
            log_printer.warn("Couldn't delete {}. {}".format(
                os.path.relpath(ofile), oserror.strerror))

    if not_deleted:
        log_printer.warn(
            str(not_deleted) + ' .orig backup files could not be'
            ' deleted, possibly because you lack the permission'
            ' to do so. coala may not be able to create'
            ' backup files when patches are applied.')
    return 0
Beispiel #3
0
def main():
    configure_logging()

    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()

        # Defer imports so if e.g. --help is called they won't be run
        from coalib.coala_modes import (mode_format, mode_json,
                                        mode_non_interactive, mode_normal)
        from coalib.output.ConsoleInteraction import (
            show_bears, show_language_bears_capabilities)

        console_printer = ConsolePrinter(print_colored=not args.no_color)

        if args.json:  # needs to be checked in order to display bears in json
            return mode_json(args)

        if args.show_bears:
            from coalib.settings.ConfigurationGathering import (
                get_filtered_bears)

            local_bears, global_bears = get_filtered_bears(
                args.filter_by_language, log_printer)

            show_bears(local_bears, global_bears, args.show_description
                       or args.show_details, args.show_details,
                       console_printer)

            return 0
        elif args.show_capabilities:
            from coalib.collecting.Collectors import (
                filter_capabilities_by_languages)
            from coalib.settings.ConfigurationGathering import (
                get_filtered_bears)

            local_bears, global_bears = get_filtered_bears(
                args.filter_by_language, log_printer)
            capabilities = filter_capabilities_by_languages(
                local_bears, args.show_capabilities)
            show_language_bears_capabilities(capabilities, console_printer)

            return 0

    except BaseException as exception:  # pylint: disable=broad-except
        return get_exitcode(exception, log_printer)

    if args.non_interactive:
        return mode_non_interactive(console_printer, args)

    if args.format:
        return mode_format()

    return mode_normal(console_printer, log_printer)
Beispiel #4
0
def main():
    configure_logging()

    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()
        console_printer = ConsolePrinter(print_colored=not args.no_color)

        if args.show_bears:
            from coalib.settings.ConfigurationGathering import (
                get_filtered_bears)

            local_bears, global_bears = get_filtered_bears(
                args.filter_by_language, log_printer)

            show_bears(local_bears, global_bears, args.show_description
                       or args.show_details, args.show_details,
                       console_printer)

            return 0
        elif args.show_capabilities:
            from coalib.collecting.Collectors import (
                filter_capabilities_by_languages)
            from coalib.settings.ConfigurationGathering import (
                get_filtered_bears)

            local_bears, global_bears = get_filtered_bears(
                args.filter_by_language, log_printer)
            capabilities = filter_capabilities_by_languages(
                local_bears, args.show_capabilities)
            show_language_bears_capabilities(capabilities, console_printer)

            return 0

    except BaseException as exception:  # pylint: disable=broad-except
        return get_exitcode(exception, log_printer)

    import functools

    from coalib.coala_main import run_coala

    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,
        console_printer=console_printer)

    return exitcode
Beispiel #5
0
def main():
    configure_logging()

    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()

        # Defer imports so if e.g. --help is called they won't be run
        from coalib.coala_modes import mode_format, mode_json, mode_non_interactive, mode_normal
        from coalib.output.ConsoleInteraction import show_bears, show_language_bears_capabilities

        console_printer = ConsolePrinter(print_colored=not args.no_color)

        if args.json:  # needs to be checked in order to display bears in json
            return mode_json(args)

        if args.show_bears:
            from coalib.settings.ConfigurationGathering import get_filtered_bears

            local_bears, global_bears = get_filtered_bears(args.filter_by_language, log_printer)

            show_bears(
                local_bears,
                global_bears,
                args.show_description or args.show_details,
                args.show_details,
                console_printer,
            )

            return 0
        elif args.show_capabilities:
            from coalib.collecting.Collectors import filter_capabilities_by_languages
            from coalib.settings.ConfigurationGathering import get_filtered_bears

            local_bears, global_bears = get_filtered_bears(args.filter_by_language, log_printer)
            capabilities = filter_capabilities_by_languages(local_bears, args.show_capabilities)
            show_language_bears_capabilities(capabilities, console_printer)

            return 0

    except BaseException as exception:  # pylint: disable=broad-except
        return get_exitcode(exception, log_printer)

    if args.non_interactive:
        return mode_non_interactive(console_printer, args)

    if args.format:
        return mode_format()

    return mode_normal(console_printer, log_printer)
Beispiel #6
0
def run_coala(console_printer=None,
              log_printer=None,
              print_results=do_nothing,
              acquire_settings=fail_acquire_settings,
              print_section_beginning=do_nothing,
              nothing_done=do_nothing,
              autoapply=True,
              force_show_patch=False,
              arg_parser=None,
              arg_list=None):
    """
    This is a main method that should be usable for almost all purposes and
    reduces executing coala to one function call.

    :param console_printer:         Object to print messages on the console.
    :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 autoapply:               Set this to false to not autoapply any
                                    actions. If you set this to `False`,
                                    `force_show_patch` will be ignored.
    :param force_show_patch:        If set to True, a patch will be always
                                    shown. (Using ApplyPatchAction.)
    :param arg_parser:              Instance of ArgParser that is used to parse
                                    non-setting arguments.
    :param arg_list:                The CLI argument list.
    :return:                        A dictionary containing a list of results
                                    for all analyzed sections as key.
    """
    configure_logging()

    log_printer = (LogPrinter(ConsolePrinter(), LOG_LEVEL.DEBUG)
                   if log_printer is None else log_printer)

    exitcode = 0
    results = {}
    file_dicts = {}
    try:
        yielded_results = yielded_unfixed_results = False
        did_nothing = True
        sections, local_bears, global_bears, targets = gather_configuration(
            acquire_settings,
            log_printer,
            arg_parser=arg_parser,
            arg_list=arg_list)

        log_printer.debug('Platform {} -- Python {}, coalib {}'.format(
            platform.system(), platform.python_version(), VERSION))

        settings_hash = get_settings_hash(sections, targets)
        flush_cache = bool(sections['default'].get('flush_cache', False)
                           or settings_changed(log_printer, settings_hash))

        cache = None
        if not sections['default'].get('disable_caching', False):
            cache = FileCache(log_printer, os.getcwd(), flush_cache)

        for section_name, section in sections.items():
            if not section.is_enabled(targets):
                continue

            if not autoapply:
                section['default_actions'] = ''
            elif force_show_patch:
                section['default_actions'] = '*: ShowPatchAction'
                section['show_result_on_top'] = 'yeah'

            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,
                cache=cache,
                log_printer=log_printer,
                console_printer=console_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

            file_dicts[section_name] = section_result[3]

        update_settings_db(log_printer, settings_hash)
        if cache:
            cache.write()

        if did_nothing:
            nothing_done(log_printer)
            exitcode = 2
        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, file_dicts
Beispiel #7
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,
              autoapply=True,
              arg_parser=None,
              arg_list=None):
    """
    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 autoapply:               Set to False to autoapply nothing by
                                    default; this is overridable via any
                                    configuration file/CLI.
    :param arg_list:                The CLI argument list.
    :return:                        A dictionary containing a list of results
                                    for all analyzed sections as key.
    """
    configure_logging()

    log_printer = (LogPrinter(ConsolePrinter(), LOG_LEVEL.DEBUG)
                   if log_printer is None else log_printer)

    exitcode = 0
    results = {}
    file_dicts = {}
    try:
        yielded_results = yielded_unfixed_results = False
        did_nothing = True
        sections, local_bears, global_bears, targets = gather_configuration(
            acquire_settings,
            log_printer,
            autoapply=autoapply,
            arg_parser=arg_parser,
            arg_list=arg_list)

        log_printer.debug("Platform {} -- Python {}, pip {}, coalib {}".format(
            platform.system(), platform.python_version(), pip.__version__,
            VERSION))

        config_file = os.path.abspath(str(sections["default"].get("config")))

        settings_hash = get_settings_hash(sections, targets)
        flush_cache = bool(sections["default"].get("flush_cache", False)
                           or settings_changed(log_printer, settings_hash))

        disable_caching = bool(sections["default"].get("disable_caching",
                                                       False))
        cache = None
        if not sections["default"].get("disable_caching", False):
            cache = FileCache(log_printer, os.getcwd(), flush_cache)

        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,
                cache=cache,
                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

            file_dicts[section_name] = section_result[3]

        update_settings_db(log_printer, settings_hash)
        if cache:
            cache.write()

        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, file_dicts
Beispiel #8
0
def run_coala(console_printer=None,
              log_printer=None,
              print_results=do_nothing,
              acquire_settings=fail_acquire_settings,
              print_section_beginning=do_nothing,
              nothing_done=do_nothing,
              force_show_patch=False,
              arg_parser=None,
              arg_list=None):
    """
    This is a main method that should be usable for almost all purposes and
    reduces executing coala to one function call.

    :param console_printer:         Object to print messages on the console.
    :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 force_show_patch:        If set to True, a patch will be always
                                    shown. (Using ApplyPatchAction.)
    :param arg_list:                The CLI argument list.
    :return:                        A dictionary containing a list of results
                                    for all analyzed sections as key.
    """
    configure_logging()

    log_printer = (
        LogPrinter(ConsolePrinter(), LOG_LEVEL.DEBUG) if log_printer is None
        else log_printer)

    exitcode = 0
    results = {}
    file_dicts = {}
    try:
        yielded_results = yielded_unfixed_results = False
        did_nothing = True
        sections, local_bears, global_bears, targets = gather_configuration(
            acquire_settings,
            log_printer,
            arg_parser=arg_parser,
            arg_list=arg_list)

        log_printer.debug("Platform {} -- Python {}, pip {}, coalib {}"
                          .format(platform.system(), platform.python_version(),
                                  pip.__version__, VERSION))

        settings_hash = get_settings_hash(sections, targets)
        flush_cache = bool(sections["default"].get("flush_cache", False) or
                           settings_changed(log_printer, settings_hash))

        cache = None
        if not sections["default"].get("disable_caching", False):
            cache = FileCache(log_printer, os.getcwd(), flush_cache)

        for section_name, section in sections.items():
            if not section.is_enabled(targets):
                continue

            if force_show_patch:
                section['default_actions'] = "*: ShowPatchAction"

            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,
                cache=cache,
                log_printer=log_printer,
                console_printer=console_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

            file_dicts[section_name] = section_result[3]

        update_settings_db(log_printer, settings_hash)
        if cache:
            cache.write()

        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, file_dicts