Skip to content

Commit

Permalink
verify_boilerplate.py: auto skip YEAR in generated files
Browse files Browse the repository at this point in the history
We should not skip generated files entirely, but only skip checking for
the `YEAR`. This patch changes the current behavior to only strip the
year from the ref.

Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
saschagrunert committed Jun 21, 2021
1 parent ee974c8 commit 80d4b7e
Showing 2 changed files with 26 additions and 20 deletions.
44 changes: 25 additions & 19 deletions hack/verify_boilerplate.py
Original file line number Diff line number Diff line change
@@ -28,11 +28,14 @@
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"filenames", help="list of files to check, all files if unspecified", nargs='*')
"filenames",
help="list of files to check, all files if unspecified",
nargs='*')

rootdir = os.path.abspath('.')
parser.add_argument(
"--rootdir", default=rootdir, help="root directory to examine")
parser.add_argument("--rootdir",
default=rootdir,
help="root directory to examine")

default_boilerplate_dir = os.path.join(rootdir, "verify/boilerplate")
parser.add_argument("--boilerplate-dir", default=default_boilerplate_dir)
@@ -47,6 +50,7 @@ def get_args():
'third_party',
'vendor',
'verify/boilerplate/test',
'verify_boilerplate.py',
],
action='append',
help='Customize paths to avoid',
@@ -59,7 +63,7 @@ def get_refs():

template_dir = ARGS.boilerplate_dir
if not os.path.isdir(template_dir):
template_dir = os.path.dirname(template_dir)
template_dir = os.path.dirname(template_dir)
for path in glob.glob(os.path.join(template_dir, "boilerplate.*.txt")):
extension = os.path.basename(path).split(".")[1]

@@ -98,12 +102,12 @@ def file_passes(filename, refs, regexs): # pylint: disable=too-many-locals
# Pass the encoding parameter to avoid ascii decode error for some
# platform.
with open(filename, 'r', encoding='utf-8') as fp:
data = fp.read()
file_data = fp.read()
except IOError:
return False

if not data:
return True # Nothing to copyright in this empty file.
if not file_data:
return True # Nothing to copyright in this empty file.

basename = os.path.basename(filename)
extension = file_extension(filename)
@@ -112,21 +116,19 @@ def file_passes(filename, refs, regexs): # pylint: disable=too-many-locals
else:
ref = refs[basename]

# check for and skip generated files
if is_generated(data):
return True
ref = ref.copy()

# remove build tags from the top of Go files
if extension == "go":
con = regexs["go_build_constraints"]
(data, found) = con.subn("", data, 1)
(file_data, found) = con.subn("", file_data, 1)

# remove shebang from the top of shell files
if extension in ("sh", "py"):
she = regexs["shebang"]
(data, found) = she.subn("", data, 1)
(file_data, found) = she.subn("", file_data, 1)

data = data.splitlines()
data = file_data.splitlines()

# if our test file is smaller than the reference it surely fails!
if len(ref) > len(data):
@@ -135,6 +137,12 @@ def file_passes(filename, refs, regexs): # pylint: disable=too-many-locals
# trim our file to the same number of lines as the reference file
data = data[:len(ref)]

# check if we encounter a 'YEAR' placeholder if the file is generated
if is_generated(file_data):
for i, line in enumerate(ref):
if "Copyright YEAR" in line:
return False

year = regexs["year"]
for datum in data:
if year.search(datum):
@@ -160,9 +168,7 @@ def file_extension(filename):

# even when generated by bazel we will complain about some generated files
# not having the headers. since they're just generated, ignore them
IGNORE_HEADERS = [
'// Code generated by go-bindata.'
]
IGNORE_HEADERS = ['// Code generated by go-bindata.']


def has_ignored_header(pathname):
@@ -229,8 +235,8 @@ def get_regexs():
# dates can be any year between 2014 and the current year, company holder names can be anything
regexs["date"] = re.compile(get_dates())
# strip // +build \n\n build constraints
regexs["go_build_constraints"] = re.compile(
r"^(// \+build.*\n)+\n", re.MULTILINE)
regexs["go_build_constraints"] = re.compile(r"^(// \+build.*\n)+\n",
re.MULTILINE)
# strip #!.* from shell/python scripts
regexs["shebang"] = re.compile(r"^(#!.*\n)\n*", re.MULTILINE)
return regexs
@@ -253,7 +259,7 @@ def main():

if nonconforming_files:
for line in nonconforming_lines(nonconforming_files):
print(line)
print(line)
sys.exit(1)


2 changes: 1 addition & 1 deletion hack/verify_boilerplate_test.py
Original file line number Diff line number Diff line change
@@ -55,7 +55,7 @@ def __init__(self):
'./fail.py',
])) + '\n' # add trailing newline

self.assertEquals(output, expected)
self.assertEqual(output, expected)


if __name__ == '__main__':

0 comments on commit 80d4b7e

Please sign in to comment.