Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/tools/maint/add_license.py
4150 views
1
#!/usr/bin/env python3
2
# Copyright 2018 The Emscripten Authors. All rights reserved.
3
# Emscripten is available under two separate licenses, the MIT license and the
4
# University of Illinois/NCSA Open Source License. Both these licenses can be
5
# found in the LICENSE file.
6
7
"""Add or verify emscripten license header in source files."""
8
9
import sys
10
import os
11
import subprocess
12
13
script_dir = os.path.dirname(os.path.abspath(__file__))
14
__rootpath__ = os.path.dirname(os.path.dirname(script_dir))
15
16
cpp_license = '''\
17
// Copyright %s The Emscripten Authors. All rights reserved.
18
// Emscripten is available under two separate licenses, the MIT license and the
19
// University of Illinois/NCSA Open Source License. Both these licenses can be
20
// found in the LICENSE file.
21
'''
22
23
py_license = '''\
24
# Copyright %s The Emscripten Authors. All rights reserved.
25
# Emscripten is available under two separate licenses, the MIT license and the
26
# University of Illinois/NCSA Open Source License. Both these licenses can be
27
# found in the LICENSE file.
28
'''
29
30
c_license = '''\
31
/*
32
* Copyright %s The Emscripten Authors. All rights reserved.
33
* Emscripten is available under two separate licenses, the MIT license and the
34
* University of Illinois/NCSA Open Source License. Both these licenses can be
35
* found in the LICENSE file.
36
*/
37
'''
38
39
c_license_base = '''\
40
/*
41
* Copyright %s The Emscripten Authors. All rights reserved.
42
* Emscripten is available under two separate licenses, the MIT license and the
43
* University of Illinois/NCSA Open Source License. Both these licenses can be
44
* found in the LICENSE file.
45
*
46
'''
47
48
exclude_filenames = [
49
'system/include/',
50
'system/lib/libc/musl/',
51
'system/lib/html5/dom_pk_codes.c',
52
'system/lib/dlmalloc.c',
53
'third_party/',
54
'test/optimizer/',
55
'site/source/_themes/',
56
]
57
58
exclude_contents = ['Copyright', 'LICENSE.TXT', 'PUBLIC DOMAIN']
59
60
61
def process_file(filename):
62
if any(filename.startswith(ex) for ex in exclude_filenames):
63
return
64
ext = os.path.splitext(filename)[1]
65
if ext not in ('.py', '.c', '.cpp', '.h', '.js'):
66
return
67
with open(filename) as f:
68
contents = f.read()
69
header = '\n'.join(contents.splitlines()[:30])
70
if any(ex in header for ex in exclude_contents):
71
return
72
output = subprocess.check_output(['git', 'log', '--pretty=format:%cd', '--date=format:%Y', filename])
73
year = output.splitlines()[-1].split()[0]
74
print(filename)
75
with open(filename, 'w') as f:
76
if ext == '.py':
77
if contents.startswith('#!'):
78
line1, rest = contents.split('\n', 1)
79
f.write(line1 + '\n')
80
contents = rest
81
f.write(py_license % year)
82
if not contents.startswith('\n'):
83
f.write('\n')
84
elif ext in ('.c', '.h'):
85
f.write(c_license % year)
86
if not contents.startswith('\n'):
87
f.write('\n')
88
elif ext in ('.cpp', '.js'):
89
if contents.startswith('/*\n'):
90
contents = contents[3:]
91
f.write(c_license_base % year)
92
else:
93
f.write(cpp_license % year)
94
if not contents.startswith('\n'):
95
f.write('\n')
96
else:
97
assert False
98
f.write(contents)
99
100
101
def main():
102
os.chdir(__rootpath__)
103
filenames = sys.argv[1:]
104
if not filenames:
105
filenames = subprocess.check_output(['git', 'ls-files']).splitlines()
106
for filename in filenames:
107
process_file(filename)
108
return 0
109
110
111
if __name__ == '__main__':
112
sys.exit(main())
113
114