diff options
| author | Shyamnath Premnadh <Shyamnath.Premnadh@qt.io> | 2022-12-09 16:29:38 +0100 |
|---|---|---|
| committer | Shyamnath Premnadh <Shyamnath.Premnadh@qt.io> | 2022-12-22 09:35:49 +0000 |
| commit | 0b1361f4d70ef00e3a10a390f6b87b756f012838 (patch) | |
| tree | f2f6dd540befce67262446973cd92d9eb06a470c /sources/pyside-tools/deploy_lib/config.py | |
| parent | 1930ac417cc24ed74842e1a6f1c751ce2cfc47d0 (diff) | |
Rename folder: deploy to deploy_lib
- to distinguish between deploy.py and deploy folder, since both
are Python modules. This is especially useful when testing since
our tests are located in sources/pyside6/tests/tools.
Task-number: PYSIDE-1612
Change-Id: Ideb35b23f454ec64415421e00464cfb1f7055401
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Diffstat (limited to 'sources/pyside-tools/deploy_lib/config.py')
| -rw-r--r-- | sources/pyside-tools/deploy_lib/config.py | 226 |
1 files changed, 226 insertions, 0 deletions
diff --git a/sources/pyside-tools/deploy_lib/config.py b/sources/pyside-tools/deploy_lib/config.py new file mode 100644 index 000000000..d02558cca --- /dev/null +++ b/sources/pyside-tools/deploy_lib/config.py @@ -0,0 +1,226 @@ +# 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 + +from pathlib import Path +import configparser +from configparser import ConfigParser +import shutil +import logging + +from project import ProjectData + + +class Config: + """ + Wrapper class around config file, whose options are used to control the executable creation + """ + + def __init__(self, config_file: Path, source_file: Path, python_exe: Path, dry_run: bool): + self.config_file = config_file + self.parser = ConfigParser(comment_prefixes="/", allow_no_value=True) + if not self.config_file.exists(): + logging.info(f"[DEPLOY] Creating config file {self.config_file}") + shutil.copy(Path(__file__).parent / "default.spec", self.config_file) + else: + print(f"Using existing config file {config_file}") + self.parser.read(self.config_file) + + self.dry_run = dry_run + # set source_file + self.source_file = Path( + self.set_or_fetch(config_property_val=source_file, config_property_key="input_file") + ) + + # set python path + self.python_path = Path( + self.set_or_fetch( + config_property_val=python_exe, + config_property_key="python_path", + config_property_group="python", + ) + ) + + self.project_dir = None + if self.get_value("app", "project_dir"): + self.project_dir = Path(self.get_value("app", "project_dir")).absolute() + else: + self._find_and_set_project_dir() + + self.project_data: ProjectData = None + if self.get_value("app", "project_file"): + project_file = Path(self.get_value("app", "project_file")).absolute() + self.project_data = ProjectData(project_file=project_file) + else: + self._find_and_set_project_file() + + self.qml_files = [] + config_qml_files = self.get_value("qt", "qml_files") + if config_qml_files and self.project_dir: + self.qml_files = [Path(self.project_dir) / file for file in config_qml_files.split(",")] + else: + self._find_and_set_qml_files() + + def update_config(self): + logging.info("[DEPLOY] Creating {config_file}") + with open(self.config_file, "w+") as config_file: + self.parser.write(config_file, space_around_delimiters=True) + + def set_value(self, section: str, key: str, new_value: str): + try: + current_value = self.get_value(section, key) + if current_value != new_value: + self.parser.set(section, key, new_value) + except configparser.NoOptionError: + logging.warning(f"[DEPLOY] key {key} does not exist") + except configparser.NoSectionError: + logging.warning(f"[DEPLOY] section {section} does not exist") + + def get_value(self, section: str, key: str): + try: + return self.parser.get(section, key) + except configparser.NoOptionError: + logging.warning(f"[DEPLOY] key {key} does not exist") + except configparser.NoSectionError: + logging.warning(f"[DEPLOY] section {section} does not exist") + + def set_or_fetch(self, config_property_val, config_property_key, config_property_group="app"): + """ + Write to config_file if 'config_property_key' is known without config_file + Fetch and return from config_file if 'config_property_key' is unknown, but + config_file exists + Otherwise, raise an exception + """ + if config_property_val: + self.set_value(config_property_group, config_property_key, str(config_property_val)) + return config_property_val + elif self.get_value(config_property_group, config_property_key): + return self.get_value(config_property_group, config_property_key) + else: + logging.exception( + f"[DEPLOY]: No {config_property_key} specified in config file or as cli option" + ) + raise + + @property + def qml_files(self): + return self._qml_files + + @qml_files.setter + def qml_files(self, qml_files): + self._qml_files = qml_files + + @property + def project_dir(self): + return self._project_dir + + @project_dir.setter + def project_dir(self, project_dir): + self._project_dir = project_dir + + @property + def source_file(self): + return self._source_file + + @source_file.setter + def source_file(self, source_file): + self._source_file = source_file + + @property + def python_path(self): + return self._python_path + + @python_path.setter + def python_path(self, python_path): + self._python_path = python_path + + def _find_and_set_qml_files(self): + """Fetches all the qml_files in the folder and sets them if the + field qml_files is empty in the config_dir""" + + if self.project_data: + qml_files = self.project_data.qml_files + for sub_project_file in self.project_data.sub_projects_files: + qml_files.extend(ProjectData(project_file=sub_project_file).qml_files) + self.qml_files = qml_files + else: + qml_files_temp = None + source_file = ( + Path(self.get_value("app", "input_file")) + if self.get_value("app", "input_file") + else None + ) + python_exe = ( + Path(self.get_value("python", "python_path")) + if self.get_value("python", "python_path") + else None + ) + if source_file and python_exe: + if not self.qml_files: + qml_files_temp = list(source_file.parent.glob("**/*.qml")) + + # add all QML files, excluding the ones shipped with installed PySide6 + # The QML files shipped with PySide6 gets added if venv is used, + # because of recursive glob + if python_exe.parent.parent == source_file.parent: + # python venv path is inside the main source dir + qml_files_temp = list( + set(qml_files_temp) - set(python_exe.parent.parent.rglob("*.qml")) + ) + + if len(qml_files_temp) > 500: + if "site-packages" in str(qml_files_temp[-1]): + logging.warning( + "You seem to include a lot of QML files from a \ + local virtual env. Are they intended?" + ) + else: + logging.warning( + "You seem to include a lot of QML files. \ + Are they intended?" + ) + + if qml_files_temp: + extra_qml_files = [Path(file) for file in qml_files_temp] + self.qml_files.extend(extra_qml_files) + if self.qml_files: + self.set_value( + "qt", + "qml_files", + ",".join([str(file.relative_to(self.project_dir)) for file in self.qml_files]), + ) + logging.info("[DEPLOY] QML files identified and set in config_file") + + def _find_and_set_project_dir(self): + # there is no other way to find the project_dir than assume it is the parent directory + # of source_file + self.project_dir = self.source_file.parent + + # update input_file path + logging.info("[DEPLOY] Update input_file path") + self.set_value("app", "input_file", str(self.source_file.relative_to(self.project_dir))) + + logging.info("[DEPLOY] Update project_dir path") + if self.project_dir != Path.cwd(): + self.set_value("app", "project_dir", str(self.project_dir)) + else: + self.set_value("app", "project_dir", str(self.project_dir.relative_to(Path.cwd()))) + + def _find_and_set_project_file(self): + logging.info("[DEPLOY] Searching for .pyproject file") + + if self.project_dir: + files = list(self.project_dir.glob("*.pyproject")) + else: + logging.exception("[DEPLOY] Project directory not set in config file") + raise + + if not files: + logging.info("[DEPLOY] No .pyproject file found. Project file not set") + elif len(files) > 1: + logging.warning("DEPLOY: More that one .pyproject files found. Project file not set") + raise + else: + self.project_data = ProjectData(files[0]) + self.set_value("app", "project_file", str(files[0].relative_to(self.project_dir))) + logging.info(f"[DEPLOY] Project file {files[0]} found and set in config file") + |
