Пример #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)
                    )
Пример #2
0
def comment_out_header(header_text, user_filepath, args, config):
    """Add user's commenting syntax to each line of the header's text.
    Commenting syntax is chosen by `user_filepath` matching a configured regex.
    Comment tokens added to empty lines will be stripped of trailing whitespace.
    """
    header_lines = header_text.splitlines(keepends=True)

    if "CommentedFiles" in config:
        matched_comment_formats = [
            regex
            for regex in config["CommentedFiles"]
            if re.match(
                config_handling.param_ignoredfiles_to_regex(regex),
                user_filepath
            )
        ]

        if len(matched_comment_formats) > 1 and args.info_level == "verbose":
            print((
                "WARNING: {} matches more than one commenting format. Using"
                " first match."
            ).format(user_filepath))
            print("  List of matches:")
            for filepattern in matched_comment_formats:
                print("    " + filepattern)

        if matched_comment_formats:
            comment_format = (
                config["CommentedFiles"][matched_comment_formats[0]]
            )

            if "BlockComments" in comment_format:
                block_format = comment_format["BlockComments"]

                if block_format["BlockEnd"] in header_text:
                    print(
                        "WARNING: the license header contains the comment token"
                        " used to indicate a block comment end."
                    )
                elif block_format["BlockEnd"].strip() in header_text:
                    print(
                        "WARNING: the license header contains a whitespace-"
                        "stripped version of the block comment end token."
                    )

                header_lines = [
                    block_format["BlockStart"] + header_lines[0]
                ] + [
                    block_format.get("BlockLine", "") + line
                    if not line.isspace()
                    else block_format.get("BlockLine", "").rstrip() + line
                    for line in header_lines[1:]
                ] + [
                    block_format["BlockEnd"] + "\n"
                ]

            else:  # elif "LineCommentStart" in comment_format:
                header_lines = [
                    comment_format["LineCommentStart"] + line
                    if line != "\n"
                    else comment_format["LineCommentStart"].rstrip() + line
                    for line in header_lines
                ]

    if matched_comment_formats:
        comment_format = config["CommentedFiles"][matched_comment_formats[0]]
    else:
        comment_format = None

    header_text = "".join(header_lines)
    return header_text, comment_format