Ejemplo n.º 1
0
def main_check(args, config):
    """CLI program command: check
    Check project files for license/header text.
    """
    if args.files:
        filepaths = set(args.files)
    else:
        filepaths = (os.path.relpath(os.path.join(cwd, file))
                     for cwd, dirs, files in os.walk(".")
                     for file in files
                     if file != config_handling.CONFIG_FILENAME)

    if (not args.no_ignore) and config["IgnoredFiles"]:
        ignore_regex = "|".join(
            config_handling.param_ignoredfiles_to_regex(ignore_string)
            for ignore_string
            in config["IgnoredFiles"]
        )

        filepaths = (file for file in filepaths
                     if not re.match(ignore_regex, file))

    if filepaths:
        text_dict = license_handling.fill_in_license(
            config["License"], config
        )

        license_text = text_dict["license_text"]
        header_text = text_dict["header_text"]

    for path in filepaths:
        if not userfiles_handling.file_has_correct_header(path, args, config):
            if args.add_missing:
                if args.info_level == "verbose":
                    print("Adding header to {}.".format(path))

                commented_header_text, comment_format = \
                    license_handling.comment_out_header(
                        header_text, path, args, config
                    )

                userfiles_handling.write_header(
                    commented_header_text,
                    path,
                    (
                        comment_format.get("InsertAtLine", 0)
                        if comment_format
                        else 0
                    ),
                )
            else:
                if args.info_level == "quiet":
                    print(path)
                else:
                    print(
                        "{} has an incorrect or missing header.".format(path)
                    )
Ejemplo n.º 2
0
def file_has_correct_header(user_filepath, args, config):
    """Return true if file designated by `path` has the correct header.
    """
    try:
        linenum = _find_header_start_line(user_filepath)
    except UnicodeDecodeError:
        if args.info_level == "verbose":
            print(
                "File {} is not standard utf-8. It may be a binary.".format(
                    user_filepath
                )
            )

        return False

    if linenum is not None:
        with open(user_filepath, "rt") as user_file:
            header_text = license_handling.fill_in_license(
                config["License"], config,
            )["header_text"]

            header_text, comment_format = license_handling.comment_out_header(
                header_text, user_filepath, args, config,
            )

            header_lines = header_text.splitlines(keepends=True)

            user_file_lines = [
                line
                for line in itertools.islice(
                    user_file,
                    linenum,
                    linenum + len(header_lines),
                )
            ]

            mismatched_lines = _compare_header_lines(
                header_lines, user_file_lines
            )

            if args.info_level == "verbose" and mismatched_lines:
                print(
                    ("In file {}: there are {} lines that do not match the"
                     " expected license header.").format(user_filepath,
                                                         mismatched_lines)
                )

            return not bool(mismatched_lines)

    else:
        if args.info_level == "verbose":
            print(
                "File {} does not appear to have a license header.".format(
                    user_filepath)
            )

        return False