Skip to content

Commit ca7c0e2

Browse files
Include version and checksum validation when updating packages (#27)
This PR closes #24. Here, we now check against two conditions when updating the package - whether the version is up to date plus valid, and - whether the SHA-256 checksum matches the one present in the recipe file. Therefore, updating the package has three scenarios, and here is a summary of what happens: - local version updated manually to the newest available PyPI version ➡️ checksums will be updated, too - both version and checksum are out of date ➡️ both of them will be updated (as it was before these changes) - the checksum is correct and the version is out of date ➡️ this is enough information for us to proceed with updating the version Additionally, there's now a case where one manually updates the version (in error, for example), to a version that is not released or available on PyPI (yet), which means that the metadata for the version won't exist either. We raise an exception early here, asking the user so that they can check the version while updating. This is a rare situation, so it's more about raising a helpful error that aids the user in debugging the problem. I have skipped adding a test for such a case, but I can add one if needed. Please let me know your thoughts!
1 parent f1f36bd commit ca7c0e2

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Changed
1111

12+
- The `pyodide skeleton pypi --update` command and the `--update-patched` variant now
13+
validate the version and the source checksum when updating a package's recipe.
14+
[#27](https://github.com/pyodide/pyodide-build/pull/27)
15+
1216
- `pyo3_config_file` is no longer available in `pyodide config` command.
1317
Pyodide now sets `PYO3_CROSS_PYTHON_VERSION`, `PYO3_CROSS_LIB_DIR` to specify the cross compilation environment
1418
for PyO3.

pyodide_build/mkpkg.py

+32-3
Original file line numberDiff line numberDiff line change
@@ -245,18 +245,46 @@ def update_package(
245245
old_fmt = "sdist"
246246

247247
pypi_metadata = _get_metadata(package, version)
248+
249+
# Grab versions from metadata
248250
pypi_ver = Version(pypi_metadata["info"]["version"])
249251
local_ver = Version(yaml_content["package"]["version"])
250-
already_up_to_date = pypi_ver <= local_ver and (
252+
253+
# and grab checksums from metadata
254+
source_fmt = source_fmt or old_fmt
255+
dist_metadata = _find_dist(pypi_metadata, [source_fmt])
256+
sha256 = dist_metadata["digests"]["sha256"]
257+
sha256_local = yaml_content["source"].get("sha256")
258+
259+
# fail if local version is newer than PyPI version
260+
# since updating isn't possible in that case
261+
if pypi_ver < local_ver:
262+
raise MkpkgFailedException(
263+
f"Local version {local_ver} is newer than PyPI version {pypi_ver}, "
264+
f"cannot update {package}. Please verify in case the version was "
265+
"updated manually and is correct."
266+
)
267+
268+
# conditions to check if the package is up to date
269+
is_sha256_up_to_date = sha256 == sha256_local
270+
is_version_up_to_date = pypi_ver == local_ver
271+
272+
already_up_to_date = (is_sha256_up_to_date and is_version_up_to_date) and (
251273
source_fmt is None or source_fmt == old_fmt
252274
)
253275
if already_up_to_date:
254276
logger.success(
255-
f"{package} already up to date. Local: {local_ver} PyPI: {pypi_ver}"
277+
f"{package} already up to date."
278+
f" Local: {local_ver} and PyPI: {pypi_ver}"
279+
f" and checksum received: {sha256} matches local: {sha256_local} ✅"
256280
)
257281
return
258282

259-
logger.info(f"{package} is out of date: {local_ver} <= {pypi_ver}.")
283+
logger.info(
284+
f"{package} is out of date:"
285+
f" either {local_ver} < {pypi_ver}"
286+
f" or checksums might have mismatched: received {sha256} against local {sha256_local} 🚨"
287+
)
260288

261289
if yaml_content["source"].get("patches"):
262290
if update_patched:
@@ -267,6 +295,7 @@ def update_package(
267295
else:
268296
raise MkpkgFailedException(
269297
f"Pyodide applies patches to {package}. Skipping update."
298+
f" Use --update-patched to force updating {package}."
270299
)
271300

272301
if source_fmt:

0 commit comments

Comments
 (0)