python3Packages.pythonMetadataCheckHook: rewrite comparison in Python

This commit is contained in:
Robert Schütz
2026-07-27 13:56:37 -07:00
parent d3731cb537
commit b40c514e9a
3 changed files with 23 additions and 9 deletions

View File

@@ -369,6 +369,7 @@ in
inherit pythonInterpreter pythonSitePackages;
pythonWithPackaging = lib.getExe (pythonOnBuildForHost.withPackages (ps: [ ps.packaging ]));
retrieveMetadata = ./python-metadata-check-hook-retrieve.py;
compareMetadata = ./python-metadata-check-hook-compare.py;
};
meta = {
maintainers = [ lib.maintainers.dotlambda ];

View File

@@ -0,0 +1,21 @@
from packaging.version import InvalidVersion, parse, Version
from sys import argv, exit
derivation_pname = argv[1]
derivation_version = argv[2]
metadata_version = argv[3]
try:
parse(derivation_version)
except InvalidVersion as e:
print(e)
print('Make sure you follow https://github.com/NixOS/nixpkgs/blob/master/pkgs/README.md#versioning.')
exit(1)
if Version(derivation_version) != Version(metadata_version):
print(f"The '{derivation_pname}' derivation has version '{derivation_version}' but .dist-info/METADATA specifies version '{metadata_version}'.")
print('This usually means that the wrong version is hardcoded in pyproject.toml or setup.{py,cfg}.')
print("Use the pyprojectVersionPatchHook or patch the version manually so that the project metadata matches the derivation's version.")
exit(1)

View File

@@ -21,15 +21,7 @@ pythonMetadataCheckPhase() {
metadataVersion="$(PYTHONPATH="$pythonMetadataCheckOutput/@pythonSitePackages@:$PYTHONPATH" \
@pythonInterpreter@ -P @retrieveMetadata@ "$derivationPname")"
# check that both versions can be parsed
@pythonWithPackaging@ -c "from packaging.version import Version; from sys import argv; Version(argv[1]); Version(argv[2])" "$derivationVersion" "$metadataVersion"
if @pythonWithPackaging@ -c "from packaging.version import Version; from sys import argv, exit; exit(Version(argv[1]) == Version(argv[2]))" "$derivationVersion" "$metadataVersion"; then
echo "The '$derivationPname' derivation has version '$derivationVersion' but .dist-info/METADATA specifies version '$metadataVersion'."
echo "This usually means that the wrong version is hardcoded in pyproject.toml or setup.{py,cfg}."
echo "Use the pyprojectVersionPatchHook or patch the version manually so that the project metadata matches the derivation's version."
exit 1
fi
@pythonWithPackaging@ @compareMetadata@ "$derivationPname" "$derivationVersion" "$metadataVersion"
}
if [ -z "${dontCheckPythonMetadata-}" ]; then