python3Packages.*Hook: support __structuredAttrs = true (the easier ones) (#351734)

This commit is contained in:
Emily
2024-11-05 03:02:40 +00:00
committed by GitHub
11 changed files with 91 additions and 39 deletions
@@ -1,22 +1,27 @@
# Setup hook to use for pip projects
echo "Sourcing pip-build-hook"
# shellcheck shell=bash
declare -a pipBuildFlags
echo "Sourcing pip-build-hook"
pipBuildPhase() {
echo "Executing pipBuildPhase"
runHook preBuild
mkdir -p dist
local -a flagsArray=(
--verbose
--no-index
--no-deps
--no-clean
--no-build-isolation
--wheel-dir dist
)
concatTo flagsArray pipBuildFlags
echo "Creating a wheel..."
@pythonInterpreter@ -m pip wheel \
--verbose \
--no-index \
--no-deps \
--no-clean \
--no-build-isolation \
--wheel-dir dist \
$pipBuildFlags .
echoCmd 'pip build flags' "${flagsArray[@]}"
@pythonInterpreter@ -m pip wheel "${flagsArray[@]}" .
echo "Finished creating a wheel..."
runHook postBuild
@@ -1,17 +1,27 @@
# Setup hook for pip.
echo "Sourcing pip-install-hook"
# shellcheck shell=bash
declare -a pipInstallFlags
echo "Sourcing pip-install-hook"
pipInstallPhase() {
echo "Executing pipInstallPhase"
runHook preInstall
# shellcheck disable=SC2154
mkdir -p "$out/@pythonSitePackages@"
export PYTHONPATH="$out/@pythonSitePackages@:$PYTHONPATH"
local -a flagsArray=(
--no-index
--no-warn-script-location
--prefix="$out"
--no-cache
)
concatTo flagsArray pipInstallFlags
pushd dist || return 1
@pythonInterpreter@ -m pip install ./*.whl --no-index --no-warn-script-location --prefix="$out" --no-cache $pipInstallFlags
echoCmd 'pip install flags' "${flagsArray[@]}"
@pythonInterpreter@ -m pip install ./*.whl "${flagsArray[@]}"
popd || return 1
runHook postInstall
@@ -10,9 +10,9 @@
'';
# the source of the example project
projectSource = runCommand "my-project-source" {} ''
mkdir -p $out/src
mkdir -p $out/src/my_project
cp ${pyprojectToml} $out/pyproject.toml
touch $out/src/__init__.py
touch $out/src/my_project/__init__.py
'';
in
# this build must never triger conflicts
@@ -20,11 +20,13 @@
pname = "dont-propagate-conflicting-deps";
version = "0.0.0";
src = projectSource;
format = "pyproject";
propagatedBuildInputs = [
pyproject = true;
dependencies = [
# At least one dependency of `build` should be included here to
# keep the test meaningful
(mkConflict pythonOnBuildForHost.pkgs.tomli)
];
build-system = [
# setuptools is also needed to build the example project
pythonOnBuildForHost.pkgs.setuptools
];
@@ -1,12 +1,22 @@
# Setup hook to use for pypa/build projects
# shellcheck shell=bash
echo "Sourcing pypa-build-hook"
pypaBuildPhase() {
echo "Executing pypaBuildPhase"
runHook preBuild
local -a flagsArray=(
--no-isolation
--outdir dist/
--wheel
)
concatTo flagsArray pypaBuildFlags
echo "Creating a wheel..."
@build@/bin/pyproject-build --no-isolation --outdir dist/ --wheel $pypaBuildFlags
echoCmd 'pypa build flags' "${flagsArray[@]}"
@build@/bin/pyproject-build "${flagsArray[@]}"
echo "Finished creating a wheel..."
runHook postBuild
@@ -1,22 +1,28 @@
# shellcheck shell=bash
# Setup hook for checking whether Python imports succeed
echo "Sourcing python-imports-check-hook.sh"
pythonImportsCheckPhase() {
echo "Executing pythonImportsCheckPhase"
if [ -n "$pythonImportsCheck" ]; then
echo "Check whether the following modules can be imported: $pythonImportsCheck"
pythonImportsCheckOutput=$out
if [ -n "$python" ]; then
if [[ -n "${pythonImportsCheck[*]-}" ]]; then
echo "Check whether the following modules can be imported: ${pythonImportsCheck[*]}"
# shellcheck disable=SC2154
pythonImportsCheckOutput="$out"
if [[ -n "${python-}" ]]; then
echo "Using python specific output \$python for imports check"
pythonImportsCheckOutput=$python
fi
export PYTHONPATH="$pythonImportsCheckOutput/@pythonSitePackages@:$PYTHONPATH"
(cd $pythonImportsCheckOutput && @pythonCheckInterpreter@ -c 'import os; import importlib; list(map(lambda mod: importlib.import_module(mod), os.environ["pythonImportsCheck"].split()))')
# Python modules and namespaces names are Python identifiers, which must not contain spaces.
# See https://docs.python.org/3/reference/lexical_analysis.html
# shellcheck disable=SC2048,SC2086
(cd "$pythonImportsCheckOutput" && @pythonCheckInterpreter@ -c 'import sys; import importlib; list(map(lambda mod: importlib.import_module(mod), sys.argv[1:]))' ${pythonImportsCheck[*]})
fi
}
if [ -z "${dontUsePythonImportsCheck-}" ]; then
if [[ -z "${dontUsePythonImportsCheck-}" ]]; then
echo "Using pythonImportsCheckPhase"
appendToVar preDistPhases pythonImportsCheckPhase
fi
@@ -1,20 +1,26 @@
# Clean up __init__.py's found in namespace directories
# shellcheck shell=bash
echo "Sourcing python-namespaces-hook"
pythonNamespacesHook() {
echo "Executing pythonNamespacesHook"
for namespace in ${pythonNamespaces[@]}; do
# Python namespaces names are Python identifiers, which must not contain spaces.
# See https://docs.python.org/3/reference/lexical_analysis.html
# shellcheck disable=SC2048
for namespace in ${pythonNamespaces[*]-}; do
echo "Enforcing PEP420 namespace: ${namespace}"
# split namespace into segments. "azure.mgmt" -> "azure mgmt"
IFS='.' read -ra pathSegments <<<$namespace
IFS='.' read -ra pathSegments <<<"$namespace"
# shellcheck disable=SC2154
constructedPath=$out/@pythonSitePackages@
# Need to remove the __init__.py at each namespace level
# E.g `azure/__init__.py` and `azure/mgmt/__init__.py`
# The __pycache__ entry also needs to be removed
for pathSegment in ${pathSegments[@]}; do
for pathSegment in "${pathSegments[@]}"; do
constructedPath=${constructedPath}/${pathSegment}
pathToRemove=${constructedPath}/__init__.py
pycachePath=${constructedPath}/__pycache__/
@@ -30,9 +36,9 @@ pythonNamespacesHook() {
# event of a "meta-package" package, which will just install
# other packages, but not produce anything in site-packages
# besides meta information
if [ -d "${constructedPath}/../" -a -z ${dontRemovePth-} ]; then
if [[ -d "${constructedPath}/../" ]] && [[ -z "${dontRemovePth-}" ]]; then
# .pth files are located in the parent directory of a module
@findutils@/bin/find ${constructedPath}/../ -name '*-nspkg.pth' -exec rm -v "{}" +
@findutils@/bin/find "${constructedPath}/../" -name '*-nspkg.pth' -exec rm -v "{}" +
fi
# remove __pycache__/ entry, can be interpreter specific. E.g. __init__.cpython-38.pyc
@@ -46,6 +52,6 @@ pythonNamespacesHook() {
echo "Finished executing pythonNamespacesHook"
}
if [ -z "${dontUsePythonNamespacesHook-}" -a -n "${pythonNamespaces-}" ]; then
if [[ -z "${dontUsePythonNamespacesHook-}" ]] && [[ -n "${pythonNamespaces-}" ]]; then
postFixupHooks+=(pythonNamespacesHook)
fi
@@ -1,9 +1,12 @@
# Setup hook for storing dist folder (wheels/sdists) in a separate output
# shellcheck shell=bash
echo "Sourcing python-catch-conflicts-hook.sh"
pythonOutputDistPhase() {
echo "Executing pythonOutputDistPhase"
if [[ -d dist ]]; then
# shellcheck disable=SC2154
mv "dist" "$dist"
else
cat >&2 <<EOF
@@ -21,4 +24,4 @@ EOF
echo "Finished executing pythonOutputDistPhase"
}
preFixupPhases+=" pythonOutputDistPhase"
appendToVar preFixupPhases pythonOutputDistPhase
@@ -42,13 +42,14 @@
_pythonRelaxDeps() {
local -r metadata_file="$1"
if [[ -z "${pythonRelaxDeps:-}" ]] || [[ "$pythonRelaxDeps" == 0 ]]; then
if [[ -z "${pythonRelaxDeps[*]-}" ]] || [[ "$pythonRelaxDeps" == 0 ]]; then
return
elif [[ "$pythonRelaxDeps" == 1 ]]; then
sed -i "$metadata_file" -r \
-e 's/(Requires-Dist: [a-zA-Z0-9_.-]+\s*(\[[^]]+\])?)[^;]*(;.*)?/\1\3/'
else
for dep in $pythonRelaxDeps; do
# shellcheck disable=SC2048
for dep in ${pythonRelaxDeps[*]}; do
sed -i "$metadata_file" -r \
-e "s/(Requires-Dist: $dep\s*(\[[^]]+\])?)[^;]*(;.*)?/\1\3/i"
done
@@ -58,13 +59,14 @@ _pythonRelaxDeps() {
_pythonRemoveDeps() {
local -r metadata_file="$1"
if [[ -z "${pythonRemoveDeps:-}" ]] || [[ "$pythonRemoveDeps" == 0 ]]; then
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
# shellcheck disable=SC2048
for dep in ${pythonRemoveDeps[*]-}; do
sed -i "$metadata_file" \
-e "/Requires-Dist: $dep/d"
done
@@ -86,11 +88,14 @@ pythonRelaxDepsHook() {
rm -rf "$wheel"
# Using no quotes on purpose since we need to expand the glob from `$metadata_file`
# shellcheck disable=SC2086
_pythonRelaxDeps $metadata_file
# shellcheck disable=SC2086
_pythonRemoveDeps $metadata_file
if (("${NIX_DEBUG:-0}" >= 1)); then
echo "pythonRelaxDepsHook: resulting METADATA for '$wheel':"
# shellcheck disable=SC2086
cat $metadata_file
fi
@@ -1,11 +1,14 @@
# Clean up top-level tests directory in site-package installation.
# shellcheck shell=bash
echo "Sourcing python-remove-tests-dir-hook"
pythonRemoveTestsDir() {
echo "Executing pythonRemoveTestsDir"
rm -rf $out/@pythonSitePackages@/tests
rm -rf $out/@pythonSitePackages@/test
# shellcheck disable=SC2154
rm -rf "$out/@pythonSitePackages@/tests"
rm -rf "$out/@pythonSitePackages@/test"
echo "Finished executing pythonRemoveTestsDir"
}
@@ -1,3 +1,5 @@
# shellcheck shell=bash
echo "Sourcing setuptools-rust-hook"
setuptoolsRustSetup() {