Ejemplo n.º 1
0
def main(args: Sequence[str] = None,
         logger: Logger = None,
         handler: Handler = None):
    if args is None:
        args = argv[1:]

    if logger is None:
        logger = LOGGER

    config = parse_arguments(*args)
    configure_app(config, logger, handler=handler)

    try:
        yaml_config = YamlLintConfig(file=".yamllint")
    except IOError:
        yaml_config = None

    paths = set()
    for path in config.get("paths", []):
        paths.update(find_files(path, yaml_config=yaml_config))

    check_only = config.get("check")
    fail_text = "would reformat" if check_only else "reformatted"
    changed_file_count = 0
    error_count = 0
    with ProcessPoolExecutor(max_workers=cpu_count()) as executor:
        # executor.map would trigger if one of the underlying function calls
        # raised an exception on iterating
        futures = map(
            lambda pth: executor.submit(
                format_file, pth, dry_run=check_only, yaml_config=yaml_config),
            paths,
        )

        # list makes sure the futures are all submitted before waiting on the first results
        for pth, future in list(zip(paths, futures)):
            full_path = abspath(pth)
            try:
                if future.result():
                    changed_file_count += 1
                    LOGGER.warning("%s %s", fail_text, full_path)
                else:
                    LOGGER.debug("%s already well formatted, good job.",
                                 full_path)
            except Exception as e:
                error_count += 1
                LOGGER.error("error: cannot format %s: %s", full_path, e)

    LOGGER.log(*format_result(
        len(paths), changed_file_count, error_count, check_only=check_only))
    return error_count > 0 and (not check_only or changed_file_count == 0)
Ejemplo n.º 2
0
    def test_debug_config(self):
        with StringIO() as stream:
            config = Config({"log_level": DEBUG // 10})
            logger = getLogger("ConfigTest.test_debug_config")
            configure_app(config, logger, handler=StreamHandler(stream))
            self.assert_empty_log(stream)

            logger.debug(DEBUG_MSG)
            self.assert_log(DEBUG_MSG, stream)

            logger.info(INFO_MSG)
            self.assert_log(INFO_MSG, stream)

            logger.warning(WARNING_MSG)
            self.assert_log(WARNING_MSG, stream)

            logger.error(ERROR_MSG)
            self.assert_log(ERROR_MSG, stream)

            logger.critical(CRITICAL_MSG)
            self.assert_log(CRITICAL_MSG, stream)