-
-
Notifications
You must be signed in to change notification settings - Fork 421
/
Copy pathpyenv-virtualenv-realpath
50 lines (46 loc) · 1.08 KB
/
pyenv-virtualenv-realpath
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env bash
# Summary: Substitute realpath if unavailable as a builtin or file
# Usage: . pyenv-virtualenv-realpath
if ! {
enable -f "${BASH_SOURCE%/*}"/../libexec/pyenv-realpath.dylib realpath ||
type realpath
} >/dev/null 2>&1; then
if [ -n "$PYENV_NATIVE_EXT" ]; then
echo "pyenv: failed to load \`realpath' builtin" >&2
exit 1
fi
READLINK=$(type -p greadlink readlink | head -1)
if [ -z "$READLINK" ]; then
echo "pyenv: cannot find readlink - are you missing GNU coreutils?" >&2
exit 1
fi
resolve_link() {
$READLINK "$1"
}
realpath() {
local f="$*" \
name dir
[[ $f ]] || {
>&2 echo ${FUNCNAME[0]}: missing operand
return
}
while [[ -L $f ]]; do
f="$(resolve_link "$f")"
done
if [[ ! -d $f ]]; then
name="/${f##*/}"
# parent?
dir="${f%/*}"
if [[ $dir == $f ]]; then
#lacks /: parent is current directory
f="$PWD"
else
f="$dir"
fi
fi
#absolute directory
dir="$(cd "$f"
pwd)"
echo "$dir$name"
}
fi