From 898080e481a28353c280109bffcb558aa0812498 Mon Sep 17 00:00:00 2001 From: Matthias Dellweg Date: Wed, 13 Nov 2024 12:36:06 +0100 Subject: [PATCH] WIP Allow to use pyproject.toml instead of setup.py --- .github/workflows/test.yml | 2 +- plugin-template | 4 +- .../github/.ci/scripts/calc_constraints.py.j2 | 47 +++++++++++++------ .../github/.github/workflows/build.yml.j2 | 4 +- .../workflows/scripts/before_install.sh.j2 | 8 +++- 5 files changed, 45 insertions(+), 20 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 65af1fd4..8057b54d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -80,7 +80,7 @@ jobs: - name: "Install python dependencies" run: | echo ::group::PYDEPS - pip install towncrier twine wheel httpie docker netaddr boto3 ansible~=10.3.0 mkdocs jq jsonpatch + pip install towncrier twine wheel httpie docker netaddr boto3 'ansible~=10.3.0' mkdocs jq jsonpatch echo "HTTPIE_CONFIG_DIR=$GITHUB_WORKSPACE/pulp_catdog/.ci/assets/httpie/" >> $GITHUB_ENV echo ::endgroup:: diff --git a/plugin-template b/plugin-template index 3d7343b3..30101770 100755 --- a/plugin-template +++ b/plugin-template @@ -424,17 +424,19 @@ def write_template_section(config, name, plugin_root_dir, verbose=False): except Exception: gitref = "unknown" + setup_py = os.path.exists(os.path.join(plugin_root_dir, "setup.py")) ci_update_branches = [config["plugin_default_branch"]] latest_release_branch = config["latest_release_branch"] if latest_release_branch is not None: if latest_release_branch not in config["supported_release_branches"]: ci_update_branches.append(latest_release_branch) ci_update_branches.extend(config["supported_release_branches"]) - ci_update_hour = sum((ord(c) for c in config["plugin_app_label"])) % 24 + template_vars = { "section": name, "gitref": gitref, + "setup_py": setup_py, "ci_update_branches": ci_update_branches, "python_version": "3.11", "ci_update_hour": ci_update_hour, diff --git a/templates/github/.ci/scripts/calc_constraints.py.j2 b/templates/github/.ci/scripts/calc_constraints.py.j2 index 77253a95..a9aa42fb 100644 --- a/templates/github/.ci/scripts/calc_constraints.py.j2 +++ b/templates/github/.ci/scripts/calc_constraints.py.j2 @@ -8,6 +8,11 @@ from packaging.requirements import Requirement from packaging.version import Version import yaml +try: + import tomllib +except ImportError: + import tomli as tomllib + CORE_TEMPLATE_URL = "https://raw.githubusercontent.com/pulp/pulpcore/main/template_config.yml" @@ -54,11 +59,11 @@ def to_upper_bound(req): operator = "~=" version = Version(spec.version) if version.micro != 0: - max_version = f"{version.major}.{version.minor}.{version.micro-1}" + max_version = f"{version.major}.{version.minor}.{version.micro - 1}" elif version.minor != 0: - max_version = f"{version.major}.{version.minor-1}" + max_version = f"{version.major}.{version.minor - 1}" elif version.major != 0: - max_version = f"{version.major-1}.0" + max_version = f"{version.major - 1}.0" else: return f"# NO BETTER CONSTRAINT: {req}" return f"{requirement.name}{operator}{max_version}" @@ -95,18 +100,32 @@ def main(): parser.add_argument("filename", nargs="*") args = parser.parse_args() - with fileinput.input(files=args.filename) as req_file: - for line in req_file: - if line.strip().startswith("#"): - # Shortcut comment only lines - print(line.strip()) - else: - req, comment = split_comment(line) - if args.upper: - new_req = to_upper_bound(req) + modifier = to_upper_bound if args.upper else to_lower_bound + + req_files = [filename for filename in args.filename if not filename.endswith("pyproject.toml")] + pyp_files = [filename for filename in args.filename if filename.endswith("pyproject.toml")] + if req_files: + with fileinput.input(files=req_files) as req_file: + for line in req_file: + if line.strip().startswith("#"): + # Shortcut comment only lines + print(line.strip()) else: - new_req = to_lower_bound(req) - print(new_req + comment) + req, comment = split_comment(line) + new_req = modifier(req) + print(new_req + comment) + for filename in pyp_files: + with open(filename, "rb") as fp: + pyproject = tomllib.load(fp) + for req in pyproject["project"]["dependencies"]: + new_req = modifier(req) + print(new_req) + optional_dependencies = pyproject["project"].get("optional-dependencies") + if optional_dependencies: + for opt in optional_dependencies.values(): + for req in opt: + new_req = modifier(req) + print(new_req) if __name__ == "__main__": diff --git a/templates/github/.github/workflows/build.yml.j2 b/templates/github/.github/workflows/build.yml.j2 index 1dfff3e5..a49def96 100644 --- a/templates/github/.github/workflows/build.yml.j2 +++ b/templates/github/.github/workflows/build.yml.j2 @@ -22,7 +22,7 @@ jobs: {{ checkout(path=plugin_name) | indent(6) }} {{ checkout(repository="pulp/pulp-openapi-generator", path="pulp-openapi-generator") | indent(6) }} {{ setup_python() | indent(6) }} - {{ install_python_deps(["packaging", "twine", "wheel", "mkdocs", "jq"]) | indent(6) }} + {{ install_python_deps(["build", "packaging", "twine", "wheel", "mkdocs", "jq"]) | indent(6) }} {%- if os_required_packages %} - name: "Install OS packages" run: | @@ -30,7 +30,7 @@ jobs: {%- endif %} - name: "Build package" run: | - python3 setup.py sdist bdist_wheel --python-tag py3 + python3 -m build twine check dist/* - name: "Install built packages" run: | diff --git a/templates/github/.github/workflows/scripts/before_install.sh.j2 b/templates/github/.github/workflows/scripts/before_install.sh.j2 index b7002d63..14ebb6d8 100755 --- a/templates/github/.github/workflows/scripts/before_install.sh.j2 +++ b/templates/github/.github/workflows/scripts/before_install.sh.j2 @@ -25,7 +25,11 @@ fi COMMIT_MSG=$(git log --format=%B --no-merges -1) export COMMIT_MSG +{% if setup_py -%} COMPONENT_VERSION=$(sed -ne "s/\s*version.*=.*['\"]\(.*\)['\"][\s,]*/\1/p" setup.py) +{%- else -%} +COMPONENT_VERSION=$(python3 -c "import tomllib; print(tomllib.load(open('pyproject.toml', 'rb'))['project']['version'])") +{%- endif %} mkdir .ci/ansible/vars || true echo "---" > .ci/ansible/vars/main.yaml @@ -61,10 +65,10 @@ then fi if [[ "$TEST" = "pulp" ]]; then - python3 .ci/scripts/calc_constraints.py -u requirements.txt > upperbounds_constraints.txt + python3 .ci/scripts/calc_constraints.py -u {% if setup_py -%} requirements.txt {% else -%} pyproject.toml {% endif -%} > upperbounds_constraints.txt fi if [[ "$TEST" = "lowerbounds" ]]; then - python3 .ci/scripts/calc_constraints.py requirements.txt > lowerbounds_constraints.txt + python3 .ci/scripts/calc_constraints.py {% if setup_py -%} requirements.txt {% else -%} pyproject.toml {% endif -%} > lowerbounds_constraints.txt fi if [ -f $POST_BEFORE_INSTALL ]; then