Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions pkgs/development/interpreters/python/hooks/pip-build-hook.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 13 additions & 3 deletions pkgs/development/interpreters/python/hooks/pip-install-hook.sh
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not blocking, but: would be nice to do a trick like that /dev/null thing to get declarations of all these essential variables.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The /dev/null / source stuff was there before, see my reasoning in #351734 (comment) for removing it for now.

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@
'';
# 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
Comment on lines 13 to 15
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed, or just general clean‐up of the example?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change makes it closer to how an actual project would be structured. The resulting package after this change could be Python-imported like import my_project.

Such change is not strictly necessary to build the test anyway.

'';
in
# this build must never triger conflicts
pythonOnBuildForHost.pkgs.buildPythonPackage {
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
];
Expand Down
12 changes: 11 additions & 1 deletion pkgs/development/interpreters/python/hooks/pypa-build-hook.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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__/
Expand All @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -21,4 +24,4 @@ EOF
echo "Finished executing pythonOutputDistPhase"
}

preFixupPhases+=" pythonOutputDistPhase"
appendToVar preFixupPhases pythonOutputDistPhase
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# shellcheck shell=bash

echo "Sourcing setuptools-rust-hook"

setuptoolsRustSetup() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
mesonPythonBuildFlagsHook() {
# Add all of mesonFlags to -Csetup-args for pypa builds
for f in $mesonFlags; do
pypaBuildFlags+=" -Csetup-args=$f"
appendToVar pypaBuildFlags "-Csetup-args=$f"
# This requires pip>23.0.1, see: https://meson-python.readthedocs.io/en/latest/how-to-guides/config-settings.html
pipBuildFlags+=" --config-settings=setup-args=$f"
appendToVar pipBuildFlags "--config-settings=setup-args=$f"
done
}

Expand Down