diff --git a/pkgs/development/interpreters/python/hooks/default.nix b/pkgs/development/interpreters/python/hooks/default.nix index 1006172c9945..f5f8dc59c072 100644 --- a/pkgs/development/interpreters/python/hooks/default.nix +++ b/pkgs/development/interpreters/python/hooks/default.nix @@ -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, diff --git a/pkgs/development/interpreters/python/hooks/pyproject-version-patch-hook.py b/pkgs/development/interpreters/python/hooks/pyproject-version-patch-hook.py new file mode 100644 index 000000000000..53eef8166a3c --- /dev/null +++ b/pkgs/development/interpreters/python/hooks/pyproject-version-patch-hook.py @@ -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) diff --git a/pkgs/development/interpreters/python/hooks/pyproject-version-patch-hook.sh b/pkgs/development/interpreters/python/hooks/pyproject-version-patch-hook.sh new file mode 100644 index 000000000000..2627772874a6 --- /dev/null +++ b/pkgs/development/interpreters/python/hooks/pyproject-version-patch-hook.sh @@ -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)