diff options
author | Shyamnath Premnadh <Shyamnath.Premnadh@qt.io> | 2024-02-09 15:32:39 +0100 |
---|---|---|
committer | Shyamnath Premnadh <Shyamnath.Premnadh@qt.io> | 2024-02-15 12:11:07 +0100 |
commit | 7967b70159ccf330c38e8bdd73cdafafe6dc0cf2 (patch) | |
tree | f47cdb3e4c0dcbb3afd3e4fc9563b4d6bf007ab9 /sources/pyside-tools | |
parent | b930d88c7341ebefdca2dfc206fd7f747cdf5e11 (diff) |
pyside6-project: Fix --dry-run
Bug caused from 10715102f01bfee9c0122f21680f05414a947357
Pick-to: 6.6 6.5
Change-Id: I50631239134f154baebab0eef4d36c52e8ba398b
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Diffstat (limited to 'sources/pyside-tools')
-rw-r--r-- | sources/pyside-tools/project.py | 20 | ||||
-rw-r--r-- | sources/pyside-tools/project/__init__.py | 26 | ||||
-rw-r--r-- | sources/pyside-tools/project/utils.py | 12 |
3 files changed, 39 insertions, 19 deletions
diff --git a/sources/pyside-tools/project.py b/sources/pyside-tools/project.py index b3d8b9cd6..5367e75b7 100644 --- a/sources/pyside-tools/project.py +++ b/sources/pyside-tools/project.py @@ -30,7 +30,7 @@ from project import (QmlProjectData, check_qml_decorators, is_python_file, TRANSLATION_SUFFIX, requires_rebuild, run_command, remove_path, ProjectData, resolve_project_file, new_project, - ProjectType) + ProjectType, ClOptions) MODE_HELP = """build Builds the project run Builds the project and runs the first file") @@ -72,6 +72,7 @@ class Project: """ def __init__(self, project_file: Path): self.project = ProjectData(project_file=project_file) + self.cl_options = ClOptions() # Files for QML modules using the QmlElement decorators self._qml_module_sources: List[Path] = [] @@ -84,7 +85,7 @@ class Project: """Run a pre-check on Python source files and find the ones with QML decorators (representing a QML module).""" # Quick check for any QML files (to avoid running moc for no reason). - if not opt_qml_module and not self.project.qml_files: + if not self.cl_options.qml_module and not self.project.qml_files: return for file in self.project.files: if is_python_file(file): @@ -106,7 +107,7 @@ class Project: print(self._qml_module_dir) self._qml_dir_file = self._qml_module_dir / QMLDIR_FILE - if not opt_quiet: + if not self.cl_options.quiet: count = len(self._qml_module_sources) print(f"{self.project.project_file.name}, {count} QML file(s)," f" {self._qml_project_data}") @@ -146,9 +147,9 @@ class Project: def _regenerate_qmldir(self): """Regenerate the 'qmldir' file.""" - if opt_dry_run or not self._qml_dir_file: + if self.cl_options.dry_run or not self._qml_dir_file: return - if opt_force or requires_rebuild(self._qml_module_sources, self._qml_dir_file): + if self.cl_options.force or requires_rebuild(self._qml_module_sources, self._qml_dir_file): with self._qml_dir_file.open("w") as qf: qf.write(f"module {self._qml_project_data.import_name}\n") for f in self._qml_module_dir.glob("*.qmltypes"): @@ -158,7 +159,7 @@ class Project: """Build an artifact.""" artifacts, command = self._get_artifacts(source) for artifact in artifacts: - if opt_force or requires_rebuild([source], artifact): + if self.cl_options.force or requires_rebuild([source], artifact): run_command(command, cwd=self.project.project_file.parent) self._build_file(artifact) # Recurse for QML (json->qmltypes) @@ -259,10 +260,9 @@ if __name__ == "__main__": parser.add_argument("file", help="Project file", nargs="?", type=str) options = parser.parse_args() - opt_quiet = options.quiet - opt_dry_run = options.dry_run - opt_force = options.force - opt_qml_module = options.qml_module + cl_options = ClOptions(dry_run=options.dry_run, quiet=options.quiet, force=options.force, + qml_module=options.qml_module) + mode = options.mode new_project_type = NEW_PROJECT_TYPES.get(mode) diff --git a/sources/pyside-tools/project/__init__.py b/sources/pyside-tools/project/__init__.py index 7d28b78cf..9ac8de813 100644 --- a/sources/pyside-tools/project/__init__.py +++ b/sources/pyside-tools/project/__init__.py @@ -1,10 +1,7 @@ # Copyright (C) 2022 The Qt Company Ltd. # SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only -opt_quiet = False -opt_dry_run = False -opt_force = False -opt_qml_module = False +from dataclasses import dataclass QTPATHS_CMD = "qtpaths6" MOD_CMD = "pyside6-metaobjectdump" @@ -20,6 +17,27 @@ QT_MODULES = "QT_MODULES" METATYPES_JSON_SUFFIX = "metatypes.json" TRANSLATION_SUFFIX = ".ts" + +class Singleton(type): + _instances = {} + + def __call__(cls, *args, **kwargs): + if cls not in cls._instances: + cls._instances[cls] = super().__call__(*args, **kwargs) + return cls._instances[cls] + + +@dataclass(frozen=True) +class ClOptions(metaclass=Singleton): + """ + Dataclass to store the cl options that needs to be passed as arguments. + """ + dry_run: bool + quiet: bool + force: bool + qml_module: bool + + from .utils import (run_command, requires_rebuild, remove_path, package_dir, qtpaths, qt_metatype_json_dir, resolve_project_file) from .project_data import (is_python_file, ProjectData, QmlProjectData, diff --git a/sources/pyside-tools/project/utils.py b/sources/pyside-tools/project/utils.py index b3fd03584..d2bff65af 100644 --- a/sources/pyside-tools/project/utils.py +++ b/sources/pyside-tools/project/utils.py @@ -6,14 +6,15 @@ import subprocess from pathlib import Path from typing import List, Dict, Optional -from . import opt_dry_run, opt_quiet, QTPATHS_CMD, PROJECT_FILE_SUFFIX +from . import QTPATHS_CMD, PROJECT_FILE_SUFFIX, ClOptions def run_command(command: List[str], cwd: str = None, ignore_fail: bool = False): """Run a command observing quiet/dry run""" - if not opt_quiet or opt_dry_run: + cloptions = ClOptions() + if not cloptions.quiet or cloptions.dry_run: print(" ".join(command)) - if not opt_dry_run: + if not cloptions.dry_run: ex = subprocess.call(command, cwd=cwd) if ex != 0 and not ignore_fail: sys.exit(ex) @@ -42,11 +43,12 @@ def _remove_path_recursion(path: Path): def remove_path(path: Path): """Remove path (file or directory) observing opt_dry_run.""" + cloptions = ClOptions() if not path.exists(): return - if not opt_quiet: + if not cloptions.quiet: print(f"Removing {path.name}...") - if opt_dry_run: + if cloptions.dry_run: return _remove_path_recursion(path) |