-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanaconda-aliases.bash
executable file
·269 lines (250 loc) · 10 KB
/
anaconda-aliases.bash
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
#! /bin/env bash
# Anaconda support based on conda installation
#
# usage example:
# source ~/bin/tomohara-aliases.bash
# source ~/bin/anaconda-aliases.bash
#
# NOTE: This depends on Tom usual aliases (see all-tomohara-aliases-etc.bash).
#
# TODO: reproduce alias definitions from tomohara-aliases.bash here so independent
#
#------------------------------------------------------------------------
# Copyright (c) 2020 Thomas P. O'Hara
#
# This software is Open Source, licensed under the GNU Lesser General Public Version 3 (LGPLv3). See LICENSE.txt in directory (or repository).
#
# Uncomment the following line(s) for tracing:
## set -o xtrace
## DEBUG: echo "in anaconda-aliases.bash"
# Find directory for conda
# TODO: Use a more standard fallback location
export ANACONDA_HOME
ANACONDA_HOME=$(realpath "$(dirname "$(which conda)" 2> /dev/null)"/../)
if [ "$ANACONDA_HOME" = "" ]; then
export ANACONDA_HOME=/usr/local/misc/programs/anaconda3
fi
# add-conda-env-to-xterm-title(): puts conda prompt modifier in xterm title
# along with python version (e.g., "...; Py3.6:(old_tensorflow)")
# note: also resets prompt to '$ ' (following change by conda scripts)
# See set_xterm_title.bash for underyling support (e.g., XTERM_TITLE_SUFFIX).
function add-conda-env-to-xterm-title {
## DEBUG: echo "in add-conda-env-to-xterm-title"
export XTERM_TITLE_SUFFIX
## OLD: XTERM_TITLE_SUFFIX="Py$(python --version 2>&1 | extract_matches.perl -i 'python (\d+.\d+)'):$CONDA_PROMPT_MODIFIER"
XTERM_TITLE_SUFFIX="Py$(python --version 2>&1 | extract_matches.perl -i 'python (\d+.\d+)')"
if [ "$CONDA_PROMPT_MODIFIER" != "" ]; then
XTERM_TITLE_SUFFIX="$XTERM_TITLE_SUFFIX:$CONDA_PROMPT_MODIFIER"
fi
## Note: temp check for odd repeated version spec (e.g., "Py3.10; Py3.10")
if [[ $XTERM_TITLE_SUFFIX =~ Py[0-9]\.[0-9].*Py[0-9]\.[0-9] ]]; then
echo "Warning: unexptected python spec in w/ xterm title suffix ($XTERM_TITLE_SUFFIX)"
fi
reset-prompt
set-title-to-current-dir
}
# TODO: Put in a separate file (e.g., .bashrc.anaconda)
anaconda3_dir="$HOME/anaconda3"
anaconda2_dir="$HOME/anaconda2"
# init-condaN([- | dir]): initialize anaconda using specified dir (or ~/anaconda3)
function init-condaN {
local anaconda_dir="$1"
## DEBUG: echo "in init-condaN $1*"
if [ "$anaconda_dir" = "" ]; then echo "Usage: init-condaN anaconda-dir"; return; fi
if [ "$anaconda_dir" = "-" ]; then anaconda_dir="$anaconda3_dir"; fi
# start: >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
# Note: assignment separated so that $? preserved:
# local x; x="$(...)"; if [ $? -eq 0 ]; ...
# See https://unix.stackexchange.com/questions/506352/bash-what-does-masking-return-values-mean
local conda_setup
conda_setup="$("$anaconda_dir"'/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$conda_setup"
else
if [ -f "$anaconda_dir/etc/profile.d/conda.sh" ]; then
. "$anaconda_dir/etc/profile.d/conda.sh"
fi
prepend-path "$anaconda_dir/bin"
fi
# end: <<< conda initialize <<<
# Restore Bash prompt and put conda environment in xterm title instead
add-conda-env-to-xterm-title
# HACK: make sure cls aliased to default Unix version of clear (anaconda3 quirk) and also that it doesn't clear the terminal buffer [n.b., maldito stupid feature introduction in long-stading utility!]
alias cls='/usr/bin/clear -x'
## DEBUG: echo "out: init-condaN()"
}
alias init-conda3='init-condaN "$anaconda3_dir"'
alias init-conda2='init-condaN "$anaconda2_dir"'
# note: version that makes dir arg optional
alias init-conda3-new='init-condaN'
## TODO: reference-var "$anaconda2_dir"
echo "$anaconda2_dir" > /dev/null
# show-python-path(): display current path for python binary to stderr
function trace-python-path {
local python_path=""
python_path=$(/usr/bin/which python 2> /dev/null)
trace-vars python_path
reference-variable "$python_path"
}
# Work around for intermittent problems w/ 'conda activate' requiring 'source activate' instead.
# activation-helper is to handle deactivate as well
## OLD: function activation-helper () {
function activation-helper {
## DEBUG: echo "in activation-helper($@)"
local command="$1"
local env="$2"
local conda_command="conda"
local alt_conda_command=""
## DEBUG: trace-vars conda_command alt_conda_command
# Note: need to use conda's alias not the script returned by which
local conda_path=""
conda_path=$(/usr/bin/which conda 2> /dev/null)
if [ "$conda_path" = "" ]; then
## DEBUG: echo "Switching conda command"
conda_command="source"
alt_conda_command="conda"
else
## TODO:
## conda_command=""
## alt_conda_command="conda"
true
fi
## DEBUG: trace-vars conda_command alt_conda_command
## DEBUG:
echo "Issuing: $conda_command" "$command" "$env"
echo "Alt: $alt_conda_command" "$command" "$env"
$conda_command "$command" "$env"
## DEBUG:
trace-python-path
## OLD:
## local python_path=""
## python_path=$(/usr/bin/which python 2> /dev/null)
## trace-vars python_path
## reference-variable "$python_path"
## DEBUG: echo "out activation-helper($@)"
}
alias conda-activate='activation-helper activate'
## OLD: alias conda-deactivate='source deactivate'
alias conda-deactivate='activation-helper deactivate'
#
alias add-tensorfow='conda-activate env_tensorflow_gpu'
alias all-conda-to-pythonpath='export PYTHONPATH=$anaconda3_dir/envs/env_tensorflow_gpu/lib/python3.7/site-packages:$anaconda3_dir/lib/python3.7:$PYTHONPATH'
# note: various conda-xyz aliases for sake of tab completion
# conda-list-env: show environent names
alias conda-list-env='conda list env'
#
# conda-list-env-hack(): show environent names from typical directories
# maldito shellcheck [SC2010]: Don't use ls | grep. Use a glob or a for loop ...
# shellcheck disable=SC2010
function conda-list-env-hack-aux { ls ~/.conda/envs "${ANACONDA_HOME}"/envs 2> /dev/null | grep -v ':$' | sort; }
function conda-list-env-hack { conda-list-env-hack-aux | echoize; }
#
alias conda-env-list='conda env list'
alias conda-env-name='conda env list | extract_matches.perl "^(\S+ ) " | echoize'
# conda-activate-env(name, [use_hack=0]): Activate NAME or show list of environment if empty or "-"
# Note: 'conda env list' is slow, so USE_HACK resorts to 'ls ~/.conda/envs'
function conda-activate-env {
## DEBUG: echo "in conda-activate-env $*"
local env="$1"
local use_hack="${2:-0}"
if [[ ("$env" = "") || ("$env" = "-") ]]; then
echo "Usage: conda-activate-env env [use_hack=0]"
echo ""
echo "Note: available environments:"
## TODO: use columns
## BAD:
if [ "$use_hack" != "0" ]; then
conda-list-env-hack | perl -pe 's/ / /;'
else
conda-env-name
fi
echo ""
fi
conda-activate "$env"
add-conda-env-to-xterm-title;
}
# conda-activate-env-hack(name): invokes conda-activate-env with hack enabled
function conda-activate-env-hack { conda-activate-env "$1" "1"; }
#
# conda-activate-env-like(env): switch from current anaconda environment to ENV
# note: this currently just modifies the path and is only intended for quick switching
# ex: /home/tomohara/anaconda3/envs/nlp-py-3-10 => /home/tomohara/anaconda3/envs/test-nlp-py-3-10
function conda-activate-env-like {
local env_name="$1"
# note: regex groups 1 222 3333333333
# /.../_ENV_/bin/python
local bin_dir
bin_dir=$(which python | perl -pe "s@^((.*)/[^/]+/bin/python$)@\2/$env_name/bin@;")
if [ ! -e "$bin_dir" ]; then
echo "Error: unable to resolve anaconda bin dir from current path"
else
echo "Warning: only prepending PATH w/ $bin_dir (e.g., no CONDA_ env updates)"
export PATH="$bin_dir:$PATH";
fi
add-conda-env-to-xterm-title
## DEBUG:
trace-python-path
}
#
function conda-deactivate-env {
conda-deactivate
add-conda-env-to-xterm-title
}
# older Miniconda3-based initializaion
# # >>> conda initialize >>>
# # !! Contents within this block are managed by 'conda init' !!
function old-init-conda {
local base="$ANACONDA_HOME"
local conda_setup
conda_setup="$("$base/bin/conda" 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$conda_setup"
else
if [ -f "$base/etc/profile.d/conda.sh" ]; then
. "$base/etc/profile.d/conda.sh"
fi
prepend-path "$base/bin"
fi
# Restore Bash prompt and put conda environment in xterm title instead
add-conda-env-to-xterm-title
}
# # <<< conda initialize <<<
# conda-create-env(name, [python_version=3.9]): create Python3 environment for Python 3.9 by default
# TODO: determine the version, make sure ipython gets installed
## OLD: function conda-create-env () {
function conda-create-env {
if [ "$1" = "" ]; then
echo "usage: conda-create-env name [python_version=3.9]"
return
fi
local name="$1"
local python_version="$2"
if [ "$python_version" = "" ]; then python_version="3.9"; fi
ensure-conda-loaded
echo "issuing: conda create --yes --name '$name' python='$python_version'"
conda create --yes --name "$name" python="$python_version"
}
# ensure-conda-loaded(): Make sure conda environment loaded (e.g., via old-init-conda)
#
function ensure-conda-loaded {
if [ "$CONDA_PYTHON_EXE" = "" ]; then
echo "Warning: Conda not initialized so using old-init-conda"
old-init-conda;
fi
}
# conda-init-env(name): make sure conda environment NAME is active, as well
# as ensuring conda itself is active.
#
function conda-init-env {
local env_name="$1"
ensure-conda-loaded
conda-activate "$env_name"
add-conda-env-to-xterm-title
}
# Misc. aliases
alias conda-info-env='conda info --envs'
# Uncomment the following line for tracing:
## echo "out anaconda-aliases.bash"
## TODO: delete
## dummy change for git