Skip to content

Commit

Permalink
Merge pull request #2 from pfheatwole/work
Browse files Browse the repository at this point in the history
Tidy and finish basic functionality
  • Loading branch information
pfheatwole authored Feb 22, 2021
2 parents 3362a98 + 9d96ea0 commit 53d4a37
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
33 changes: 16 additions & 17 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
37 changes: 24 additions & 13 deletions bin/pyenv-users
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#
# Usage: pyenv users [-r|--raw] [directory]
#
# -r/--raw Raw output strings as "<version>:<venv-path>"
# -A/--absolute-paths Output absolute paths instead of relative paths
# -r/--raw Raw output strings as "<version>:<venv-path>"
#
# Scans [directory] for virtual environments whose `python` commands
# are symlinks back into a pyenv version. Default: current directory.
Expand Down Expand Up @@ -35,17 +36,24 @@ 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=true
RAW=1
;;
"h" | "help" )
pyenv help users
exit 0
;;
* )
echo "pyenv-users: unrecognized option '$option'"
exit 1
;;
esac
done

Expand All @@ -54,10 +62,15 @@ 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
if [ -n "$ABSOLUTE" ]; then
PREFIX=""
else
PREFIX="$DIR/"
fi


# ----------------------------------------------------------------------------
# Finished parsing the arguments. Begin the actual functionality.
Expand All @@ -73,14 +86,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")
Expand All @@ -90,8 +101,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

Expand All @@ -102,9 +113,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]}"
if [ -n "$RAW" ]; then
echo "${versions[$k]}":"${venvs[$k]#$PREFIX}"
else
echo "${versions[$k]}":"${venvs[$k]}"
printf "%-*s %s\n" "$maxwidth" "${versions[$k]}" "${venvs[k]#$PREFIX}"
fi
done | sort

0 comments on commit 53d4a37

Please sign in to comment.