示例#1
0
 def test_tag_results(self):
     try:
         tag_results("test_tag", "test_path", {})
         results = load_tagged_results("test_tag", "test_path")
         self.assertEqual(results, {})
     finally:
         delete_tagged_results("test_tag", "test_path")
示例#2
0
文件: TaggingTest.py 项目: 707/coala
    def test_tag_results(self):
        path = get_tag_path("test_tag_create", "test_path", self.log_printer)
        try:
            tag_results("test_tag_create", "test_path", {}, self.log_printer)
            results = load_tagged_results("test_tag_create", "test_path",
                                          self.log_printer)
            self.assertEqual(results, {})
        finally:
            delete_tagged_results("test_tag_create", "test_path",
                                  self.log_printer)

        none_path = get_tag_path("None", "test_path", self.log_printer)
        tag_results("None", "test_path", {}, self.log_printer)
        self.assertFalse(os.path.exists(none_path))
        results = load_tagged_results("None", "test_path", self.log_printer)
        self.assertEquals(results, None)
示例#3
0
    def test_tag_results(self):
        path = get_tag_path("test_tag_create", "test_path", self.log_printer)
        try:
            tag_results("test_tag_create", "test_path", {}, self.log_printer)
            results = load_tagged_results("test_tag_create",
                                          "test_path",
                                          self.log_printer)
            self.assertEqual(results, {})
        finally:
            delete_tagged_results("test_tag_create",
                                  "test_path",
                                  self.log_printer)

        none_path = get_tag_path("None", "test_path", self.log_printer)
        tag_results("None", "test_path", {}, self.log_printer)
        self.assertFalse(os.path.exists(none_path))
        results = load_tagged_results("None", "test_path", self.log_printer)
        self.assertEquals(results, None)
示例#4
0
    def test_delete_tagged_results_no_file(self):
        path = get_tag_path("test_tag_del", "test_path", self.log_printer)
        none_path = get_tag_path("None", "test_path", self.log_printer)
        open(path, "a").close()
        open(none_path, "a").close()
        delete_tagged_results("None", "test_path", self.log_printer)
        self.assertTrue(os.path.exists(none_path))

        delete_tagged_results("test_tag_del", "test_path", self.log_printer)
        self.assertFalse(os.path.exists(path))

        delete_tagged_results("test_tag_del2", "test_path", self.log_printer)
        self.assertFalse(os.path.exists(path))

        os.remove(none_path)
示例#5
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,
              show_bears=do_nothing,
              autoapply=True):
    """
    This is a main method that should be usable for almost all purposes and
    reduces executing coala to one function call.

    :param log_printer:             A LogPrinter object to use for logging.
    :param print_results:           A callback that takes a LogPrinter, a
                                    section, a list of results to be printed,
                                    the file dict and the mutable file diff
                                    dict.
    :param acquire_settings:        The method to use for requesting settings.
                                    It will get a parameter which is a
                                    dictionary with the settings name as key
                                    and a list containing a description in [0]
                                    and the names of the bears who need this
                                    setting in all following indexes.
    :param print_section_beginning: A callback that will be called with a
                                    section name string whenever analysis of a
                                    new section is started.
    :param nothing_done:            A callback that will be called with only a
                                    log printer that shall indicate that
                                    nothing was done.
    :param show_bears:              A callback that will be called with first
                                    a list of local bears, second a list of
                                    global bears to output them. A third bool
                                    parameter may be used to indicate if a
                                    compressed output (True) or a normal output
                                    (False) is desired, the former being used
                                    for showing all available bears to the
                                    user.
    :param autoapply:               Set to False to autoapply nothing by
                                    default; this is overridable via any
                                    configuration file/CLI.
    :return:                        A dictionary containing a list of results
                                    for all analyzed sections as key.
    """
    log_printer = log_printer or LogPrinter(ConsolePrinter())

    exitcode = 0
    results = None
    try:
        yielded_results = False
        did_nothing = True
        sections, local_bears, global_bears, targets = (
            gather_configuration(acquire_settings, log_printer))

        tag = str(sections['default'].get('tag', None))
        dtag = str(sections['default'].get('dtag', None))

        if not autoapply and 'autoapply' not in sections['default']:
            sections['default']['autoapply'] = "False"

        show_all_bears = bool(sections['default'].get('show_all_bears', False))
        show_bears_ = bool(sections["default"].get("show_bears", "False"))
        if show_all_bears:
            show_bears_ = True
            for section in sections:
                bear_dirs = sections[section].bear_dirs()
                local_bears[section] = collect_bears(bear_dirs,
                                                     ["**"],
                                                     [BEAR_KIND.LOCAL],
                                                     log_printer)
                global_bears[section] = collect_bears(bear_dirs,
                                                      ["**"],
                                                      [BEAR_KIND.GLOBAL],
                                                      log_printer)

        if dtag != "None":
            delete_tagged_results(
                dtag,
                os.path.abspath(str(sections["default"].get("config"))))

        if show_bears_:
            show_bears(local_bears,
                       global_bears,
                       show_all_bears)
            did_nothing = False
        else:
            results = {}
            for section_name in sections:
                section = sections[section_name]
                if not section.is_enabled(targets):
                    continue

                print_section_beginning(section)
                section_result = execute_section(
                    section=section,
                    global_bear_list=global_bears[section_name],
                    local_bear_list=local_bears[section_name],
                    print_results=print_results,
                    log_printer=log_printer)
                yielded_results = yielded_results or section_result[0]

                results_for_section = []
                for value in chain(section_result[1].values(),
                                   section_result[2].values()):
                    if value is None:
                        continue

                    for result in value:
                        results_for_section.append(result)

                results[section_name] = results_for_section
                did_nothing = False

            if tag != "None":
                tag_results(
                    tag,
                    os.path.abspath(str(sections["default"].get("config"))),
                    results)

        if did_nothing:
            nothing_done(log_printer)

        if yielded_results:
            exitcode = 1
    except BaseException as exception:  # pylint: disable=broad-except
        exitcode = exitcode or get_exitcode(exception, log_printer)

    return results, exitcode
示例#6
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,
              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
示例#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,
              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
示例#8
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,
              show_bears=do_nothing,
              autoapply=True):
    """
    This is a main method that should be usable for almost all purposes and
    reduces executing coala to one function call.

    :param log_printer:             A LogPrinter object to use for logging.
    :param print_results:           A callback that takes a LogPrinter, a
                                    section, a list of results to be printed,
                                    the file dict and the mutable file diff
                                    dict.
    :param acquire_settings:        The method to use for requesting settings.
                                    It will get a parameter which is a
                                    dictionary with the settings name as key
                                    and a list containing a description in [0]
                                    and the names of the bears who need this
                                    setting in all following indexes.
    :param print_section_beginning: A callback that will be called with a
                                    section name string whenever analysis of a
                                    new section is started.
    :param nothing_done:            A callback that will be called with only a
                                    log printer that shall indicate that
                                    nothing was done.
    :param show_bears:              A callback that will be called with first
                                    a list of local bears, second a list of
                                    global bears to output them. A third bool
                                    parameter may be used to indicate if a
                                    compressed output (True) or a normal output
                                    (False) is desired, the former being used
                                    for showing all available bears to the
                                    user.
    :param autoapply:               Set to False to autoapply nothing by
                                    default; this is overridable via any
                                    configuration file/CLI.
    :return:                        A dictionary containing a list of results
                                    for all analyzed sections as key.
    """
    log_printer = log_printer or LogPrinter(ConsolePrinter())

    exitcode = 0
    results = None
    try:
        yielded_results = False
        did_nothing = True
        sections, local_bears, global_bears, targets = (gather_configuration(
            acquire_settings, log_printer))

        tag = str(sections['default'].get('tag', None))
        dtag = str(sections['default'].get('dtag', None))

        if not autoapply and 'autoapply' not in sections['default']:
            sections['default']['autoapply'] = "False"

        show_all_bears = bool(sections['default'].get('show_all_bears', False))
        show_bears_ = bool(sections["default"].get("show_bears", "False"))
        if show_all_bears:
            show_bears_ = True
            for section in sections:
                bear_dirs = sections[section].bear_dirs()
                local_bears[section] = collect_bears(bear_dirs, ["**"],
                                                     [BEAR_KIND.LOCAL],
                                                     log_printer)
                global_bears[section] = collect_bears(bear_dirs, ["**"],
                                                      [BEAR_KIND.GLOBAL],
                                                      log_printer)

        if dtag != "None":
            delete_tagged_results(
                dtag, os.path.abspath(str(sections["default"].get("config"))))

        if show_bears_:
            show_bears(local_bears, global_bears, show_all_bears)
            did_nothing = False
        else:
            results = {}
            for section_name in sections:
                section = sections[section_name]
                if not section.is_enabled(targets):
                    continue

                print_section_beginning(section)
                section_result = execute_section(
                    section=section,
                    global_bear_list=global_bears[section_name],
                    local_bear_list=local_bears[section_name],
                    print_results=print_results,
                    log_printer=log_printer)
                yielded_results = yielded_results or section_result[0]

                results_for_section = []
                for value in chain(section_result[1].values(),
                                   section_result[2].values()):
                    if value is None:
                        continue

                    for result in value:
                        results_for_section.append(result)

                results[section_name] = results_for_section
                did_nothing = False

            if tag != "None":
                tag_results(
                    tag,
                    os.path.abspath(str(sections["default"].get("config"))),
                    results)

        if did_nothing:
            nothing_done(log_printer)

        if yielded_results:
            exitcode = 1
    except BaseException as exception:  # pylint: disable=broad-except
        exitcode = exitcode or get_exitcode(exception, log_printer)

    return results, exitcode
示例#9
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):
    """
    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.
    :return:                        A dictionary containing a list of results
                                    for all analyzed sections as key.
    """
    log_printer = log_printer or LogPrinter(ConsolePrinter(), LOG_LEVEL.DEBUG)

    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)

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

        tag = str(sections['default'].get('tag', None))
        dtag = str(sections['default'].get('dtag', None))
        config_file = os.path.abspath(str(sections["default"].get("config")))

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

        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

            file_dicts[section_name] = section_result[3]

        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, file_dicts
示例#10
0
 def test_delete_tagged_results_no_file(self):
     delete_tagged_results("test_tag", "test_path")
     self.assertFalse(os.path.exists(get_tag_path("test_tag", "test_path")))
示例#11
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,
):
    """
    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.
    :return:                        A dictionary containing a list of results
                                    for all analyzed sections as key.
    """
    log_printer = log_printer or LogPrinter(ConsolePrinter())

    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
        )

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

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

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

        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

            file_dicts[section_name] = section_result[3]

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