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>
  • Loading branch information
saschagrunert committed Jun 10, 2021
1 parent ee974c8 commit 4fe875a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
22 changes: 14 additions & 8 deletions hack/verify_boilerplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ 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:
if not file_data:
return True # Nothing to copyright in this empty file.

basename = os.path.basename(filename)
Expand All @@ -112,21 +112,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):
Expand All @@ -135,6 +133,14 @@ 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)]

# remove the 'YEAR' placeholder from the ref if the file is generated, but
# not the current script (gets matched by is_generated())
if not os.path.realpath(filename) == os.path.realpath(__file__) and is_generated(file_data):
for i, line in enumerate(ref):
ref[i] = line.replace("Copyright YEAR", "Copyright")
if ref[i] != line:
break

year = regexs["year"]
for datum in data:
if year.search(datum):
Expand Down
6 changes: 4 additions & 2 deletions hack/verify_boilerplate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ def __init__(self):
self.filenames = []
self.rootdir = '.'
self.boilerplate_dir = '../'
self.skip = []
self.skip = [
'external/io_k8s_repo_infra/hack/verify_boilerplate.py'
]
self.verbose = True

verify_boilerplate.ARGS = Args()
Expand All @@ -55,7 +57,7 @@ def __init__(self):
'./fail.py',
])) + '\n' # add trailing newline

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


if __name__ == '__main__':
Expand Down

0 comments on commit 4fe875a

Please sign in to comment.