-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkon
executable file
·281 lines (246 loc) · 7.68 KB
/
workon
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env bash
#
# workon-py - work on virtualenvs via sub-shell
# http://github.com/rafi/workon-py
#
# Copyright (C) 2015-2019 Rafael Bodill <justrafi at gmail>
# Distributed under the GNU General Public License, version 2.0.
set -eu
# Help screen
_workon_usage() {
local __version=1.2.0
echo "usage: $(basename "$0") [arguments] <virtualenv>"
echo "version: ${__version}"
echo
echo 'arguments:'
echo ' -c, --create create and activate a new virtualenv'
echo " --no-install when creating, don't install requirements"
echo ' -d, --delete Delete virtualenv'
echo ' -r, --run <cmd> Run a binary from virtualenv'
echo ' -h, --help show this help message and exit'
echo
echo 'environment variables:'
echo " WORKON_HOME Path to create venvs, default: \${XDG_DATA_HOME}/python/envs"
echo ' WORKON_VIRTUALENV Optional, set specific virtualenv path'
echo ' Leave empty for interactive selection.'
}
# Creates a new virtualenv in specific path, using 'virtualenv' from
# WORKON_VIRTUALENV environment variable or selected virtualenv, if many are
# found. Also, supports the straight-forward 'python3 -m venv'
_workon_create() {
[ -z "${1+set}" ] && exit 3
local venv="${__workon_home}${1}"
local is_upgrade="${2:-0}"
if [ -d "${venv}" ] && [ "${is_upgrade}" = "0" ] ; then
echo "Virtualenv '${1}' already exist at ${venv}" >&2
echo 'You can force an upgrade with "-u".' >&2
exit 2
fi
local virtualenv_cmd="${WORKON_VIRTUALENV:=}"
if [ "${virtualenv_cmd}" = "" ]; then
# Push all $PATH directories into an array
declare -a paths=()
while IFS= read -r -d ':'; do paths+=("$REPLY"); done < <(echo "${PATH}")
# Push available virtualenv executables and hard-coded `-m venv` option
declare -a cmds=()
if hash python3 2>/dev/null; then
cmds+=("python3 -m venv")
fi
while IFS= read -r -d $'\0'; do cmds+=("$REPLY"); done < <(\
find "${paths[@]}" -maxdepth 1 -name "virtualenv*" -print0 2>/dev/null)
if [ ${#cmds[@]} -eq 0 ]; then
echo 'Cannot find any virtualenv commands, aborting.' >&2
exit 3
elif [ ${#cmds[@]} -eq 1 ]; then
# Found only one executable, use it without asking
virtualenv_cmd="${cmds[0]}"
else
PS3=' :: Select virtualenv program to use: '
select virtualenv_cmd in "${cmds[@]}"; do
[ -n "${virtualenv_cmd}" ] && break;
done
fi
fi
if [ -z "${virtualenv_cmd}" ]; then
exit 2
elif [ "${virtualenv_cmd}" = "python3 -m venv" ]; then
# Use python3 venv module or a different command for creation
python3 -m venv "${venv}"
else
# Use selected virtualenv version for creation
"${virtualenv_cmd}" "${venv}"
fi
}
# Install requirements*txt with pip
_workon_requirements() {
[ -z "${1+set}" ] && exit 3
local venv="${__workon_home}${1}"
local pattern='requirements*txt'
find . -maxdepth 1 -name "${pattern}" | while IFS= read -r file; do
echo -e "\n :: Installing dependencies from '${file}'\n"
"${venv}"/bin/pip install -r "${file}"
echo
done
}
# Run a binary from a virtualenv
_workon_run() {
if [ $# -lt 2 ]; then
echo 'Please specify a command to run inside the virtualenv.' >&2
exit 3
fi
local venv="${__workon_home}${1}"
local bin="${2}"
shift 2
exec "${venv}"/bin/"${bin}" "$@"
}
# Deletes a virtualenv
_workon_delete() {
[ -z "${1+set}" ] && exit 3
local venv="${__workon_home}${1}"
if [ "${venv}" = "${__workon_home}" ]; then
echo "Invalid virtualenv name, aborting." >&2
exit 2
elif [ ! -d "${venv}" ]; then
echo "Virtualenv '$1' doesn't exist at ${venv}" >&2
exit 2
elif [ -n "${VIRTUAL_ENV+set}" ] && [ "${VIRTUAL_ENV}" = "${venv}" ]; then
echo "You are currently activated within '${1}', deactivate first." >&2
exit 2
fi
rm -rf "$venv"
echo "Virtual environment '${1}' deleted ${venv}"
}
# Executes a sub-shell with virtualenv variable prepended to PATH
_workon_activate() {
# Abort if we're already activated
if [ -n "${VIRTUAL_ENV+set}" ]; then
echo 'Already inside a virtualenv, exit first.' >&2
exit 1
fi
[ -z "${1+set}" ] && exit 3
local VIRTUAL_ENV="${__workon_home}${1}"
if [ ! -d "${VIRTUAL_ENV}" ]; then
echo "Virtualenv '$1' doesn't exist at ${VIRTUAL_ENV}" >&2
exit 2
fi
echo " :: Activating \`${VIRTUAL_ENV}\`"
echo ' To deactivate, exit the shell (using "exit" or Ctrl-D).'
# This is what it all boils down to
export VIRTUAL_ENV
export PATH="${VIRTUAL_ENV}/bin:${PATH}"
unset PYTHONHOME
exec "${SHELL}"
}
# List all virtual-environments, or interactively activate
# selected environment if fzf or fzy are available.
_workon_select() {
local selectors=(fzf fzy)
local name='' selector=''
for exe in "${selectors[@]}"; do
hash "${exe}" 2>/dev/null && selector="${exe}" && break
done
if [ -n "${selector}" ]; then
_workon_list | "${selector}"
else
echo "Unable to find one of these dependencies: ${selectors[*]}" >&2
echo "Try installing https://github.com/junegunn/fzf"
exit 4
fi
}
# Return list of virtual-environments names
_workon_list() {
find "${__workon_home}" ! -path "${__workon_home}" \
-type d -maxdepth 1 -exec basename "{}" \;
}
# Resolve real name of provided name/path
__workon_resolve() {
local name="${1:-}"
if [ -z "${name}" ]; then
echo "Please specify a virtualenv for this command." >&2
exit 2
elif [ "${name}" = "." ]; then
__workon_get_path_name "$PWD"
elif [[ ${name} == ./* ]]; then
if [ -d "${name}" ]; then
__workon_get_path_name "$(realpath "${name}")"
else
echo "Directory you provided doesn't exist." >&2
exit 2
fi
else
echo "${name}"
fi
}
# Returns a path's base name with a crc32 (if available) of the entire path
__workon_get_path_name() {
[ -z "${1+set}" ] && exit 3
local cwd="${1}" name='' hash=''
name="$(basename "${cwd}")"
if hash crc32 2>/dev/null; then
hash="$(crc32 <(echo "${cwd}"))"
name="${name}-${hash}"
fi
echo "${name}"
}
# Program entry-point
_workon () {
local data="${WORKON_HOME:-${XDG_DATA_HOME:-$HOME/.local/share}/python/envs}"
local __workon_home="${data%/}/" env_wanted='' env_name='' positional=()
local is_create=0 is_run=0 is_delete=0 is_upgrade=0 is_list=0 is_install=1
# Parse command-line arguments
while [[ $# -gt 0 ]]; do
case "${1}" in
-c|--create) is_create=1; shift ;;
-d|--delete) is_delete=1; shift ;;
-u|--upgrade) is_upgrade=1; shift ;;
-l|--list) is_list=1; shift ;;
--no-install) is_install=0; shift ;;
-r|--run) is_run=1; shift ;;
-h|--help) _workon_usage; exit ;;
-*) echo "Warning, unrecognized option ${1}" >&2; shift ;;
*) positional+=("${1}"); shift ;;
esac
done
set -- "${positional[@]}"
# Create the root path for all virtual-environments
if [ ! -d "${__workon_home}" ]; then
echo " :: Virtualenvs directory doesn't exist," >&2
echo " Creating '${__workon_home}'..." >&2
mkdir -p "${__workon_home}"
fi
if [ "${is_list}" = "1" ]; then
# List all virtual-environments if one is not specified
_workon_list
else
# Define the virtualenv in question
if [ -z "${1+set}" ]; then
if [ "${is_create}" = "1" ]; then
echo 'You must specify a name or path to create.' >&2
exit 2
fi
env_wanted="$(_workon_select)"
[ -z "$env_wanted" ] && exit $?
else
env_wanted="${1}"
shift
fi
env_name="$(__workon_resolve "${env_wanted}")"
# Create and activate / delete / activate
if [ "${is_create}" = "1" ]; then
_workon_create "${env_name}" "${is_upgrade}"
[ "${is_install}" = "1" ] && _workon_requirements "${env_name}"
_workon_activate "${env_name}"
else
if [ "${is_run}" = "1" ]; then
_workon_run "${env_name}" "$@"
elif [ "${is_delete}" = "1" ]; then
_workon_delete "${env_name}"
else
_workon_activate "${env_name}"
fi
fi
fi
exit $?
}
_workon "$@"
# vim: set ts=2 sw=2 tw=80 noet :