Set minimal package versions by parsing Cargo.lock

This commit is contained in:
messense 2023-01-12 10:14:23 +08:00
parent 15b1ff6c9a
commit 40a51985b3
No known key found for this signature in database
GPG Key ID: BB41A8A2C716CCA9
1 changed files with 50 additions and 39 deletions

View File

@ -406,7 +406,14 @@ def check_changelog(session: nox.Session):
@nox.session(name="set-minimal-package-versions") @nox.session(name="set-minimal-package-versions")
def set_minimal_package_versions(session: nox.Session): def set_minimal_package_versions(session: nox.Session, venv_backend="none"):
from collections import defaultdict
try:
import tomllib as toml
except ImportError:
import toml
projects = ( projects = (
None, None,
"examples/decorator", "examples/decorator",
@ -414,6 +421,22 @@ def set_minimal_package_versions(session: nox.Session):
"examples/setuptools-rust-starter", "examples/setuptools-rust-starter",
"examples/word-count", "examples/word-count",
) )
min_pkg_versions = {
"indexmap": "1.6.2",
"hashbrown": "0.9.1",
"plotters": "0.3.1",
"plotters-svg": "0.3.1",
"plotters-backend": "0.3.2",
"bumpalo": "3.10.0",
"once_cell": "1.14.0",
"rayon": "1.5.3",
"rayon-core": "1.9.3",
# string_cache 0.8.4 depends on parking_lot 0.12
"string_cache": "0.8.3",
# 1.15.0 depends on hermit-abi 0.2.6 which has edition 2021 and breaks 1.48.0
"num_cpus": "1.14.0",
"parking_lot": "0.11.0",
}
# run cargo update first to ensure that everything is at highest # run cargo update first to ensure that everything is at highest
# possible version, so that this matches what CI will resolve to. # possible version, so that this matches what CI will resolve to.
@ -429,44 +452,32 @@ def set_minimal_package_versions(session: nox.Session):
external=True, external=True,
) )
_run_cargo_set_package_version(session, "indexmap", "1.6.2")
_run_cargo_set_package_version(session, "hashbrown:0.13.1", "0.9.1")
_run_cargo_set_package_version(session, "plotters", "0.3.1")
_run_cargo_set_package_version(session, "plotters-svg", "0.3.1")
_run_cargo_set_package_version(session, "plotters-backend", "0.3.2")
_run_cargo_set_package_version(session, "bumpalo", "3.10.0")
_run_cargo_set_package_version(session, "once_cell", "1.14.0")
_run_cargo_set_package_version(session, "rayon", "1.5.3")
_run_cargo_set_package_version(session, "rayon-core", "1.9.3")
# string_cache 0.8.4 depends on parking_lot 0.12
_run_cargo_set_package_version(session, "string_cache:0.8.4", "0.8.3")
# 1.15.0 depends on hermit-abi 0.2.6 which has edition 2021 and breaks 1.48.0
_run_cargo_set_package_version(session, "num_cpus", "1.14.0")
_run_cargo_set_package_version(
session, "num_cpus", "1.14.0", project="examples/word-count"
)
projects = (
None,
"examples/decorator",
"examples/maturin-starter",
"examples/setuptools-rust-starter",
"examples/word-count",
)
for project in projects: for project in projects:
_run_cargo_set_package_version( lock_file = Path(project or "") / "Cargo.lock"
session, "parking_lot:0.12.1", "0.11.0", project=project
)
_run_cargo_set_package_version(session, "once_cell", "1.14.0", project=project)
_run_cargo_set_package_version( def load_pkg_versions():
session, "rayon", "1.5.3", project="examples/word-count" cargo_lock = toml.loads(lock_file.read_text())
) # Cargo allows to depends on multiple versions of the same package
_run_cargo_set_package_version( pkg_versions = defaultdict(list)
session, "rayon-core", "1.9.3", project="examples/word-count" for pkg in cargo_lock["package"]:
) name = pkg["name"]
if name not in min_pkg_versions:
continue
pkg_versions[name].append(pkg["version"])
return pkg_versions
pkg_versions = load_pkg_versions()
for (pkg_name, min_version) in min_pkg_versions.items():
versions = pkg_versions.get(pkg_name, [])
for version in versions:
if version != min_version:
pkg_id = pkg_name + ":" + version
_run_cargo_set_package_version(
session, pkg_id, min_version, project=project
)
# assume `_run_cargo_set_package_version` has changed something
# and re-read `Cargo.lock`
pkg_versions = load_pkg_versions()
# As a smoke test, cargo metadata solves all dependencies, so # As a smoke test, cargo metadata solves all dependencies, so
# will break if any crates rely on cargo features not # will break if any crates rely on cargo features not
@ -595,12 +606,12 @@ def _run_cargo_publish(session: nox.Session, *, package: str) -> None:
def _run_cargo_set_package_version( def _run_cargo_set_package_version(
session: nox.Session, session: nox.Session,
package: str, pkg_id: str,
version: str, version: str,
*, *,
project: Optional[str] = None, project: Optional[str] = None,
) -> None: ) -> None:
command = ["cargo", "update", "-p", package, "--precise", version] command = ["cargo", "update", "-p", pkg_id, "--precise", version]
if project: if project:
command.append(f"--manifest-path={project}/Cargo.toml") command.append(f"--manifest-path={project}/Cargo.toml")
_run(session, *command, external=True) _run(session, *command, external=True)