diff --git a/pkgs/development/interpreters/python/hooks/pip-build-hook.sh b/pkgs/development/interpreters/python/hooks/pip-build-hook.sh index 3d70de729f2df..430951bac7c95 100644 --- a/pkgs/development/interpreters/python/hooks/pip-build-hook.sh +++ b/pkgs/development/interpreters/python/hooks/pip-build-hook.sh @@ -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 diff --git a/pkgs/development/interpreters/python/hooks/pip-install-hook.sh b/pkgs/development/interpreters/python/hooks/pip-install-hook.sh index a4f08b8b14cbd..0f718a6c4bb89 100644 --- a/pkgs/development/interpreters/python/hooks/pip-install-hook.sh +++ b/pkgs/development/interpreters/python/hooks/pip-install-hook.sh @@ -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 diff --git a/pkgs/development/interpreters/python/hooks/pypa-build-hook-test.nix b/pkgs/development/interpreters/python/hooks/pypa-build-hook-test.nix index 4153c21ca4f97..f862429d3987b 100644 --- a/pkgs/development/interpreters/python/hooks/pypa-build-hook-test.nix +++ b/pkgs/development/interpreters/python/hooks/pypa-build-hook-test.nix @@ -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 ]; diff --git a/pkgs/development/interpreters/python/hooks/pypa-build-hook.sh b/pkgs/development/interpreters/python/hooks/pypa-build-hook.sh index dd49d935bcee7..88472b4686881 100644 --- a/pkgs/development/interpreters/python/hooks/pypa-build-hook.sh +++ b/pkgs/development/interpreters/python/hooks/pypa-build-hook.sh @@ -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 diff --git a/pkgs/development/interpreters/python/hooks/python-imports-check-hook.sh b/pkgs/development/interpreters/python/hooks/python-imports-check-hook.sh index 591060aca6d06..c3c758d6a9027 100644 --- a/pkgs/development/interpreters/python/hooks/python-imports-check-hook.sh +++ b/pkgs/development/interpreters/python/hooks/python-imports-check-hook.sh @@ -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 diff --git a/pkgs/development/interpreters/python/hooks/python-namespaces-hook.sh b/pkgs/development/interpreters/python/hooks/python-namespaces-hook.sh index 5c5b840e05c37..add56f5b64136 100644 --- a/pkgs/development/interpreters/python/hooks/python-namespaces-hook.sh +++ b/pkgs/development/interpreters/python/hooks/python-namespaces-hook.sh @@ -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 diff --git a/pkgs/development/interpreters/python/hooks/python-output-dist-hook.sh b/pkgs/development/interpreters/python/hooks/python-output-dist-hook.sh index 83b79d01fa404..be54b546cadb8 100644 --- a/pkgs/development/interpreters/python/hooks/python-output-dist-hook.sh +++ b/pkgs/development/interpreters/python/hooks/python-output-dist-hook.sh @@ -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 <= 1)); then echo "pythonRelaxDepsHook: resulting METADATA for '$wheel':" + # shellcheck disable=SC2086 cat $metadata_file fi diff --git a/pkgs/development/interpreters/python/hooks/python-remove-tests-dir-hook.sh b/pkgs/development/interpreters/python/hooks/python-remove-tests-dir-hook.sh index ad9e3c07cf244..43c991b74469f 100644 --- a/pkgs/development/interpreters/python/hooks/python-remove-tests-dir-hook.sh +++ b/pkgs/development/interpreters/python/hooks/python-remove-tests-dir-hook.sh @@ -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" } diff --git a/pkgs/development/interpreters/python/hooks/setuptools-rust-hook.sh b/pkgs/development/interpreters/python/hooks/setuptools-rust-hook.sh index 917c19ef9b31b..5a0916f8acc41 100644 --- a/pkgs/development/interpreters/python/hooks/setuptools-rust-hook.sh +++ b/pkgs/development/interpreters/python/hooks/setuptools-rust-hook.sh @@ -1,3 +1,5 @@ +# shellcheck shell=bash + echo "Sourcing setuptools-rust-hook" setuptoolsRustSetup() { diff --git a/pkgs/development/python-modules/meson-python/add-build-flags.sh b/pkgs/development/python-modules/meson-python/add-build-flags.sh index d9327960eb1df..e1b2588f07fc4 100644 --- a/pkgs/development/python-modules/meson-python/add-build-flags.sh +++ b/pkgs/development/python-modules/meson-python/add-build-flags.sh @@ -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 }