From f4b8133037896f31f1283ecfc870d925ac8cfb14 Mon Sep 17 00:00:00 2001 From: DavHau Date: Thu, 14 Mar 2024 17:19:08 +0700 Subject: [PATCH] dev-shell: make python shell load fast - Add caching for editable installs - Remove sleep statement in GUI code --- devShell-python.nix | 46 ++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/devShell-python.nix b/devShell-python.nix index d5ac4680e..4745b09ec 100644 --- a/devShell-python.nix +++ b/devShell-python.nix @@ -49,9 +49,9 @@ ## PYTHON - tmp_path=$(realpath ./.direnv) + tmp_path="$(realpath ./.direnv/python)" repo_root=$(realpath .) - mkdir -p "$tmp_path/python/${pythonWithDeps.sitePackages}" + mkdir -p "$tmp_path/${pythonWithDeps.sitePackages}" # local dependencies localPackages=( @@ -59,28 +59,41 @@ $repo_root/pkgs/clan-vm-manager ) - # Install the package in editable mode - # This allows executing `clan` from within the dev-shell using the current - # version of the code and its dependencies. - # TODO: this is slow. get rid of pip or add better caching - echo "==== Installing local python packages in editable mode ====" + # Install executable wrappers for local python packages scripts + # This is done by utilizing `pip install --editable` + # As a result, executables like `clan` can be executed from within the dev-shell + # while using the current version of the code and its dependencies. for package in "''${localPackages[@]}"; do - ${pythonWithDeps}/bin/pip install \ - --quiet \ - --disable-pip-version-check \ - --no-index \ - --no-build-isolation \ - --prefix "$tmp_path/python" \ - --editable "$package" + pname=$(basename "$package") + if + [ ! -e "$tmp_path/meta/$pname/pyproject.toml" ] \ + || [ ! -e "$package/pyproject.toml" ] \ + || ! cmp -s "$tmp_path/meta/$pname/pyproject.toml" "$package/pyproject.toml" + then + echo "==== Installing local python package $pname in editable mode ====" + mkdir -p "$tmp_path/meta/$pname" + cp $package/pyproject.toml $tmp_path/meta/$pname/pyproject.toml + ${python3.pkgs.pip}/bin/pip install \ + --quiet \ + --disable-pip-version-check \ + --no-index \ + --no-build-isolation \ + --prefix "$tmp_path" \ + --editable "$package" + fi done - export PATH="$tmp_path/python/bin:$PATH" - export PYTHONPATH="''${PYTHONPATH:+$PYTHONPATH:}$tmp_path/python/${pythonWithDeps.sitePackages}" + export PATH="$tmp_path/bin:$PATH" + export PYTHONPATH="''${PYTHONPATH:+$PYTHONPATH:}$tmp_path/${pythonWithDeps.sitePackages}" for package in "''${localPackages[@]}"; do export PYTHONPATH="$package:$PYTHONPATH" done + + + ## GUI + if ! command -v xdg-mime &> /dev/null; then echo "Warning: 'xdg-mime' is not available. The desktop file cannot be installed." fi @@ -93,7 +106,6 @@ UI_BIN="clan-vm-manager" cp -f $DESKTOP_SRC $DESKTOP_DST - sleep 2 sed -i "s|Exec=.*clan-vm-manager|Exec=$UI_BIN|" $DESKTOP_DST xdg-mime default $DESKTOP_FILE_NAME x-scheme-handler/clan echo "==== Validating desktop file installation ===="