Skip to content

Commit 0e48873

Browse files
authored
Merge pull request #133 from oracle/ap/boilerplate
Add copyright boilerplate check to CI
2 parents 9419964 + a55948e commit 0e48873

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+566
-32
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ bin/
1515
*.dll
1616
*.so
1717
*.dylib
18+
*.pyc
1819

1920
# Test binary, build with `go test -c`
2021
*.test
@@ -40,4 +41,4 @@ vendors.txt
4041
# Wercker
4142
.wercker/
4243

43-
.idea/
44+
.idea/

Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright 2018 Oracle and/or its affiliates. All rights reserved.
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+
115
REGISTRY := wcr.io/oracle
216
PKG := github.com/oracle/oci-cloud-controller-manager
317
BIN := oci-cloud-controller-manager

cmd/oci-cloud-controller-manager/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
1+
// Copyright 2017 Oracle and/or its affiliates. All rights reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright YEAR Oracle and/or its affiliates. All rights reserved.
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright YEAR Oracle and/or its affiliates. All rights reserved.
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright YEAR Oracle and/or its affiliates. All rights reserved.
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.py

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
#!/usr/bin/env python
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 difflib
21+
import glob
22+
import os
23+
import re
24+
import sys
25+
26+
parser = argparse.ArgumentParser()
27+
parser.add_argument(
28+
"filenames",
29+
help="list of files to check, all files if unspecified",
30+
nargs='*')
31+
32+
rootdir = os.path.dirname(__file__) + "/../../"
33+
rootdir = os.path.abspath(rootdir)
34+
parser.add_argument(
35+
"--rootdir", default=rootdir, help="root directory to examine")
36+
37+
default_boilerplate_dir = os.path.join(rootdir, "hack/boilerplate")
38+
parser.add_argument(
39+
"--boilerplate-dir", default=default_boilerplate_dir)
40+
41+
parser.add_argument(
42+
"-v", "--verbose",
43+
help="give verbose output regarding why a file does not pass",
44+
action="store_true")
45+
46+
args = parser.parse_args()
47+
48+
verbose_out = sys.stderr if args.verbose else open("/dev/null", "w")
49+
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+
65+
def file_passes(filename, refs, regexs):
66+
try:
67+
f = open(filename, 'r')
68+
except Exception as exc:
69+
print("Unable to open %s: %s" % (filename, exc), file=verbose_out)
70+
return False
71+
72+
data = f.read()
73+
f.close()
74+
75+
basename = os.path.basename(filename)
76+
extension = file_extension(filename)
77+
if extension != "":
78+
ref = refs[extension]
79+
else:
80+
ref = refs[basename]
81+
82+
# remove build tags from the top of Go files
83+
if extension == "go":
84+
p = regexs["go_build_constraints"]
85+
(data, found) = p.subn("", data, 1)
86+
87+
# remove shebang from the top of shell files
88+
if extension == "sh":
89+
p = regexs["shebang"]
90+
(data, found) = p.subn("", data, 1)
91+
92+
data = data.splitlines()
93+
94+
# if our test file is smaller than the reference it surely fails!
95+
if len(ref) > len(data):
96+
print('File %s smaller than reference (%d < %d)' %
97+
(filename, len(data), len(ref)),
98+
file=verbose_out)
99+
return False
100+
101+
# trim our file to the same number of lines as the reference file
102+
data = data[:len(ref)]
103+
104+
p = regexs["year"]
105+
for d in data:
106+
if p.search(d):
107+
print('File %s is missing the year' % filename, file=verbose_out)
108+
return False
109+
110+
# Replace all occurrences of the regex "2017|2016|2015|2014" with "YEAR"
111+
p = regexs["date"]
112+
for i, d in enumerate(data):
113+
(data[i], found) = p.subn('YEAR', d)
114+
if found != 0:
115+
break
116+
117+
# if we don't match the reference at this point, fail
118+
if ref != data:
119+
print("Header in %s does not match reference, diff:" % filename, file=verbose_out)
120+
if args.verbose:
121+
print(file=verbose_out)
122+
for line in difflib.unified_diff(ref, data, 'reference', filename, lineterm=''):
123+
print(line, file=verbose_out)
124+
print(file=verbose_out)
125+
return False
126+
127+
return True
128+
129+
130+
def file_extension(filename):
131+
return os.path.splitext(filename)[1].split(".")[-1].lower()
132+
133+
134+
skipped_dirs = [
135+
'.git',
136+
".wercker",
137+
"vendor",
138+
139+
# Imported from Kubernetes maintaining origional copyright header
140+
"hack/boilerplate/boilerplate.py",
141+
"hack/boilerplate/boilerplate_test.py",
142+
"hack/boilerplate/test",
143+
"hack/verify-boilerplate.sh",
144+
]
145+
146+
147+
def normalize_files(files):
148+
newfiles = []
149+
for pathname in files:
150+
if any(x in pathname for x in skipped_dirs):
151+
continue
152+
newfiles.append(pathname)
153+
for i, pathname in enumerate(newfiles):
154+
if not os.path.isabs(pathname):
155+
newfiles[i] = os.path.join(args.rootdir, pathname)
156+
return newfiles
157+
158+
159+
def get_files(extensions):
160+
files = []
161+
if len(args.filenames) > 0:
162+
files = args.filenames
163+
else:
164+
for root, dirs, walkfiles in os.walk(args.rootdir):
165+
# don't visit certain dirs. This is just a performance improvement
166+
# as we would prune these later in normalize_files(). But doing it
167+
# cuts down the amount of filesystem walking we do and cuts down
168+
# the size of the file list
169+
for d in skipped_dirs:
170+
if d in dirs:
171+
dirs.remove(d)
172+
173+
for name in walkfiles:
174+
pathname = os.path.join(root, name)
175+
files.append(pathname)
176+
177+
files = normalize_files(files)
178+
outfiles = []
179+
for pathname in files:
180+
basename = os.path.basename(pathname)
181+
extension = file_extension(pathname)
182+
if extension in extensions or basename in extensions:
183+
outfiles.append(pathname)
184+
return outfiles
185+
186+
187+
def get_regexs():
188+
regexs = {}
189+
# Search for "YEAR" which exists in the boilerplate, but shouldn't in the real thing
190+
regexs["year"] = re.compile('YEAR')
191+
# dates can be 2017, 2018; company holder names can be anything
192+
regexs["date"] = re.compile('(2017|2018)')
193+
# strip // +build \n\n build constraints
194+
regexs["go_build_constraints"] = re.compile(r"^(// \+build.*\n)+\n", re.MULTILINE)
195+
# strip #!.* from shell scripts
196+
regexs["shebang"] = re.compile(r"^(#!.*\n)\n*", re.MULTILINE)
197+
return regexs
198+
199+
200+
def main():
201+
regexs = get_regexs()
202+
refs = get_refs()
203+
filenames = get_files(refs.keys())
204+
205+
for filename in filenames:
206+
if not file_passes(filename, refs, regexs):
207+
print(filename, file=sys.stdout)
208+
209+
return 0
210+
211+
212+
if __name__ == "__main__":
213+
sys.exit(main())
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright YEAR Oracle and/or its affiliates. All rights reserved.
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+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright YEAR Oracle and/or its affiliates. All rights reserved.
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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python
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)