def has_valid_header()

in infra/build/developer-tools/build/verify_boilerplate/verify_boilerplate.py [0:0]


def has_valid_header(filename, refs):
    """Test whether a file has the correct boilerplate header.

    Tests each file against the boilerplate stored in refs for that file type
    (based on extension), or by the entire filename (eg Dockerfile, Makefile).
    Some heuristics are applied to remove build tags and shebangs, but little
    variance in header formatting is tolerated.

    Args:
        filename: A string containing the name of the file to test
        refs: A map of boilerplate headers, keyed by file extension

    Returns:
        True if the file has the correct boilerplate header, otherwise returns
        False.
    """
    try:
        with open(filename, 'r') as fp:  # pylint: disable=invalid-name
            data = fp.read()
    except IOError:
        print(filename)
        return False
    basename = os.path.basename(filename)
    extension = get_file_extension(filename)
    if extension:
        ref = refs[extension]
    else:
        ref = refs[basename]
    data = data.splitlines()
    pattern_len = len(ref)
    # if our test file is smaller than the reference it surely fails!
    if pattern_len > len(data):
        return False
    copyright_regex = re.compile("Copyright 20\\d\\d")
    substitute_string = "Copyright YYYY"
    copyright_is_found = False
    j = 0
    for datum in data:
        # if it's a copyright line
        if not copyright_is_found and copyright_regex.search(datum):
            copyright_is_found = True
            # replace the actual year (e.g. 2019) with "YYYY" placeholder
            # used in a boilerplate
            datum = copyright_regex.sub(substitute_string, datum)
        if datum == ref[j]:
            j = j + 1
        else:
            j = 0
        if j == pattern_len:
            return copyright_is_found
    return copyright_is_found and j == pattern_len