Files
photocraft/scripts/ci-toolchain-bump.py
T
aiongg 5612b2dfd4 ci: pin the toolchain in rust-toolchain.toml
The file is the one source: the jobs install what it names through
craft-common-actions' rust-channel action, and the nightly run opens a pull
request that changes only the file when a newer stable exists.

Part of #15711
2026-11-20 04:14:10 +00:00

66 lines
2.1 KiB
Python

#!/usr/bin/env python3
"""Moves rust-toolchain.toml to the newest stable Rust release.
python3 scripts/ci-toolchain-bump.py prints the newest stable release when it is newer than the file's
python3 scripts/ci-toolchain-bump.py --write V sets the file's channel to V
CI's nightly run (ci.yml `toolchain-bump`) runs both and opens a pull request that changes only
rust-toolchain.toml. Prints nothing when the file is current.
"""
import re
import sys
import tomllib
import urllib.request
from pathlib import Path
FILE = Path(__file__).resolve().parent.parent / 'rust-toolchain.toml'
MANIFEST = 'https://static.rust-lang.org/dist/channel-rust-stable.toml'
CHANNEL = re.compile(r'^(channel\s*=\s*")([^"]*)(")', re.M)
RELEASE = re.compile(r'^\d+\.\d+\.\d+$')
def numbers(version):
return tuple(int(part) for part in version.split('.'))
def current(text):
return tomllib.loads(text)['toolchain']['channel']
def latest(manifest_text):
"""`1.101.0` from the stable manifest's `[pkg.rust] version = "1.101.0 (<hash> <date>)"`."""
version = tomllib.loads(manifest_text)['pkg']['rust']['version'].split()[0]
if not RELEASE.match(version):
raise ValueError(f'unexpected stable version {version!r}')
return version
def newer(file_text, manifest_text):
"""The stable release when it is newer than the file's channel (a `1.x.y` release), else None."""
pinned = current(file_text)
stable = latest(manifest_text)
if RELEASE.match(pinned) and numbers(stable) <= numbers(pinned):
return None
return stable
def with_channel(file_text, version):
if not RELEASE.match(version):
raise ValueError(f'not a release: {version!r}')
return CHANNEL.sub(lambda m: m.group(1) + version + m.group(3), file_text, count=1)
def main(argv):
if argv[:1] == ['--write']:
FILE.write_text(with_channel(FILE.read_text(), argv[1]))
return
with urllib.request.urlopen(MANIFEST, timeout=30) as response:
stable = newer(FILE.read_text(), response.read().decode())
if stable:
print(stable)
if __name__ == '__main__':
main(sys.argv[1:])