From 06e1c75e6322220d0176f6eb9f47682d366e99eb Mon Sep 17 00:00:00 2001 From: Jeremy Harris Date: Wed, 3 Jan 2024 11:16:24 -0600 Subject: [PATCH] Add pipper shell (#17) --- README.md | 5 +---- pipper.sh | 27 ++++++++++++++++++++++++--- test_pipper.sh | 2 +- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 7172982..3275469 100644 --- a/README.md +++ b/README.md @@ -17,16 +17,13 @@ Pipper: **P**ackage **I**nstaller and **P**ython **P**roject **E**nvironment **R Note: All commands can be run from *outside* of the virtual environment and will automatically launch as needed. - `pipper create`: Create a Python virtual environment. If you have more than one version of Python installed locally, you can do `pipper create python[python-version]`. (Note: Pipper uses the `venv` directory in your project and supports one environment at a time.) +- `pipper shell`: Drops into a Bash sub-shell already configured with the virtual environment. - `pipper install`: Install ALL packages from a `requirements.txt` file (Note: if wanting to install a *specific* package, use `pip install [package-name]` instead). - `pipper freeze`: Freeze installed packages to update the `requirements.txt` file. - `pipper uninstall`: Uninstall ALL packages listed in the `requirements.txt` file (Note: if wanting to uninstall a *specific* package, use `pip uninstall [package-name]` instead). - `pipper run`: Run a Python script within the virtual environment. - `pipper test`: Run unit tests within the virtual environment (via `unittest`). -*Optionally:* - -- `pipper activate`: Not generally needed because all other Pipper commands will handle this for you; when executing directly it provides CLI instructions on how to manually start the virtual environment. - ## Getting Started ### Installation diff --git a/pipper.sh b/pipper.sh index 39843ad..f8a243e 100755 --- a/pipper.sh +++ b/pipper.sh @@ -42,6 +42,22 @@ create_venv() { activate_venv false } +# Function to launch a sub-shell with the virtual environment activated. +launch_venv_shell() { + if [ -d "$VENV_NAME" ]; then + echo "Launching a sub-shell with the virtual environment activated..." + + # Define a command to activate the venv + ACTIVATE_VENV="source $VENV_NAME/bin/activate" + + # Start a new shell instance with the venv activated + bash --init-file <(echo "$ACTIVATE_VENV; if [[ \$PS1 != *'($VENV_NAME)'* ]]; then PS1='($VENV_NAME) '\$PS1; fi") + else + echo "Virtual environment '$VENV_NAME' does not exist. Please create it first." + fi +} + + # Function to activate the virtual environment. # Accepts a boolean argument to decide whether to activate it immediately. activate_venv() { @@ -63,8 +79,9 @@ activate_venv() { source "$VENV_NAME/bin/activate" else # Display instructions on how to activate the environment manually. - echo "To activate the virtual environment, run:" - echo "source $VENV_NAME/bin/activate" + printf "\n\n" + echo " To activate the virtual environment, run:" + echo " pipper shell" printf "\n\n" fi } @@ -157,6 +174,9 @@ case $1 in create) create_venv "$2" ;; + shell) + launch_venv_shell + ;; activate) activate_venv ;; @@ -179,6 +199,7 @@ case $1 in run_tests_dry_run "$2" "$3" ;; *) - echo "Usage: $0 {create|activate|install|freeze|uninstall|run|test|test-dry-run}" + # Note: "activate" is intentionally not included, as it is primarily for testing + echo "Usage: $0 {create|shell|install|freeze|uninstall|run|test|test-dry-run}" ;; esac diff --git a/test_pipper.sh b/test_pipper.sh index 297f27e..526d83a 100755 --- a/test_pipper.sh +++ b/test_pipper.sh @@ -59,7 +59,7 @@ run_test "$PIPPER_SCRIPT create && dir_exists venv" "Create Virtual Environment # Test the activate_venv function (success is expected) activate_output=$($PIPPER_SCRIPT activate | tr -d '\n') -expected_output="To activate the virtual environment, run:source venv/bin/activate" +expected_output=" To activate the virtual environment, run: pipper shell" run_test "[ \"$activate_output\" == \"$expected_output\" ]" "Activate Virtual Environment Test" # Test the install_requirements function (success is expected)