Skip to content

Commit

Permalink
Add pipper shell (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
jzombie authored Jan 3, 2024
1 parent c1410b4 commit 06e1c75
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 24 additions & 3 deletions pipper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
}
Expand Down Expand Up @@ -157,6 +174,9 @@ case $1 in
create)
create_venv "$2"
;;
shell)
launch_venv_shell
;;
activate)
activate_venv
;;
Expand All @@ -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
2 changes: 1 addition & 1 deletion test_pipper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 06e1c75

Please sign in to comment.