-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathadd-license
executable file
·62 lines (49 loc) · 1.63 KB
/
add-license
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
#!/usr/bin/python3
# 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 = '''
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', 'coffee', 'sh'):
return [
'# ' + line
for line in license_text
]
elif lang == 'js':
return [
'// ' + line
for line in license_text
]
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)