From 6e0a4c645133e295ea86268e85877d979ebab561 Mon Sep 17 00:00:00 2001 From: Patrik Teivonen Date: Fri, 24 Mar 2023 12:55:20 +0200 Subject: Pythonize update_component_translations.sh The current script relied on realpath tool not available out of the box on macOS and Windows. Since we need to run repository builds on at least macOS in the future, it is better to have a Python script. Change-Id: I3cfe3789b16cb4635e18bd20e0beaace8fafff8a Reviewed-by: Iikka Eklund --- packaging-tools/create_installer.py | 8 +- packaging-tools/update_component_translations.py | 150 +++++++++++++++++++++++ packaging-tools/update_component_translations.sh | 122 ------------------ 3 files changed, 154 insertions(+), 126 deletions(-) create mode 100644 packaging-tools/update_component_translations.py delete mode 100755 packaging-tools/update_component_translations.sh diff --git a/packaging-tools/create_installer.py b/packaging-tools/create_installer.py index 194971c5c..d89243375 100644 --- a/packaging-tools/create_installer.py +++ b/packaging-tools/create_installer.py @@ -69,6 +69,7 @@ from runner import run_cmd from sdkcomponent import IfwPayloadItem, IfwSdkComponent, parse_ifw_sdk_comp from sign_installer import recursive_sign_notarize from threadedwork import ThreadedWork +from update_component_translations import lrelease log = init_logger(__name__, debug_mode=False) @@ -686,11 +687,10 @@ def create_target_components(task: QtInstallerTaskType) -> None: # Copy Meta data metadata_content_source_root = os.path.join(sdk_comp.pkg_template_folder, "meta") copy_tree(metadata_content_source_root, str(sdk_comp.meta_dir_dest)) - if os.path.isfile(os.path.join(task.script_root_dir, "lrelease")): + lrelease_tool = Path(task.script_root_dir, "lrelease") + if lrelease_tool.is_file(): # create translation binaries if translation source files exist for component - update_script = os.path.join(task.script_root_dir, "update_component_translations.sh") - lrelease_tool = os.path.join(task.script_root_dir, "lrelease") - run_cmd(cmd=[update_script, "-r", lrelease_tool, str(dest_base)]) + lrelease(lrelease_tool, dest_base) # add files into tag substitution task.directories_for_substitutions.append(str(sdk_comp.meta_dir_dest)) # handle archives diff --git a/packaging-tools/update_component_translations.py b/packaging-tools/update_component_translations.py new file mode 100644 index 000000000..489615e49 --- /dev/null +++ b/packaging-tools/update_component_translations.py @@ -0,0 +1,150 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +############################################################################# +# +# Copyright (C) 2023 The Qt Company Ltd. +# Contact: https://www.qt.io/licensing/ +# +# This file is part of the release tools of the Qt Toolkit. +# +# $QT_BEGIN_LICENSE:GPL-EXCEPT$ +# Commercial License Usage +# Licensees holding valid commercial Qt licenses may use this file in +# accordance with the commercial license agreement provided with the +# Software or, alternatively, in accordance with the terms contained in +# a written agreement between you and The Qt Company. For licensing terms +# and conditions see https://www.qt.io/terms-conditions. For further +# information use the contact form at https://www.qt.io/contact-us. +# +# GNU General Public License Usage +# Alternatively, this file may be used under the terms of the GNU +# General Public License version 3 as published by the Free Software +# Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +# included in the packaging of this file. Please review the following +# information to ensure the GNU General Public License requirements will +# be met: https://www.gnu.org/licenses/gpl-3.0.html. +# +# $QT_END_LICENSE$ +# +############################################################################# + +"""Update component translations""" + +import argparse +import sys +from pathlib import Path + +from bldinstallercommon import locate_paths +from logging_util import init_logger +from runner import run_cmd + +log = init_logger(__name__, debug_mode=False) + + +def usage() -> None: + """Log usage""" + log.info("Usage: $0 [OPTION]... [PKG_DIR]") + log.info("Performs linguist operations for component translations.") + log.info("This script takes only one positional argument [PKG_DIR] which") + log.info("is a path for the package templates directory to work in.") + log.info(" -u Updates source strings in existing TS files") + log.info(" -r Produces QM files out of TS files") + + +def lupdate(lupdate_path: Path, pkg_root: Path) -> None: + """ + Updates source strings in existing TS files + + Args: + lupdate_path: Path to lupdate tool + pkg_root: Path for the package templates directory containing TS files + """ + ts_files = locate_paths(pkg_root, ["*.ts"]) + for ts_file in ts_files: + ts_file_path = Path(ts_file).resolve() + ts_file_dir = ts_file_path.parent + log.info("lupdate: %s", str(ts_file_path)) + cmd = [str(lupdate_path), "-locations", "none", "-no-ui-lines", "-no-sort", "-no-obsolete"] + cmd += [str(ts_file_dir), "-ts", ts_file_path.name] + run_cmd(cmd=cmd, cwd=ts_file_dir) + log.info("lupdate: Done") + + +def lrelease(lrelease_path: Path, pkg_root: Path) -> None: + """ + Produces QM files out of TS files + + Args: + lrelease_path: Path to lrelease tool + pkg_root: Path for the package templates directory containing TS files + """ + ts_files = locate_paths(pkg_root, ["*.ts"]) + for ts_file in ts_files: + ts_file_path = Path(ts_file).resolve() + ts_file_dir = ts_file_path.parent + locale = ts_file_path.stem + log.info("lrelease: %s", str(ts_file_path)) + if locale in ("ko", "zh", "untranslated"): + continue + run_cmd(cmd=[str(lrelease_path), str(ts_file_path)], cwd=ts_file_dir) + package_xml = ts_file_dir / "package.xml" + package_xml_contents = package_xml.read_text(encoding="utf-8") + if locale + ".qm" in package_xml_contents: + log.info("Translation file '%s.qm' already defined in package.xml", locale) + else: + lines = package_xml_contents.splitlines() + if "Translations" in package_xml_contents: # check if already contains + for line in lines: + if "" in line: + lines.remove(line) # remove lines containing + if "" in line: + lines.remove(line) # remove lines containing + else: + for line in lines: + if "" in line: + lines.remove(line) # remove lines containing + lines.append(" ") # append tag + lines.append(" ${locale}.qm") + lines.append(" ") + lines.append("") + package_xml.unlink() + with package_xml.open("w", encoding="utf-8") as new_file: + for line in lines: + new_file.write(line) + log.info("lrelease: Done") + + +def main() -> None: + """Main""" + parser = argparse.ArgumentParser( + prog="Performs linguist operations for component translations.", add_help=True + ) + parser.add_argument( + "-u", dest="lupdate_path", type=Path, required=False, help="Path to lupdate" + ) + parser.add_argument( + "-r", dest="lrelease_path", type=Path, required=False, help="Path to lrelease" + ) + parser.add_argument( + dest="pkg_dir", type=Path, help="Path to package templates directory" + ) + args = parser.parse_args(sys.argv[1:]) + if args.pkg_dir is None or (args.lupdate_path is None and args.lrelease_path is None): + usage() + raise SystemExit(1) + try: + pkg_root = args.pkg_dir.resolve(strict=True) + except OSError as err: + log.error("The specified package templates directory doesn't exist: %s", str(args.pkg_dir)) + raise SystemExit(1) from err + if args.lupdate_path is not None: + lupdate_path = args.lupdate_path.resolve(strict=True) + lupdate(lupdate_path, pkg_root) + if args.lrelease_path is not None: + lrelease_path = args.lrelease_path.resolve(strict=True) + lrelease(lrelease_path, pkg_root) + + +if __name__ == "__main__": + main() diff --git a/packaging-tools/update_component_translations.sh b/packaging-tools/update_component_translations.sh deleted file mode 100755 index cb672cc1b..000000000 --- a/packaging-tools/update_component_translations.sh +++ /dev/null @@ -1,122 +0,0 @@ -#! /bin/bash -############################################################################# -## -## Copyright (C) 2021 The Qt Company Ltd. -## Contact: https://www.qt.io/licensing/ -## -## This file is part of the FOO module of the Qt Toolkit. -## -## $QT_BEGIN_LICENSE:GPL-EXCEPT$ -## Commercial License Usage -## Licensees holding valid commercial Qt licenses may use this file in -## accordance with the commercial license agreement provided with the -## Software or, alternatively, in accordance with the terms contained in -## a written agreement between you and The Qt Company. For licensing terms -## and conditions see https://www.qt.io/terms-conditions. For further -## information use the contact form at https://www.qt.io/contact-us. -## -## GNU General Public License Usage -## Alternatively, this file may be used under the terms of the GNU -## General Public License version 3 as published by the Free Software -## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -## included in the packaging of this file. Please review the following -## information to ensure the GNU General Public License requirements will -## be met: https://www.gnu.org/licenses/gpl-3.0.html. -## -## $QT_END_LICENSE$ -## -############################################################################# - -set -o errexit -set -o nounset -set -o pipefail - -pkgroot="" -lupdate="" -lrelease="" - -usage() { - echo "Usage: $0 [OPTION]... [PKG_DIR]" - echo "Performs linguist operations for component translations." - echo - echo "This script takes only one positional argument [PKG_DIR] which" - echo "is a path for the package templates directory to work in." - echo - echo " -u Updates source strings in existing TS files" - echo " -r Produces QM files out of TS files" - echo -} - -update() { - find "$pkgroot" -name "*.ts" | while read fname; - do - filepath="${fname%/*}/" - cd "$filepath" - "$lupdate" -locations none -no-ui-lines -no-sort -no-obsolete "$filepath" -ts "$fname" - done - echo "Done" -} - -release() { - find "$pkgroot" -name "*.ts" | while read fname; - do - filepath="${fname%/*}/" - cd "$filepath" - locale="$(basename $fname .ts)" - if [ $locale == "ko" -o $locale == "zh" -o $locale == "untranslated" ]; then - continue - fi - "$lrelease" "$fname" - if grep -q ${locale}.qm package.xml; then - echo "Translation file ${locale}.qm already defined in package.xml" - else - if grep -q Translations package.xml; then - sed -i '0,/<\/Package>/{//d}' package.xml - sed -i '0,/<\/Translations>/{//d}' package.xml - else - sed -i '0,/<\/Package>/{//d}' package.xml - cat <> package.xml - -EOT - fi - cat <> package.xml - ${locale}.qm - - -EOT - fi - done - echo "Done" -} - -while getopts hu:r: flag -do - case "$flag" in - h) usage && exit 0;; - u) lupdate="$OPTARG";; - r) lrelease="$OPTARG";; - esac -done - -shift $((OPTIND - 1)) -if [ "$#" -gt 0 ]; then - pkgroot="$1" -fi - -if [ -z "$pkgroot" ]; then - usage - exit 1 -else - pkgroot=$(realpath "$pkgroot") -fi - -if [ "$lupdate" ]; then - lupdate=$(realpath "$lupdate") - update -elif [ "$lrelease" ]; then - lrelease=$(realpath "$lrelease") - release -else - usage - exit 1 -fi -- cgit v1.2.3