forked from ajdawson/condawrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
condawrapper_completion.bash
141 lines (130 loc) · 3.83 KB
/
condawrapper_completion.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
#!/bin/bash
# Bash completion for condawrapper.
#
#| Copyright (c) 2014-2015 Andrew Dawson
#|
#| Permission is hereby granted, free of charge, to any person obtaining
#| a copy of this software and associated documentation files (the
#| "Software"), to deal in the Software without restriction, including
#| without limitation the rights to use, copy, modify, merge, publish,
#| distribute, sublicense, and/or sell copies of the Software, and to
#| permit persons to whom the Software is furnished to do so, subject to
#| the following conditions:
#|
#| The above copyright notice and this permission notice shall be
#| included in all copies or substantial portions of the Software.
#|
#| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#| EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
#| MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
#| NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
#| BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
#| ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
#| CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#| SOFTWARE.
#
# Description:
# Defines a completion for condawrapper's activate function allowing
# the use of tab completion for environment names.
#
#-----------------------------------------------------------------------
# Check that conda is available in the current shell.
#
# Globals:
# None
# Arguments:
# None
# Returns:
# None
# Exits:
# 0 if conda is available, 1 otherwise.
#-----------------------------------------------------------------------
__conda_check () {
if ! which conda &> /dev/null; then
return 1
fi
return 0
}
#-----------------------------------------------------------------------
# Find the root environment's Python interpreter.
#
# This is found rather crudely by looking at the '#!' line of the conda
# executable to get the full path to the required Python interpreter.
#
# Globals:
# None
# Arguments:
# None
# Returns:
# The full path to the root environment's Python interpreter, or
# empty if the root environment cannot be located.
#-----------------------------------------------------------------------
__find_root_python () {
if __conda_check; then
head -n 1 $(which conda) | cut -c 3-
fi
}
#-----------------------------------------------------------------------
# Find the directories containing
#-----------------------------------------------------------------------
__find_env_dirs () {
local root_python=$(__find_root_python)
if [ -z "$root_python" ]; then
echo "$HOME/envs"
else
"$root_python" << EOF
from __future__ import print_function
import sys
try:
import conda.config
except ImportErrors:
print("failed to import conda, aborting", file=sys.stderr)
sys.exit(1)
print(" ".join(conda.config.envs_dirs))
sys.exit(0)
EOF
fi
}
#-----------------------------------------------------------------------
# Build a list of environment names.
#
# Globals:
# None
# Arguments:
# None
# Returns:
# A list of environment names.
#-----------------------------------------------------------------------
__generate_env_list () {
envdir=/anaconda3/envs
ls -1 "$envdir"
}
#-----------------------------------------------------------------------
# Generic completion function for condawrapper.
#
# Globals:
# COMPREPLY
# COMP_WORDS
# COMP_CWORD
# Arguments:
# None
# Returns:
# None
#-----------------------------------------------------------------------
__envcomplete () {
local cur
local env_list
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
case "$cur" in
*)
env_list="$(__generate_env_list)"
COMPREPLY=($(compgen -W "$env_list" -- $cur))
;;
esac
return 0
}
# Register the completions:
complete -F __envcomplete activate
complete -F __envcomplete workon
complete -F __envcomplete rmcondaenv