From b40c514e9a544e4947d7b0dfd54381c0eebeda88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=BCtz?= Date: Mon, 27 Jul 2026 13:56:37 -0700 Subject: [PATCH] python3Packages.pythonMetadataCheckHook: rewrite comparison in Python --- .../interpreters/python/hooks/default.nix | 1 + .../python-metadata-check-hook-compare.py | 21 +++++++++++++++++++ .../hooks/python-metadata-check-hook.sh | 10 +-------- 3 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 pkgs/development/interpreters/python/hooks/python-metadata-check-hook-compare.py diff --git a/pkgs/development/interpreters/python/hooks/default.nix b/pkgs/development/interpreters/python/hooks/default.nix index 183bc7af3592..afe3f132ac64 100644 --- a/pkgs/development/interpreters/python/hooks/default.nix +++ b/pkgs/development/interpreters/python/hooks/default.nix @@ -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 ]; diff --git a/pkgs/development/interpreters/python/hooks/python-metadata-check-hook-compare.py b/pkgs/development/interpreters/python/hooks/python-metadata-check-hook-compare.py new file mode 100644 index 000000000000..09de970cbd4c --- /dev/null +++ b/pkgs/development/interpreters/python/hooks/python-metadata-check-hook-compare.py @@ -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) diff --git a/pkgs/development/interpreters/python/hooks/python-metadata-check-hook.sh b/pkgs/development/interpreters/python/hooks/python-metadata-check-hook.sh index 2f2dfd126f9c..a328f319b00c 100644 --- a/pkgs/development/interpreters/python/hooks/python-metadata-check-hook.sh +++ b/pkgs/development/interpreters/python/hooks/python-metadata-check-hook.sh @@ -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