Пример #1
0
def main(log_printer=None, section: Section = None):
    configure_logging()

    start_path = get_config_directory(section)

    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:
        logging.info('Deleting old backup file... ' + os.path.relpath(ofile))
        try:
            os.remove(ofile)
        except OSError as oserror:
            not_deleted += 1
            logging.warning("Couldn't delete {}. {}".format(
                os.path.relpath(ofile), oserror.strerror))

    if not_deleted:
        logging.warning(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
Пример #2
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)
        configure_logging(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.format:
        return mode_format()

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

    return mode_normal(console_printer, log_printer)
Пример #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)
        configure_logging(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.format:
        return mode_format()

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

    return mode_normal(console_printer, log_printer)
 def test_run_coala_bear_run_raises(self):
     configure_logging()
     with bear_test_module(), \
             prepare_file(['#fixme  '], None) as (lines, filename), \
             self.assertRaisesRegex(
                 RuntimeError, r"^That's all the RaiseTestBear can do\.$"):
         run_coala(console_printer=ConsolePrinter(),
                   log_printer=LogPrinter(),
                   arg_list=('-c', os.devnull, '-f', re.escape(filename),
                             '-b', 'RaiseTestBear'),
                   debug=True)
Пример #5
0
    def test_logged_error_causes_non_zero_exitcode(self):
        configure_logging()
        with bear_test_module(), \
                prepare_file(['#fixme  '], None) as (lines, filename):
            _, exitcode, _ = run_coala(console_printer=ConsolePrinter(),
                                       log_printer=LogPrinter(),
                                       arg_list=('-c', os.devnull, '-f',
                                                 filename, '-b',
                                                 'ErrorTestBear'),
                                       autoapply=False)

            assert exitcode == 1
 def test_run_coala_bear__init__raises(self):
     configure_logging()
     with bear_test_module(), \
             prepare_file(['#fixme  '], None) as (lines, filename), \
             self.assertRaisesRegex(
                 RuntimeError,
                 r'^The bear ErrorTestBear does not fulfill all '
                 r"requirements. 'I_do_not_exist' is not installed.$"):
         run_coala(console_printer=ConsolePrinter(),
                   log_printer=LogPrinter(),
                   arg_list=('-c', os.devnull, '-f', re.escape(filename),
                             '-b', 'ErrorTestBear'),
                   debug=True)
Пример #7
0
 def test_run_coala_bear_run_raises(self):
     configure_logging()
     with bear_test_module(), \
             prepare_file(['#fixme  '], None) as (lines, filename), \
             self.assertRaisesRegex(
                 RuntimeError, r"^That's all the RaiseTestBear can do\.$"):
         run_coala(
             console_printer=ConsolePrinter(),
             log_printer=LogPrinter(),
             arg_list=(
                 '-c', os.devnull,
                 '-f', filename,
                 '-b', 'RaiseTestBear'
             ),
             debug=True)
Пример #8
0
    def test_logged_error_causes_non_zero_exitcode(self):
        configure_logging()
        with bear_test_module():
            with prepare_file(['#fixme  '], None) as (lines, filename):
                _, exitcode, _ = run_coala(
                    console_printer=ConsolePrinter(),
                    log_printer=LogPrinter(),
                    arg_list=(
                        '-c', os.devnull,
                        '-f', filename,
                        '-b', 'ErrorTestBear'
                    ),
                    autoapply=False
                )

                assert exitcode == 1
Пример #9
0
 def test_run_coala_bear__init__raises(self):
     configure_logging()
     with bear_test_module(), \
             prepare_file(['#fixme  '], None) as (lines, filename), \
             self.assertRaisesRegex(
                 RuntimeError,
                 r'^The bear ErrorTestBear does not fulfill all '
                 r"requirements. 'I_do_not_exist' is not installed.$"):
         run_coala(
             console_printer=ConsolePrinter(),
             log_printer=LogPrinter(),
             arg_list=(
                 '-c', os.devnull,
                 '-f', filename,
                 '-b', 'ErrorTestBear'
             ),
             debug=True)
Пример #10
0
def main(debug=False):
    configure_logging()

    args = None  # to have args variable in except block when parse_args fails
    try:
        # Note: We parse the args here once to check whether to show bears or
        # not.
        args = default_arg_parser().parse_args()
        if args.debug:
            req_ipdb = PipRequirement('ipdb')
            if not req_ipdb.is_installed():
                logging.error(
                    '--debug flag requires ipdb. '
                    'You can install it with:\n%s',
                    ' '.join(req_ipdb.install_command()))
                sys.exit(13)

        if debug or args.debug:
            args.log_level = 'DEBUG'

        # 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)
        configure_logging(not args.no_color)

        if args.show_bears:
            from coalib.settings.ConfigurationGathering import get_all_bears
            kwargs = {}
            if args.bears:
                kwargs['bear_globs'] = args.bears
            filtered_bears = get_all_bears(**kwargs)
            if args.filter_by_language:
                logging.warning("'--filter-by-language ...' is deprecated. "
                                "Use '--filter-by language ...' instead.")
                if args.filter_by is None:
                    args.filter_by = []
                args.filter_by.append(['language'] + args.filter_by_language)
            if args.filter_by:
                # Each iteration of the following loop applies
                # filters one by one provided as arguments
                try:
                    filtered_bears = apply_filters(args.filter_by,
                                                   filtered_bears)
                except (InvalidFilterException, NotImplementedError) as ex:
                    # If filter is not available or is unusable
                    console_printer.print(ex)
                    return 2

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

            return 0
        elif args.show_capabilities:
            from coalib.collecting.Collectors import (
                filter_capabilities_by_languages)
            local_bears, _ = apply_filter('language', args.show_capabilities)
            capabilities = filter_capabilities_by_languages(
                local_bears, args.show_capabilities)
            show_language_bears_capabilities(capabilities, console_printer)

            return 0

        if args.json:
            return mode_json(args, debug=debug)

    except BaseException as exception:  # pylint: disable=broad-except
        if not isinstance(exception, SystemExit):
            if args and args.debug:
                import ipdb
                with ipdb.launch_ipdb_on_exception():
                    raise

            if debug:
                raise

        return get_exitcode(exception)

    if args.format:
        return mode_format(args, debug=debug)

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

    return mode_normal(console_printer, None, args, debug=debug)
Пример #11
0
def main(debug=False):
    configure_logging()

    args = None  # to have args variable in except block when parse_args fails
    try:
        # Note: We parse the args here once to check whether to show bears or
        # not.
        args = default_arg_parser().parse_args()
        if args.debug:
            req_ipdb = PipRequirement('ipdb')
            if not req_ipdb.is_installed():
                logging.error('--debug flag requires ipdb. '
                              'You can install it with:\n%s',
                              ' '.join(req_ipdb.install_command()))
                sys.exit(13)

        if debug or args.debug:
            args.log_level = 'DEBUG'

        # 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)
        configure_logging(not args.no_color)

        if args.show_bears:
            from coalib.settings.ConfigurationGathering import get_all_bears
            kwargs = {}
            if args.bears:
                kwargs['bear_globs'] = args.bears
            filtered_bears = get_all_bears(**kwargs)
            if args.filter_by_language:
                logging.warning(
                    "'--filter-by-language ...' is deprecated. "
                    "Use '--filter-by language ...' instead.")
                if args.filter_by is None:
                    args.filter_by = []
                args.filter_by.append(['language'] + args.filter_by_language)
            if args.filter_by:
                # Each iteration of the following loop applies
                # filters one by one provided as arguments
                try:
                    args.filter_by = filter_vector_to_dict(args.filter_by)
                    filtered_bears = apply_filters(
                        args.filter_by, filtered_bears)
                except (InvalidFilterException, NotImplementedError) as ex:
                    # If filter is not available or is unusable
                    console_printer.print(ex)
                    return 2

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

            return 0
        elif args.show_capabilities:
            from coalib.collecting.Collectors import (
                filter_capabilities_by_languages)
            local_bears, _ = apply_filter('language', args.show_capabilities)
            capabilities = filter_capabilities_by_languages(
                local_bears, args.show_capabilities)
            show_language_bears_capabilities(capabilities, console_printer)

            return 0

        if args.json:
            return mode_json(args, debug=debug)

    except BaseException as exception:  # pylint: disable=broad-except
        if not isinstance(exception, SystemExit):
            if args and args.debug:
                import ipdb
                with ipdb.launch_ipdb_on_exception():
                    raise

            if debug:
                raise

        return get_exitcode(exception)

    if args.format:
        return mode_format(args, debug=debug)

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

    return mode_normal(console_printer, None, args, debug=debug)