python3Packages.pyprojectVersionPatchHook: init

This commit is contained in:
Robert Schütz
2026-07-01 19:05:01 -07:00
parent 3994a6869a
commit ff19be7fd1
3 changed files with 69 additions and 0 deletions
@@ -143,6 +143,24 @@ in
inherit (pythonOnBuildForHost.pkgs) installer;
};
pyprojectVersionPatchHook = callPackage (
{ makePythonHook }:
makePythonHook {
name = "pyproject-version-patch-hook.sh";
substitutions = {
pythonInterpreter =
(pythonOnBuildForHost.withPackages (ps: [
ps.packaging
ps.tomlkit
])).interpreter;
script = ./pyproject-version-patch-hook.py;
};
meta = {
maintainers = [ lib.maintainers.dotlambda ];
};
} ./pyproject-version-patch-hook.sh
) { };
pytestCheckHook = callPackage (
{
makePythonHook,
@@ -0,0 +1,37 @@
from functools import reduce
from operator import getitem
from packaging.version import Version
from sys import argv, exit
from tomlkit import dump, load
from tomlkit.exceptions import NonExistentKey
new_version = argv[1]
with open('pyproject.toml', 'r') as f:
pyproject = load(f)
sections = ['project', 'tool.poetry']
for path in sections:
try:
section = reduce(getitem, path.split('.'), pyproject)
except NonExistentKey:
continue
if 'version' in section:
if Version(section['version']) == Version(new_version):
print("The version in pyproject.toml already matches the derivation's version. Remove pyprojectVersionPatchHook.")
exit(1)
print(f"Changing version in pyproject.toml from '{section['version']}' to '{new_version}'.")
section['version'] = new_version
break
if 'dynamic' in section and 'version' in section['dynamic']:
print(f"Changing dynamic version in pyproject.toml to '{new_version}'.")
section['dynamic'].remove('version')
section['version'] = new_version
break
else:
print('No patchable version specification found in pyproject.toml.')
exit(1)
with open('pyproject.toml', 'w') as f:
dump(pyproject, f)
@@ -0,0 +1,14 @@
# shellcheck shell=bash
echo "Sourcing pyproject-version-patch-hook.sh"
pyprojectVersionPatchPhase() {
echo "Executing pyprojectVersionPatchPhase"
# shellcheck disable=SC2154
@pythonInterpreter@ @script@ "$version"
echo "Finished executing pyprojectVersionPatchPhase"
}
postPatchHooks+=(pyprojectVersionPatchPhase)