Skip to content

Commit e6b5aa0

Browse files
authored
ci(python): a weekly job keeps the Python version tiers current and opens a PR (#1660) (#1716)
1 parent 0b9c508 commit e6b5aa0

19 files changed

Lines changed: 4787 additions & 91 deletions

‎.github/python-versions.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# The Python version tiers (#1660). Every workflow matrix and every
22
# single-interpreter job's version is read from here through
33
# util/python_versions.py, and tests/project/test_python_versions_1660.py pins
4-
# the classifiers and docs to it. A tier change is a one-line edit below plus
5-
# the matching edit to ruleset 23497679's Compat contexts.
4+
# the classifiers and docs to it. cron-python-versions.yml recomputes it weekly
5+
# and proposes each change, and the ruleset 23497679 edit it needs, as a PR.
66
#
77
# Tiers: supported (blocking), ktlo (end-of-life, non-blocking) and pre-release
88
# (non-blocking; none listed today). `runner` is the image a version needs when

‎.github/workflows/coverage-comment.yml‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: Coverage Comment
22

3-
# Posts the coverage the `test` job's Python 3.15 leg measured in
3+
# Posts the coverage the `test` job's `latest` leg measured in
44
# unit-tests.yml as a pull-request comment. It runs no tests. The comment cannot
55
# be posted from unit-tests.yml itself: that file is also a reusable workflow,
66
# and a called workflow can only narrow the token its caller passes, never widen
@@ -81,8 +81,13 @@ jobs:
8181
if tests not in ('success', 'failure', 'cancelled', 'skipped'):
8282
sys.exit(f'invalid test outcome in artifact: {tests!r}')
8383
total = number(data['total'])
84+
# The leg's version, from the artifact; one written before #1660 has none.
85+
python = data.get('python')
86+
if python is not None and not (isinstance(python, str) and re.fullmatch(r'3\.\d+', python)):
87+
sys.exit(f'invalid Python version in artifact: {python!r}')
88+
leg = f'Python {python}' if python else 'the coverage leg'
8489
status = '' if tests == 'success' else f' -- **tests {tests}**, so this is partial'
85-
print(f"**Coverage: {total:.2f}%**{status} (unit tier, Python 3.15, "
90+
print(f"**Coverage: {total:.2f}%**{status} (unit tier, {leg}, "
8691
f"`{os.environ['HEAD_SHA'][:9]}`, Unit Tests run {os.environ['RUN_CONCLUSION']})")
8792
print()
8893
print('| Package | Statements | Missed | Branches | Partial | Cover |')

‎.github/workflows/create-release.yml‎

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ concurrency:
102102
# artefact -- the tag it checks is the one it is about to create -- but ``tag``,
103103
# ``pypi`` and ``conda`` now each read their own evidence instead: ``tag`` checks
104104
# for the ``conda-<version>+0`` tag it creates, ``pypi`` checks PyPI's file count
105-
# for the version against the 7 expected, and ``conda`` both checks Anaconda's
105+
# for the version against the count expected, and ``conda`` both checks Anaconda's
106106
# aggregate count as a job-level cost saver and, per leg, whether that leg's own
107107
# platform/Python distribution is already there -- the correctness fix, since
108108
# retrying an upload Anaconda already has fails outright rather than skipping
@@ -220,8 +220,9 @@ jobs:
220220
fetch-depth: 0
221221

222222
# #1660: .github/python-versions.toml, read with the runner's own python3:
223-
# `latest` for this job, the ``pypi`` and ``conda`` matrices, and the tags
224-
# the two completeness checks below count, all from this run's commit.
223+
# `latest` for this job and the ``pypi`` and ``conda`` matrices, from this
224+
# run's commit, and the tags the two completeness checks below count until
225+
# the version is tagged.
225226
- name: Read the Python versions
226227
id: python-versions
227228
run: python3 util/python_versions.py --github-output
@@ -262,11 +263,58 @@ jobs:
262263
with:
263264
tag: "conda-${{ steps.get_version.outputs.PCAPKIT_VERSION }}+0"
264265

265-
# PyPI evidence: compare the file count for this version against the 7
266-
# expected -- the 6 supported legs (``pypi``'s ``python-version`` axis,
267-
# whose wheel tags ``PCAPKIT_PYPI_WHEELS`` lists) each producing a wheel,
268-
# plus the sdist that the "No Source Distribution" step keeps only on the
269-
# 3.15 leg, ``latest``. A release-level 200 only proves *some*
266+
# #1660: the two checks below count what the version was released with.
267+
# Once its ``v*`` tag exists, that is the tier file at the tag's commit,
268+
# read by that commit's own util/python_versions.py, not this run's file:
269+
# a version added to the supported tier since was never built for it, and
270+
# counting it would read a finished release as incomplete, run the gate and
271+
# the publishers, and fail #1634's tag check on every run until the next
272+
# release. A tag older than the file counts the tiers the file was added
273+
# with, which #1677 copied unchanged from the workflows of the time.
274+
- name: Read the released Python versions
275+
id: released
276+
env:
277+
PCAPKIT_TAG: v${{ steps.get_version.outputs.PCAPKIT_VERSION }}
278+
PCAPKIT_TAG_EXISTS: ${{ steps.check_tag.outputs.exists }}
279+
PCAPKIT_PYPI_WHEELS: ${{ steps.python-versions.outputs.pypi-wheels }}
280+
PCAPKIT_CONDA_BUILDS: ${{ steps.python-versions.outputs.conda-builds }}
281+
run: |
282+
set -euo pipefail
283+
284+
wheels="$PCAPKIT_PYPI_WHEELS"
285+
builds="$PCAPKIT_CONDA_BUILDS"
286+
tiers=.github/python-versions.toml
287+
if [ "$PCAPKIT_TAG_EXISTS" = true ]; then
288+
# The checkout fetched every tag, but not one pushed since.
289+
git rev-parse -q --verify "refs/tags/${PCAPKIT_TAG}^{commit}" > /dev/null \
290+
|| git fetch -q --no-tags origin "+refs/tags/${PCAPKIT_TAG}:refs/tags/${PCAPKIT_TAG}"
291+
from="$(git rev-parse "refs/tags/${PCAPKIT_TAG}^{commit}")"
292+
if ! git cat-file -e "${from}:${tiers}" 2> /dev/null; then
293+
from="$(git log --diff-filter=A --format=%H -- "$tiers" | tail -n 1)"
294+
if [ -z "$from" ]; then
295+
echo "::error title=No tier file in the history::${tiers} was never added to this checkout's history."
296+
exit 1
297+
fi
298+
echo "::notice title=Released before the tier file::${PCAPKIT_TAG} predates ${tiers}; counting the tiers ${from} added it with."
299+
fi
300+
if [ "$from" != "$(git rev-parse HEAD)" ]; then
301+
released="$(mktemp -d)"
302+
git show "${from}:${tiers}" > "${released}/python-versions.toml"
303+
git show "${from}:util/python_versions.py" > "${released}/python_versions.py"
304+
wheels="$(python3 "${released}/python_versions.py" --file "${released}/python-versions.toml" pypi-wheels)"
305+
builds="$(python3 "${released}/python_versions.py" --file "${released}/python-versions.toml" conda-builds)"
306+
fi
307+
fi
308+
309+
echo "${PCAPKIT_TAG}: counting the wheels ${wheels} and the conda builds ${builds}"
310+
echo "pypi-wheels=${wheels}" >> "$GITHUB_OUTPUT"
311+
echo "conda-builds=${builds}" >> "$GITHUB_OUTPUT"
312+
313+
# PyPI evidence: compare the file count for this version against the one
314+
# expected -- a wheel from each supported leg (``pypi``'s ``python-version``
315+
# axis, whose wheel tags ``PCAPKIT_PYPI_WHEELS`` lists from the tiers read
316+
# above), plus the sdist that the "No Source Distribution" step keeps only
317+
# on the ``latest`` leg. A release-level 200 only proves *some*
270318
# file landed, not all of them, so presence alone -- ``PCAPKIT_TAG_EXISTS``'s
271319
# mistake, repeated -- is not enough. Only the supported tier's wheels are
272320
# counted (#1633): a KTLO leg's wheel neither completes the release nor,
@@ -292,7 +340,7 @@ jobs:
292340
id: check_pypi
293341
env:
294342
PCAPKIT_VERSION: ${{ steps.get_version.outputs.PCAPKIT_VERSION }}
295-
PCAPKIT_PYPI_WHEELS: ${{ steps.python-versions.outputs.pypi-wheels }}
343+
PCAPKIT_PYPI_WHEELS: ${{ steps.released.outputs.pypi-wheels }}
296344
run: |
297345
set -euo pipefail
298346
@@ -342,19 +390,19 @@ jobs:
342390
echo "http $status, found $count of $expected expected files for ${PCAPKIT_VERSION} on PyPI"
343391
echo "PCAPKIT_PYPI_COMPLETE=$complete" >> "$GITHUB_OUTPUT"
344392
345-
# Anaconda evidence, same shape as PyPI's above but for the 10-leg
346-
# ``conda`` matrix (2 OS x 5 Python versions). Unlike PyPI's, this endpoint
347-
# reported ``cf-cache-status: DYNAMIC`` on every request checked while
348-
# writing this, i.e. not cached -- so there is no staleness direction to
393+
# Anaconda evidence, same shape as PyPI's above but for the ``conda``
394+
# matrix (2 OS x the supported tier read above). Unlike PyPI's, this
395+
# endpoint reported ``cf-cache-status: DYNAMIC`` on every request checked
396+
# while writing this, i.e. not cached -- so there is no staleness direction to
349397
# reason about here. This is the job-level cost saver mirroring ``pypi``'s;
350398
# ``conda``'s own per-leg checks (see that job) are the actual correctness
351-
# fix and still run underneath it, since a release with only 9 of 10 legs
399+
# fix and still run underneath it, since a release one leg short of
352400
# complete must not have the missing leg mistaken for "nothing to do".
353401
- name: Check Anaconda publish completeness
354402
id: check_conda
355403
env:
356404
PCAPKIT_VERSION: ${{ steps.get_version.outputs.PCAPKIT_VERSION }}
357-
PCAPKIT_CONDA_BUILDS: ${{ steps.python-versions.outputs.conda-builds }}
405+
PCAPKIT_CONDA_BUILDS: ${{ steps.released.outputs.conda-builds }}
358406
run: |
359407
set -euo pipefail
360408
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
name: Python Versions
2+
3+
# #1660: keeps .github/python-versions.toml current. `detect` recomputes the
4+
# tiers from python.org's release cycle, PyPy's releases, setup-python's builds
5+
# and the hosted runner images (util/update_python_versions.py, whose docstring
6+
# gives the rules), rewrites the file and the prose the audit tests pin to it,
7+
# and runs that audit. When something changed, `pr` pushes the change to
8+
# `bot/python-versions` and opens that branch's pull request, or updates the
9+
# open one, for the owner to merge. Nothing here pushes `main` or edits ruleset
10+
# 23497679: the pull request body lists the required contexts to change by hand.
11+
#
12+
# Only these two triggers. A push to `bot/python-versions` starts the pull
13+
# request's own checks, which is why `pr` spends the release App's token rather
14+
# than GITHUB_TOKEN, whose pushes start nothing; nothing it starts runs this
15+
# workflow again.
16+
on:
17+
schedule:
18+
- cron: '41 5 * * 1' # Mondays at 05:41 UTC
19+
workflow_dispatch:
20+
21+
# Nothing by default; each job asks for what it needs.
22+
permissions: {}
23+
24+
# One run at a time, so two runs cannot race for the branch. A queued run is
25+
# kept, not cancelled: it recomputes from the sources and skips what the branch
26+
# already proposes.
27+
concurrency:
28+
group: python-versions
29+
cancel-in-progress: false
30+
31+
jobs:
32+
# No secret and a read-only token. The computation reads the network and
33+
# writes only this checkout, and the audit is the repository's own tests.
34+
detect:
35+
name: Recompute the Python version tiers
36+
runs-on: ubuntu-latest
37+
timeout-minutes: 15
38+
permissions:
39+
contents: read
40+
pull-requests: read
41+
outputs:
42+
changed: ${{ steps.update.outputs.changed }}
43+
publish: ${{ steps.update.outputs.publish }}
44+
45+
steps:
46+
- uses: actions/checkout@v7
47+
with:
48+
persist-credentials: false
49+
50+
# `latest` for this job, like every single-interpreter job (#1661).
51+
- name: Read the Python versions
52+
id: python-versions
53+
run: python3 util/python_versions.py --github-output
54+
55+
- uses: actions/setup-python@v7
56+
with:
57+
python-version: ${{ steps.python-versions.outputs.latest }}
58+
59+
# PyYAML, from the `test` extra, for the workflow readers and the audit;
60+
# neither imports pcapkit.
61+
- name: Install PyYAML
62+
run: python -m pip install PyYAML
63+
64+
# Fetches the four sources, rewrites the files, and writes the proposal to
65+
# $RUNNER_TEMP/proposal. `publish` is false when nothing changed, or when
66+
# `bot/python-versions` has an open pull request and a commit it holds
67+
# ahead of main proposes these tiers, hand edits or merges of main on top
68+
# or not (an anonymous, commits-only fetch), so an unmerged proposal is not
69+
# pushed, nor its approval asked for, every week. When that branch holds
70+
# commits it did not make and proposes other tiers, this fails as
71+
# `publish` would refuse, before any approval. A branch with no open pull
72+
# request was merged or closed, and is proposed afresh. The token reads
73+
# that pull request's state.
74+
- name: Recompute the tiers
75+
id: update
76+
env:
77+
GH_TOKEN: ${{ github.token }}
78+
run: >-
79+
python util/update_python_versions.py update --write --out "$RUNNER_TEMP/proposal"
80+
--proposal-branch bot/python-versions --github-output
81+
82+
# The three modules that pin the workflows and the prose to the file, on
83+
# the rewritten tree. A failure here does not stop the proposal: a tier
84+
# move can need hand edits the script names, and the body lists what failed.
85+
- name: Run the audit tests
86+
id: audit
87+
continue-on-error: true
88+
shell: bash
89+
run: >-
90+
python -m unittest -v tests.project.test_python_versions_1660
91+
tests.project.test_python_matrix_tiers_1633 tests.project.test_ktlo_legs_stay_green_1672
92+
2>&1 | tee "$RUNNER_TEMP/audit.log"
93+
94+
- name: Write the pull request body
95+
if: ${{ steps.update.outputs.changed == 'true' }}
96+
env:
97+
AUDIT_OUTCOME: ${{ steps.audit.outcome }}
98+
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
99+
run: >-
100+
python util/update_python_versions.py body "$RUNNER_TEMP/proposal"
101+
--audit-outcome "$AUDIT_OUTCOME" --audit-log "$RUNNER_TEMP/audit.log" --run-url "$RUN_URL"
102+
103+
# Kept for as long as an environment approval can wait. The proposal holds
104+
# files/.github/..., which upload-artifact leaves out unless told otherwise.
105+
- name: Upload the proposal
106+
if: ${{ steps.update.outputs.publish == 'true' }}
107+
uses: actions/upload-artifact@v7
108+
with:
109+
name: python-versions-proposal
110+
path: ${{ runner.temp }}/proposal
111+
include-hidden-files: true
112+
retention-days: 30
113+
114+
- name: Say why no pull request opens
115+
if: ${{ steps.update.outputs.publish == 'true' && vars.RELEASE_APP_CLIENT_ID == '' }}
116+
run: >-
117+
echo "::warning title=No pull request without the release App::vars.RELEASE_APP_CLIENT_ID is unset,
118+
so the pr job skips. Set up the App as docs/source/contributing/releasing.rst describes."
119+
120+
# With nothing changed the tree is main's, so a failing audit is main's own
121+
# failure: the run says so rather than passing green over it.
122+
- name: Fail on a failing audit of an unchanged tree
123+
if: ${{ steps.update.outputs.changed != 'true' && steps.audit.outcome == 'failure' }}
124+
run: |
125+
echo "::error title=The audit fails on main::the tiers need no change, but the audit fails; see above."
126+
exit 1
127+
128+
# The release App's private key is a secret of the `github-release`
129+
# environment, which needs the owner's approval, so he is asked only on a run
130+
# that has something to propose. The token is contents and pull requests
131+
# write and nothing else: no `workflows`, so it cannot push a workflow file,
132+
# and `update` writes none. `publish` force-pushes `bot/python-versions` alone,
133+
# and while its pull request is open refuses it if it holds commits it did not
134+
# make; with none open it recreates the branch from main.
135+
pr:
136+
name: Open or update the pull request
137+
needs: detect
138+
if: >-
139+
${{ needs.detect.outputs.publish == 'true' && vars.RELEASE_APP_CLIENT_ID != '' &&
140+
github.ref == 'refs/heads/main' }}
141+
runs-on: ubuntu-latest
142+
timeout-minutes: 10
143+
environment: github-release
144+
# GITHUB_TOKEN only downloads `detect`'s artifact; the App's token does the rest.
145+
permissions:
146+
actions: read
147+
148+
steps:
149+
- name: Mint the release App's token
150+
id: app
151+
uses: actions/create-github-app-token@v3
152+
with:
153+
client-id: ${{ vars.RELEASE_APP_CLIENT_ID }}
154+
private-key: ${{ secrets.RELEASE_APP_PRIVATE_KEY }}
155+
permission-contents: write
156+
permission-pull-requests: write
157+
158+
# The commit `detect` read, with full history for the branch's merge base.
159+
- uses: actions/checkout@v7
160+
with:
161+
ref: ${{ github.sha }}
162+
fetch-depth: 0
163+
token: ${{ steps.app.outputs.token }}
164+
165+
- name: Download the proposal
166+
env:
167+
GH_TOKEN: ${{ github.token }}
168+
run: >-
169+
gh run download "$GITHUB_RUN_ID" --repo "$GITHUB_REPOSITORY" --name python-versions-proposal
170+
--dir "$RUNNER_TEMP/proposal"
171+
172+
- name: Push the proposal and open or update its pull request
173+
env:
174+
GH_TOKEN: ${{ steps.app.outputs.token }}
175+
APP_SLUG: ${{ steps.app.outputs.app-slug }}
176+
APP_BOT_ID: ${{ vars.RELEASE_APP_BOT_ID }}
177+
run: python3 util/update_python_versions.py publish "$RUNNER_TEMP/proposal"

‎.github/workflows/lint.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ jobs:
160160
- uses: actions/checkout@v7
161161

162162
# One interpreter, not a matrix. Unit Tests and Python Compatibility sweep
163-
# 3.11-3.15, plus the non-blocking KTLO legs 3.7-3.10 (#1633), because
163+
# the supported tier, plus the non-blocking KTLO legs (#1633), because
164164
# they check that the package *runs* everywhere; these tools read the
165165
# source, and running them several times over would produce several
166166
# copies of the same findings for several times the runner cost.

‎.github/workflows/unit-tests.yml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ jobs:
316316
c['num_partial_branches'], round(pct(c), 2)] for name, c in sorted(packages.items())]
317317
total = round(data['totals']['percent_covered'], 2)
318318
tests = os.environ['TESTS_OUTCOME']
319-
json.dump({'total': total, 'tests': tests, 'packages': rows},
319+
json.dump({'total': total, 'tests': tests, 'packages': rows, 'python': os.environ['PYTHON']},
320320
open('coverage-comment/coverage-summary.json', 'w'))
321321
status = '' if tests == 'success' else f' -- **tests {tests}**, so this is partial'
322322
print(f"**Coverage: {total:.2f}%**{status} (unit tier, Python {os.environ['PYTHON']}, `{os.environ['SHA'][:9]}`)")
@@ -1748,8 +1748,8 @@ jobs:
17481748
- name: Run full test suite
17491749
run: python -m pytest -q -n auto --dist load
17501750

1751-
# Ruleset 23497679's required_status_checks requires six contexts: this
1752-
# job's `Required checks passed`, and five for `Compat Python 3.11`-`3.15`
1751+
# Ruleset 23497679's required_status_checks requires these contexts: this
1752+
# job's `Required checks passed`, and a `Compat Python` one per supported version,
17531753
# from the `compatibility` job in `.github/workflows/python-compatibility.yml`.
17541754
# This job aggregates the gating jobs of *this* file, the ones its `needs:`
17551755
# lists, because rulesets match check names literally and support no

0 commit comments

Comments
 (0)