-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathcopyright.py
executable file
·93 lines (63 loc) · 2.04 KB
/
copyright.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#! /usr/bin/env python
"""
Helpers to format copyright headers.
"""
import sys
from langkit.utils import SourcePostProcessor
BOX_SIZE = 78
TEXT_SIZE = 72
copyright = 'Copyright (C) 2014-2022, AdaCore'
header = f"""
{copyright}
SPDX-License-Identifier: Apache-2.0
""".strip().splitlines()
def concat(header, source):
return '\n'.join(header) + '\n\n' + source.lstrip()
def format_start(prefix):
result = [prefix.strip()]
for line in header:
result.append(f"{prefix}{line}")
result.append(prefix.strip())
return result
class AdaPostProcessor(SourcePostProcessor):
def process(self, source):
return concat(format_start('-- '), source)
class PythonPostProcessor(SourcePostProcessor):
def process(self, source):
# If there is a shebang, add the copyright header after it
if source.startswith('#!'):
shebang, rest = source.split('\n', 1)
shebang += '\n\n'
else:
shebang = ''
rest = source
return shebang + concat(format_start('# '), rest)
def format_tags(source, opening, closing):
prefix = " *" + " " * (len(opening) - 1)
result = [opening]
for i, line in enumerate(header):
result.append(prefix + line)
result.append(closing)
return concat(result, source)
class OCamlPostProcessor(SourcePostProcessor):
def process(self, source):
return format_tags(source, "(*", " *)")
class CCppPostProcessor(SourcePostProcessor):
def process(self, source):
return format_tags(source, "/*", " */")
def run(argv):
for filename in argv:
_, ext = filename.rsplit('.', 1)
cls = {
'ads': AdaPostProcessor,
'adb': AdaPostProcessor,
'c': CCppPostProcessor,
'h': CCppPostProcessor,
'py': PythonPostProcessor,
}[ext]
with open(filename, 'rb') as f:
content = f.read()
with open(filename, 'wb') as f:
f.write(cls().process(content))
if __name__ == '__main__':
run(sys.argv[1:])