aboutsummaryrefslogtreecommitdiff
path: root/infra/libkookie/nixpkgs/pkgs/development/interpreters/python/hooks/python-namespaces-hook.sh
blob: 15d2bd0eb34c5495d419c1e87bf8d799f06f23b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Clean up __init__.py's found in namespace directories
echo "Sourcing python-namespaces-hook"

pythonNamespacesHook() {
    echo "Executing pythonNamespacesHook"

    for namespace in ${pythonNamespaces[@]}; do
        echo "Enforcing PEP420 namespace: ${namespace}"

        # split namespace into segments. "azure.mgmt" -> "azure mgmt"
        IFS='.' read -ra pathSegments <<< $namespace
        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
            constructedPath=${constructedPath}/${pathSegment}
            pathToRemove=${constructedPath}/__init__.py
            pycachePath=${constructedPath}/__pycache__/

            # remove __init__.py
            if [ -f "$pathToRemove" ]; then
                rm -v "$pathToRemove"
            fi

            # remove __pycache__/ entry, can be interpreter specific. E.g. __init__.cpython-38.pyc
            # use null characters to perserve potential whitespace in filepath
            if [ -d "$pycachePath" ]; then
                @findutils@/bin/find "$pycachePath" -name '__init__*' -exec rm -v "{}" +
            fi
        done
    done

    echo "Finished executing pythonNamespacesHook"
}

if [ -z "${dontUsePythonNamespacesHook-}" -a -n "${pythonNamespaces-}" ]; then
    postFixupHooks+=(pythonNamespacesHook)
fi