From afeb971fa25b343760051423149aaa4514f3d41d Mon Sep 17 00:00:00 2001 From: hardikpnsp Date: Mon, 15 Aug 2022 22:55:46 +0530 Subject: [PATCH] Add support for multiple versions in `pyenv uninstall` (#2432) --- COMMANDS.md | 4 +- README.md | 2 +- man/man1/pyenv.1 | 6 +-- plugins/python-build/bin/pyenv-uninstall | 66 ++++++++++++++---------- plugins/python-build/test/hooks.bats | 35 +++++++++++++ plugins/python-build/test/pyenv.bats | 24 +++++++-- 6 files changed, 99 insertions(+), 38 deletions(-) diff --git a/COMMANDS.md b/COMMANDS.md index 04d0f577..4d4676f1 100644 --- a/COMMANDS.md +++ b/COMMANDS.md @@ -238,9 +238,9 @@ To install the latest major release for Python 3 try: ## `pyenv uninstall` -Uninstall a specific Python version. +Uninstall Python versions. - Usage: pyenv uninstall [-f|--force] + Usage: pyenv uninstall [-f|--force] ... -f Attempt to remove the specified version without prompting for confirmation. If the version does not exist, do not diff --git a/README.md b/README.md index 5ddb5a50..c57ab354 100644 --- a/README.md +++ b/README.md @@ -432,7 +432,7 @@ for more details on how the selection works and more information on its usage. As time goes on, you will accumulate Python versions in your `$(pyenv root)/versions` directory. -To remove old Python versions, use [`pyenv uninstall `](COMMANDS.md#pyenv-uninstall). +To remove old Python versions, use [`pyenv uninstall `](COMMANDS.md#pyenv-uninstall). Alternatively, you can simply `rm -rf` the directory of the version you want to remove. You can find the directory of a particular Python version diff --git a/man/man1/pyenv.1 b/man/man1/pyenv.1 index 04c32d15..621a3c3e 100644 --- a/man/man1/pyenv.1 +++ b/man/man1/pyenv.1 @@ -106,7 +106,7 @@ Set or show the shell\-specific Python version List existing pyenv shims .TP .B uninstall -Uninstall a specific Python version +Uninstall Python versions .TP .B version Show the current Python version(s) and its origin @@ -452,10 +452,10 @@ $ pyenv versions .fi .IP "" 0 .SS "pyenv uninstall" -Uninstall a specific Python version\. +Uninstall Python versions\. .IP "" 4 .nf -Usage: pyenv uninstall [\-f|\-\-force] +Usage: pyenv uninstall [\-f|\-\-force] ... \-f Attempt to remove the specified version without prompting for confirmation\. If the version does not exist, do not diff --git a/plugins/python-build/bin/pyenv-uninstall b/plugins/python-build/bin/pyenv-uninstall index 9f892097..3bf6d98e 100755 --- a/plugins/python-build/bin/pyenv-uninstall +++ b/plugins/python-build/bin/pyenv-uninstall @@ -1,8 +1,8 @@ #!/usr/bin/env bash # -# Summary: Uninstall a specific Python version +# Summary: Uninstall Python versions # -# Usage: pyenv uninstall [-f|--force] +# Usage: pyenv uninstall [-f|--force] ... # # -f Attempt to remove the specified version without prompting # for confirmation. If the version does not exist, do not @@ -33,14 +33,17 @@ if [ "$1" = "-f" ] || [ "$1" = "--force" ]; then shift fi -[ "$#" -eq 1 ] || usage 1 >&2 +[ "$#" -gt 0 ] || usage 1 >&2 -DEFINITION="$1" -case "$DEFINITION" in -"" | -* ) - usage 1 >&2 - ;; -esac +versions=("$@") + +for version in "${versions[@]}"; do + case "$version" in + "" | -* ) + usage 1 >&2 + ;; + esac +done declare -a before_hooks after_hooks @@ -59,29 +62,36 @@ IFS=$'\n' scripts=(`pyenv-hooks uninstall`) IFS="$OLDIFS" for script in "${scripts[@]}"; do source "$script"; done +uninstall-python() { + local DEFINITION="$1" -VERSION_NAME="${DEFINITION##*/}" -PREFIX="${PYENV_ROOT}/versions/${VERSION_NAME}" + local VERSION_NAME="${DEFINITION##*/}" + local PREFIX="${PYENV_ROOT}/versions/${VERSION_NAME}" -if [ -z "$FORCE" ]; then - if [ ! -d "$PREFIX" ]; then - echo "pyenv: version \`$VERSION_NAME' not installed" >&2 - exit 1 + if [ -z "$FORCE" ]; then + if [ ! -d "$PREFIX" ]; then + echo "pyenv: version \`$VERSION_NAME' not installed" >&2 + exit 1 + fi + + read -p "pyenv: remove $PREFIX? [y|N] " + case "$REPLY" in + y | Y | yes | YES ) ;; + * ) exit 1 ;; + esac fi - read -p "pyenv: remove $PREFIX? [y|N] " - case "$REPLY" in - y | Y | yes | YES ) ;; - * ) exit 1 ;; - esac -fi + for hook in "${before_hooks[@]}"; do eval "$hook"; done -for hook in "${before_hooks[@]}"; do eval "$hook"; done + if [ -d "$PREFIX" ]; then + rm -rf "$PREFIX" + pyenv-rehash + echo "pyenv: $VERSION_NAME uninstalled" + fi -if [ -d "$PREFIX" ]; then - rm -rf "$PREFIX" - pyenv-rehash - echo "pyenv: $VERSION_NAME uninstalled" -fi + for hook in "${after_hooks[@]}"; do eval "$hook"; done +} -for hook in "${after_hooks[@]}"; do eval "$hook"; done +for version in "${versions[@]}"; do + uninstall-python "$version" +done diff --git a/plugins/python-build/test/hooks.bats b/plugins/python-build/test/hooks.bats index 4a1a2945..613c5c2e 100644 --- a/plugins/python-build/test/hooks.bats +++ b/plugins/python-build/test/hooks.bats @@ -55,3 +55,38 @@ OUT refute [ -d "${PYENV_ROOT}/versions/3.6.2" ] } + +@test "pyenv-uninstall hooks with multiple versions" { + cat > "${HOOK_PATH}/uninstall.bash" <