aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2022-11-25 18:25:59 +0100
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2022-11-26 09:57:08 +0100
commitff12d16c557c2c6a34306f8f0fb016e6e590796d (patch)
tree1fb2c184a8ccea8e723e8265da1d283651f63cb7
parent2883b5d7bc791b710d77d7bd799529e103d6955b (diff)
snippets_translate: Fix the license handlingHEADdev
Adapt to SPDX licenses, leave the old code path as is. As a drive-by, avoid reading the snippets file twice. Pick-to: 6.4 6.2 Task-number: PYSIDE-1106 Change-Id: Ia518124b09e22d01e37970f30605d94b86123106 Reviewed-by: Adrian Herrmann <adrian.herrmann@qt.io> Reviewed-by: Christian Tismer <tismer@stackless.com>
-rw-r--r--tools/snippets_translate/main.py56
1 files changed, 27 insertions, 29 deletions
diff --git a/tools/snippets_translate/main.py b/tools/snippets_translate/main.py
index 3c959dfda..12b6e54f1 100644
--- a/tools/snippets_translate/main.py
+++ b/tools/snippets_translate/main.py
@@ -284,37 +284,35 @@ def get_snippets(lines: List[str], rel_path: str) -> List[List[str]]:
return result.values()
-def get_license_from_file(filename):
- lines = []
- try:
- with open(filename, "r", encoding="utf-8") as f:
- line = True
- while line:
- line = f.readline().rstrip()
-
- if line.startswith("/*") or line.startswith("**"):
- lines.append(line)
- # End of the comment
- if line.endswith("*/"):
- break
- except Exception as e:
- log.error(f"Error reading {filename}: {e}")
- raise
- if lines:
- # We know we have the whole block, so we can
- # perform replacements to translate the comment
- lines[0] = lines[0].replace("/*", "**").replace("*", "#")
- lines[-1] = lines[-1].replace("*/", "**").replace("*", "#")
-
- for i in range(1, len(lines) - 1):
- lines[i] = re.sub(r"^\*\*", "##", lines[i])
-
- return "\n".join(lines)
- else:
- return ""
+def get_license_from_file(lines):
+ result = []
+ spdx = len(lines) >= 2 and lines[0].startswith("//") and "SPDX" in lines[1]
+ if spdx: # SPDX, 6.4
+ for line in lines:
+ if line.startswith("//"):
+ result.append("# " + line[3:])
+ else:
+ break
+ else: # Old style, C-Header, 6.2
+ for line in lines:
+ if line.startswith("/*") or line.startswith("**"):
+ result.append(line)
+ # End of the comment
+ if line.endswith("*/"):
+ break
+ if result:
+ # We know we have the whole block, so we can
+ # perform replacements to translate the comment
+ result[0] = result[0].replace("/*", "**").replace("*", "#")
+ result[-1] = result[-1].replace("*/", "**").replace("*", "#")
+
+ for i in range(1, len(result) - 1):
+ result[i] = re.sub(r"^\*\*", "##", result[i])
+ return "\n".join(result)
def translate_file(file_path, final_path, qt_path, debug, write):
+ lines = []
snippets = []
try:
with file_path.open("r", encoding="utf-8") as f:
@@ -326,7 +324,7 @@ def translate_file(file_path, final_path, qt_path, debug, write):
raise
if snippets:
# TODO: Get license header first
- license_header = get_license_from_file(str(file_path))
+ license_header = get_license_from_file(lines)
if debug:
if have_rich:
console = Console()