blob: 58ae6f523e4adcce5e34e826ee141dd3cac1c649 (
plain)
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
|
#!/usr/bin/env python3
import sys
import os
if not len(sys.argv) >= 3:
print("Usage: %s license files..." % os.path.basename(sys.argv[0]))
sys.exit()
def detectLicense(fileContent: list[str]) -> tuple[int, int]:
for i, line in enumerate(fileContent):
if fileContent[i].startswith('//'):
"""
new license style (commented with //): return first noncomment-line
after commented license
"""
for j, line in enumerate(fileContent[i:]):
if not line.startswith('//'):
return (i, i + j)
return (i, len(fileContent))
if fileContent[i].startswith('/*'):
"""
old license style (commented as /*block*/): return line after the
block-comment
"""
for j, line in enumerate(fileContent[i:]):
if line.endswith('*/\n'):
return (i, i + j + 1)
raise ValueError("Encountered EOF while in the license comment")
# else case: probably the generated codes "#line ..."
raise ValueError("Encountered EOF without finding any license")
licenseFileName = sys.argv[1]
licenseText = ""
with open(licenseFileName, 'r') as f:
licenseText = f.read().splitlines(keepends=True)
start, stop = detectLicense(licenseText)
licenseText = licenseText[start:stop]
files = sys.argv[2:]
for fileName in files:
with open(fileName, 'r') as f:
text = f.read().splitlines(keepends=True)
start, stop = detectLicense(text)
if stop == -1:
stop = 0
with open(fileName, 'w') as f:
f.writelines(text[0:start])
f.writelines(licenseText)
f.writelines(text[stop:])
|