-
Notifications
You must be signed in to change notification settings - Fork 0
/
conda_venv.sh
190 lines (162 loc) · 6.51 KB
/
conda_venv.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
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
#!/usr/bin/env bash
# Copyright 2019-2023 Darren Weber
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Source this file from ~/.bashrc, ~/.zshrc or a compatible shell-init, such
# as copy the file to /etc/profile.d/
# https://python-release-cycle.glitch.me/
# https://endoflife.date/python
# Declare a default python version to support; this could be the lowest
# version that is still in active maintenance or it could match the current
# default version used in Homebrew or an Ubuntu LTS release.
export PYTHON_SUPPORT_VERSION=${PYTHON_SUPPORT_VERSION:-3.11}
# The conda-venv-py?? functions could be run anytime there is a patch release to
# any python version, updates to conda or anytime a clean conda env is required
# with a non-default python version.
# Support for Different Architectures
# https://stackoverflow.com/questions/33709391/using-multiple-python-engines-32bit-64bit-and-2-7-3-5/58014896#58014896
conda-subdir () {
if [ $(uname) = "Darwin" ]; then
if [ $(uname -m) = "x86_64" ]; then
echo "osx-64"
fi
if [ $(uname -m) = "arm64" ]; then
echo "osx-arm64"
fi
fi
if [ $(uname) = "Linux" ]; then
if [ $(uname -m) = "x86_64" ]; then
echo "linux-64"
fi
if [ $(uname -m) = "aarch64" ]; then
echo "linux-arm64"
fi
fi
# TODO: support windows?
}
export CONDA_SUBDIR=$(conda-subdir)
conda-venv-base () {
py_ver="${1:-${PYTHON_SUPPORT_VERSION}}"
conda deactivate
conda env remove -n py"${py_ver}" || true
conda create -y -n py"${py_ver}" python="${py_ver}" -c conda-forge \
&& conda activate py"${py_ver}" \
&& conda config --env --add channels conda-forge \
&& conda config --env --set channel_priority strict \
&& conda config --env --set subdir ${CONDA_SUBDIR}
# ?? conda env config vars set CONDA_SUBDIR=osx-arm64
}
conda-venv-py3.8 () {
conda-venv-base 3.8
}
conda-venv-py3.9 () {
conda-venv-base 3.9
}
conda-venv-py3.10 () {
conda-venv-base 3.10
}
conda-venv-py3.11 () {
conda-venv-base 3.11
}
# declare some useful aliases to use a conda python version;
# these aliases simply try to activate an existing env.
alias conda-py3.8='conda deactivate; conda activate py3.8'
alias conda-py3.9='conda deactivate; conda activate py3.9'
alias conda-py3.10='conda deactivate; conda activate py3.10'
alias conda-py3.11='conda deactivate; conda activate py3.11'
conda-project () {
# The project name is defined by CONDA_ENV or the current working directory
project=${CONDA_ENV:-$(pwd)}
basename "${project}"
}
conda-venv-activate () {
# try to activate a conda environment with the name of
# the current directory (often this is a project name).
wd=$(conda-project)
conda deactivate
conda activate "$wd"
}
conda-venv-create () {
# create and activate a conda environment with the name
# of the current directory (often this is a project name).
py_ver="${1:-${PYTHON_SUPPORT_VERSION}}"
wd=$(conda-project)
conda deactivate
conda create -n "${wd}" python="${py_ver}" \
--channel conda-forge --override-channels \
&& conda activate "${wd}" \
&& conda config --env --add channels conda-forge \
&& conda config --env --set channel_priority strict \
&& conda config --env --set subdir ${CONDA_SUBDIR}
}
conda-venv-remove () {
# try to activate a conda environment with the name of
# the current directory (often this is a project name).
wd=$(conda-project)
conda deactivate
conda env remove -n "$wd"
}
conda-venv () {
# create and activate a conda environment with the name
# of the current directory (often this is a project name).
py_ver="${1:-${PYTHON_SUPPORT_VERSION}}"
wd=$(conda-project)
if conda env list | grep -E "^${wd}\s+" > /dev/null; then
conda-venv-activate
else
conda-venv-create "${py_ver}"
fi
command -v poetry > /dev/null
if command -v python > /dev/null; then
python --version
fi
}
conda-install () {
root_url="https://github.com/conda-forge/miniforge/releases/latest/download"
installer="Miniforge3-$(uname)-$(uname -m).sh"
install_script="/tmp/${installer}"
echo "Downloading: ${root_url}/${installer}"
curl -L -s "${root_url}/${installer}" > "$install_script"
echo "To run the downloaded installer: sudo /bin/bash $install_script -f -b"
if [ $(uname -m) = "x86_64" ]; then
echo "Optional installation to custom path: sudo /bin/bash $install_script -p /usr/local/miniforge3 -f -b"
echo "Set permissions on the installation: sudo chown -R $USER:admin /usr/local/miniforge3"
fi
if [ $(uname -m) = "arm64" ]; then
echo "Optional installation to custom path: sudo /bin/bash $install_script -p /opt/miniforge3 -f -b"
echo "Set permissions on the installation: sudo chown -R $USER:admin /opt/miniforge3"
fi
}
mamba-install () {
root_url="https://github.com/conda-forge/miniforge/releases/latest/download"
installer="Mambaforge-$(uname)-$(uname -m).sh"
install_script="/tmp/${installer}"
echo "Downloading: ${root_url}/${installer}"
curl -L -s "${root_url}/${installer}" > "$install_script"
echo "To run the downloaded installer: sudo /bin/bash $install_script -f -b"
if [ $(uname -m) = "x86_64" ]; then
echo "Optional installation to custom path: sudo /bin/bash $install_script -p /usr/local/mambaforge -f -b"
echo "Set permissions on the installation: sudo chown -R ${USER}:staff /usr/local/mambaforge"
fi
if [ $(uname -m) = "arm64" ]; then
echo "Optional installation to custom path: sudo /bin/bash $install_script -p /opt/mambaforge -f -b"
echo "Set permissions on the installation: sudo chown -R ${USER}:staff /opt/mambaforge"
fi
}
_conda-venv-completions () {
command_options="3.8 3.9 3.10 3.11"
COMPREPLY=($(compgen -W "${command_options}" "${COMP_WORDS[1]}"))
}
complete -F _conda-venv-completions conda-venv
complete -F _conda-venv-completions conda-venv-base
complete -F _conda-venv-completions conda-venv-create