Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pythonImportsCheck: run prePythonImportsCheck and postPythonImportsCheck hooks #19

Open
milahu opened this issue Jan 16, 2024 · 0 comments

Comments

@milahu
Copy link
Owner

milahu commented Jan 16, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant