Skip to content

Commit 998cc97

Browse files
committed
Enforce correct file headers
Signed-off-by: Stefan Büringer [email protected]
1 parent 79b5481 commit 998cc97

14 files changed

+477
-12
lines changed

Makefile

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
#!/usr/bin/env bash
2-
3-
# Copyright 2020 The Kubernetes Authors.
1+
# Copyright 2020 The Kubernetes Authors.
42
#
5-
# Licensed under the Apache License, Version 2.0 (the "License");
6-
# you may not use this file except in compliance with the License.
7-
# You may obtain a copy of the License at
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
86
#
9-
# http://www.apache.org/licenses/LICENSE-2.0
7+
# http://www.apache.org/licenses/LICENSE-2.0
108
#
11-
# Unless required by applicable law or agreed to in writing, software
12-
# distributed under the License is distributed on an "AS IS" BASIS,
13-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
# See the License for the specific language governing permissions and
15-
# limitations under the License.
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
1614

1715
# If you update this file, please follow
1816
# https://suva.sh/posts/well-documented-makefiles
@@ -146,6 +144,10 @@ verify-modules: modules $(GO_MOD_CHECK) ## Verify go modules are up to date
146144

147145
APIDIFF_OLD_COMMIT ?= $(shell git rev-parse origin/main)
148146

147+
.PHONY: verify-boilerplate
148+
verify-boilerplate: ## Verify boilerplate text exists in each file
149+
TRACE=1 ./hack/verify-boilerplate.sh
150+
149151
.PHONY: apidiff
150152
verify-apidiff: $(GO_APIDIFF) ## Check for API differences
151153
$(GO_APIDIFF) $(APIDIFF_OLD_COMMIT) --print-compatible

hack/boilerplate/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Boilerplate utilities
2+
3+
The boilerplate validation utilities have been copied and adapted from upstream Kubernetes, see https://github.com/kubernetes/kubernetes/blob/4dfd73940396730caf331e35cbb28235d233f2a0/hack/boilerplate.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright YEAR The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+

hack/boilerplate/boilerplate.go.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
Copyright YEAR The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/

hack/boilerplate/boilerplate.py

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
#!/usr/bin/env python3
2+
3+
# Copyright 2015 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
from __future__ import print_function
18+
19+
import argparse
20+
import datetime
21+
import difflib
22+
import glob
23+
import os
24+
import re
25+
import sys
26+
27+
parser = argparse.ArgumentParser()
28+
parser.add_argument(
29+
"filenames",
30+
help="list of files to check, all files if unspecified",
31+
nargs='*')
32+
33+
rootdir = os.path.dirname(__file__) + "/../../"
34+
rootdir = os.path.abspath(rootdir)
35+
parser.add_argument(
36+
"--rootdir", default=rootdir, help="root directory to examine")
37+
38+
default_boilerplate_dir = os.path.join(rootdir, "hack/boilerplate")
39+
parser.add_argument(
40+
"--boilerplate-dir", default=default_boilerplate_dir)
41+
42+
parser.add_argument(
43+
"-v", "--verbose",
44+
help="give verbose output regarding why a file does not pass",
45+
action="store_true")
46+
47+
args = parser.parse_args()
48+
49+
verbose_out = sys.stderr if args.verbose else open("/dev/null", "w")
50+
51+
def get_refs():
52+
refs = {}
53+
54+
for path in glob.glob(os.path.join(args.boilerplate_dir, "boilerplate.*.txt")):
55+
extension = os.path.basename(path).split(".")[1]
56+
57+
ref_file = open(path, 'r')
58+
ref = ref_file.read().splitlines()
59+
ref_file.close()
60+
refs[extension] = ref
61+
62+
return refs
63+
64+
def is_generated_file(filename, data, regexs):
65+
for d in skipped_ungenerated_files:
66+
if d in filename:
67+
return False
68+
69+
p = regexs["generated"]
70+
return p.search(data)
71+
72+
def file_passes(filename, refs, regexs):
73+
try:
74+
f = open(filename, 'r')
75+
except Exception as exc:
76+
print("Unable to open %s: %s" % (filename, exc), file=verbose_out)
77+
return False
78+
79+
data = f.read()
80+
f.close()
81+
82+
# determine if the file is automatically generated
83+
generated = is_generated_file(filename, data, regexs)
84+
85+
basename = os.path.basename(filename)
86+
extension = file_extension(filename)
87+
if generated:
88+
if extension == "go":
89+
extension = "generatego"
90+
elif extension == "bzl":
91+
extension = "generatebzl"
92+
93+
if extension != "":
94+
ref = refs[extension]
95+
else:
96+
ref = refs[basename]
97+
98+
# remove extra content from the top of files
99+
if extension == "go" or extension == "generatego":
100+
p = regexs["go_build_constraints"]
101+
(data, found) = p.subn("", data, 1)
102+
elif extension in ["sh", "py"]:
103+
p = regexs["shebang"]
104+
(data, found) = p.subn("", data, 1)
105+
106+
data = data.splitlines()
107+
108+
# if our test file is smaller than the reference it surely fails!
109+
if len(ref) > len(data):
110+
print('File %s smaller than reference (%d < %d)' %
111+
(filename, len(data), len(ref)),
112+
file=verbose_out)
113+
return False
114+
115+
# trim our file to the same number of lines as the reference file
116+
data = data[:len(ref)]
117+
118+
p = regexs["year"]
119+
for d in data:
120+
if p.search(d):
121+
if generated:
122+
print('File %s has the YEAR field, but it should not be in generated file' %
123+
filename, file=verbose_out)
124+
else:
125+
print('File %s has the YEAR field, but missing the year of date' %
126+
filename, file=verbose_out)
127+
return False
128+
129+
if not generated:
130+
# Replace all occurrences of the regex "2014|2015|2016|2017|2018" with "YEAR"
131+
p = regexs["date"]
132+
for i, d in enumerate(data):
133+
(data[i], found) = p.subn('YEAR', d)
134+
if found != 0:
135+
break
136+
137+
# if we don't match the reference at this point, fail
138+
if ref != data:
139+
print("Header in %s does not match reference, diff:" %
140+
filename, file=verbose_out)
141+
if args.verbose:
142+
print(file=verbose_out)
143+
for line in difflib.unified_diff(ref, data, 'reference', filename, lineterm=''):
144+
print(line, file=verbose_out)
145+
print(file=verbose_out)
146+
147+
return False
148+
149+
return True
150+
151+
def file_extension(filename):
152+
return os.path.splitext(filename)[1].split(".")[-1].lower()
153+
154+
skipped_dirs = ['_output', '.git', "hack/boilerplate/test"]
155+
156+
# list all the files contain 'DO NOT EDIT', but are not generated
157+
skipped_ungenerated_files = [
158+
'hack/lib/swagger.sh',
159+
'hack/boilerplate/boilerplate.py',
160+
# The generator injects `DO NOT EDIT` and thus needs to get excluded to not
161+
# get detected as false positive.
162+
'hack/tools/prowjob-gen/generator.go',
163+
]
164+
165+
def normalize_files(files):
166+
newfiles = []
167+
for pathname in files:
168+
if any(x in pathname for x in skipped_dirs):
169+
continue
170+
newfiles.append(pathname)
171+
for i, pathname in enumerate(newfiles):
172+
if not os.path.isabs(pathname):
173+
newfiles[i] = os.path.join(args.rootdir, pathname)
174+
return newfiles
175+
176+
def get_files(extensions):
177+
files = []
178+
if len(args.filenames) > 0:
179+
files = args.filenames
180+
else:
181+
for root, dirs, walkfiles in os.walk(args.rootdir):
182+
# don't visit certain dirs. This is just a performance improvement
183+
# as we would prune these later in normalize_files(). But doing it
184+
# cuts down the amount of filesystem walking we do and cuts down
185+
# the size of the file list
186+
for d in skipped_dirs:
187+
if d in dirs:
188+
dirs.remove(d)
189+
190+
for name in walkfiles:
191+
pathname = os.path.join(root, name)
192+
files.append(pathname)
193+
194+
files = normalize_files(files)
195+
outfiles = []
196+
for pathname in files:
197+
basename = os.path.basename(pathname)
198+
extension = file_extension(pathname)
199+
if extension in extensions or basename in extensions:
200+
outfiles.append(pathname)
201+
return outfiles
202+
203+
def get_dates():
204+
years = datetime.datetime.now().year
205+
return '(%s)' % '|'.join((str(year) for year in range(2014, years+1)))
206+
207+
def get_regexs():
208+
regexs = {}
209+
# Search for "YEAR" which exists in the boilerplate, but shouldn't in the real thing
210+
regexs["year"] = re.compile('YEAR')
211+
# get_dates return 2014, 2015, 2016, 2017, or 2018 until the current year as a regex like: "(2014|2015|2016|2017|2018)";
212+
# company holder names can be anything
213+
regexs["date"] = re.compile(get_dates())
214+
# strip the following build constraints/tags:
215+
# //go:build
216+
# // +build \n\n
217+
regexs["go_build_constraints"] = re.compile(
218+
r"^(//(go:build| \+build).*\n)+\n", re.MULTILINE)
219+
# strip #!.* from scripts
220+
regexs["shebang"] = re.compile(r"^(#!.*\n)\n*", re.MULTILINE)
221+
# Search for generated files
222+
regexs["generated"] = re.compile('DO NOT EDIT')
223+
return regexs
224+
225+
def main():
226+
regexs = get_regexs()
227+
refs = get_refs()
228+
filenames = get_files(refs.keys())
229+
230+
for filename in filenames:
231+
if not file_passes(filename, refs, regexs):
232+
print(filename, file=sys.stdout)
233+
234+
return 0
235+
236+
if __name__ == "__main__":
237+
sys.exit(main())

hack/boilerplate/boilerplate.py.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright YEAR The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.

hack/boilerplate/boilerplate.sh.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright YEAR The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.

hack/boilerplate/boilerplate_test.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python3
2+
3+
# Copyright 2016 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import boilerplate
18+
import unittest
19+
import StringIO
20+
import os
21+
import sys
22+
23+
class TestBoilerplate(unittest.TestCase):
24+
"""
25+
Note: run this test from the hack/boilerplate directory.
26+
27+
$ python -m unittest boilerplate_test
28+
"""
29+
30+
def test_boilerplate(self):
31+
os.chdir("test/")
32+
33+
class Args(object):
34+
def __init__(self):
35+
self.filenames = []
36+
self.rootdir = "."
37+
self.boilerplate_dir = "../"
38+
self.verbose = True
39+
40+
# capture stdout
41+
old_stdout = sys.stdout
42+
sys.stdout = StringIO.StringIO()
43+
44+
boilerplate.args = Args()
45+
ret = boilerplate.main()
46+
47+
output = sorted(sys.stdout.getvalue().split())
48+
49+
sys.stdout = old_stdout
50+
51+
self.assertEquals(
52+
output, ['././fail.go', '././fail.py'])

0 commit comments

Comments
 (0)