From e9d32bd99c37de59b00ba50f4600b66c7b7df2a6 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Mon, 25 Mar 2024 02:28:53 -0500 Subject: [PATCH] Use uv for Package Constraints (#225) * Use uv for dependency_type managemange * fixups * cleanup * fix test * fix test * cleanup --- .github/actions/base-setup/action.yml | 10 +++-- .../actions/base-setup/setup_constraints.sh | 11 ----- .github/actions/install-minimums/action.yml | 45 ------------------- .../create_constraints_file.py | 35 --------------- .github/workflows/tests.yml | 32 +------------ README.md | 43 +++--------------- pyproject.toml | 2 +- 7 files changed, 16 insertions(+), 162 deletions(-) delete mode 100644 .github/actions/base-setup/setup_constraints.sh delete mode 100644 .github/actions/install-minimums/action.yml delete mode 100644 .github/actions/install-minimums/create_constraints_file.py diff --git a/.github/actions/base-setup/action.yml b/.github/actions/base-setup/action.yml index 42c3d0e..aa84c0c 100644 --- a/.github/actions/base-setup/action.yml +++ b/.github/actions/base-setup/action.yml @@ -21,7 +21,7 @@ runs: # Handle default python value based on dependency type. if [ $DEPENDENCY_TYPE == "pre" ]; then - DEFAULT_PYTHON="3.11" + DEFAULT_PYTHON="3.12" elif [ $DEPENDENCY_TYPE == "minimum" ]; then DEFAULT_PYTHON="3.8" elif [ $DEPENDENCY_TYPE != "standard" ]; then @@ -114,6 +114,7 @@ runs: set -eux echo "::group::Upgrade packaging dependencies" python -m pip install --upgrade pip + pipx install uv if [ "$RUNNER_OS" != "Windows" ]; then pipx install hatch --python $(which python) else @@ -125,11 +126,14 @@ runs: shell: bash run: | set -eux + FLAGS="" if [ $DEPENDENCY_TYPE == 'pre' ]; then - echo "PIP_PRE=1" >> $GITHUB_ENV + FLAGS="--prerelease=allow" elif [ $DEPENDENCY_TYPE == 'minimum' ]; then - source ${{ github.action_path }}/setup_constraints.sh + FLAGS="--resolution=lowest-direct" fi + uv pip compile $FLAGS pyproject.toml -o $HOME/constraints.txt + echo "PIP_CONSTRAINT=$HOME/constraints.txt" >> $GITHUB_ENV - name: Print Diagnostics shell: bash diff --git a/.github/actions/base-setup/setup_constraints.sh b/.github/actions/base-setup/setup_constraints.sh deleted file mode 100644 index fc93f85..0000000 --- a/.github/actions/base-setup/setup_constraints.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash -python -m venv $HOME/.venv -source $HOME/.venv/bin/activate -pip install build packaging pkginfo -mkdir $HOME/dist -python -m build --outdir $HOME/dist --wheel . - -SCRIPT_DIR=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd) -python $SCRIPT_DIR/../install-minimums/create_constraints_file.py $HOME/constraints.txt $HOME/dist/*.whl -cat $HOME/constraints.txt -echo "PIP_CONSTRAINT=$HOME/constraints.txt" >> $GITHUB_ENV diff --git a/.github/actions/install-minimums/action.yml b/.github/actions/install-minimums/action.yml deleted file mode 100644 index ea6004c..0000000 --- a/.github/actions/install-minimums/action.yml +++ /dev/null @@ -1,45 +0,0 @@ -name: Install Minimums -description: "Install the minimum versions of dependencies" -inputs: - extras: - description: The Extras to Install (comma separated) - default: test - working-directory: - description: The working directory - default: . - constraints_file_path: - description: The path to the constraints file - default: ./contraints_file.txt - only_create_file: - description: Whether to only create the constraints file - default: "" - -runs: - using: "composite" - steps: - - name: Run the script - shell: bash - run: | - set -ex - - echo "::group::Create Constraints file" - CONSTRAINTS_FILE=${{inputs.constraints_file_path}} - python -m pip install -U pip build packaging - cd ${{ inputs.working-directory }} - python -m build --wheel . - python ${{ github.action_path }}/create_constraints_file.py $CONSTRAINTS_FILE dist/*.whl - cat ${{inputs.constraints_file_path}} - echo "::endgroup::" - - if [ -z ${{inputs.only_create_file}} ]; then - echo "::group::Install from Constraints file" - pip install -e ".[${{ inputs.extras }}]" -c $CONSTRAINTS_FILE - rm $CONSTRAINTS_FILE - - # Show installed packages - pip check - pip list - echo "::endgroup::" - else - echo "PIP_CONSTRAINT=${{inputs.constraints_file_path}}" >> $GITHUB_ENV - fi diff --git a/.github/actions/install-minimums/create_constraints_file.py b/.github/actions/install-minimums/create_constraints_file.py deleted file mode 100644 index 2d26770..0000000 --- a/.github/actions/install-minimums/create_constraints_file.py +++ /dev/null @@ -1,35 +0,0 @@ -import sys -from pathlib import Path -from zipfile import ZipFile - -from packaging.requirements import Requirement - -output_file = sys.argv[-2] -fname = sys.argv[-1] -constraints = {} - -archive = ZipFile(fname) -reqs = [] -for f in archive.namelist(): - if f.endswith("METADATA"): - for li in archive.open(f).read().decode("utf-8").split("\n"): - if li.startswith("Requires-Dist"): - reqs.append(li.replace("Requires-Dist: ", "")) -archive.close() - -for req in reqs: - r = Requirement(req) - for specifier in r.specifier: - if "!" in specifier.operator: - continue - if "~" in specifier.operator or ">" in specifier.operator: - spec = str(specifier).replace("~", "=") - spec = spec.replace(">=", "==") - spec = spec.replace(">", "==") - constraints[r.name] = spec - -constraints_list = [f"{key}{value}\n" for (key, value) in constraints.items()] - -# Write the constraints to to a pip constraints file. -with Path(output_file).open("w") as fid: - fid.writelines(constraints_list) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ca01dc9..c30e234 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -54,7 +54,7 @@ jobs: pip install -e ".[test]" # NOTE: keep this version in sync with README python --version - python --version | grep "3.11" + python --version | grep "3.12" hatch run check_pre test_lint: @@ -148,35 +148,6 @@ jobs: pre_commit: true target: "https://github.com/jupyterlab/maintainer-tools/pull/39" - install_minimums: - runs-on: ubuntu-latest - strategy: - matrix: - python-version: ["3.8"] # Test against minimum Python version as well - steps: - - name: Checkout - uses: actions/checkout@v4 - - name: Base Setup - uses: ./.github/actions/base-setup - - name: Clone a repo - run: git clone --depth=1 https://github.com/jupyter/nbformat.git - - name: Install minimum versions - uses: ./.github/actions/install-minimums - with: - working-directory: nbformat - - name: Run the unit tests - working-directory: nbformat - run: pytest -vv - - name: Only create file - uses: ./.github/actions/install-minimums - with: - only_create_file: 1 - constraints_file_path: "./constraints-test.txt" - - name: Ensure constraints file - run: | - cat ./constraints-test.txt - echo $PIP_CONSTRAINT | grep "constraints-test.txt" - update_snapshots-manual-server: runs-on: ubuntu-latest env: @@ -311,7 +282,6 @@ jobs: - check_links - binder_link - pr_script - - install_minimums - downstream_defaults - downstream_overrides - update_snapshots-manual-server diff --git a/README.md b/README.md index a4a80d7..8e75491 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ jobs: If you want to use your minimum dependencies, you can use the following option, which will create a constraints file and set the `PIP_CONSTRAINT` environment variable, so that installations will use that file. -By default the Python version will be "3.11", which can be overridden with +By default the Python version will be "3.7", which can be overridden with `python_version`. Note that the environment variable also works if you use virtual environments like `hatch`. @@ -68,9 +68,12 @@ you use virtual environments like `hatch`. run: pytest ``` -If you want to run against prereleases and the latest stable Python, -use the following, which will install Python 3.11 and set the -`PIP_PRE` environment variable:: +If you want to use your minimum dependencies, you can use the following +option, which will create a constraints file and set the `PIP_CONSTRAINT` +environment variable, so that installations will use that file. +By default the Python version will be "3.12", which can be overridden with +`python_version`. Note that the environment variable also works if +you use virtual environments like `hatch`. ```yaml prereleases: @@ -193,38 +196,6 @@ jobs: To test against a prerelease use `package_download_extra_args: "--pre"`. -## Test Against Dependency Minimum Version - -**DEPRECATED**. Use `dependency_type: minimum` in the `base-setup` action -instead. - -Use this action to test that your minimum dependency version constraints are valid. Note: you may want to also use the minimum supported version of Python -since the minimum versions might not have wheels on newer Pythons. Note that you should use `pytest -W default` if you are using `filterwarnings` and relying on newer versions of the library to have removed warnings. - -```yaml -name: Minimum Dependencies - -on: - push: - branches: ["main"] - pull_request: - -jobs: - test_minimums: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v2 - - name: Base Setup - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1 - with: - python_version: "3.8" # Test against minimum Python version as well - - name: Install minimum versions - uses: jupyterlab/maintainer-tools/.github/actions/install-minimums@v1 - - name: Run the unit tests - run: pytest -vv -W default -``` - ## Test SDist Use this pair of actions to build an sdist for your package, and then test it diff --git a/pyproject.toml b/pyproject.toml index 99331c0..5ded738 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,7 +22,7 @@ source = "nodejs" [tool.hatch.envs.default.scripts] check_minimum = "python -c 'from jupyter_core import __version__; assert __version__ == \"4.12.0\"'" -check_pre = "python -c 'import os; assert os.environ[\"PIP_PRE\"] == \"1\"'" +check_pre = "python -c 'import os; assert os.environ[\"PIP_CONSTRAINT\"]'" [tool.hatch.envs.typing] dependencies = ["pre-commit"]