From 545ca796dbd93edb66dc3c21c74511fab8e9d0a3 Mon Sep 17 00:00:00 2001 From: Jaime Resano Date: Mon, 24 Feb 2025 16:11:28 +0100 Subject: pyproject.toml: 2. Add pyproject.toml support for pyside6 tools This patch adds support for pyproject.toml files to the pyside6-project tool. A new command argument is added to migrate a .pyproject JSON file to the new pyproject.toml file: `pyside6-project migrate-pyproject` The new features are tested and it is guaranteed that the current behavior is preserved. A new flag is added to the project creation operations, "--legacy-pyproject", in order to generate a .pyproject file instead of a pyproject.toml file. Note that the tomlkit library is added to the requirements.txt file. https://github.com/python-poetry/tomlkit Task-number: PYSIDE-2714 Change-Id: If33956dea73b79df0a52d4dcda3934c85e57182d Reviewed-by: Friedemann Kleint --- sources/pyside-tools/project_lib/utils.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'sources/pyside-tools/project_lib/utils.py') diff --git a/sources/pyside-tools/project_lib/utils.py b/sources/pyside-tools/project_lib/utils.py index 183a0aa04..c1c406507 100644 --- a/sources/pyside-tools/project_lib/utils.py +++ b/sources/pyside-tools/project_lib/utils.py @@ -7,7 +7,9 @@ import sys import xml.etree.ElementTree as ET from pathlib import Path -from . import QTPATHS_CMD, PYPROJECT_JSON_PATTERN, PYPROJECT_FILE_PATTERNS, ClOptions +from . import (QTPATHS_CMD, PYPROJECT_JSON_PATTERN, PYPROJECT_TOML_PATTERN, PYPROJECT_FILE_PATTERNS, + ClOptions) +from .pyproject_toml import parse_pyproject_toml from .pyproject_json import parse_pyproject_json @@ -138,16 +140,19 @@ def resolve_valid_project_file( If the provided file name is a valid project file, return it. Otherwise, search for a known project file in the current working directory with the given patterns. - Raises a ValueError if no project file is found or multiple project files are found in the same - directory. + Raises a ValueError if no project file is found, multiple project files are found in the same + directory or the provided path is not a valid project file or folder. - :param project_path_input: The command-line argument specifying a project file path. + :param project_path_input: The command-line argument specifying a project file or folder path. :param project_file_patterns: The list of project file patterns to search for. - :return: The resolved project file path if found, otherwise None. + :return: The resolved project file path """ if project_path_input and (project_file := Path(project_path_input).resolve()).is_file(): - if project_file.match(PYPROJECT_JSON_PATTERN): + if project_file.match(PYPROJECT_TOML_PATTERN): + if bool(parse_pyproject_toml(project_file).errors): + raise ValueError(f"Invalid project file: {project_file}") + elif project_file.match(PYPROJECT_JSON_PATTERN): pyproject_json_result = parse_pyproject_json(project_file) if errors := '\n'.join(str(e) for e in pyproject_json_result.errors): raise ValueError(f"Invalid project file: {project_file}\n{errors}") @@ -163,8 +168,7 @@ def resolve_valid_project_file( # Search a project file in the project folder using the provided patterns for pattern in project_file_patterns: - matches = list(project_folder.glob(pattern)) - if not matches: + if not (matches := list(project_folder.glob(pattern))): # No project files found with the specified pattern continue @@ -174,7 +178,12 @@ def resolve_valid_project_file( project_file = matches[0] - if pattern == PYPROJECT_JSON_PATTERN: + if pattern == PYPROJECT_TOML_PATTERN: + if parse_pyproject_toml(project_file).errors: + # Invalid file, but a .pyproject file may exist + # We can not raise an error due to ensuring backward compatibility + continue + elif pattern == PYPROJECT_JSON_PATTERN: pyproject_json_result = parse_pyproject_json(project_file) if errors := '\n'.join(str(e) for e in pyproject_json_result.errors): raise ValueError(f"Invalid project file: {project_file}\n{errors}") -- cgit v1.2.3