milahu / nixpkgs

Nix Packages collection
MIT License
1 stars 0 forks source link

pythonImportsCheck: run prePythonImportsCheck and postPythonImportsCheck hooks #19

Open milahu opened 10 months ago

milahu commented 10 months ago

pythonImportsCheck can do stupid stuff like create temporary files which i want to remove in postPythonImportsCheck but currently postPythonImportsCheck is not called

proper fix: patch nixpkgs pkgs/development/interpreters/python/hooks/python-imports-check-hook.sh

quickfix: patch the pythonImportsCheckPhase function

pythonImportsCheck is the last of all phases, so it runs after postInstall

{

  postInstall = ''
    echo patching pythonImportsCheckPhase
    s="$(type pythonImportsCheckPhase | tail -n +2)"
    if ! echo "$s" | grep -q "^    runHook prePythonImportsCheck"; then
      s="$(echo "$s" | sed 's/^{/{\n    runHook prePythonImportsCheck/')"
    fi
    if ! echo "$s" | grep -q "^    runHook postPythonImportsCheck"; then
      s="$(echo "$s" | sed 's/^}/    runHook postPythonImportsCheck\n}/')"
    fi
    eval "$s"
    echo "patched pythonImportsCheckPhase:"; type pythonImportsCheckPhase
  '';

  postPythonImportsCheck = ''
    echo removing tempfiles from postPythonImportsCheck
    pushd $out >/dev/null
    rm -rf -v local_storage.json output profiles profiles.json tasks
    popd >/dev/null
  '';

the patched pythonImportsCheckPhase function looks like

pythonImportsCheckPhase () 
{ 
    runHook prePythonImportsCheck;
    echo "Executing pythonImportsCheckPhase";
    if [ -n "$pythonImportsCheck" ]; then
        echo "Check whether the following modules can be imported: $pythonImportsCheck";
        export PYTHONPATH="$out/lib/python3.10/site-packages:$PYTHONPATH";
        ( cd $out && eval "/nix/store/pzf6dnxg8gf04xazzjdwarm7s03cbrgz-python3-3.10.12/bin/python3.10 -c 'import os; import importlib; list(map(lambda mod: importlib.import_module(mod), os.environ[\"pythonImportsCheck\"].split()))'" );
    fi;
    runHook postPythonImportsCheck
}

example use in my botasaurus.nix