changeLicense.py: support new license style

Add support for the new licenses (that are commented out linewise via
//) to changeLicense.py that until know just knew about the old-style
licenses commented as /*block*/.

Change-Id: If29c4a49e210cf7516ae93fb1b7ef7e9f5a51f34
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
This commit is contained in:
Sami Shalayel
2022-12-19 14:00:30 +01:00
parent f0556b08b8
commit 5d6765c08e

View File

@@ -1,4 +1,4 @@
#!/usr/bin/python #!/usr/bin/env python3
import sys import sys
import os import os
@@ -6,19 +6,47 @@ if not len(sys.argv) >= 3:
print("Usage: %s license files..." % os.path.basename(sys.argv[0])) print("Usage: %s license files..." % os.path.basename(sys.argv[0]))
sys.exit() 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] licenseFileName = sys.argv[1]
licenseText = "" licenseText = ""
with open(licenseFileName, 'r') as f: with open(licenseFileName, 'r') as f:
licenseText = f.read() licenseText = f.read().splitlines(keepends=True)
licenseText = licenseText[0:licenseText.find('*/')] start, stop = detectLicense(licenseText)
licenseText = licenseText[start:stop]
files = sys.argv[2:] files = sys.argv[2:]
for fileName in files: for fileName in files:
with open(fileName, 'r') as f: with open(fileName, 'r') as f:
text = f.read() text = f.read().splitlines(keepends=True)
oldEnd = text.find('*/') start, stop = detectLicense(text)
if oldEnd == -1: if stop == -1:
oldEnd = 0 stop = 0
text = licenseText + text[oldEnd:]
with open(fileName, 'w') as f: with open(fileName, 'w') as f:
f.write(text) f.writelines(text[0:start])
f.writelines(licenseText)
f.writelines(text[stop:])