-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipx-activate.bashrc
More file actions
executable file
·56 lines (49 loc) · 1.59 KB
/
pipx-activate.bashrc
File metadata and controls
executable file
·56 lines (49 loc) · 1.59 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
#!/usr/bin/env bash
##
# Attributions:
#
# - https://stackoverflow.com/a/78876474
pipx-activate() {
local -;
set -eET;
local _package_name _pipx_local_venvs _package_directory _reply _package_activate;
_package_name="${1:?Undefined package name}"
printf 1>&2 'Attempting to activate -> %s\n' "${_package_name}"
_pipx_local_venvs="$(awk -F '=' '/PIPX_LOCAL_VENVS/{ print $2; }' <(pipx environment))"
if ! (( ${#_pipx_local_venvs} )); then
printf 1>&2 'Error: cannot locate directory for PIPX_LOCAL_VENVS\n'
exit 1
fi
_package_directory="${_pipx_local_venvs}/${_package_name}"
if ! [[ -d "${_package_directory}" ]]; then
if [[ -t 0 ]]; then
read -rdt 60 "Warning: missing package \"${_package_name}\", okay to install? (Y/n) "
_reply="${REPLY:?Undefined reply}"
shopt -s extglob
case "${_reply,,}" in
yes|yep|y)
pipx install "${_package_name}"
if ! [[ -d "${_package_directory}" ]]; then
printf 1>&2 'Error: cannot locate "%s" even after install\n' "${_package_name}"
exit 1
fi
;;
*)
printf 1>&2 'Warning: non-yes like reply received, exiting now\n'
exit 1
;;
esac
else
printf 1>&2 'Error: cannot assume non-interactive session wants to install missing package -> %s\n' "${_package_name}"
exit 1
fi
fi
_package_activate="${_package_directory}/bin/activate"
if ! [[ -f "${_package_activate}" ]]; then
printf 1>&2 'Error cannot locate activate path -> %s\n' "${_package_activate}"
fi
printf 1>&2 'source "%s"\n' "${_package_activate}"
# shellcheck disable=SC1090
source "${_package_activate}"
}
export -f pipx-activate