-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_license_headers.py
112 lines (95 loc) · 2.96 KB
/
update_license_headers.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'''~
M-Trix - Social Experiments Application.
Copyright (C) 2015-2016 The M-Trix authors.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
~'''
import fnmatch
import os
import re
license_txt = """~
M-Trix - Social Experiments Application.
Copyright (C) 2015-2016 The M-Trix authors.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
~"""
# get the directory of the script
project_dir = os.path.dirname(os.path.realpath(__file__))
# choose files types to include
# choose directories to exclude from search
includes = ['*.java','*.sql', '*.xhtml']
excludes = ['AUTHORS',
'.gitignore',
'*.ogg'
'*.mp3',
'*.db',
'*.jsp',
'*.gif',
'*.png',
'*.jpg',
'*.xls',
'*.properties',
'*.MF',
'*.xml',
'*.c',
'*.jar'
'*.md',
'*.png',
'*.svg',
'*.ico',
'*.sh',
'*.icns',
'*.spec',
'.git/*',
'LabCulturaGame/build/*',
'LabCulturaGame/dist/*',
'LabCulturaGame/nbproject/*',
'LabCulturaGame/test/*'
]
# transform glob patterns to regular expressions
includes = r'|'.join([fnmatch.translate(x) for x in includes])
excludes = r'|'.join([fnmatch.translate(x) for x in excludes]) or r'$.'
def get_files(start_dir, includes, excludes):
# use os.walk to recursively dig down into the project directory
match_files = []
for root, dirs, files in os.walk(start_dir):
if not re.search(excludes, root):
files = [f for f in files if re.search(includes, f) and not re.search(excludes, f)]
files = [os.path.join(root, f) for f in files]
match_files += files
return match_files
def write_header(file_name, license_txt):
# find and replace license header
# or add new header if not existing
file_type = os.path.splitext(file_name)[-1]
if file_type == '.py':
comment = ["'''\n","\n'''\n"]
pattern = re.compile("'''~(.+?)~'''", re.DOTALL|re.MULTILINE)
if file_type == '.c' or '.java' or '.sql':
comment = ['/*', '*/\n']
pattern = re.compile('/[*]~(.+?)~[*]/', re.DOTALL|re.MULTILINE)
if file_type == '.xhtml':
comment = ['<!--', '-->\n']
pattern = re.compile('<!--~(.+?)~-->', re.DOTALL|re.MULTILINE)
license_txt = comment[0] + license_txt + comment[1]
with file(file_name, 'r') as original:
data = original.read()
with file(file_name, 'w') as modified:
if re.findall(pattern, data):
# if header already exists, then update, but dont add the last newline.
modified.write(re.sub(pattern, license_txt[:-1], data))
modified.close()
else:
# else write the license header
modified.write(license_txt + data)
modified.close()
def update_header():
# Add a license/docstring header to selected files
match_files = get_files(project_dir, includes, excludes)
for f in match_files:
print f
for f in match_files:
write_header(f, license_txt)
if __name__ == '__main__':
# run update_header() to add headers to find files
update_header()