Skip to content

Commit

Permalink
Use cog to de-duplicate Python versions (#59)
Browse files Browse the repository at this point in the history
* Use cog to de-duplicate Python versions

* oops

* another oops
  • Loading branch information
hynek committed Jun 6, 2023
1 parent 66eb2b4 commit 991c53d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 41 deletions.
51 changes: 36 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ permissions:
contents: read

jobs:
cog-check:
name: Ensure cogified files are up-to-date
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4

- run: python -Im pip install --upgrade wheel nox

- run: python -Im nox -e cog -- --check

build-sdist:
name: 📦 Build the source distribution
runs-on: ubuntu-latest
Expand Down Expand Up @@ -55,14 +66,18 @@ jobs:
strategy:
matrix:
python-version:
# [[[cog
# for line in open("pyproject.toml"):
# if "Programming Language :: Python :: " in line:
# cog.outl(f'- "{line.rsplit(" ")[-1][:-3]}"')
# ]]]
- "3.7"
- "3.8"
- "3.9"
- "3.10"
- "3.11"
- "~3.12.0-0"
continue-on-error: >-
${{ contains(matrix.python-version, '~') && true || false }}
- "3.12"
# [[[end]]]

steps:
- name: Get source code from pre-built sdist
Expand All @@ -73,19 +88,11 @@ jobs:
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- run: python -Im pip install --upgrade wheel nox
allow-prereleases: true

- name: Determine Python version for Nox
run: |
V=${{ matrix.python-version }}
if [[ "$V" = ~* ]]; then
# Extract version from a '~3.XX.0-0' specifier.
V=${V:1:4}
fi
echo NOX_PYTHON=$V >>$GITHUB_ENV
- run: python -Im pip install --upgrade wheel nox

- run: python -Im nox --python ${{ env.NOX_PYTHON }} --tags tests
- run: python -Im nox --python ${{ matrix.python-version }} --tags tests

- name: Upload coverage data
uses: actions/upload-artifact@v3
Expand Down Expand Up @@ -139,11 +146,18 @@ jobs:
fail-fast: false
matrix:
python-version:
# [[[cog
# for line in open("pyproject.toml"):
# if "Programming Language :: Python :: " in line:
# cog.outl(f'- "{line.rsplit(" ")[-1][:-3]}"')
# ]]]
- "3.7"
- "3.8"
- "3.9"
- "3.10"
- "3.11"
- "3.12"
# [[[end]]]

steps:
- name: Get source code from pre-built sdist
Expand All @@ -154,6 +168,7 @@ jobs:
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
allow-prereleases: true
- run: python -Im pip install --upgrade wheel nox

- run: python -Im nox -e mypy
Expand All @@ -170,8 +185,14 @@ jobs:
workflow-artifact-name: ${{ env.sdist-artifact }}
- uses: actions/setup-python@v4
with:
# Keep in sync with .readthedocs.yaml
# [[[cog
# import yaml
# with open(".readthedocs.yaml") as f:
# rtd = yaml.safe_load(f)
# cog.outl(f'python-version: "{rtd["build"]["tools"]["python"]}"')
# ]]]
python-version: "3.11"
# [[[end]]]
- run: python -Im pip install --upgrade wheel nox

- run: python -Im nox -e docs
Expand Down
3 changes: 1 addition & 2 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
version: 2

build:
os: ubuntu-20.04
os: ubuntu-22.04
tools:
# Keep in sync with ci.yml/docs
python: "3.11"

python:
Expand Down
71 changes: 47 additions & 24 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from __future__ import annotations

import os
import re

from pathlib import Path

import nox

Expand All @@ -12,27 +9,53 @@
nox.options.error_on_external_run = True


# Avoid dependencies on YAML / TOML libs using a questionable hacks.
match_docs_python = re.compile(r"\s+python\: \"(\d\.\d\d)\"").fullmatch
for line in Path(".readthedocs.yaml").read_text().splitlines():
m = match_docs_python(line)
if m:
DOCS_PYTHON = m.group(1)
break
RUN_UNDER_COVERAGE = ["3.7", "3.11"]
ALL_SUPPORTED = [
# [[[cog
# for line in open("pyproject.toml"):
# if "Programming Language :: Python :: " in line:
# cog.outl(f'"{line.rsplit(" ")[-1][:-3]}",')
# ]]]
"3.7",
"3.8",
"3.9",
"3.10",
"3.11",
"3.12",
# [[[end]]]
]
OLDEST_PYTHON = ALL_SUPPORTED[0]
NOT_COVERAGE = [v for v in ALL_SUPPORTED if v not in RUN_UNDER_COVERAGE]

# [[[cog
# import yaml
# with open(".readthedocs.yaml") as f:
# rtd = yaml.safe_load(f)
# cog.outl(f'DOCS_PYTHON = "{rtd["build"]["tools"]["python"]}"')
# ]]]
DOCS_PYTHON = "3.11"
# [[[end]]]

# [[[cog
# import tomllib
# with open("pyproject.toml", "rb") as f:
# deps = tomllib.load(f)["project"]["dependencies"]
# for dep in deps:
# if dep.startswith("attrs"):
# cog.outl(f'OLDEST_ATTRS = "{dep[7:]}"')
# break
# ]]]
OLDEST_ATTRS = "17.4.0"
# [[[end]]]


match_oldest_python = re.compile(
r"requires-python = \">=(\d\.\d+)\""
).fullmatch
match_oldest_attrs = re.compile(r".*\"attrs>=(\d+\.\d+\.\d+)\",.*").match
for line in Path("pyproject.toml").read_text().splitlines():
m = match_oldest_python(line)
if m:
OLDEST_PYTHON = m.group(1)
@nox.session
def cog(session: nox.Session) -> None:
session.install("cogapp", "PyYAML")

m = match_oldest_attrs(line)
if m:
OLDEST_ATTRS = m.group(1)
break # dependencies always come after requires-python
session.run(
"cog", *session.posargs, "-r", "noxfile.py", ".github/workflows/ci.yml"
)


@nox.session
Expand All @@ -49,14 +72,14 @@ def _cov(session: nox.Session) -> None:
session.notify("coverage_report")


@nox.session(python=["3.7", "3.11"], tags=["tests"])
@nox.session(python=RUN_UNDER_COVERAGE, tags=["tests"])
def tests_cov(session: nox.Session) -> None:
session.install(".[tests]")

_cov(session)


@nox.session(python=["3.8", "3.9", "3.10", "3.12"], tags=["tests"])
@nox.session(python=NOT_COVERAGE, tags=["tests"])
def tests(session: nox.Session) -> None:
session.install(".[tests]")

Expand Down

0 comments on commit 991c53d

Please sign in to comment.