forked from Xinglab/IRIS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconda.sh
57 lines (48 loc) · 1.58 KB
/
conda.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
#!/bin/bash
#
# Provide functions for working with conda
# Usage:
# * `source conda.sh`
# * `conda::create_env_with_name_and_python_version {env_name} {python_version}`
# + example: `conda::create_env_with_name_and_python_version my-conda-env 3.6`
# * `conda::activate_env {env_name}`
# + example: `conda::activate_env my-conda-env`
# * `conda::deactivate_env`
#
# Assumes that conda is installed
# https://docs.conda.io/en/latest/miniconda.html
# https://repo.anaconda.com/miniconda/Miniconda2-latest-Linux-x86_64.sh
function conda::create_env_with_name_and_python_version() {
local ERROR_PREFIX="error in conda::create_env_with_name_and_python_version()"
local ENV_NAME="$1"
local PYTHON_VERSION="$2"
local FOUND_COUNT=$(conda info --envs | grep "^${ENV_NAME} .*/${ENV_NAME}$" | wc -l)
if [[ "$?" -ne 0 ]]; then
echo "${ERROR_PREFIX}: checking conda envs" >&2
return 1
fi
if [[ "${FOUND_COUNT}" -eq 1 ]]; then
echo "using existing ${ENV_NAME} conda environment"
return 0
fi
echo "creating new conda environment: ${ENV_NAME} python=${PYTHON_VERSION}"
conda create --name "${ENV_NAME}" python="${PYTHON_VERSION}"
if [[ "$?" -ne 0 ]]; then
echo "${ERROR_PREFIX}: creating env" >&2
return 1
fi
}
export -f conda::create_env_with_name_and_python_version
function conda::activate_env() {
conda activate "$1"
}
export -f conda::activate_env
function conda::deactivate_env() {
conda deactivate
}
export -f conda::deactivate_env
function main() {
# need to use the setup that conda init writes to .bashrc
source ${HOME}/.bashrc || return 1
}
main "$@"