-
Notifications
You must be signed in to change notification settings - Fork 2
/
add-header-to-file.py
executable file
·69 lines (55 loc) · 1.9 KB
/
add-header-to-file.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
#!/usr/bin/python3
# Copyright (c) the JSVM authors
# https://github.com/ntrrgc/jsvm
#
# 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/.
import sys
license_text = '''
Copyright (c) the JSVM authors
https://github.com/ntrrgc/jsvm
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/.
'''.strip().split('\n')
def license_comment(lang):
if lang in ('py', 'sh', 'txt'):
return [
('# ' + line).rstrip()
for line in license_text
]
elif lang in ('js', 'java', 'kt', 'c', 'h', 'cpp', 'gradle'):
return ['/* ' + license_text[0]] + \
[
(' * ' + line).rstrip()
for line in license_text[1:-1]
] + [' * ' + license_text[-1] + ' */']
else:
raise RuntimeError('Unknown language: ' + lang)
def add_header(path):
with open(path, 'r+') as f:
text = f.read()
if text.strip() == '':
return
if 'Mozilla Public License' not in text:
lines = text.split('\n')
lang = path.split('.')[-1]
if lines[0].startswith('#!'): # shebang
add_before_line = 1
if 'python' in lines[0]:
lang = 'py'
else:
add_before_line = 0
lines[add_before_line:add_before_line] = \
license_comment(lang) + ['']
text = '\n'.join(lines)
f.seek(0)
f.write(text)
f.truncate()
if __name__ == "__main__":
if len(sys.argv) == 1:
print('Usage: %s <file>' % sys.argv[0])
sys.exit(0)
for path in sys.argv[1:]:
add_header(path)