Skip to content

Commit 3ae7c02

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

15 files changed

+490
-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+
./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+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
Copyright 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.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: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
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+
91+
if extension != "":
92+
ref = refs[extension]
93+
else:
94+
ref = refs[basename]
95+
96+
# remove extra content from the top of files
97+
if extension == "go" or extension == "generatego":
98+
p = regexs["go_build_constraints"]
99+
(data, found) = p.subn("", data, 1)
100+
elif extension in ["sh", "py"]:
101+
p = regexs["shebang"]
102+
(data, found) = p.subn("", data, 1)
103+
104+
data = data.splitlines()
105+
106+
# if our test file is smaller than the reference it surely fails!
107+
if len(ref) > len(data):
108+
print('File %s smaller than reference (%d < %d)' %
109+
(filename, len(data), len(ref)),
110+
file=verbose_out)
111+
return False
112+
113+
# trim our file to the same number of lines as the reference file
114+
data = data[:len(ref)]
115+
116+
p = regexs["year"]
117+
for d in data:
118+
if p.search(d):
119+
if generated:
120+
print('File %s has the YEAR field, but it should not be in generated file' %
121+
filename, file=verbose_out)
122+
else:
123+
print('File %s has the YEAR field, but missing the year of date' %
124+
filename, file=verbose_out)
125+
return False
126+
127+
if not generated:
128+
# Replace all occurrences of the regex "2014|2015|2016|2017|2018" with "YEAR"
129+
p = regexs["date"]
130+
for i, d in enumerate(data):
131+
(data[i], found) = p.subn('YEAR', d)
132+
if found != 0:
133+
break
134+
135+
# if we don't match the reference at this point, fail
136+
if ref != data:
137+
print("Header in %s does not match reference, diff:" %
138+
filename, file=verbose_out)
139+
if args.verbose:
140+
print(file=verbose_out)
141+
for line in difflib.unified_diff(ref, data, 'reference', filename, lineterm=''):
142+
print(line, file=verbose_out)
143+
print(file=verbose_out)
144+
145+
return False
146+
147+
return True
148+
149+
def file_extension(filename):
150+
return os.path.splitext(filename)[1].split(".")[-1].lower()
151+
152+
skipped_dirs = ['_output', '.git', "hack/boilerplate/test"]
153+
154+
# list all the files contain 'DO NOT EDIT', but are not generated
155+
skipped_ungenerated_files = [
156+
'hack/lib/swagger.sh',
157+
'hack/boilerplate/boilerplate.py',
158+
# The generator injects `DO NOT EDIT` and thus needs to get excluded to not
159+
# get detected as false positive.
160+
'hack/tools/prowjob-gen/generator.go',
161+
]
162+
163+
def normalize_files(files):
164+
newfiles = []
165+
for pathname in files:
166+
if any(x in pathname for x in skipped_dirs):
167+
continue
168+
newfiles.append(pathname)
169+
for i, pathname in enumerate(newfiles):
170+
if not os.path.isabs(pathname):
171+
newfiles[i] = os.path.join(args.rootdir, pathname)
172+
return newfiles
173+
174+
def get_files(extensions):
175+
files = []
176+
if len(args.filenames) > 0:
177+
files = args.filenames
178+
else:
179+
for root, dirs, walkfiles in os.walk(args.rootdir):
180+
# don't visit certain dirs. This is just a performance improvement
181+
# as we would prune these later in normalize_files(). But doing it
182+
# cuts down the amount of filesystem walking we do and cuts down
183+
# the size of the file list
184+
for d in skipped_dirs:
185+
if d in dirs:
186+
dirs.remove(d)
187+
188+
for name in walkfiles:
189+
pathname = os.path.join(root, name)
190+
files.append(pathname)
191+
192+
files = normalize_files(files)
193+
outfiles = []
194+
for pathname in files:
195+
basename = os.path.basename(pathname)
196+
extension = file_extension(pathname)
197+
if extension in extensions or basename in extensions:
198+
outfiles.append(pathname)
199+
return outfiles
200+
201+
def get_dates():
202+
years = datetime.datetime.now().year
203+
return '(%s)' % '|'.join((str(year) for year in range(2014, years+1)))
204+
205+
def get_regexs():
206+
regexs = {}
207+
# Search for "YEAR" which exists in the boilerplate, but shouldn't in the real thing
208+
regexs["year"] = re.compile('YEAR')
209+
# get_dates return 2014, 2015, 2016, 2017, or 2018 until the current year as a regex like: "(2014|2015|2016|2017|2018)";
210+
# company holder names can be anything
211+
regexs["date"] = re.compile(get_dates())
212+
# strip the following build constraints/tags:
213+
# //go:build
214+
# // +build \n\n
215+
regexs["go_build_constraints"] = re.compile(
216+
r"^(//(go:build| \+build).*\n)+\n", re.MULTILINE)
217+
# strip #!.* from scripts
218+
regexs["shebang"] = re.compile(r"^(#!.*\n)\n*", re.MULTILINE)
219+
# Search for generated files
220+
regexs["generated"] = re.compile('DO NOT EDIT')
221+
return regexs
222+
223+
def main():
224+
regexs = get_regexs()
225+
refs = get_refs()
226+
filenames = get_files(refs.keys())
227+
228+
for filename in filenames:
229+
if not file_passes(filename, refs, regexs):
230+
print(filename, file=sys.stdout)
231+
232+
return 0
233+
234+
if __name__ == "__main__":
235+
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.

0 commit comments

Comments
 (0)