From c6786b9d491e25e861d0ce1d92ef0f406cf86399 Mon Sep 17 00:00:00 2001 From: Peter Heatwole Date: Mon, 22 Feb 2021 14:00:37 -0800 Subject: [PATCH 1/7] Simplify error response --- bin/pyenv-users | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bin/pyenv-users b/bin/pyenv-users index f4dfa90..5d5c4ee 100755 --- a/bin/pyenv-users +++ b/bin/pyenv-users @@ -54,8 +54,7 @@ if [[ "${#ARGUMENTS[@]}" == 0 ]]; then elif [[ "${#ARGUMENTS[@]}" == 1 ]]; then DIR="${ARGUMENTS[0]}" else - echo -e "\nToo many directory arguments.\n" - pyenv help users + echo "pyenv-users: too many directory arguments" exit 1 fi From ff61f9c99fe0bfb46bdf20f4cd788521ee25be6e Mon Sep 17 00:00:00 2001 From: Peter Heatwole Date: Mon, 22 Feb 2021 14:01:08 -0800 Subject: [PATCH 2/7] Check for unrecognized options --- bin/pyenv-users | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bin/pyenv-users b/bin/pyenv-users index 5d5c4ee..51122dd 100755 --- a/bin/pyenv-users +++ b/bin/pyenv-users @@ -46,6 +46,10 @@ for option in "${OPTIONS[@]}"; do pyenv help users exit 0 ;; + * ) + echo "pyenv-users: unrecognized option '$option'" + exit 1 + ;; esac done From 034f902842ff36ae09e2525727c5630f3e16d7f4 Mon Sep 17 00:00:00 2001 From: Peter Heatwole Date: Mon, 22 Feb 2021 14:03:48 -0800 Subject: [PATCH 3/7] Eliminate unnecessary index variable --- bin/pyenv-users | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/bin/pyenv-users b/bin/pyenv-users index 51122dd..9a158ae 100755 --- a/bin/pyenv-users +++ b/bin/pyenv-users @@ -76,14 +76,12 @@ fi # Collect all symlinks named `python` that point into $PYENV_ROOT cmd="readlink -f '{}' | grep -q ${PYENV_ROOT}" -unset i while IFS= read -r -d $'\0' file; do - links[i++]="$file" + links+=("$file") done < <(find -H "$DIR" -name "python" -type l -exec sh -c "$cmd" \; -print0) # Turn each link into a (version, venv) string pair regex="${PYENV_ROOT}/versions/(.+)/bin/(.+)" -unset i for link in "${links[@]}"; do linkpath=$(realpath -s "$link") target=$(readlink -f "$link") @@ -93,8 +91,8 @@ for link in "${links[@]}"; do if grep -v -q "$PYENV_ROOT" <<< "$linkpath" || \ grep -q "$PYENV_ROOT/versions/$version/envs" <<< "$linkpath" then - versions[i]="$version" - venvs[i++]="${link%/bin/python}" + versions+=("$version") + venvs+=("${link%/bin/python}") fi done From fbf3f4ede7b27ab002e57140aa6b45db4efe8f9f Mon Sep 17 00:00:00 2001 From: Peter Heatwole Date: Mon, 22 Feb 2021 14:19:14 -0800 Subject: [PATCH 4/7] Simplify example in README --- README.rst | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/README.rst b/README.rst index 6dea09e..dbda449 100644 --- a/README.rst +++ b/README.rst @@ -68,17 +68,16 @@ specified directory: .. code-block:: bash - $ pyenv versions - * system (set by /home/peter/.local/pyenv/version) - 3.6.10 - 3.7.5 - 3.7.9 - 3.8.6 - 3.9.1 - pypy3.6-7.3.0 - pypy3.6-7.3.1 - - $ comm -3 <(pyenv users --raw ~ | cut -d: -f1 | uniq) <(pyenv versions | tail -n+2 | tr -d "[:blank:]") | tr -d "[:blank:]" + $ pyenv versions --bare + 3.6.10 + 3.7.5 + 3.7.9 + 3.8.6 + 3.9.1 + pypy3.6-7.3.0 + pypy3.6-7.3.1 + + $ comm -3 <(pyenv users --raw ~ | cut -d: -f1 | uniq) <(pyenv versions --bare) | tr -d "[:blank:]" 3.6.10 3.7.5 3.9.1 From 81b769dad4f307e36ad49037aabd9e12da8cf813 Mon Sep 17 00:00:00 2001 From: Peter Heatwole Date: Mon, 22 Feb 2021 15:01:44 -0800 Subject: [PATCH 5/7] Prefer positive `if` test --- bin/pyenv-users | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/pyenv-users b/bin/pyenv-users index 9a158ae..0ef6741 100755 --- a/bin/pyenv-users +++ b/bin/pyenv-users @@ -40,7 +40,7 @@ parse_options "$@" for option in "${OPTIONS[@]}"; do case "$option" in "r" | "raw" ) - RAW=true + RAW=1 ;; "h" | "help" ) pyenv help users @@ -103,9 +103,9 @@ for (( k=0; k < K; k++ )); do if (( width > maxwidth )); then maxwidth=$width; fi done for (( k=0; k < K; k++ )); do - if [ -z "$RAW" ]; then - printf "%-*s %s\n" "$maxwidth" "${versions[$k]}" "${venvs[k]}" - else + if [ -n "$RAW" ]; then echo "${versions[$k]}":"${venvs[$k]}" + else + printf "%-*s %s\n" "$maxwidth" "${versions[$k]}" "${venvs[k]}" fi done | sort From d18e46c60d6e74eee19334067909bfc48ffa0a98 Mon Sep 17 00:00:00 2001 From: Peter Heatwole Date: Mon, 22 Feb 2021 15:03:17 -0800 Subject: [PATCH 6/7] Add option to output absolute paths Switching to relative paths by default. For human readable output the prefix just takes up terminal space. --- bin/pyenv-users | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/bin/pyenv-users b/bin/pyenv-users index 0ef6741..e12d8d5 100755 --- a/bin/pyenv-users +++ b/bin/pyenv-users @@ -4,7 +4,8 @@ # # Usage: pyenv users [-r|--raw] [directory] # -# -r/--raw Raw output strings as ":" +# -A/--absolute-paths Output absolute paths instead of relative paths +# -r/--raw Raw output strings as ":" # # Scans [directory] for virtual environments whose `python` commands # are symlinks back into a pyenv version. Default: current directory. @@ -35,10 +36,13 @@ parse_options() { done } -unset RAW +unset ABSOLUTE RAW parse_options "$@" for option in "${OPTIONS[@]}"; do case "$option" in + "A" | "absolute-paths" ) + ABSOLUTE=1 + ;; "r" | "raw" ) RAW=1 ;; @@ -61,6 +65,12 @@ else echo "pyenv-users: too many directory arguments" exit 1 fi +if [ -n "$ABSOLUTE" ]; then + PREFIX="" +else + PREFIX="$DIR/" +fi + # ---------------------------------------------------------------------------- # Finished parsing the arguments. Begin the actual functionality. @@ -104,8 +114,8 @@ for (( k=0; k < K; k++ )); do done for (( k=0; k < K; k++ )); do if [ -n "$RAW" ]; then - echo "${versions[$k]}":"${venvs[$k]}" + echo "${versions[$k]}":"${venvs[$k]#$PREFIX}" else - printf "%-*s %s\n" "$maxwidth" "${versions[$k]}" "${venvs[k]}" + printf "%-*s %s\n" "$maxwidth" "${versions[$k]}" "${venvs[k]#$PREFIX}" fi done | sort From 9d96ea05868bfe5d403531bd4c61867ddd359127 Mon Sep 17 00:00:00 2001 From: Peter Heatwole Date: Mon, 22 Feb 2021 15:16:48 -0800 Subject: [PATCH 7/7] Update README --- README.rst | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.rst b/README.rst index dbda449..edc81c7 100644 --- a/README.rst +++ b/README.rst @@ -37,17 +37,17 @@ search your home directory: .. code-block:: bash $ pyenv users ~ - 3.7.9 /home/peter/.cache/pypoetry/virtualenvs/my_project-KM_3YcvM-py3.7 - 3.7.9 /home/peter/work/venvs/long name with spaces - 3.8.6 /home/peter/.cache/pypoetry/virtualenvs/my_project-KM_3YcvM-py3.8 - pypy3.6-7.3.1 /home/peter/work/venvs/example1 + 3.7.9 .cache/pypoetry/virtualenvs/my_project-KM_3YcvM-py3.7 + 3.7.9 work/venvs/long name with spaces + 3.8.6 .cache/pypoetry/virtualenvs/my_project-KM_3YcvM-py3.8 + pypy3.6-7.3.1 work/venvs/example1 For scripting, use the ``--raw`` option to output a list of ``:`` separated -items: +items. The ``--absolute-paths`` option may also be useful in this case: .. code-block:: bash - $ pyenv users --raw ~ + $ pyenv users --raw --absolute-paths ~ 3.7.9:/home/peter/.cache/pypoetry/virtualenvs/my_project-KM_3YcvM-py3.7 3.7.9:/home/peter/work/venvs/long name with spaces 3.8.6:/home/peter/.cache/pypoetry/virtualenvs/my_project-KM_3YcvM-py3.8