-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivate
More file actions
executable file
·158 lines (136 loc) · 4.84 KB
/
activate
File metadata and controls
executable file
·158 lines (136 loc) · 4.84 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
#!/usr/bin/env bash
# Dewey environment activation script
# Usage: source ./activate <deploy-name>
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
echo "Error: This script must be sourced, not executed."
echo "Usage: source ./activate <deploy-name>"
exit 1
fi
_dewey_script_path() {
if [[ -n "${ZSH_VERSION:-}" ]]; then
printf '%s\n' "${(%):-%x}"
elif [[ -n "${BASH_SOURCE[0]:-}" ]]; then
printf '%s\n' "${BASH_SOURCE[0]}"
else
printf '%s\n' "$0"
fi
}
_DEWEY_SCRIPT_PATH="$(_dewey_script_path)"
DEWEY_ROOT="$(cd "$(dirname "${_DEWEY_SCRIPT_PATH}")" && pwd)"
unset -f _dewey_script_path
unset _DEWEY_SCRIPT_PATH
_GREEN='\033[0;32m'
_YELLOW='\033[1;33m'
_CYAN='\033[0;36m'
_NC='\033[0m'
_dewey_fail() {
echo -e " ${_YELLOW}⚠${_NC} $1" >&2
return 1
}
_dewey_validate_deploy_name() {
local deploy_name="$1"
if [[ ! "$deploy_name" =~ ^[A-Za-z0-9-]{2,8}$ ]]; then
echo -e " ${_YELLOW}⚠${_NC} deploy-name must match ^[A-Za-z0-9-]{2,8}$" >&2
return 1
fi
}
if [[ "$#" -ne 1 ]]; then
echo "Error: Dewey activation requires exactly one positional deploy-name."
echo "Usage: source ./activate <deploy-name>"
return 1
fi
_dewey_deployment_code="$1"
if ! _dewey_validate_deploy_name "${_dewey_deployment_code}"; then
return 1
fi
_DEWEY_CONDA_ENV_BASE="DEWEY"
_DEWEY_CONDA_ENV_NAME="${_DEWEY_CONDA_ENV_BASE}-${_dewey_deployment_code}"
unset _dewey_deployment_code
export DEWEY_ROOT
export DEWEY_DEPLOYMENT_CODE="${_DEWEY_CONDA_ENV_NAME#DEWEY-}"
export DEPLOYMENT_CODE="${DEWEY_DEPLOYMENT_CODE}"
export LSMC_DEPLOYMENT_CODE="${DEWEY_DEPLOYMENT_CODE}"
export DEWEY_ACTIVE=1
export DEWEY_PROJECT_ROOT="$DEWEY_ROOT"
echo -e "${_CYAN}Activating Dewey environment...${_NC}"
if ! command -v conda &> /dev/null; then
_dewey_fail "Conda is required but was not found on PATH."
unset -f _dewey_fail
return 1
fi
_DEWEY_CONDA_BASE="$(conda info --base 2>/dev/null)"
if [[ -z "${_DEWEY_CONDA_BASE}" ]] || [[ ! -f "${_DEWEY_CONDA_BASE}/etc/profile.d/conda.sh" ]]; then
_dewey_fail "Unable to locate conda.sh from 'conda info --base'."
unset _DEWEY_CONDA_BASE
unset -f _dewey_fail
return 1
fi
source "${_DEWEY_CONDA_BASE}/etc/profile.d/conda.sh" 2>/dev/null || {
_dewey_fail "Failed to source conda.sh from ${_DEWEY_CONDA_BASE}."
unset _DEWEY_CONDA_BASE
unset -f _dewey_fail
return 1
}
_DEWEY_CONDA_ENV_CREATED=0
if [[ "${CONDA_DEFAULT_ENV:-}" == "${_DEWEY_CONDA_ENV_NAME}" ]] || \
[[ "${CONDA_PREFIX:-}" == "${_DEWEY_CONDA_BASE}/envs/${_DEWEY_CONDA_ENV_NAME}" ]]; then
echo -e " ${_GREEN}✓${_NC} Conda environment already active: ${_DEWEY_CONDA_ENV_NAME}"
elif conda info --envs | grep -Eq "(^|[[:space:]])${_DEWEY_CONDA_ENV_NAME}([[:space:]]|$)"; then
echo -e " ${_GREEN}✓${_NC} Activating conda environment: ${_DEWEY_CONDA_ENV_NAME}"
if ! conda activate "${_DEWEY_CONDA_ENV_NAME}"; then
_dewey_fail "Failed to activate conda environment: ${_DEWEY_CONDA_ENV_NAME}"
unset _DEWEY_CONDA_BASE
unset -f _dewey_fail
return 1
fi
else
echo -e " ${_YELLOW}⚠${_NC} Conda environment '${_DEWEY_CONDA_ENV_NAME}' not found."
if [[ ! -f "$DEWEY_ROOT/environment.yaml" ]]; then
_dewey_fail "Conda environment '${_DEWEY_CONDA_ENV_NAME}' was not found and environment.yaml is missing."
unset _DEWEY_CONDA_BASE
unset -f _dewey_fail
return 1
fi
echo -e " ${_CYAN}→${_NC} Installing conda environment from environment.yaml..."
if ! conda env create -n "${_DEWEY_CONDA_ENV_NAME}" -f "$DEWEY_ROOT/environment.yaml"; then
_dewey_fail "Failed to create conda environment from environment.yaml."
unset _DEWEY_CONDA_BASE
unset -f _dewey_fail
return 1
fi
echo -e " ${_GREEN}✓${_NC} Activating conda environment: ${_DEWEY_CONDA_ENV_NAME}"
if ! conda activate "${_DEWEY_CONDA_ENV_NAME}"; then
_dewey_fail "Failed to activate conda environment: ${_DEWEY_CONDA_ENV_NAME}"
unset _DEWEY_CONDA_BASE
unset -f _dewey_fail
return 1
fi
_DEWEY_CONDA_ENV_CREATED=1
fi
if [[ -z "${CONDA_PREFIX:-}" ]] || [[ ! -d "${CONDA_PREFIX}/bin" ]]; then
_dewey_fail "Conda environment '${_DEWEY_CONDA_ENV_NAME}' is not active after activation."
unset _DEWEY_CONDA_BASE
unset -f _dewey_fail
return 1
fi
export PATH="${CONDA_PREFIX}/bin:${PATH}"
if [[ "${_DEWEY_CONDA_ENV_CREATED}" == "1" ]]; then
echo -e " ${_CYAN}→${_NC} Installing editable Dewey checkout..."
if ! "${CONDA_PREFIX}/bin/python" -m pip install -e "$DEWEY_ROOT" -q; then
_dewey_fail "Failed to install editable Dewey checkout from ${DEWEY_ROOT}"
unset _DEWEY_CONDA_BASE
unset -f _dewey_fail
return 1
fi
fi
if command -v rehash >/dev/null 2>&1; then
rehash
elif command -v hash >/dev/null 2>&1; then
hash -r 2>/dev/null || true
fi
echo -e " ${_GREEN}✓${_NC} Dewey environment activated!"
unset _DEWEY_CONDA_BASE
unset _DEWEY_CONDA_ENV_CREATED
unset _DEWEY_CONDA_ENV_NAME
unset _DEWEY_CONDA_ENV_BASE
unset -f _dewey_fail