2022-05-18 14:49:53 +00:00
|
|
|
# shellcheck shell=bash
|
|
|
|
|
|
|
|
# Setup hook that modifies Python dependencies versions.
|
|
|
|
#
|
|
|
|
# Example usage in a derivation:
|
|
|
|
#
|
|
|
|
# { …, pythonPackages, … }:
|
|
|
|
#
|
|
|
|
# pythonPackages.buildPythonPackage {
|
|
|
|
# …
|
|
|
|
# nativeBuildInputs = [ pythonPackages.pythonRelaxDepsHook ];
|
|
|
|
#
|
|
|
|
# # This will relax the dependency restrictions
|
|
|
|
# # e.g.: abc>1,<=2 -> abc
|
|
|
|
# pythonRelaxDeps = [ "abc" ];
|
|
|
|
# # This will relax all dependencies restrictions instead
|
|
|
|
# # pythonRelaxDeps = true;
|
|
|
|
# # This will remove the dependency
|
|
|
|
# # e.g.: cde>1,<=2 -> <nothing>
|
|
|
|
# pythonRemoveDeps = [ "cde" ];
|
|
|
|
# # This will remove all dependencies from the project
|
|
|
|
# # pythonRemoveDeps = true;
|
|
|
|
# …
|
|
|
|
# }
|
2022-11-21 17:40:18 +00:00
|
|
|
#
|
|
|
|
# IMPLEMENTATION NOTES:
|
|
|
|
#
|
|
|
|
# The "Requires-Dist" dependency specification format is described in PEP 508.
|
|
|
|
# Examples that the regular expressions in this hook needs to support:
|
|
|
|
#
|
|
|
|
# Requires-Dist: foo
|
|
|
|
# -> foo
|
|
|
|
# Requires-Dist: foo[optional]
|
|
|
|
# -> foo[optional]
|
|
|
|
# Requires-Dist: foo[optional]~=1.2.3
|
|
|
|
# -> foo[optional]
|
|
|
|
# Requires-Dist: foo[optional, xyz] (~=1.2.3)
|
|
|
|
# -> foo[optional, xyz]
|
|
|
|
# Requires-Dist: foo[optional]~=1.2.3 ; os_name = "posix"
|
|
|
|
# -> foo[optional] ; os_name = "posix"
|
|
|
|
#
|
|
|
|
# Currently unsupported: URL specs (foo @ https://example.com/a.zip).
|
2022-05-18 14:49:53 +00:00
|
|
|
|
|
|
|
_pythonRelaxDeps() {
|
|
|
|
local -r metadata_file="$1"
|
|
|
|
|
|
|
|
if [[ -z "${pythonRelaxDeps:-}" ]] || [[ "$pythonRelaxDeps" == 0 ]]; then
|
|
|
|
return
|
|
|
|
elif [[ "$pythonRelaxDeps" == 1 ]]; then
|
|
|
|
sed -i "$metadata_file" -r \
|
2022-11-21 17:40:18 +00:00
|
|
|
-e 's/(Requires-Dist: [a-zA-Z0-9_.-]+\s*(\[[^]]+\])?)[^;]*(;.*)?/\1\3/'
|
2022-05-18 14:49:53 +00:00
|
|
|
else
|
|
|
|
for dep in $pythonRelaxDeps; do
|
|
|
|
sed -i "$metadata_file" -r \
|
2024-01-13 08:15:51 +00:00
|
|
|
-e "s/(Requires-Dist: $dep\s*(\[[^]]+\])?)[^;]*(;.*)?/\1\3/i"
|
2022-05-18 14:49:53 +00:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
_pythonRemoveDeps() {
|
|
|
|
local -r metadata_file="$1"
|
|
|
|
|
|
|
|
if [[ -z "${pythonRemoveDeps:-}" ]] || [[ "$pythonRemoveDeps" == 0 ]]; then
|
|
|
|
return
|
|
|
|
elif [[ "$pythonRemoveDeps" == 1 ]]; then
|
|
|
|
sed -i "$metadata_file" \
|
|
|
|
-e '/Requires-Dist:.*/d'
|
|
|
|
else
|
|
|
|
for dep in $pythonRemoveDeps; do
|
|
|
|
sed -i "$metadata_file" \
|
|
|
|
-e "/Requires-Dist: $dep/d"
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
pythonRelaxDepsHook() {
|
|
|
|
pushd dist
|
|
|
|
|
2022-07-14 12:49:19 +00:00
|
|
|
# See https://peps.python.org/pep-0491/#escaping-and-unicode
|
2023-10-09 19:29:22 +00:00
|
|
|
local -r pkg_name="${pname//[^[:alnum:].]/_}"
|
2022-05-18 14:49:53 +00:00
|
|
|
local -r unpack_dir="unpacked"
|
2023-10-09 19:29:22 +00:00
|
|
|
local -r metadata_file="$unpack_dir/$pkg_name*/$pkg_name*.dist-info/METADATA"
|
2022-05-18 14:49:53 +00:00
|
|
|
|
2022-07-14 12:49:19 +00:00
|
|
|
# We generally shouldn't have multiple wheel files, but let's be safer here
|
|
|
|
for wheel in "$pkg_name"*".whl"; do
|
2023-10-09 19:29:22 +00:00
|
|
|
PYTHONPATH="@wheel@/@pythonSitePackages@:$PYTHONPATH" \
|
|
|
|
@pythonInterpreter@ -m wheel unpack --dest "$unpack_dir" "$wheel"
|
2022-07-14 12:49:19 +00:00
|
|
|
rm -rf "$wheel"
|
2022-05-18 14:49:53 +00:00
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
# Using no quotes on purpose since we need to expand the glob from `$metadata_file`
|
|
|
|
_pythonRelaxDeps $metadata_file
|
|
|
|
_pythonRemoveDeps $metadata_file
|
2022-05-18 14:49:53 +00:00
|
|
|
|
2022-07-14 12:49:19 +00:00
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then
|
|
|
|
echo "pythonRelaxDepsHook: resulting METADATA for '$wheel':"
|
2023-10-09 19:29:22 +00:00
|
|
|
cat $metadata_file
|
2022-07-14 12:49:19 +00:00
|
|
|
fi
|
2022-05-18 14:49:53 +00:00
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
PYTHONPATH="@wheel@/@pythonSitePackages@:$PYTHONPATH" \
|
|
|
|
@pythonInterpreter@ -m wheel pack "$unpack_dir/$pkg_name"*
|
2022-07-14 12:49:19 +00:00
|
|
|
done
|
2022-05-18 14:49:53 +00:00
|
|
|
|
2022-10-30 15:09:59 +00:00
|
|
|
# Remove the folder since it will otherwise be in the dist output.
|
|
|
|
rm -rf "$unpack_dir"
|
|
|
|
|
2022-05-18 14:49:53 +00:00
|
|
|
popd
|
|
|
|
}
|
|
|
|
|
|
|
|
postBuild+=" pythonRelaxDepsHook"
|