File: add_license.py

package info (click to toggle)
emscripten 3.1.6~dfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 114,112 kB
  • sloc: ansic: 583,052; cpp: 391,943; javascript: 79,361; python: 54,180; sh: 49,997; pascal: 4,658; makefile: 3,426; asm: 2,191; lisp: 1,869; ruby: 488; cs: 142
file content (112 lines) | stat: -rwxr-xr-x 3,412 bytes parent folder | download
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
#!/usr/bin/env python3
# Copyright 2018 The Emscripten Authors.  All rights reserved.
# Emscripten is available under two separate licenses, the MIT license and the
# University of Illinois/NCSA Open Source License.  Both these licenses can be
# found in the LICENSE file.

"""Add or verify emscripten license header in source files."""

import sys
import os
import subprocess

script_dir = os.path.dirname(os.path.abspath(__file__))
__rootpath__ = os.path.dirname(script_dir)

cpp_license = '''\
// Copyright %s The Emscripten Authors.  All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License.  Both these licenses can be
// found in the LICENSE file.
'''

py_license = '''\
# Copyright %s The Emscripten Authors.  All rights reserved.
# Emscripten is available under two separate licenses, the MIT license and the
# University of Illinois/NCSA Open Source License.  Both these licenses can be
# found in the LICENSE file.
'''

c_license = '''\
/*
 * Copyright %s The Emscripten Authors.  All rights reserved.
 * Emscripten is available under two separate licenses, the MIT license and the
 * University of Illinois/NCSA Open Source License.  Both these licenses can be
 * found in the LICENSE file.
 */
'''

c_license_base = '''\
/*
 * Copyright %s The Emscripten Authors.  All rights reserved.
 * Emscripten is available under two separate licenses, the MIT license and the
 * University of Illinois/NCSA Open Source License.  Both these licenses can be
 * found in the LICENSE file.
 *
'''

exclude_filenames = [
    'system/include/',
    'system/lib/libc/musl/',
    'system/lib/html5/dom_pk_codes.c',
    'system/lib/dlmalloc.c',
    'third_party/',
    'tests/optimizer/',
    'site/source/_themes/',
]

exclude_contents = ['Copyright', 'LICENSE.TXT', 'PUBLIC DOMAIN']


def process_file(filename):
  if any(filename.startswith(ex) for ex in exclude_filenames):
    return
  ext = os.path.splitext(filename)[1]
  if ext not in ('.py', '.c', '.cpp', '.h', '.js'):
    return
  with open(filename) as f:
    contents = f.read()
  header = '\n'.join(contents.splitlines()[:30])
  if any(ex in header for ex in exclude_contents):
    return
  output = subprocess.check_output(['git', 'log', '--pretty=format:%cd', '--date=format:%Y', filename])
  year = output.splitlines()[-1].split()[0]
  print(filename)
  with open(filename, 'w') as f:
    if ext == '.py':
      if contents.startswith('#!'):
        line1, rest = contents.split('\n', 1)
        f.write(line1 + '\n')
        contents = rest
      f.write(py_license % year)
      if not contents.startswith('\n'):
        f.write('\n')
    elif ext in ('.c', '.h'):
      f.write(c_license % year)
      if not contents.startswith('\n'):
        f.write('\n')
    elif ext in ('.cpp', '.js'):
        if contents.startswith('/*\n'):
          contents = contents[3:]
          f.write(c_license_base % year)
        else:
          f.write(cpp_license % year)
          if not contents.startswith('\n'):
            f.write('\n')
    else:
      assert False
    f.write(contents)


def main():
  os.chdir(__rootpath__)
  filenames = sys.argv[1:]
  if not filenames:
    filenames = subprocess.check_output(['git', 'ls-files']).splitlines()
  for filename in filenames:
    process_file(filename)
  return 0


if __name__ == '__main__':
  sys.exit(main())