- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchpythonz.sh
More file actions
Latest commit
111 lines (100 loc) · 2.35 KB
/
Copy pathchpythonz.sh
File metadata and controls
111 lines (100 loc) · 2.35 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
CHPYTHON_VERSION='0.1.0.dev'
PYTHONS=()
[ -d"${HOME}/.pythonz/pythons" ] && PYTHONS+=("${HOME}/.pythonz/pythons"/*)
function_chpython_exec {
local python
python=$1
shift
# TODO: Clear path so invoking `chpython 3.5 python -V` when
# $CHPYTHON_VERSION is set to a 2.x Python gives a command not
# found error instead of running $CHPYTHON_VERSION/bin/python.
env PATH="$python/bin:$PATH"$*
}
function_chpython_reset {
[ -z"$CHPYTHON_ROOT" ] &&return
PATH=":$PATH:"
PATH=${PATH//:$CHPYTHON_ROOT\/bin:/:}
PATH=${PATH#:}; PATH=${PATH%:}
unset CHPYTHON_ROOT
hash -r
type python ||type python3
}
function_chpython_select {
local dir python match
fordirin${PYTHONS[@]};do
dir=${dir%%/}
python=${dir##*/}
case"$python"in
"$1")
# Match using 3-part version number, e.g. `chruby 2.7.10`
match=$dir&&break
;;
"$1"*)
# Match best using 2-part version number, e.g., `chpython 2.7`
match=$dir
;;
esac
done
echo"$match"
}
function_chpython_use {
if [[ !-x"$1/bin/python"&&!-x"$1/bin/python3" ]];then
echo"chpython: neither $1/bin/python nor $1/bin/python3 are executable">&2
return 1
fi
# TODO: Alias *3 stuff to base name, so, e.g., `python` can be called
# instead of `python3`
export CHPYTHON_ROOT=$1
export PATH="${CHPYTHON_ROOT}/bin:${PATH}"
hash -r
type python ||type python3
}
functionchpython {
case"$1"in
-h|--help)
cat <<EOS
Usage:
chpython [PYTHON|VERSION|system]
chpython exec [PYTHON|VERSION] args...
EOS
;;
-V|--version)
echo"chpython $CHPYTHON_VERSION"
;;
exec)
shift
local match
match=$(_chpython_select $1)
if [ -z"$match" ];then
echo"chpython: unknown Python: $1">&2
return 1
fi
shift
_chpython_exec $match$*
;;
system)
_chpython_reset
;;
"")
local dir python
fordirin"${PYTHONS[@]}";do
dir="${dir%%/}"; python="${dir##*/}"
if [[ "$dir"=="$CHPYTHON_ROOT" ]];then
echo" * ${python}"#${RUBYOPT}"
else
echo"${python}"
fi
done
;;
*)
local match
match=$(_chpython_select $1)
if [ -z"$match" ];then
echo"chpython: unknown Python: $1">&2
return 1
fi
shift
_chpython_use $match$*
;;
esac
}