From ccb418b69938ebdac9337baa9cdceeb43b3497cd Mon Sep 17 00:00:00 2001 From: Yueh-Shun Li Date: Mon, 7 Oct 2024 07:42:31 +0800 Subject: [PATCH] pythonNamespacesHook: lint with ShellCheck --- .../python/hooks/python-namespaces-hook.sh | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/pkgs/development/interpreters/python/hooks/python-namespaces-hook.sh b/pkgs/development/interpreters/python/hooks/python-namespaces-hook.sh index 5c5b840e05c3..add56f5b6413 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