python3Packages.pythonMetadataCheckHook: init

This commit is contained in:
Robert Schütz
2026-06-17 12:18:27 -07:00
parent 8ab1ccd1c1
commit 006c7936a2
3 changed files with 67 additions and 0 deletions
@@ -361,6 +361,20 @@ in
} ./python-imports-check-hook.sh
) { };
pythonMetadataCheckHook = callPackage (
{ makePythonHook }:
makePythonHook {
name = "python-metadata-check-hook.sh";
substitutions = {
inherit pythonInterpreter pythonSitePackages;
pythonWithPackaging = lib.getExe (pythonOnBuildForHost.withPackages (ps: [ ps.packaging ]));
};
meta = {
maintainers = [ lib.maintainers.dotlambda ];
};
} ./python-metadata-check-hook.sh
) { };
pythonNamespacesHook = callPackage (
{ makePythonHook, buildPackages }:
makePythonHook {
@@ -0,0 +1,37 @@
# shellcheck shell=bash
# Setup hook for checking whether metadata in .dist-info matches the derivation
echo "Sourcing python-metadata-check-hook.sh"
pythonMetadataCheckPhase() {
echo "Executing pythonMetadataCheckPhase"
# shellcheck disable=SC2154
pythonMetadataCheckOutput="$out"
if [[ -n "${python-}" ]]; then
echo "Using python specific output \$python for metadata check"
pythonMetadataCheckOutput=$python
fi
# shellcheck disable=SC2154
derivationPname="$pname"
# shellcheck disable=SC2154
derivationVersion="$version"
# `python -P` avoids picking up egg-info dirs in $PWD
metadataVersion="$(PYTHONPATH="$pythonMetadataCheckOutput/@pythonSitePackages@:$PYTHONPATH" \
@pythonInterpreter@ -P -c 'from importlib.metadata import version; import sys; print(version(sys.argv[1]))' "$derivationPname")"
# chethat 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
}
if [ -z "${dontCheckPythonMetadata-}" ]; then
echo "Using pythonMetadataCheckPhase"
appendToVar preDistPhases pythonMetadataCheckPhase
fi
@@ -18,6 +18,7 @@
pypaInstallHook,
pythonCatchConflictsHook,
pythonImportsCheckHook,
pythonMetadataCheckHook,
pythonNamespacesHook,
pythonOutputDistHook,
pythonRelaxDepsHook,
@@ -358,6 +359,21 @@ lib.extendMkDerivation {
# This is a test, however, it should be ran independent of the checkPhase and checkInputs
pythonImportsCheckHook
]
++
optionals
(
finalAttrs ? "pname"
&& finalAttrs ? "version"
# We don't care about the METADATA of Python applications.
&& isPythonModule finalAttrs.passthru
# METADATA is unlikely to be correct if pyproject is false or null.
&& pyproject == true
&& !lib.hasInfix "unstable-" finalAttrs.version
&& !isBootstrapPackage
)
[
pythonMetadataCheckHook
]
++ optionals (python.pythonAtLeast "3.3") [
# Optionally enforce PEP420 for python3
pythonNamespacesHook