-
Notifications
You must be signed in to change notification settings - Fork 90
/
applyheader.py
61 lines (47 loc) · 1.97 KB
/
applyheader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import re
import os
coding_text = "# -*- coding: utf-8 -*-\n"
coding_re = "^"+re.escape(coding_text)
copyright_re = ("\"\"\"\nCopyright 2019 DScribe developers\n")
copyright_text = """\"\"\"Copyright 2019 DScribe developers
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
\"\"\"
"""
def match_func(filename):
return filename.endswith(".py")
def crawl():
# folders = ["./dscribe/utils"]
# folders = ["./dscribe/kernels"]
# folders = ["./dscribe/core"]
folders = ["./dscribe/descriptors"]
for folder in folders:
for path, dirs, files in os.walk(folder):
for f in files:
filename = os.path.join(path, f)
if match_func(f):
with open(filename, "r+") as fh:
text = fh.read()
pos = 0
# Try to find coding.
match = re.search(coding_re, text)
if match is None:
text = coding_text + text
else:
pos = match.end()
# Try to find copyright. If not present add after coding
match = re.search(copyright_re, text)
if match is None:
text = text[0:pos] + copyright_text + text[pos:]
# Write new contents
fh.seek(0)
fh.write(text)
fh.truncate()
crawl()