blob: 0304992132bf8f1a81e55540d9c863c51966a2ed [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001# Copyright 2017-present Open Networking Foundation
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
15import sys
16import os
17
18COPYRIGHTS = {
19 "slash": """
20/*
21 * Copyright 2017-present Open Networking Foundation
22
23 * Licensed under the Apache License, Version 2.0 (the "License");
24 * you may not use this file except in compliance with the License.
25 * You may obtain a copy of the License at
26
27 * http://www.apache.org/licenses/LICENSE-2.0
28
29 * Unless required by applicable law or agreed to in writing, software
30 * distributed under the License is distributed on an "AS IS" BASIS,
31 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32 * See the License for the specific language governing permissions and
33 * limitations under the License.
34 */
35""",
36 "hash": """
37# Copyright 2017-present Open Networking Foundation
38#
39# Licensed under the Apache License, Version 2.0 (the "License");
40# you may not use this file except in compliance with the License.
41# You may obtain a copy of the License at
42#
43# http://www.apache.org/licenses/LICENSE-2.0
44#
45# Unless required by applicable law or agreed to in writing, software
46# distributed under the License is distributed on an "AS IS" BASIS,
47# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
48# See the License for the specific language governing permissions and
49# limitations under the License.
50""",
51 "html": """
52<!--
53Copyright 2017-present Open Networking Foundation
54
55Licensed under the Apache License, Version 2.0 (the "License");
56you may not use this file except in compliance with the License.
57You may obtain a copy of the License at
58
59http://www.apache.org/licenses/LICENSE-2.0
60
61Unless required by applicable law or agreed to in writing, software
62distributed under the License is distributed on an "AS IS" BASIS,
63WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
64See the License for the specific language governing permissions and
65limitations under the License.
66-->
67""",
68 "jinja": """
69{#
70Copyright 2017-present Open Networking Foundation
71
72Licensed under the Apache License, Version 2.0 (the "License");
73you may not use this file except in compliance with the License.
74You may obtain a copy of the License at
75
76http://www.apache.org/licenses/LICENSE-2.0
77
78Unless required by applicable law or agreed to in writing, software
79distributed under the License is distributed on an "AS IS" BASIS,
80WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
81See the License for the specific language governing permissions and
82limitations under the License.
83#}
84""",
85 "ini": """
86;Copyright 2017-present Open Networking Foundation
87;
88;Licensed under the Apache License, Version 2.0 (the "License");
89;you may not use this file except in compliance with the License.
90;You may obtain a copy of the License at
91;
92;http://www.apache.org/licenses/LICENSE-2.0
93;
94;Unless required by applicable law or agreed to in writing, software
95;distributed under the License is distributed on an "AS IS" BASIS,
96;WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
97;See the License for the specific language governing permissions and
98;limitations under the License.
99""",
100}
101
102EXT_MAPPING = {
103 ".js": COPYRIGHTS["slash"],
104 ".ts": COPYRIGHTS["slash"],
105 ".scss": COPYRIGHTS["slash"],
106 ".css": COPYRIGHTS["slash"],
107 ".gradle": COPYRIGHTS["slash"],
108
109 "docker": COPYRIGHTS["hash"],
110 ".py": COPYRIGHTS["hash"],
111 ".model": COPYRIGHTS["hash"], # attic files
112 ".sh": COPYRIGHTS["hash"],
113 ".yaml": COPYRIGHTS["hash"],
114 ".yml": COPYRIGHTS["hash"],
115 ".m4": COPYRIGHTS["hash"],
116 ".sql": COPYRIGHTS["hash"],
117
118 ".html": COPYRIGHTS["html"],
119 ".j2": COPYRIGHTS["jinja"],
120 ".ini": COPYRIGHTS["ini"],
121}
122
123def get_copyright(file):
124 name, ext = os.path.splitext(file)
125
126 if "Dockerfile" in name:
127 return EXT_MAPPING["docker"]
128 try:
129 return EXT_MAPPING[ext]
130 except KeyError, e:
131 print "Missing copyright for file of type: %s" % ext
132 return None
133
134def add_copyright(file):
135 with open(file, 'r') as original: data = original.read()
136 if not "Copyright 2017-present Open Networking Foundation" in data:
137 print "Adding copyright to: %s" % file
138
139 copy = get_copyright(file)
140 if copy:
141 with open(file, 'w') as modified: modified.write(copy + "\n\n" + data)
142 return
143
144def get_files_ignore_by_git(root):
145 # NOTE this is not perfect, some file will still be copyrighted, but we save some time
146 if root == ".gitignore":
147 gitignore = root
148 else:
149 gitignore = os.path.join(root, ".gitignore")
150
151 exclusion_list = []
152 if os.path.exists(gitignore):
153 for line in open(gitignore).readlines():
154 if not "#" in line:
155 line = line.strip()
156 if line.endswith("/"):
157 line = line.replace("/", "")
158 exclusion_list.append(line)
159 return exclusion_list
160
161def should_skip(entry):
162
163 # do not skip directories
164 if os.path.isdir(entry):
165 return False
166
167 if "LICENSE.txt" in entry \
168 or ".git" in entry \
169 or ".idea" in entry:
170 return True
171
172 name, ext = os.path.splitext(entry)
173 if not ext or ext == "" \
174 or ext ==".pyc" \
175 or ext == ".txt" \
176 or ext == ".in" \
177 or ext == ".crt" \
178 or ext == ".unused" \
179 or ext == ".list" \
180 or ext == ".README" \
181 or ext == ".json" \
182 or ext == ".log" \
183 or ext == ".asc" \
184 or ext == ".dot" \
185 or ext == ".do" \
186 or ext == ".template" \
187 or ext == ".svg" \
188 or ext == ".ttf" \
189 or ext == ".woff" \
190 or ext == ".woof2" \
191 or ext == ".eot" \
192 or ext == ".md" \
193 or ext == ".png" \
194 or ext == ".PNG" \
195 or ext == ".jpg" \
196 or ext == ".gif" \
197 or ext == ".ico" \
198 or ext == ".conf"\
199 or ext == ".key" \
200 or ext == ".proto" \
201 or ext == ".xproto" \
202 or ext == ".xtarget" \
203 or ext == ".otf" \
204 or ext == ".desc":
205 return True
206 return False
207
208def recursive_iterate_dirs(source, apply_copyright=True):
209 # print "Iteranting on: %s" % source
210 # skipping files in the gitignore
211 gitignored = get_files_ignore_by_git(source)
212 entries = []
213 for entry in os.listdir(source):
214
215 if entry in gitignored:
216 # print "Skipping because gitignored: %s" % entry
217 continue
218
219 entry = os.path.join(source, entry)
220 if should_skip(entry):
221 # print "Skipping: %s" % entry
222 continue
223
224 if os.path.isdir(entry):
225 entries.append(recursive_iterate_dirs(entry, apply_copyright))
226 elif os.path.isfile(entry):
227 entries.append(entry)
228 if apply_copyright is True:
229 add_copyright(entry)
230 return entries
231
232def flatten(aList):
233 t = []
234 for i in aList:
235 if not isinstance(i, list):
236 t.append(i)
237 else:
238 t.extend(flatten(i))
239 return t
240
241def list_file_types(source):
242 file_types = []
243 files = flatten(recursive_iterate_dirs(source, apply_copyright=False))
244 for entry in files:
245 name, ext = os.path.splitext(entry)
246 if not ext in file_types:
247 file_types.append(ext)
248 print file_types
249
250def main():
251 if len(sys.argv) < 2:
252 raise Exception("You must provide a path to the source folder as arguments to the script")
253 source_root = os.path.abspath(os.path.join(os.getcwd(), sys.argv[1]))
254 if not os.path.exists(source_root):
255 raise Exception("You must provide an existing the source folder")
256 if len(sys.argv) == 3:
257 list_file_types(source_root)
258 else:
259 recursive_iterate_dirs(source_root)
260
261
262if __name__ == "__main__":
263 main()