buildPythonPackage: recompile bytecode for reproducibility
Due to a change in pip the unpacked wheels are no longer reproducible. We recompile the bytecode to cleanup this error. https://github.com/NixOS/nixpkgs/issues/81441
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
# Hooks for building Python packages.
|
||||
{ python
|
||||
, lib
|
||||
, callPackage
|
||||
, makeSetupHook
|
||||
, disabledIf
|
||||
@@ -97,6 +98,16 @@ in rec {
|
||||
};
|
||||
} ./python-namespaces-hook.sh) {};
|
||||
|
||||
pythonRecompileBytecodeHook = callPackage ({ }:
|
||||
makeSetupHook {
|
||||
name = "python-recompile-bytecode-hook";
|
||||
substitutions = {
|
||||
inherit pythonInterpreter pythonSitePackages;
|
||||
compileArgs = lib.concatStringsSep " " (["-q" "-f" "-i -"] ++ lib.optionals isPy3k ["-j $NIX_BUILD_CORES"]);
|
||||
bytecodeName = if isPy3k then "__pycache__" else "*.pyc";
|
||||
};
|
||||
} ./python-recompile-bytecode-hook.sh ) {};
|
||||
|
||||
pythonRemoveBinBytecodeHook = callPackage ({ }:
|
||||
makeSetupHook {
|
||||
name = "python-remove-bin-bytecode-hook";
|
||||
|
||||
@@ -0,0 +1,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
|
||||
Reference in New Issue
Block a user