Skip to content

Commit 53d4a37

Browse files
authored
Merge pull request #2 from pfheatwole/work
Tidy and finish basic functionality
2 parents 3362a98 + 9d96ea0 commit 53d4a37

File tree

2 files changed

+40
-30
lines changed

2 files changed

+40
-30
lines changed

README.rst

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@ search your home directory:
3737
.. code-block:: bash
3838
3939
$ pyenv users ~
40-
3.7.9 /home/peter/.cache/pypoetry/virtualenvs/my_project-KM_3YcvM-py3.7
41-
3.7.9 /home/peter/work/venvs/long name with spaces
42-
3.8.6 /home/peter/.cache/pypoetry/virtualenvs/my_project-KM_3YcvM-py3.8
43-
pypy3.6-7.3.1 /home/peter/work/venvs/example1
40+
3.7.9 .cache/pypoetry/virtualenvs/my_project-KM_3YcvM-py3.7
41+
3.7.9 work/venvs/long name with spaces
42+
3.8.6 .cache/pypoetry/virtualenvs/my_project-KM_3YcvM-py3.8
43+
pypy3.6-7.3.1 work/venvs/example1
4444
4545
For scripting, use the ``--raw`` option to output a list of ``:`` separated
46-
items:
46+
items. The ``--absolute-paths`` option may also be useful in this case:
4747

4848
.. code-block:: bash
4949
50-
$ pyenv users --raw ~
50+
$ pyenv users --raw --absolute-paths ~
5151
3.7.9:/home/peter/.cache/pypoetry/virtualenvs/my_project-KM_3YcvM-py3.7
5252
3.7.9:/home/peter/work/venvs/long name with spaces
5353
3.8.6:/home/peter/.cache/pypoetry/virtualenvs/my_project-KM_3YcvM-py3.8
@@ -68,17 +68,16 @@ specified directory:
6868

6969
.. code-block:: bash
7070
71-
$ pyenv versions
72-
* system (set by /home/peter/.local/pyenv/version)
73-
3.6.10
74-
3.7.5
75-
3.7.9
76-
3.8.6
77-
3.9.1
78-
pypy3.6-7.3.0
79-
pypy3.6-7.3.1
80-
81-
$ comm -3 <(pyenv users --raw ~ | cut -d: -f1 | uniq) <(pyenv versions | tail -n+2 | tr -d "[:blank:]") | tr -d "[:blank:]"
71+
$ pyenv versions --bare
72+
3.6.10
73+
3.7.5
74+
3.7.9
75+
3.8.6
76+
3.9.1
77+
pypy3.6-7.3.0
78+
pypy3.6-7.3.1
79+
80+
$ comm -3 <(pyenv users --raw ~ | cut -d: -f1 | uniq) <(pyenv versions --bare) | tr -d "[:blank:]"
8281
3.6.10
8382
3.7.5
8483
3.9.1

bin/pyenv-users

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
#
55
# Usage: pyenv users [-r|--raw] [directory]
66
#
7-
# -r/--raw Raw output strings as "<version>:<venv-path>"
7+
# -A/--absolute-paths Output absolute paths instead of relative paths
8+
# -r/--raw Raw output strings as "<version>:<venv-path>"
89
#
910
# Scans [directory] for virtual environments whose `python` commands
1011
# are symlinks back into a pyenv version. Default: current directory.
@@ -35,17 +36,24 @@ parse_options() {
3536
done
3637
}
3738

38-
unset RAW
39+
unset ABSOLUTE RAW
3940
parse_options "$@"
4041
for option in "${OPTIONS[@]}"; do
4142
case "$option" in
43+
"A" | "absolute-paths" )
44+
ABSOLUTE=1
45+
;;
4246
"r" | "raw" )
43-
RAW=true
47+
RAW=1
4448
;;
4549
"h" | "help" )
4650
pyenv help users
4751
exit 0
4852
;;
53+
* )
54+
echo "pyenv-users: unrecognized option '$option'"
55+
exit 1
56+
;;
4957
esac
5058
done
5159

@@ -54,10 +62,15 @@ if [[ "${#ARGUMENTS[@]}" == 0 ]]; then
5462
elif [[ "${#ARGUMENTS[@]}" == 1 ]]; then
5563
DIR="${ARGUMENTS[0]}"
5664
else
57-
echo -e "\nToo many directory arguments.\n"
58-
pyenv help users
65+
echo "pyenv-users: too many directory arguments"
5966
exit 1
6067
fi
68+
if [ -n "$ABSOLUTE" ]; then
69+
PREFIX=""
70+
else
71+
PREFIX="$DIR/"
72+
fi
73+
6174

6275
# ----------------------------------------------------------------------------
6376
# Finished parsing the arguments. Begin the actual functionality.
@@ -73,14 +86,12 @@ fi
7386

7487
# Collect all symlinks named `python` that point into $PYENV_ROOT
7588
cmd="readlink -f '{}' | grep -q ${PYENV_ROOT}"
76-
unset i
7789
while IFS= read -r -d $'\0' file; do
78-
links[i++]="$file"
90+
links+=("$file")
7991
done < <(find -H "$DIR" -name "python" -type l -exec sh -c "$cmd" \; -print0)
8092

8193
# Turn each link into a (version, venv) string pair
8294
regex="${PYENV_ROOT}/versions/(.+)/bin/(.+)"
83-
unset i
8495
for link in "${links[@]}"; do
8596
linkpath=$(realpath -s "$link")
8697
target=$(readlink -f "$link")
@@ -90,8 +101,8 @@ for link in "${links[@]}"; do
90101
if grep -v -q "$PYENV_ROOT" <<< "$linkpath" || \
91102
grep -q "$PYENV_ROOT/versions/$version/envs" <<< "$linkpath"
92103
then
93-
versions[i]="$version"
94-
venvs[i++]="${link%/bin/python}"
104+
versions+=("$version")
105+
venvs+=("${link%/bin/python}")
95106
fi
96107
done
97108

@@ -102,9 +113,9 @@ for (( k=0; k < K; k++ )); do
102113
if (( width > maxwidth )); then maxwidth=$width; fi
103114
done
104115
for (( k=0; k < K; k++ )); do
105-
if [ -z "$RAW" ]; then
106-
printf "%-*s %s\n" "$maxwidth" "${versions[$k]}" "${venvs[k]}"
116+
if [ -n "$RAW" ]; then
117+
echo "${versions[$k]}":"${venvs[$k]#$PREFIX}"
107118
else
108-
echo "${versions[$k]}":"${venvs[$k]}"
119+
printf "%-*s %s\n" "$maxwidth" "${versions[$k]}" "${venvs[k]#$PREFIX}"
109120
fi
110121
done | sort

0 commit comments

Comments
 (0)