aboutsummaryrefslogtreecommitdiff
path: root/nixpkgs/pkgs/development/interpreters/python/hooks/python-recompile-bytecode-hook.sh
blob: 649d0c17ea0c8465bd68d523b70fa82aba90b40b (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
# Setup hook for recompiling bytecode.
# https://github.com/NixOS/nixpkgs/issues/81441
echo "Sourcing python-recompile-bytecode-hook.sh"

# Remove all bytecode from the $out output. Then, recompile only site packages folder
# Note this effectively duplicates `python-remove-bin-bytecode`, but long-term
# this hook should be removed again.

pythonRecompileBytecodePhase () {
    # TODO: consider other outputs than $out

    items="$(find "$out" -name "@bytecodeName@")"
    if [[ -n $items ]]; then
        for pycache in $items; do
            rm -rf "$pycache"
        done
    fi

    find "$out"/@pythonSitePackages@ -name "*.py" -exec @pythonInterpreter@ -OO -m compileall @compileArgs@ {} +
}

if [ -z "${dontUsePythonRecompileBytecode-}" ]; then
    postPhases+=" pythonRecompileBytecodePhase"
fi