Skip to content
This repository was archived by the owner on Mar 13, 2022. It is now read-only.

Commit 8497dfb

Browse files
authored
Merge pull request #107 from iamneha/verify-scripts
Verify-boilerplate script
2 parents 260f855 + d56fdbc commit 8497dfb

23 files changed

+317
-19
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ install:
3535

3636
script:
3737
- ./run_tox.sh tox
38+
- ./hack/verify-boilerplate.sh
3839

config/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python
2+
13
# Copyright 2016 The Kubernetes Authors.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");

config/config_exception.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python
2+
13
# Copyright 2016 The Kubernetes Authors.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");

config/dateutil.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python
2+
13
# Copyright 2017 The Kubernetes Authors.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");

config/dateutil_test.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python
2+
13
# Copyright 2016 The Kubernetes Authors.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");

config/exec_provider.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python
2+
13
# Copyright 2018 The Kubernetes Authors.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");

config/exec_provider_test.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python
2+
13
# Copyright 2018 The Kubernetes Authors.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");

config/incluster_config.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python
2+
13
# Copyright 2016 The Kubernetes Authors.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");

config/incluster_config_test.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python
2+
13
# Copyright 2016 The Kubernetes Authors.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");

config/kube_config.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python
2+
13
# Copyright 2018 The Kubernetes Authors.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");

config/kube_config_test.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python
2+
13
# Copyright 2018 The Kubernetes Authors.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");

hack/boilerplate/boilerplate.py

+197
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2018 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+
52+
def get_refs():
53+
refs = {}
54+
55+
for path in glob.glob(os.path.join(args.boilerplate_dir, "boilerplate.*.txt")):
56+
extension = os.path.basename(path).split(".")[1]
57+
58+
ref_file = open(path, 'r')
59+
ref = ref_file.read().splitlines()
60+
ref_file.close()
61+
refs[extension] = ref
62+
63+
return refs
64+
65+
66+
def file_passes(filename, refs, regexs):
67+
try:
68+
f = open(filename, 'r')
69+
except Exception as exc:
70+
print("Unable to open %s: %s" % (filename, exc), file=verbose_out)
71+
return False
72+
73+
data = f.read()
74+
f.close()
75+
76+
basename = os.path.basename(filename)
77+
extension = file_extension(filename)
78+
79+
if extension != "":
80+
ref = refs[extension]
81+
else:
82+
ref = refs[basename]
83+
84+
# remove extra content from the top of files
85+
if extension == "sh":
86+
p = regexs["shebang"]
87+
(data, found) = p.subn("", data, 1)
88+
89+
data = data.splitlines()
90+
91+
# if our test file is smaller than the reference it surely fails!
92+
if len(ref) > len(data):
93+
print('File %s smaller than reference (%d < %d)' %
94+
(filename, len(data), len(ref)),
95+
file=verbose_out)
96+
return False
97+
98+
# trim our file to the same number of lines as the reference file
99+
data = data[:len(ref)]
100+
101+
p = regexs["year"]
102+
for d in data:
103+
if p.search(d):
104+
print('File %s has the YEAR field, but missing the year of date' %
105+
filename, file=verbose_out)
106+
return False
107+
108+
# Replace all occurrences of the regex "2014|2015|2016|2017|2018" with "YEAR"
109+
p = regexs["date"]
110+
for i, d in enumerate(data):
111+
(data[i], found) = p.subn('YEAR', d)
112+
if found != 0:
113+
break
114+
115+
# if we don't match the reference at this point, fail
116+
if ref != data:
117+
print("Header in %s does not match reference, diff:" %
118+
filename, file=verbose_out)
119+
if args.verbose:
120+
print(file=verbose_out)
121+
for line in difflib.unified_diff(ref, data, 'reference', filename, lineterm=''):
122+
print(line, file=verbose_out)
123+
print(file=verbose_out)
124+
return False
125+
126+
return True
127+
128+
129+
def file_extension(filename):
130+
return os.path.splitext(filename)[1].split(".")[-1].lower()
131+
132+
133+
# list all the files contain 'DO NOT EDIT', but are not generated
134+
skipped_ungenerated_files = ['hack/boilerplate/boilerplate.py']
135+
136+
137+
def normalize_files(files):
138+
newfiles = []
139+
for pathname in files:
140+
newfiles.append(pathname)
141+
for i, pathname in enumerate(newfiles):
142+
if not os.path.isabs(pathname):
143+
newfiles[i] = os.path.join(args.rootdir, pathname)
144+
return newfiles
145+
146+
147+
def get_files(extensions):
148+
files = []
149+
if len(args.filenames) > 0:
150+
files = args.filenames
151+
else:
152+
for root, dirs, walkfiles in os.walk(args.rootdir):
153+
for name in walkfiles:
154+
pathname = os.path.join(root, name)
155+
files.append(pathname)
156+
157+
files = normalize_files(files)
158+
outfiles = []
159+
for pathname in files:
160+
basename = os.path.basename(pathname)
161+
extension = file_extension(pathname)
162+
if extension in extensions or basename in extensions:
163+
outfiles.append(pathname)
164+
return outfiles
165+
166+
167+
def get_dates():
168+
years = datetime.datetime.now().year
169+
return '(%s)' % '|'.join((str(year) for year in range(2014, years+1)))
170+
171+
172+
def get_regexs():
173+
regexs = {}
174+
# Search for "YEAR" which exists in the boilerplate, but shouldn't in the real thing
175+
regexs["year"] = re.compile('YEAR')
176+
# get_dates return 2014, 2015, 2016, 2017, or 2018 until the current year as a regex like: "(2014|2015|2016|2017|2018)";
177+
# company holder names can be anything
178+
regexs["date"] = re.compile(get_dates())
179+
# strip #!.* from shell scripts
180+
regexs["shebang"] = re.compile(r"^(#!.*\n)\n*", re.MULTILINE)
181+
return regexs
182+
183+
184+
def main():
185+
regexs = get_regexs()
186+
refs = get_refs()
187+
filenames = get_files(refs.keys())
188+
189+
for filename in filenames:
190+
if not file_passes(filename, refs, regexs):
191+
print(filename, file=sys.stdout)
192+
193+
return 0
194+
195+
196+
if __name__ == "__main__":
197+
sys.exit(main())

hack/boilerplate/boilerplate.py.txt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright YEAR 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.

hack/boilerplate/boilerplate.sh.txt

+13
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/verify-boilerplate.sh

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2018 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+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
20+
21+
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
22+
23+
boilerDir="${KUBE_ROOT}/hack/boilerplate"
24+
boiler="${boilerDir}/boilerplate.py"
25+
26+
files_need_boilerplate=($(${boiler} "$@"))
27+
28+
# Run boilerplate check
29+
if [[ ${#files_need_boilerplate[@]} -gt 0 ]]; then
30+
for file in "${files_need_boilerplate[@]}"; do
31+
echo "Boilerplate header is wrong for: ${file}" >&2
32+
done
33+
34+
exit 1
35+
fi

run_tox.sh

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# Unless required by applicable law or agreed to in writing, software
1212
# distributed under the License is distributed on an "AS IS" BASIS,
1313
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
# See the License for the specific language governing permissions and
14+
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

1717
set -o errexit
@@ -51,4 +51,3 @@ git status
5151
echo "Running tox from the main repo on $TOXENV environment"
5252
# Run the user-provided command.
5353
"${@}"
54-

stream/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python
2+
13
# Copyright 2017 The Kubernetes Authors.
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");

stream/stream.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2-
# not use this file except in compliance with the License. You may obtain
3-
# a copy of the License at
1+
#!/usr/bin/env python
2+
3+
# Copyright 2018 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
48
#
5-
# http://www.apache.org/licenses/LICENSE-2.0
9+
# http://www.apache.org/licenses/LICENSE-2.0
610
#
711
# Unless required by applicable law or agreed to in writing, software
8-
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9-
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10-
# License for the specific language governing permissions and limitations
11-
# under the License.
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.
1216

1317
from . import ws_client
1418

0 commit comments

Comments
 (0)