-
Notifications
You must be signed in to change notification settings - Fork 182
/
update_copyright
executable file
·148 lines (121 loc) · 6.22 KB
/
update_copyright
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python
# Copyright (c) 2008-2024 the MRtrix3 contributors.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Covered Software is provided under this License on an "as is"
# basis, without warranty of any kind, either expressed, implied, or
# statutory, including, without limitation, warranties that the
# Covered Software is free of defects, merchantable, fit for a
# particular purpose or non-infringing.
# See the Mozilla Public License v. 2.0 for more details.
#
# For more details, see http://www.mrtrix.org/.
# note: deal with these warnings properly when we drop support for Python 2:
# pylint: disable=unspecified-encoding
import datetime, os
from collections import namedtuple
COPYRIGHT = [
'Copyright (c) 2008-' + str(datetime.date.today().year) + ' the MRtrix3 contributors.',
'',
'This Source Code Form is subject to the terms of the Mozilla Public',
'License, v. 2.0. If a copy of the MPL was not distributed with this',
'file, You can obtain one at http://mozilla.org/MPL/2.0/.',
'',
'Covered Software is provided under this License on an "as is"',
'basis, without warranty of any kind, either expressed, implied, or',
'statutory, including, without limitation, warranties that the',
'Covered Software is free of defects, merchantable, fit for a',
'particular purpose or non-infringing.',
'See the Mozilla Public License v. 2.0 for more details.',
'',
'For more details, see http://www.mrtrix.org/.']
SearchPath = namedtuple('SearchPath', 'dirname recursive need_shebang comment extensions blacklist')
ProcessFile = namedtuple('ProcessFile', 'path keep_shebang comment')
MRTRIX_ROOT = os.path.abspath(os.path.dirname(__file__))
APP_CPP_PATH = os.path.join(MRTRIX_ROOT, 'core', 'app.cpp')
APP_CPP_STRING = 'const char* COPYRIGHT ='
APP_PY_PATH = os.path.join(MRTRIX_ROOT, 'lib', 'mrtrix3', 'app.py')
APP_PY_STRING = '_DEFAULT_COPYRIGHT ='
SEARCH_PATHS = [
SearchPath('.', False, True, '#', [], ['mrtrix-mrview.desktop']),
SearchPath('bin', False, True, '#', [], []),
SearchPath('bin', False, True, '#', ['.py'], []),
SearchPath('cmd', False, False, '*', ['.cpp'], []),
SearchPath('core', True, False, '*', ['.cpp', '.h'], [os.path.join('file', 'json.h'), os.path.join('file', 'nifti1.h'), 'version.h']),
SearchPath('docs', False, True, '#', [], []),
SearchPath('matlab', True, False, '%', ['.m'], []),
SearchPath('lib', True, False, '#', ['.py'], [os.path.join('mrtrix3', '_version.py')]),
SearchPath('share', True, False, '#', ['.txt'], []),
SearchPath('src', True, False, '*', ['.cpp', '.h'], []),
SearchPath(os.path.join('testing', 'cmd'), False, False, '*', ['.cpp'], []),
SearchPath(os.path.join('testing', 'src'), True, False, '*', ['.cpp', '.h'], [])]
def main():
def has_shebang(filepath):
try:
with open(filepath, 'r') as shebang_file:
return shebang_file.read(2) == '#!'
except UnicodeDecodeError:
return False
filelist = []
for path in SEARCH_PATHS:
start = os.path.join(MRTRIX_ROOT, path.dirname)
if path.recursive:
for root, _, files in os.walk(start):
for filename in sorted(files):
if (not path.extensions) or any(filename.endswith(extension) for extension in path.extensions):
fullpath = os.path.join(root, filename)
relpath = os.path.relpath(fullpath, start)
if (relpath not in path.blacklist) and (not path.need_shebang or has_shebang(fullpath)):
filelist.append(ProcessFile(fullpath, path.need_shebang, path.comment))
else:
for filename in sorted(os.listdir(start)):
if (not path.extensions) or any(filename.endswith(extension) for extension in path.extensions):
fullpath = os.path.join(start, filename)
if os.path.isfile(fullpath) and (filename not in path.blacklist) and (not path.need_shebang or has_shebang(fullpath)):
filelist.append(ProcessFile(fullpath, path.need_shebang, path.comment))
for item in filelist:
with open(item.path, 'r') as infile:
data = infile.readlines()
if not data:
continue
if item.keep_shebang:
shebang = data[0]
data = data[next(index for index, value in enumerate(data[1:]) if value.strip())+1:]
else:
shebang = None
if item.comment == '*':
# Crop existing copyright notice
data = data[next(index for index, value in enumerate(data) if not any(value.lstrip().startswith(prefix) for prefix in ['/*', '*', '*/'])):]
# Insert new copyright notice
data = ['/* ' + COPYRIGHT[0] + '\n'] + [' *' + (' ' + line if line else '') + '\n' for line in COPYRIGHT[1:]] + [' */\n'] + data
else:
# Crop existing copyright notice
data = data[next(index for index, value in enumerate(data) if not value.startswith(item.comment)):]
# Insert new copyright notice
data = [item.comment + (' ' + line if line else '') + '\n' for line in COPYRIGHT] + data
with open(item.path, 'w') as outfile:
if shebang:
outfile.write(shebang + '\n')
outfile.write(''.join(data))
with open(APP_CPP_PATH, 'r') as app_cpp_file:
data = app_cpp_file.readlines()
first_line = next(index for index, value in enumerate(data) if value.strip().startswith(APP_CPP_STRING)) + 1
indent = ' ' * (len(data[first_line]) - len(data[first_line].lstrip()))
with open(APP_CPP_PATH, 'w') as app_cpp_file:
app_cpp_file.write(''.join(data[:first_line]))
for line in COPYRIGHT[:-1]:
app_cpp_file.write(indent + '"' + line.replace('"', '\\"') + '\\n"\n')
app_cpp_file.write(indent + '"' + COPYRIGHT[-1].replace('"', '\\"') + '\\n";\n')
app_cpp_file.write(''.join(data[first_line+len(COPYRIGHT):]))
with open(APP_PY_PATH, 'r') as app_py_file:
data = app_py_file.readlines()
first_line = next(index for index, value in enumerate(data) if value.startswith(APP_PY_STRING)) + 1
with open(APP_PY_PATH, 'w') as app_py_file:
app_py_file.write(''.join(data[:first_line]))
app_py_file.write('\'\'\'' + '\n'.join(COPYRIGHT) + '\'\'\'\n')
app_py_file.write(''.join(data[first_line+len(COPYRIGHT):]))
if __name__ == '__main__':
main()