-
Notifications
You must be signed in to change notification settings - Fork 7
/
10-bashrc.venv.sh
234 lines (199 loc) · 7.19 KB
/
10-bashrc.venv.sh
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
#!/usr/bin/env bash
### bashrc.venv.sh
# note: most of these aliases and functions are overwritten by `we`
## Variables
function _setup_venv {
# _setup_venv() -- configure __PROJECTSRC, PATH, __VENV, _setup_venv_SRC()
# __PROJECTSRC (str): path to local project settings script to source
export __PROJECTSRC="${__WRK}/.projectsrc.sh"
# shellcheck disable=1090
[ -f "$__PROJECTSRC" ] && source "$__PROJECTSRC"
# PATH="~/.local/bin:$PATH" (if not already there)
PATH_prepend "${HOME}/.local/bin"
# __VENV -- path to local venv config script (executable)
export __VENV="${__DOTFILES}/scripts/venv.py"
# CdAlias functions and completions
# shellcheck source=../venv/scripts/venv.sh
source "${__DOTFILES}/etc/venv/scripts/venv.sh"
if [ "${VENVPREFIX}" == "/" ]; then
# shellcheck source=../venv/scripts/venv_root_prefix.sh
source "${__DOTFILES}/etc/venv/scripts/venv_root_prefix.sh"
fi
# You must run this manually if you want a default src venv
# _setup_venv_SRC
}
function _setup_venv_SRC {
# _setup_venv_SRC() -- configure __SRCVENV and __SRC global virtualenv
# __SRCVENV (str): global 'src' venv symlink (~/-wrk/src)
# (e.g. ln -s ~/-wrk/-ve27/src ~/-wrk/src)
export __SRCVENV="${__WRK}/src"
# __SRC (str): global 'src' venv ./src directory path (~/-wrk/src/src)
export __SRC="${__SRCVENV}/src"
if [ ! -e "${__SRCVENV}" ]; then
if [ ! -d "${WORKON_HOME}/src" ]; then
mkvirtualenv -p "$(command -v python)" \
-i pyrpo -i pyline -i pgs src
fi
ln -s "${WORKON_HOME}/src" "${__SRCVENV}"
fi
# ($__SRC/git $__SRC/git)
if [ ! -d "$__SRC" ]; then
mkdir -p \
"${__SRC}/git/github.com" \
"${__SRC}/git/gitlab.com" \
"${__SRC}/git/bitbucket.org" \
"${__SRC}/hg/bitbucket.org"
fi
}
_setup_venv
## Functions
function venv {
# venv $@ -- call $_VENV $@
# venv -h -- print venv --help
# venv --print-bash -- print bash configuration
# venv --print-json -- print IPython configuration as JSON
(set -x; $__VENV "${@}")
}
function venvw {
# venvw $@ -- venv -E $@ (for the current environment)
(set -x; $__VENV -e "${@}")
}
function workon_venv {
# workon_venv() -- workon a virtualenv and load venv (TAB-completion)
# param $1: $VIRTUAL_ENV_NAME ("dotfiles")
# param $2: $_APP ("dotfiles") [default: $1)
# ${WORKON_HOME}/${VIRTUAL_ENV_NAME} # == $VIRTUAL_ENV
# ${VIRTUAL_ENV}/src # == $_SRC
# ${_SRC}/${VIRTUAL_ENV_NAME} # == $_WRD
# examples:
# we dotfiles
# we dotfiles dotfiles
if [ -n "${1}" ]; then
local _venvstr
local _workon_home
if [ -d "${WORKON_HOME}/${1}" ]; then
_venvstr="${1}"
_workon_home="${WORKON_HOME}"
shift
elif [ -d "${1}" ]; then
_venvstr="$(basename "${1}")"
_workon_home="$(dirname "${1}")"
shift
else
echo "err: venv not found: ${1}"
return 1
fi
#append to shell history
history -a
# shellcheck disable=1090
workon "${_venvstr}" && \
source <($__VENV \
--wrk="$__WRK" \
--wh="${_workon_home}" \
--print-bash \
"${_venvstr}" "${@}" ) && \
dotfiles_status && \
declare -f '_setup_venv_prompt' > /dev/null 2>&1 \
&& _setup_venv_prompt "${_TERM_ID:-${_venvstr}}"
else
#if no arguments are specified, list virtual environments
lsvirtualenvs
return 1
fi
}
function we {
# we() -- workon_venv
workon_venv "${@}"
}
complete -o default -o nospace -F _virtualenvs workon_venv
complete -o default -o nospace -F _virtualenvs we
function _setup_venv_aliases {
# _setup_venv_aliases() -- load venv aliases
# note: these are overwritten by `we` [`source <(venv -b)`]
# shellcheck source=../../scripts/_ewrd.sh
source "${__DOTFILES}/scripts/_ewrd.sh"
# shellcheck source=../../scripts/_grinwrd.sh
source "${__DOTFILES}/scripts/_grinwrd.sh"
# makew -- make -C "${WRD}" ${@} [scripts/makew <TAB>]
# shellcheck source=../../scripts/makew
source "${__DOTFILES}/scripts/makew"
# shellcheck source=../../scripts/ssv
source "${__DOTFILES}/scripts/ssv"
# shellcheck disable=2119
_setup_supervisord
# hgw -- hg -R ${_WRD} [scripts/hgw <TAB>]
# shellcheck source=../../scripts/hgw
source "${__DOTFILES}/scripts/hgw"
# gitw -- git -C ${_WRD} [scripts/gitw <TAB>]
# shellcheck source=../../scripts/gitw
source "${__DOTFILES}/scripts/gitw"
# serve-() -- ${_SERVE_}
# alias serve-='${_SERVE_}'
# shell-() -- ${_SHELL_}
# alias shell-='${_SHELL_}'
# test-() -- cd ${_WRD} && python setup.py test
alias testpyw='(cd ${_WRD} && python "${_WRD_SETUPY}" test)'
# testr-() -- reset; cd ${_WRD} && python setup.py test
alias testpywr='(reset; cd ${_WRD} && python "${_WRD_SETUPY}" test)'
}
_setup_venv_aliases
function _setup_venv_prompt {
# _setup_venv_prompt() -- set PS1 with $WINDOW_TITLE, $VIRTUAL_ENV_NAME,
# and ${debian_chroot}
# "WINDOW_TITLE (venvprompt) [debian_chroot]"
# try: _APP, VIRTUAL_ENV_NAME, $(basename VIRTUAL_ENV)
local venvprompt=""
venvprompt=${_APP:-${VIRTUAL_ENV_NAME:-${VIRTUAL_ENV:+"$(basename $VIRTUAL_ENV)"}}}
# TODO: CONDA
# shellcheck disable=2154
export VENVPROMPT="${venvprompt:+"($venvprompt) "}${debian_chroot:+"[$debian_chroot] "}${WINDOW_TITLE:+"$WINDOW_TITLE "}"
if [ -n "$BASH_VERSION" ]; then
# shellcheck disable=2154
if [ "$color_prompt" == yes ]; then
PS1='${VENVPROMPT}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\n\$ '
else
PS1='${VENVPROMPT}\u@\h:\w\n\$ '
unset color_prompt
fi
fi
}
_setup_venv_prompt
function venv_ls {
# venv_ls() -- list virtualenv directories
prefix=${1:-${VIRTUAL_ENV}}
lsargs=${2:-${lsargs:-"-ld"}}
if [ -z "${prefix}" ]; then
return
fi
#ls -ld ${prefix}/**
find "${prefix}" "${prefix}/lib" -maxdepth 2 -type d -print0 \
| xargs -0 ls --color=auto ${lsargs}
}
function lsvenv {
# lsvenv() -- venv_ls()
venv_ls "${@}"
}
function venv_mkdirs {
# venv_mkdirs() -- create FSH paths in ${1} or ${VIRTUAL_ENV}
prefix=${1:-${VIRTUAL_ENV}}
if [ -z "${prefix}" ]; then
return
fi
ensure_mkdir "${prefix}"
ensure_mkdir "${prefix}/bin"
ensure_mkdir "${prefix}/etc"
#ensure_mkdir "${prefix}/home"
ensure_mkdir "${prefix}/lib"
#ensure_mkdir "${prefix}/opt"
#ensure_mkdir "${prefix}/sbin"
#ensure_mkdir "${prefix}/share/doc"
ensure_mkdir "${prefix}/src"
#ensure_mkdir "${prefix}/srv"
ensure_mkdir "${prefix}/tmp"
ensure_mkdir "${prefix}/usr/share/doc"
ensure_mkdir "${prefix}/var/cache"
ensure_mkdir "${prefix}/var/log"
ensure_mkdir "${prefix}/var/run"
ensure_mkdir "${prefix}/var/www"
venv_ls "${prefix}"
}