1# Clean up __init__.py's found in namespace directories
2# shellcheck shell=bash
3
4echo "Sourcing python-namespaces-hook"
5
6pythonNamespacesHook() {
7 echo "Executing pythonNamespacesHook"
8
9 # Python namespaces names are Python identifiers, which must not contain spaces.
10 # See https://docs.python.org/3/reference/lexical_analysis.html
11 # shellcheck disable=SC2048
12 for namespace in ${pythonNamespaces[*]-}; do
13 echo "Enforcing PEP420 namespace: ${namespace}"
14
15 # split namespace into segments. "azure.mgmt" -> "azure mgmt"
16 IFS='.' read -ra pathSegments <<<"$namespace"
17 # shellcheck disable=SC2154
18 constructedPath=$out/@pythonSitePackages@
19
20 # Need to remove the __init__.py at each namespace level
21 # E.g `azure/__init__.py` and `azure/mgmt/__init__.py`
22 # The __pycache__ entry also needs to be removed
23 for pathSegment in "${pathSegments[@]}"; do
24 constructedPath=${constructedPath}/${pathSegment}
25 pathToRemove=${constructedPath}/__init__.py
26 pycachePath=${constructedPath}/__pycache__/
27
28 # remove __init__.py
29 if [ -f "$pathToRemove" ]; then
30 rm -v "$pathToRemove"
31 fi
32
33 # remove ${pname}-${version}-${python-interpeter}-nspkg.pth
34 #
35 # Still need to check that parent directory exists in the
36 # event of a "meta-package" package, which will just install
37 # other packages, but not produce anything in site-packages
38 # besides meta information
39 if [[ -d "${constructedPath}/../" ]] && [[ -z "${dontRemovePth-}" ]]; then
40 # .pth files are located in the parent directory of a module
41 @findutils@/bin/find "${constructedPath}/../" -name '*-nspkg.pth' -exec rm -v "{}" +
42 fi
43
44 # remove __pycache__/ entry, can be interpreter specific. E.g. __init__.cpython-38.pyc
45 # use null characters to perserve potential whitespace in filepath
46 if [ -d "$pycachePath" ]; then
47 @findutils@/bin/find "$pycachePath" -name '__init__*' -exec rm -v "{}" +
48 fi
49 done
50 done
51
52 echo "Finished executing pythonNamespacesHook"
53}
54
55if [[ -z "${dontUsePythonNamespacesHook-}" ]] && [[ -n "${pythonNamespaces-}" ]]; then
56 postFixupHooks+=(pythonNamespacesHook)
57fi