forked from conda/conda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart
234 lines (214 loc) · 7.12 KB
/
start
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
#!/usr/bin/env sh
# NOTE: This script should be sourced! The shebang is only here to help syntax highlighters.
if ! (return 0 2> /dev/null); then
echo "ERROR: Source this script: source '$0'." >&2
exit 1
fi
cleanup() {
unset _AUTOBASE
unset _BASEEXE
unset _CMD
unset _DEVENV
unset _DRYRUN
unset _ENV
unset _ENVEXE
unset _EXPORTED
unset _NAME
unset _PYTHON
unset _PYTHONEXE
unset _SRC
unset _UPDATE
unset _UPDATED
}
updating() {
# check if explicitly updating or if 24 hrs since last update
[ ${_UPDATE} = 0 ] && return 0
[ -f "${_UPDATED}" ] || return 0
return $(( $(( $(date +%s) - $(date -r "${_UPDATED}" +%s) )) < 86400 ))
}
cleanup
# parse args
while [ $# -gt 0 ]; do
case $1 in
-p|--python)
_PYTHON="$2"
shift
shift
;;
--python=*)
_PYTHON="${1#*=}"
shift
;;
-u|--update)
_UPDATE=0
shift
;;
-d|--devenv)
_DEVENV="$2"
shift
shift
;;
--devenv=*)
_DEVENV="${1#*=}"
shift
;;
-n|--dry-run)
_DRYRUN=0
shift
;;
-h|--help)
# since zsh is MacOS standard fallback to $0 if not in bash
echo "Usage: source ${BASH_SOURCE:-$0} [options]"
echo ""
echo "Options:"
echo " -p, --python VERSION Python version for the env to activate. (default: 3.8)"
echo " -u, --update Force update packages. (default: update every 24 hours)"
echo " -d, --devenv PATH Path to base env install, can also be defined in ~/.condarc."
echo " Path is appended with $(uname). (default: devenv)"
echo " -n, --dry-run Display env to activate. (default: false)"
echo " -h, --help Display this."
return 0
;;
*)
echo "Error: unknown option $1" >&2
return 1
;;
esac
done
# fallback to default values
_PYTHON="${_PYTHON:-3.8}"
_UPDATE="${_UPDATE:-1}"
_DRYRUN="${_DRYRUN:-1}"
# get source path
# since zsh is MacOS standard fallback to $0 if not in bash
_SRC="$(cd "$(dirname "$(dirname "${BASH_SOURCE:-$0}")")" 2>&1 > /dev/null; pwd -P)"
# read devenv key from ~/.condarc
if [ -f ~/.condarc ]; then
_DEVENV="${_DEVENV:-$(grep "^devenv:" ~/.condarc | tail -n 1 | sed -e "s/^.*:[[:space:]]*//" -e "s/[[:space:]]*$//")}"
fi
# fallback to devenv in source default
_DEVENV="${_DEVENV:-${_SRC}/devenv}"
# tilde expansion
_DEVENV="${_DEVENV/#\~/${HOME}}"
# include OS
_DEVENV="${_DEVENV}/$(uname)"
# ensure exists and absolute path
if [ ${_DRYRUN} = 1 ]; then
mkdir -p "${_DEVENV}"
_DEVENV="$( cd "${_DEVENV}" 2>&1 > /dev/null ; pwd -P)"
fi
# other environment values
_NAME="devenv-${_PYTHON}-c"
_ENV="${_DEVENV}/envs/${_NAME}"
_UPDATED="${_ENV}/.devenv-updated"
case "$(uname)" in
Darwin|Linux) _BASEEXE="${_DEVENV}/bin/conda" ; _ENVEXE="${_ENV}/bin/conda" ; _PYTHONEXE="${_ENV}/bin/python" ;;
*) _BASEEXE="${_DEVENV}/Scripts/conda.exe" ; _ENVEXE="${_ENV}/Scripts/conda.exe" ; _PYTHONEXE="${_ENV}/Scripts/python.exe" ;;
esac
# dryrun printout
if [ ${_DRYRUN} = 0 ]; then
echo "Python: ${_PYTHON}"
echo "Updating: $(updating && echo "[yes]" || echo "[no]")"
echo "Devenv: ${_DEVENV} $([ -e "${_DEVENV}" ] && echo "[exists]" || echo "[pending]")"
echo ""
echo "Name: ${_NAME}"
echo "Path: ${_ENV} $([ -e "${_ENV}" ] && echo "[exists]" || echo "[pending]")"
echo ""
echo "Source: ${_SRC}"
return 0
fi
# deactivate any prior envs
if ! [ ${CONDA_SHLVL:-0} = 0 ]; then
echo "Deactivating ${CONDA_SHLVL} environment(s)..."
while ! [ ${CONDA_SHLVL:-0} = 0 ]; do
if ! conda deactivate; then
echo "Error: failed to deactivate environment(s)" 1>&2
return 1
fi
done
fi
# does miniconda install exist?
if ! [ -f "${_DEVENV}/conda-meta/history" ]; then
# downloading miniconda
if [ "$(uname)" = "Darwin" ]; then
[ -f "${_DEVENV}/miniconda.sh" ] || _CMD='curl -s "https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh" -o "${_DEVENV}/miniconda.sh"'
elif [ "$(uname)" = "Linux" ]; then
[ -f "${_DEVENV}/miniconda.sh" ] || _CMD='curl -s "https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh" -o "${_DEVENV}/miniconda.sh"'
else
[ -f "${_DEVENV}/miniconda.exe" ] || _CMD='powershell.exe -Command "Invoke-WebRequest -Uri 'https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe' -OutFile '${_DEVENV}/miniconda.exe' | Out-Null"'
fi
if [ ${_CMD:+0} = 0 ]; then
echo "Downloading miniconda..."
eval ${_CMD}
if ! [ $? = 0 ]; then
echo "Error: failed to download miniconda" 1>&2
return 1
fi
fi
# installing miniconda
echo "Installing development environment..."
case "$(uname)" in
Darwin|Linux) bash "${_DEVENV}/miniconda.sh" -bfp "${_DEVENV}" > /dev/null ;;
*) cmd.exe /c "start /wait \"\" \"${_DEVENV}\miniconda.exe\" /InstallationType=JustMe /RegisterPython=0 /AddToPath=0 /S /D=${_DEVENV} > NUL" ;;
esac
if ! [ $? = 0 ]; then
echo "Error: failed to install development environment" 1>&2
return 1
fi
fi
# create empty env if it doesn't exist
if ! [ -d "${_ENV}" ]; then
echo "Creating ${_NAME}..."
if ! "${_BASEEXE}" create -yq --prefix "${_ENV}" > /dev/null; then
echo "Error: failed to create ${_NAME}" 1>&2
return 1
fi
fi
# check if explicitly updating or if 24 hrs since last update
if updating; then
echo "Updating ${_NAME}..."
if ! "${_BASEEXE}" update -yq --all > /dev/null; then
echo "Error: failed to update development environment" 1>&2
return 1
fi
if ! "${_BASEEXE}" install \
-yq \
--prefix "${_ENV}" \
--override-channels \
-c defaults \
--file "${_SRC}/tests/requirements.txt" \
$([ "$(uname)" = "Linux" ] && echo "patchelf") \
"python=${_PYTHON}" > /dev/null; then
echo "Error: failed to update ${_NAME}" 1>&2
return 1
fi
# update timestamp
touch "${_UPDATED}"
fi
# initialize conda command
echo "Initializing shell integration..."
_AUTOBASE="${CONDA_AUTO_ACTIVATE_BASE:-undefined}"
_EXPORTED="$(export -p | grep CONDA_AUTO_ACTIVATE_BASE)"
export CONDA_AUTO_ACTIVATE_BASE=false
eval "$(cd "${_SRC}" ; "${_ENVEXE}" init --dev bash)" > /dev/null
if [ "${_AUTOBASE}" = "undefined" ]; then
unset CONDA_AUTO_ACTIVATE_BASE
elif [ -n ${_EXPORTED+x} ]; then
export CONDA_AUTO_ACTIVATE_BASE="${_AUTOBASE}"
else
CONDA_AUTO_ACTIVATE_BASE="${_AUTOBASE}"
fi
if ! [ $? = 0 ]; then
echo "Error: failed to initialize shell integration" 1>&2
return 1
fi
# activate env
echo "Activating ${_NAME}..."
if ! conda activate "${_ENV}" > /dev/null; then
echo "Error: failed to activate ${_NAME}" 1>&2
return 1
fi
export PYTHONPATH="${_SRC}:${PYTHONPATH}"
cleanup
unset -f cleanup
unset -f updating