Ejemplo n.º 1
0
    def __init__(
            self,
            log_printer: LogPrinter,
            project_dir: str,
            flush_cache: bool=False):
        """
        Initialize FileCache.

        :param log_printer: A LogPrinter object to use for logging.
        :param project_dir: The root directory of the project to be used
                            as a key identifier.
        :param flush_cache: Flush the cache and rebuild it.
        """
        self.log_printer = log_printer
        self.project_dir = project_dir
        self.current_time = int(time.time())

        cache_data = pickle_load(log_printer, project_dir, {})
        last_time = -1
        if "time" in cache_data:
            last_time = cache_data["time"]
        if not flush_cache and last_time > self.current_time:
            log_printer.warn("It seems like you went back in time - your "
                             "system time is behind the last recorded run "
                             "time on this project. The cache will "
                             "be force flushed.")
            flush_cache = True

        self.data = cache_data.get("files", {})
        if flush_cache:
            self.flush_cache()
Ejemplo n.º 2
0
    def __init__(self,
                 section: Section,
                 message_queue,
                 timeout=0):
        """
        Constructs a new bear.

        :param section:       The section object where bear settings are
                              contained.
        :param message_queue: The queue object for messages. Can be ``None``.
        :param timeout:       The time the bear is allowed to run. To set no
                              time limit, use 0.
        :raises TypeError:    Raised when ``message_queue`` is no queue.
        :raises RuntimeError: Raised when bear requirements are not fulfilled.
        """
        Printer.__init__(self)
        LogPrinter.__init__(self, self)

        if message_queue is not None and not hasattr(message_queue, "put"):
            raise TypeError("message_queue has to be a Queue or None.")

        self.section = section
        self.message_queue = message_queue
        self.timeout = timeout

        self.setup_dependencies()
        cp = type(self).check_prerequisites()
        if cp is not True:
            error_string = ("The bear " + self.name +
                            " does not fulfill all requirements.")
            if cp is not False:
                error_string += " " + cp

            self.warn(error_string)
            raise RuntimeError(error_string)
Ejemplo n.º 3
0
    def __init__(self,
                 log_level=LOG_LEVEL.WARNING,
                 timestamp_format="%X"):
        Printer.__init__(self)
        LogPrinter.__init__(self, self, log_level, timestamp_format)

        self.logs = []
Ejemplo n.º 4
0
    def __init__(self, section: Section, message_queue, timeout=0):
        """
        Constructs a new bear.

        :param section:       The section object where bear settings are
                              contained.
        :param message_queue: The queue object for messages. Can be `None`.
        :param timeout:       The time the bear is allowed to run. To set no
                              time limit, use 0.
        :raises TypeError:    Raised when `message_queue` is no queue.
        :raises RuntimeError: Raised when bear requirements are not fulfilled.
        """
        Printer.__init__(self)
        LogPrinter.__init__(self, self)

        if message_queue is not None and not hasattr(message_queue, "put"):
            raise TypeError("message_queue has to be a Queue or None.")

        self.section = section
        self.message_queue = message_queue
        self.timeout = timeout

        cp = type(self).check_prerequisites()
        if cp is not True:
            error_string = ("The bear " + type(self).__name__ +
                            " does not fulfill all requirements.")
            if cp is not False:
                error_string += " " + cp

            self.warn(error_string)
            raise RuntimeError(error_string)
Ejemplo n.º 5
0
    def test_run_coala_no_autoapply(self, debug=False):
        with bear_test_module(), \
                prepare_file(['#fixme  '], None) as (lines, filename):
            self.assertEqual(
                1,
                len(
                    run_coala(console_printer=ConsolePrinter(),
                              log_printer=LogPrinter(),
                              arg_list=('-c', os.devnull, '-f',
                                        re.escape(filename), '-b',
                                        'SpaceConsistencyTestBear',
                                        '--apply-patches', '-S',
                                        'use_spaces=yeah'),
                              autoapply=False,
                              debug=debug)[0]['cli']))

            self.assertEqual(
                0,
                len(
                    run_coala(console_printer=ConsolePrinter(),
                              log_printer=LogPrinter(),
                              arg_list=('-c', os.devnull, '-f',
                                        re.escape(filename), '-b',
                                        'SpaceConsistencyTestBear',
                                        '--apply-patches', '-S',
                                        'use_spaces=yeah'),
                              debug=debug)[0]['cli']))
Ejemplo n.º 6
0
    def __init__(self,
                 section: Section,
                 message_queue,
                 timeout=0):
        Printer.__init__(self)
        LogPrinter.__init__(self, self)

        if not hasattr(message_queue, "put") and message_queue is not None:
            raise TypeError("message_queue has to be a Queue or None.")

        self.section = section
        self.message_queue = message_queue
        self.timeout = timeout
    def run_coala(path):
        """
        Run coala on the file at the given path. The config file is got using
        the `find-config` option of coala.

        :param path: The path of the file to analyze.
        :return:     The result dictionary from coala.
        """
        results = {}
        log_printer = LogPrinter(ConsolePrinter())
        cwd = os.getcwd()
        try:
            os.chdir(os.path.dirname(path))
            args = [
                "--find-config", "--limit-files", path, '-S', 'autoapply=false'
            ]
            sections, local_bears, global_bears, targets = (
                # Use `lambda *args: True` so that `gather_configuration` does
                # nothing when it needs to request settings from user.
                gather_configuration(lambda *args: True,
                                     log_printer,
                                     arg_list=args))

            for section_name in sections:
                section = sections[section_name]
                if not section.is_enabled(targets):
                    continue

                section_result = execute_section(
                    section=section,
                    global_bear_list=global_bears[section_name],
                    local_bear_list=local_bears[section_name],
                    print_results=lambda *args: True,
                    log_printer=log_printer)

                results_for_section = []
                for i in 1, 2:
                    for value in section_result[i].values():
                        for result in value:
                            if isinstance(result, HiddenResult):
                                continue
                            results_for_section.append(result)
                results[section_name] = results_for_section
        except BaseException as exception:
            log_printer.log_exception(str(exception), exception)
        finally:
            os.chdir(cwd)

        return results
    def run_coala(path):
        """
        Run coala on the file at the given path. The config file is got using
        the `find-config` option of coala.

        :param path: The path of the file to analyze.
        :return:     The result dictionary from coala.
        """
        results = {}
        log_printer = LogPrinter(ConsolePrinter())
        cwd = os.getcwd()
        try:
            os.chdir(os.path.dirname(path))
            args = ["--find-config", "--limit-files", path, '-S',
                    'autoapply=false']
            sections, local_bears, global_bears, targets = (
                # Use `lambda *args: True` so that `gather_configuration` does
                # nothing when it needs to request settings from user.
                gather_configuration(lambda *args: True,
                                     log_printer,
                                     arg_list=args))

            for section_name in sections:
                section = sections[section_name]
                if not section.is_enabled(targets):
                    continue

                section_result = execute_section(
                    section=section,
                    global_bear_list=global_bears[section_name],
                    local_bear_list=local_bears[section_name],
                    print_results=lambda *args: True,
                    log_printer=log_printer)

                results_for_section = []
                for i in 1, 2:
                    for value in section_result[i].values():
                        for result in value:
                            if isinstance(result, HiddenResult):
                                continue
                            results_for_section.append(result)
                results[section_name] = results_for_section
        except BaseException as exception:
            log_printer.log_exception(str(exception), exception)
        finally:
            os.chdir(cwd)

        return results
Ejemplo n.º 9
0
    def test_file_cache_proxy_integration(self, debug=False):
        with bear_test_module(), \
                prepare_file(['disk-copy\n'], None) as (_, filename):

            memory_data = 'in-memory\n'
            proxy = FileProxy(filename, None, memory_data)
            proxymap = FileProxyMap([proxy])
            self.cache.set_proxymap(proxymap)

            results, exitcode, file_dicts = run_coala(
                console_printer=ConsolePrinter(),
                log_printer=LogPrinter(),
                arg_list=(
                    '-c',
                    os.devnull,
                    '-f',
                    filename,
                    '-b',
                    'TestBear',
                ),
                autoapply=False,
                debug=debug,
                cache=self.cache)

            self.assertEqual(exitcode, 0)
            self.assertEqual(len(results), 1)

            # run_coala() output's name is always lower case
            self.assertEqual(file_dicts['cli'][filename.lower()],
                             (memory_data, ))
Ejemplo n.º 10
0
Archivo: coala.py Proyecto: yland/coala
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
Ejemplo n.º 11
0
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
Ejemplo n.º 12
0
def main(log_printer=None, section: Section = None):
    start_path = get_config_directory(section)
    log_printer = log_printer or LogPrinter(ConsolePrinter())

    if start_path is None:
        log_printer.err("Can only delete .orig files if .coafile is found")
        return 255

    orig_files = Globbing.glob(
        os.path.abspath(os.path.join(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
Ejemplo n.º 13
0
def main():
    arg_parser = _get_arg_parser()
    args = arg_parser.parse_args()

    printer = ConsolePrinter()
    log_printer = LogPrinter(printer)

    project_dir = os.getcwd()
    if not args.non_interactive:
        print_welcome_message(printer)
        project_dir = ask_question("What is your project directory?",
                                   default=project_dir,
                                   typecast=valid_path)

    project_files, ignore_globs = get_project_files(log_printer, printer,
                                                    project_dir)

    used_languages = list(get_used_languages(project_files))
    print_used_languages(printer, used_languages)

    relevant_bears = filter_relevant_bears(used_languages, arg_parser)
    print_relevant_bears(printer, relevant_bears)

    if args.non_interactive:
        unusable_bears = get_non_optional_settings_bears(relevant_bears)
        remove_unusable_bears(relevant_bears, unusable_bears)
        print_relevant_bears(printer, relevant_bears, 'usable')

    settings = generate_settings(project_dir, project_files, ignore_globs,
                                 relevant_bears)
    write_coafile(printer, project_dir, settings)
Ejemplo n.º 14
0
def generate_ignore_field(project_dir, languages, extset, ignore_globs):
    """
    Generate the ignore field for the ``default`` section.

    :param project_dir:
        Full path of the user's project directory.
    :param languages:
        A list of languages present in the project.
    :param extset:
        A dict with language name as key and a set of extensions as
        value. This includes only those extensions used by the project.
    :return:
        A comma-separated string containing the globs to ignore.
    """
    null_printer = LogPrinter(NullPrinter())

    all_files = set(
        collect_files("**", null_printer, ignored_file_paths=ignore_globs))

    ignores = []
    for glob in ignore_globs:
        gitignore_files = {
            file
            for file in collect_files([glob], null_printer)
        }
        if not all_files.isdisjoint(gitignore_files):
            ignores.append(os.path.relpath(glob, project_dir))

    return ", ".join(ignores)
Ejemplo n.º 15
0
 def setUp(self):
     current_dir = os.path.split(__file__)[0]
     self.caching_test_dir = os.path.join(current_dir, 'caching_testfiles')
     self.log_printer = LogPrinter(NullPrinter())
     self.cache = FileCache(self.log_printer,
                            'coala_test',
                            flush_cache=True)
Ejemplo n.º 16
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
Ejemplo n.º 17
0
    def apply(self, result, original_file_dict, file_diff_dict):
        """
        (G)enerate patches
        """

        console_printer = ConsolePrinter()
        log_printer = LogPrinter()
        to_filename = sorted(
            result.diffs.items())[OBJECT_INDEX][FILENAME_INDEX]

        filtered_bears = filter_bears(find_language(to_filename))
        filtered_bears.insert(0, DefaultBear())
        possible_options = [b.name for b in filtered_bears]

        console_printer.print('[{:>4}] *0. Do Nothing'.format(''))

        # Let the user choose a bear that wants to apply on the files
        for i, action in enumerate(possible_options, 1):
            show_possibilities(console_printer, i, action)

        choose_action = str(input('[{:>4}]  Enter a number: '.format('')))
        if choose_action is '' or choose_action is '0':
            return False

        choose_action = int(choose_action)
        chosen_bear = [possible_options[choose_action - 1]]

        return mode_normal(console_printer,
                           log_printer,
                           create_arg_parser([to_filename], chosen_bear),
                           debug=False)
Ejemplo n.º 18
0
    def setUp(self):
        config_path = os.path.abspath(os.path.join(
            os.path.dirname(__file__),
            'section_executor_test_files',
            '.coafile'))
        self.testcode_c_path = os.path.join(os.path.dirname(config_path),
                                            'testcode.c')
        self.unreadable_path = os.path.join(os.path.dirname(config_path),
                                            'unreadable')

        self.result_queue = queue.Queue()
        self.queue = queue.Queue()
        self.log_queue = queue.Queue()
        self.console_printer = ConsolePrinter()
        log_printer = LogPrinter(ConsolePrinter())
        self.log_printer = ProcessingTestLogPrinter(self.log_queue)

        (self.sections,
         self.local_bears,
         self.global_bears,
         targets) = gather_configuration(lambda *args: True,
                                         log_printer,
                                         arg_list=['--config',
                                                   re.escape(config_path)])
        self.assertEqual(len(self.local_bears['cli']), 1)
        self.assertEqual(len(self.global_bears['cli']), 1)
        self.assertEqual(targets, [])
Ejemplo n.º 19
0
    def test_profiler_dependency(self, debug=False):
        with bear_test_module():
            with prepare_file(['#fixme  '], None) as (lines, filename):
                results = run_coala(console_printer=ConsolePrinter(),
                                    log_printer=LogPrinter(),
                                    arg_list=(
                                        '-c',
                                        os.devnull,
                                        '-f',
                                        filename,
                                        '-b',
                                        'DependentBear',
                                        '-S',
                                        'use_spaces=yeah',
                                        '--profile',
                                        'profiled_bears',
                                    ),
                                    autoapply=False,
                                    debug=debug)
                cli_result = results[0]['cli']
                self.assertEqual(len(cli_result), 1)

        profiled_files = os.listdir('profiled_bears')
        self.assertEqual(len(profiled_files), 1)
        self.assertEqual(profiled_files[0],
                         'cli_SpaceConsistencyTestBear.prof')
        shutil.rmtree('profiled_bears')
Ejemplo n.º 20
0
    def setUp(self):
        self.log_printer = LogPrinter(NullPrinter())

        # Needed so coala doesn't error out
        self.min_args = ['-f', '*.java', '-b', 'JavaTestBear']

        self.original_user_coafile = Constants.user_coafile
        self.original_system_coafile = Constants.system_coafile
Ejemplo n.º 21
0
 def test_section_deprecation(self):
     section = Section('')
     log_printer = LogPrinter(NullPrinter())
     with LogCapture() as capture:
         fail_acquire_settings(log_printer, {}, section)
         capture.check(
             ('root', 'WARNING', 'fail_acquire_settings: section parameter '
              'is deprecated.'))
Ejemplo n.º 22
0
def get_all_bears_names():
    from coalib.settings.Section import Section
    printer = LogPrinter(NullPrinter())
    local_bears, global_bears = collect_bears(
        Section("").bear_dirs(), ["**"], [BEAR_KIND.LOCAL, BEAR_KIND.GLOBAL],
        printer,
        warn_if_unused_glob=False)
    return [bear.name for bear in itertools.chain(local_bears, global_bears)]
Ejemplo n.º 23
0
 def test_(self):
     log_printer = LogPrinter(NullPrinter())
     self.assertRaises(TypeError, fail_acquire_settings, log_printer, None)
     self.assertRaises(AssertionError,
                       fail_acquire_settings,
                       log_printer,
                       {"setting": ["description", "bear"]})
     self.assertEqual(fail_acquire_settings(log_printer, {}), None)
Ejemplo n.º 24
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)
Ejemplo n.º 25
0
 def setUp(self):
     self.project_dir = os.path.join(
         os.path.dirname(os.path.realpath(__file__)), "project_dir")
     os.makedirs(self.project_dir, exist_ok=True)
     self.arg_parser = _get_arg_parser()
     self.printer = ConsolePrinter()
     self.log_printer = LogPrinter(self.printer)
     self.old_argv = deepcopy(sys.argv)
     del sys.argv[1:]
Ejemplo n.º 26
0
def generate_settings(project_dir,
                      project_files,
                      ignore_globs,
                      relevant_bears,
                      incomplete_sections=False):
    """
    Generates the settings for the given project.

    :param project_dir:
        Full path of the user's project directory.
    :param project_files:
        A list of file paths matched in the user's project directory.
    :param ignore_globs:
        The list of ignore glob expressions.
    :param relevant_bears:
        A dict with language name as key and bear classes as value.
    :param incomplete_sections:
        When bears with non optional settings are found, user is asked for
        setting value of non optional bears and then a ``Section`` object
        having ``files`` field, ``bears`` field and settings, is returned.
        If incomplete_sections is set to ``True``, then no user input will
        be asked and a ``Section`` object having only the ``files`` and
        ``bears`` field will be returned.

        In CI mode, bears with non optional setting are not added in coafile.
        But if incomplete_sections is set to ``True`` in CI mode, then those
        bears are also added in the coafile.
    :return:
        A dict with section name as key and a ``Section`` object as value.
    """
    lang_map = {lang.lower(): lang for lang in relevant_bears}
    lang_files = split_by_language(project_files)
    extset = get_extensions(project_files)

    settings = OrderedDict()

    settings["default"] = generate_section(
        "default", [ext for lang in lang_files for ext in extset[lang]],
        relevant_bears[lang_map["all"]])

    ignored_files = generate_ignore_field(project_dir, lang_files.keys(),
                                          extset, ignore_globs)

    if ignored_files:
        settings["default"]["ignore"] = ignored_files

    for lang in lang_files:
        if lang != "unknown" and lang != "all":
            settings[lang_map[lang]] = generate_section(
                lang, extset[lang], relevant_bears[lang_map[lang]])

    log_printer = LogPrinter(ConsolePrinter())

    if not incomplete_sections:
        fill_settings(settings, acquire_settings, log_printer)

    return settings
Ejemplo n.º 27
0
    def setUp(self):
        self.file = os.path.join(tempfile.gettempdir(), 'ConfParserTestFile')
        with open(self.file, 'w', encoding='utf-8') as file:
            file.write(self.example_file)

        self.log_printer = LogPrinter()
        self.write_file_name = os.path.join(tempfile.gettempdir(),
                                            'ConfWriterTestFile')
        self.uut = ConfWriter(self.write_file_name)
 def test_(self):
     log_printer = LogPrinter(NullPrinter())
     section = Section('')
     self.assertRaises(TypeError, fail_acquire_settings, log_printer, None,
                       section)
     self.assertRaises(AssertionError, fail_acquire_settings, log_printer,
                       {'setting': ['description', 'bear']}, section)
     self.assertEqual(fail_acquire_settings(log_printer, {}, section), None,
                      section)
Ejemplo n.º 29
0
 def test_tagging(self):
     log_printer = LogPrinter(NullPrinter())
     execute_coala(coala_ci.main, "coala-ci", 'docs', "-S", "tag=test_tag",
                   "-c", self.coafile)
     tag_path = get_tag_path("test_tag", self.unescaped_coafile,
                             log_printer)
     self.assertTrue(os.path.exists(tag_path))
     execute_coala(coala_ci.main, "coala-ci", 'docs', "-S", "dtag=test_tag",
                   "-c", self.coafile)
     self.assertFalse(os.path.exists(tag_path))
Ejemplo n.º 30
0
    def __init__(self,
                 filename,
                 log_level=LOG_LEVEL.WARNING,
                 timestamp_format="%X"):
        """
        Creates a new FilePrinter. If the directory of the given file doesn't exist or if there's any access problems,
        an exception will be thrown.

        :param filename: the name of the file to put the data into (string).
        """
        self.file = None
        if not isinstance(filename, str):
            raise TypeError("filename must be a string.")

        LogPrinter.__init__(self,
                            timestamp_format=timestamp_format,
                            log_level=log_level)

        self.file = open(filename, 'a+')
Ejemplo n.º 31
0
def mode_json(args):
    import json

    from coalib.coala_main import run_coala
    from coalib.misc.DictUtilities import inverse_dicts
    from coalib.misc.Exceptions import get_exitcode
    from coalib.output.Logging import configure_json_logging
    from coalib.output.JSONEncoder import create_json_encoder
    from coalib.output.printers.LogPrinter import LogPrinter
    from coalib.settings.ConfigurationGathering import get_filtered_bears

    if args.log_json:
        log_stream = configure_json_logging()

    JSONEncoder = create_json_encoder(use_relpath=args.relpath)
    results = []

    if args.show_bears:
        try:
            local_bears, global_bears = get_filtered_bears(
                args.filter_by_language, LogPrinter())
            bears = inverse_dicts(local_bears, global_bears)
            for bear, _ in sorted(bears.items(),
                                  key=lambda bear_tuple: bear_tuple[0].name):
                results.append(bear)
        except BaseException as exception:  # pylint: disable=broad-except
            return get_exitcode(exception)
    else:
        results, exitcode, _ = run_coala()

    retval = {'bears': results} if args.show_bears else {'results': results}

    if args.log_json:
        retval['logs'] = [
            json.loads(line) for line in log_stream.getvalue().splitlines()
        ]

    if args.output:
        filename = str(args.output[0])
        with open(filename, 'w+') as fp:
            json.dump(retval,
                      fp,
                      cls=JSONEncoder,
                      sort_keys=True,
                      indent=2,
                      separators=(',', ': '))
    else:
        print(
            json.dumps(retval,
                       cls=JSONEncoder,
                       sort_keys=True,
                       indent=2,
                       separators=(',', ': ')))

    return 0 if args.show_bears else exitcode
Ejemplo n.º 32
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
 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)
Ejemplo n.º 34
0
    def setUp(self):
        config_path = os.path.abspath(
            os.path.join(os.path.dirname(__file__),
                         'section_executor_test_files', '.coafile'))
        self.testcode_c_path = os.path.join(os.path.dirname(config_path),
                                            'testcode.c')
        self.unreadable_path = os.path.join(os.path.dirname(config_path),
                                            'unreadable')

        factory_test_path = os.path.abspath(
            os.path.join(os.path.dirname(__file__), 'file_factory_test_files'))
        self.factory_test_file = os.path.join(factory_test_path,
                                              'factory_test.txt')
        self.a_bear_test_path = os.path.join(factory_test_path,
                                             'a_bear_test.txt')
        self.b_bear_test_path = os.path.join(factory_test_path,
                                             'b_bear_test.txt')
        self.c_bear_test_path = os.path.join(factory_test_path,
                                             'c_bear_test.txt')
        self.d_bear_test_path = os.path.join(factory_test_path,
                                             'd_bear_test.txt')
        self.e_bear_test_path = os.path.join(factory_test_path,
                                             'e_bear_test.txt')
        self.n_bear_test_path = os.path.join(factory_test_path,
                                             'n_bear_test.txt')
        self.n_bear_test_path_2 = os.path.join(factory_test_path,
                                               'n_bear_test2.txt')
        self.x_bear_test_path = os.path.join(factory_test_path,
                                             'x_bear_test.txt')

        filename_list = [
            self.factory_test_file, self.a_bear_test_path,
            self.b_bear_test_path, self.c_bear_test_path,
            self.d_bear_test_path, self.e_bear_test_path,
            self.n_bear_test_path, self.n_bear_test_path_2,
            self.x_bear_test_path
        ]

        self.file_dict = get_file_dict(filename_list)

        self.result_queue = queue.Queue()
        self.queue = queue.Queue()
        self.log_queue = queue.Queue()
        self.console_printer = ConsolePrinter()
        log_printer = LogPrinter(ConsolePrinter())
        self.log_printer = ProcessingTestLogPrinter(self.log_queue)

        (self.sections, self.local_bears, self.global_bears,
         targets) = gather_configuration(lambda *args: True,
                                         log_printer,
                                         arg_list=['--config', config_path])
        self.assertEqual(len(self.local_bears['cli']), 1)
        self.assertEqual(len(self.global_bears['cli']), 1)
        self.assertEqual(targets, [])
Ejemplo n.º 35
0
def get_all_bears():
    """
    Get a ``list`` of all available bears.
    """
    from coalib.settings.Section import Section
    printer = LogPrinter(NullPrinter())
    local_bears, global_bears = collect_bears(
        Section('').bear_dirs(), ['**'], [BEAR_KIND.LOCAL, BEAR_KIND.GLOBAL],
        printer,
        warn_if_unused_glob=False)
    return list(itertools.chain(local_bears, global_bears))
Ejemplo n.º 36
0
 def __init__(self):
     LogPrinter.__init__(self, self)
Ejemplo n.º 37
0
 def __init__(self, result_queue, log_queue):
     Interactor.__init__(self)
     LogPrinter.__init__(self)
     self.result_queue = result_queue
     self.log_queue = log_queue
     self.set_up = False
Ejemplo n.º 38
0
 def __init__(self, log_level=LOG_LEVEL.WARNING, timestamp_format="%X"):
     ColorPrinter.__init__(self)
     LogPrinter.__init__(self, log_level=log_level, timestamp_format=timestamp_format)
 def __init__(self):
     GObject.Object.__init__(self)
     self.log_printer = LogPrinter(ConsolePrinter())
class CoalaViewActivatable(GObject.Object, Gedit.ViewActivatable):
    """
    A class inherited from Gedit.ViewActivatable - it gets created for every
    gedit view. This class has a property `view` which is the Gedit.View that
    the class is related to. From the Gedit.View, the Gedit.Document associated
    to it can be got using `view.get_buffer()`.
    """
    __gtype_name__ = "CoalaViewActivatable"

    view = GObject.Property(type=Gedit.View)

    def __init__(self):
        GObject.Object.__init__(self)
        self.log_printer = LogPrinter(ConsolePrinter())

    def do_activate(self):
        """
        This function is called when the view is created - it will
        be called only once in a lifetime of a view.
        """
        self.register_marks()
        self.view.get_buffer().connect("saved", lambda x: self.analyze())

    def register_marks(self):
        """
        Creates mark-attributes for all result severities.
        """
        self.view.set_show_line_marks(True)
        for name, val in RESULT_SEVERITY.str_dict.items():
            attr = GtkSource.MarkAttributes()
            attr.set_icon_name(RESULT_SEVERITY_ICONS[val])
            attr.connect("query_tooltip_markup", self.show_mark_tooltip)
            self.view.set_mark_attributes(get_mark_category(name), attr, 0)
            self.log_printer.info("Mark attribute created for", name)

    def show_mark_tooltip(self, mark_attr, mark):
        result = getattr(mark, COALA_KEY + "Result")
        return str(result.origin) + ": " + str(result.message)

    def show_result(self, result):
        """
        Takes a result and shows it in gedit's UI.

        :param result: The result to display.
        """
        document = self.view.get_buffer()

        for sourcerange in result.affected_code:

            _iter = document.get_iter_at_line(sourcerange.start.line-1)
            mark = document.create_source_mark(
                None,
                get_mark_category(result.severity),
                _iter)
            setattr(mark, COALA_KEY + "Result", result)
            self.log_printer.info("Created mark at", sourcerange.start.line)

    def analyze(self):
        """
        This function fetches the location of the file in this view
        and runs coala on it.
        """
        document = self.view.get_buffer()
        location = document.get_location()
        if location == None:
            self.log_printer.warn("coala cannot run on unsaved files")
            return

        results = CoalaViewActivatable.run_coala(location.get_path())

        document.remove_source_marks(document.get_start_iter(),
                                     document.get_end_iter())
        for section_results in results.values():
            for result in section_results:
                self.show_result(result)

    @staticmethod
    def run_coala(path):
        """
        Run coala on the file at the given path. The config file is got using
        the `find-config` option of coala.

        :param path: The path of the file to analyze.
        :return:     The result dictionary from coala.
        """
        results = {}
        log_printer = LogPrinter(ConsolePrinter())
        cwd = os.getcwd()
        try:
            os.chdir(os.path.dirname(path))
            args = ["--find-config", "--limit-files", path, '-S',
                    'autoapply=false']
            sections, local_bears, global_bears, targets = (
                # Use `lambda *args: True` so that `gather_configuration` does
                # nothing when it needs to request settings from user.
                gather_configuration(lambda *args: True,
                                     log_printer,
                                     arg_list=args))

            for section_name in sections:
                section = sections[section_name]
                if not section.is_enabled(targets):
                    continue

                section_result = execute_section(
                    section=section,
                    global_bear_list=global_bears[section_name],
                    local_bear_list=local_bears[section_name],
                    print_results=lambda *args: True,
                    log_printer=log_printer)

                results_for_section = []
                for i in 1, 2:
                    for value in section_result[i].values():
                        for result in value:
                            if isinstance(result, HiddenResult):
                                continue
                            results_for_section.append(result)
                results[section_name] = results_for_section
        except BaseException as exception:
            log_printer.log_exception(str(exception), exception)
        finally:
            os.chdir(cwd)

        return results
Ejemplo n.º 41
0
 def test_log_level(self):
     uut = LogPrinter()
     self.assertEqual(uut.log_level, logging.DEBUG)
     uut.log_level = logging.INFO
     self.assertEqual(uut.log_level, logging.INFO)
Ejemplo n.º 42
0
    def test_logging(self):
        uut = LogPrinter(timestamp_format="")
        uut.logger = mock.MagicMock()
        uut.log_message(self.log_message)

        msg = Constants.COMPLEX_TEST_STRING
        uut.logger.log.assert_called_with(logging.ERROR, msg)

        uut = LogPrinter(log_level=LOG_LEVEL.DEBUG)
        uut.logger = mock.MagicMock()

        uut.log(LOG_LEVEL.ERROR, Constants.COMPLEX_TEST_STRING)
        uut.logger.log.assert_called_with(logging.ERROR, msg)

        uut.debug(Constants.COMPLEX_TEST_STRING, "d")
        uut.logger.log.assert_called_with(logging.DEBUG, msg + " d")

        uut.log_level = LOG_LEVEL.DEBUG
        uut.log_exception("Something failed.", NotImplementedError(msg))
        uut.logger.log.assert_any_call(logging.ERROR, "Something failed.")
        uut.logger.log.assert_called_with(
            logging.INFO,
            "Exception was:\n{exception}: {msg}".format(
                exception="NotImplementedError",
                msg=msg))
Ejemplo n.º 43
0
 def test_get_state(self):
     uut = LogPrinter()
     self.assertNotIn('logger', uut.__getstate__())
Ejemplo n.º 44
0
 def test_set_state(self):
     uut = LogPrinter()
     state = uut.__getstate__()
     uut.__setstate__(state)
     self.assertIs(uut.logger, logging.getLogger())
Ejemplo n.º 45
0
 def __init__(self, log_queue):
     LogPrinter.__init__(self, self)
     self.log_queue = log_queue
     self.set_up = False
Ejemplo n.º 46
0
 def __init__(self):
     ColorPrinter.__init__(self)
     LogPrinter.__init__(self)
Ejemplo n.º 47
0
    def test_logging(self):
        uut = LogPrinter(StringPrinter(), timestamp_format="")
        uut.log_message(self.log_message, end="")
        self.assertEqual(uut.printer.string, str(self.log_message))

        uut = LogPrinter(StringPrinter(), log_level=LOG_LEVEL.DEBUG)
        uut.log_message(self.log_message, end="")
        self.assertEqual(
            uut.printer.string,
            "[ERROR][" + self.timestamp.strftime("%X") + "] " +
            Constants.COMPLEX_TEST_STRING)

        uut.printer.clear()
        uut.log(LOG_LEVEL.ERROR,
                Constants.COMPLEX_TEST_STRING,
                timestamp=self.timestamp,
                end="")
        self.assertEqual(
            uut.printer.string,
            "[ERROR][" + self.timestamp.strftime("%X") + "] " +
            Constants.COMPLEX_TEST_STRING)

        uut.printer.clear()
        uut.debug(Constants.COMPLEX_TEST_STRING,
                  "d",
                  timestamp=self.timestamp,
                  end="")
        self.assertEqual(
            uut.printer.string,
            "[DEBUG][" + self.timestamp.strftime("%X") + "] " +
            Constants.COMPLEX_TEST_STRING + " d")

        uut.printer.clear()
        uut.log_level = LOG_LEVEL.INFO
        uut.debug(Constants.COMPLEX_TEST_STRING,
                  timestamp=self.timestamp,
                  end="")
        self.assertEqual(uut.printer.string, "")

        uut.printer.clear()
        uut.info(Constants.COMPLEX_TEST_STRING,
                 "d",
                 timestamp=self.timestamp,
                 end="")
        self.assertEqual(
            uut.printer.string,
            "[INFO][" + self.timestamp.strftime("%X") + "] " +
            Constants.COMPLEX_TEST_STRING + " d")

        uut.log_level = LOG_LEVEL.WARNING
        uut.printer.clear()
        uut.debug(Constants.COMPLEX_TEST_STRING,
                  timestamp=self.timestamp,
                  end="")
        self.assertEqual(uut.printer.string, "")

        uut.printer.clear()
        uut.warn(Constants.COMPLEX_TEST_STRING,
                 "d",
                 timestamp=self.timestamp,
                 end="")
        self.assertEqual(
            uut.printer.string,
            "[WARNING][" + self.timestamp.strftime("%X") + "] " +
            Constants.COMPLEX_TEST_STRING + " d")

        uut.printer.clear()
        uut.err(Constants.COMPLEX_TEST_STRING,
                "d",
                timestamp=self.timestamp,
                end="")
        self.assertEqual(
            uut.printer.string,
            "[ERROR][" + self.timestamp.strftime("%X") + "] " +
            Constants.COMPLEX_TEST_STRING + " d")

        uut.log_level = LOG_LEVEL.DEBUG
        uut.printer.clear()
        uut.log_exception(
            "Something failed.",
            NotImplementedError(Constants.COMPLEX_TEST_STRING),
            timestamp=self.timestamp)
        self.assertTrue(uut.printer.string.startswith(
            "[ERROR][" + self.timestamp.strftime("%X") +
            "] Something failed.\n" +
            "[DEBUG][" + self.timestamp.strftime("%X") +
            "] Exception was:"))

        uut.log_level = LOG_LEVEL.INFO
        uut.printer.clear()
        logged = uut.log_exception(
            "Something failed.",
            NotImplementedError(Constants.COMPLEX_TEST_STRING),
            timestamp=self.timestamp,
            end="")
        self.assertTrue(uut.printer.string.startswith(
            "[ERROR][" + self.timestamp.strftime("%X") +
            "] Something failed."))