-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivate
More file actions
executable file
·166 lines (136 loc) · 3.87 KB
/
Copy pathactivate
File metadata and controls
executable file
·166 lines (136 loc) · 3.87 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
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
#!/usr/bin/env bash
# Source this file from bash or zsh to activate the checkout-managed Marvain
# conda environment.
__marvain_env_is_sourced() {
if [ -n "${ZSH_VERSION:-}" ]; then
case "${ZSH_EVAL_CONTEXT:-}" in
*:file|*:file:*) return 0 ;;
esac
return 1
fi
if [ -n "${BASH_VERSION:-}" ]; then
[ "${BASH_SOURCE[0]}" != "$0" ]
return
fi
return 1
}
if ! __marvain_env_is_sourced; then
echo "Error: this script must be sourced." >&2
echo "Usage: source ./activate" >&2
exit 1
fi
__marvain_env_script_path() {
if [ -n "${BASH_VERSION:-}" ]; then
printf '%s\n' "${BASH_SOURCE[0]}"
return 0
fi
if [ -n "${ZSH_VERSION:-}" ]; then
eval 'printf "%s\n" "${(%):-%N}"'
return 0
fi
return 1
}
__marvain_env_conda_is_function() {
case "$(type conda 2>/dev/null)" in
*"shell function"*|*"function"*) return 0 ;;
esac
return 1
}
__marvain_env_conda_exe() {
if [ -n "${CONDA_EXE:-}" ] && [ -x "${CONDA_EXE}" ]; then
printf '%s\n' "${CONDA_EXE}"
return 0
fi
if [ -n "${BASH_VERSION:-}" ]; then
type -p conda
return
fi
if [ -n "${ZSH_VERSION:-}" ]; then
whence -p conda
return
fi
command -v conda
}
__marvain_env_load_conda() {
if ! command -v conda >/dev/null 2>&1; then
return 1
fi
if __marvain_env_conda_is_function; then
return 0
fi
local conda_exe conda_shell conda_hook conda_base
conda_exe="$(__marvain_env_conda_exe 2>/dev/null || true)"
if [ -z "${conda_exe}" ]; then
return 1
fi
if [ -n "${BASH_VERSION:-}" ]; then
conda_shell="bash"
elif [ -n "${ZSH_VERSION:-}" ]; then
conda_shell="zsh"
else
conda_shell="posix"
fi
conda_hook="$("${conda_exe}" "shell.${conda_shell}" hook 2>/dev/null || "${conda_exe}" shell.posix hook 2>/dev/null || true)"
if [ -n "${conda_hook}" ]; then
eval "${conda_hook}"
return 0
fi
conda_base="$("${conda_exe}" info --base 2>/dev/null || true)"
if [ -n "${conda_base}" ] && [ -f "${conda_base}/etc/profile.d/conda.sh" ]; then
. "${conda_base}/etc/profile.d/conda.sh"
return 0
fi
return 1
}
__marvain_env_refresh_command_cache() {
hash -r 2>/dev/null || true
rehash 2>/dev/null || true
}
__marvain_env_env_exists() {
conda env list 2>/dev/null | awk '{print $1}' | grep -qx 'marvain'
}
__marvain_env_repo_root="$(
cd "$(dirname "$(__marvain_env_script_path)")" && pwd
)"
export MARVAIN_REPO_ROOT="${__marvain_env_repo_root}"
export MARVAIN_ACTIVE="1"
__marvain_env_load_conda || {
echo "Error: conda is not available in this shell." >&2
return 1
}
__marvain_env_created_env=0
if ! __marvain_env_env_exists; then
__marvain_env_env_yaml="${MARVAIN_REPO_ROOT}/environment.yaml"
if [ ! -f "${__marvain_env_env_yaml}" ]; then
echo "Error: Marvain environment spec not found at ${__marvain_env_env_yaml}." >&2
return 1
fi
echo "Creating marvain from ${__marvain_env_env_yaml} ..." >&2
conda env create -n marvain -f "${__marvain_env_env_yaml}" || return 1
__marvain_env_created_env=1
fi
conda activate marvain || {
echo "Error: failed to activate the marvain conda environment." >&2
return 1
}
__marvain_env_refresh_command_cache
if [ "${__marvain_env_created_env}" = "1" ]; then
echo "Installing Marvain into marvain from ${MARVAIN_REPO_ROOT} ..." >&2
python -m pip install --editable "${MARVAIN_REPO_ROOT}[dev]" || {
echo "Error: failed to install Marvain into marvain." >&2
return 1
}
__marvain_env_refresh_command_cache
echo "Installed Marvain into marvain from ${MARVAIN_REPO_ROOT}." >&2
fi
unset __marvain_env_created_env
unset __marvain_env_env_yaml
unset __marvain_env_repo_root
unset -f __marvain_env_is_sourced
unset -f __marvain_env_script_path
unset -f __marvain_env_conda_is_function
unset -f __marvain_env_conda_exe
unset -f __marvain_env_load_conda
unset -f __marvain_env_refresh_command_cache
unset -f __marvain_env_env_exists
echo "Marvain activated."