-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipenv-activate
executable file
·58 lines (48 loc) · 1.48 KB
/
pipenv-activate
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
#!/bin/bash
#
# pipenv-activate -- Activate a pipenv environment without spawning a new shell
#
# Avoid any non-local variables that would be sourced
global() {
local script="$(basename "${BASH_SOURCE}")"
local usage="usage: source ${SCRIPT} [path]"
# Make sure the script is sourced, not executed
[ "${BASH_SOURCE}" == "${0}" ] && {
echo "error: ${script} must be sourced!" >&2
return 1
}
main()
{
# Get project directory (optional argument)
local proj_path="${1}"
[ "${1}" == '' ] && proj_path='.'
# Check that project path directory exists
[ -d "${proj_path}" ] || {
echo "error: project path not found: ${proj_path}" >&2
return 1
}
# Check that pipenv is available
which pipenv >/dev/null 2>&1 || {
echo "error: pipenv not found!" >&2
return 1
}
# Path to virtual environment
local venv_path="$(cd "${proj_path}"; pipenv --venv 2>/dev/null)" || {
echo "error: you must enter a project directory!" >&2
return 1
}
# Path to activate script
local activate_path="${venv_path}/bin/activate"
[ ! -f "${activate_path}" ] && {
echo "error: activate script not found: ${activate_path}" >&2
return 1
}
# Activate environment
echo 'source "'"${activate_path}"'"'
\source "${activate_path}" || {
echo "error: cannot source activate script: ${activate_path}" >&2
return 1
}
}
main "${@}" || { echo "${USAGE}" >&2; return $?; }
}; global "${@}"