From 5d6765c08e7902ce777db64a16ac6107ca0ee496 Mon Sep 17 00:00:00 2001 From: Sami Shalayel Date: Mon, 19 Dec 2022 14:00:30 +0100 Subject: [PATCH] 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: Reviewed-by: Ulf Hermann --- src/libs/qmljs/parser/changeLicense.py | 46 +++++++++++++++++++++----- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/src/libs/qmljs/parser/changeLicense.py b/src/libs/qmljs/parser/changeLicense.py index b10ebbc938e..58ae6f523e4 100755 --- a/src/libs/qmljs/parser/changeLicense.py +++ b/src/libs/qmljs/parser/changeLicense.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 import sys import os @@ -6,19 +6,47 @@ 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() - licenseText = licenseText[0:licenseText.find('*/')] + 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() - oldEnd = text.find('*/') - if oldEnd == -1: - oldEnd = 0 - text = licenseText + text[oldEnd:] + text = f.read().splitlines(keepends=True) + start, stop = detectLicense(text) + if stop == -1: + stop = 0 with open(fileName, 'w') as f: - f.write(text) + f.writelines(text[0:start]) + f.writelines(licenseText) + f.writelines(text[stop:])