-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathactivate
More file actions
executable file
·127 lines (106 loc) · 3.61 KB
/
Copy pathactivate
File metadata and controls
executable file
·127 lines (106 loc) · 3.61 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
#!/usr/bin/env bash
# zebra_day activation entrypoint.
#
# Source from the repository root:
# 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
_zday_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
}
_ZDAY_SCRIPT_PATH="$(_zday_script_path)"
ZEBRA_DAY_PROJECT_ROOT="$(cd "$(dirname "${_ZDAY_SCRIPT_PATH}")" && pwd)"
unset -f _zday_script_path
unset _ZDAY_SCRIPT_PATH
_GREEN='\033[0;32m'
_YELLOW='\033[1;33m'
_BLUE='\033[0;34m'
_CYAN='\033[0;36m'
_NC='\033[0m'
_ZDAY_ENV_BASE="ZEBRA_DAY"
_ZDAY_PYTHON=""
_ZDAY_ENV_FILE="${ZEBRA_DAY_PROJECT_ROOT}/environment.yaml"
_zday_sanitize_deployment_code() {
local raw_value="$1"
local sanitized=""
sanitized="$(printf '%s' "$raw_value" | LC_ALL=C sed -E 's/[^A-Za-z0-9-]+/-/g; s/^-+//; s/-+$//')"
if [[ -z "$sanitized" ]]; then
sanitized="local"
fi
printf '%s\n' "$sanitized"
}
_zday_validate_deploy_name() {
local deploy_name="$1"
if [[ ! "$deploy_name" =~ ^[A-Za-z0-9-]{2,24}$ ]]; then
echo -e " ${_YELLOW}✗${_NC} deploy-name must match ^[A-Za-z0-9-]{2,24}$"
return 1
fi
}
_zday_env_exists() {
local env_name="$1"
conda env list 2>/dev/null | awk 'NF && $1 !~ /^#/ {print $1}' | grep -Fxq "$env_name"
}
if [[ "$#" -ne 1 ]]; then
echo -e " ${_YELLOW}✗${_NC} zebra_day activation requires exactly one deploy-name."
echo "Usage: source ./activate <deploy-name>"
return 1
fi
_zday_deployment_code="$1"
if ! _zday_validate_deploy_name "${_zday_deployment_code}"; then
return 1
fi
_zday_deployment_code="$(_zday_sanitize_deployment_code "${_zday_deployment_code}")"
_ZDAY_ENV_NAME="${_ZDAY_ENV_BASE}-${_zday_deployment_code}"
echo -e "${_BLUE}Activating zebra_day environment...${_NC}"
if ! command -v conda >/dev/null 2>&1; then
echo -e " ${_YELLOW}✗${_NC} conda is required for zebra_day activation."
return 1
fi
source "$(conda info --base)/etc/profile.d/conda.sh" 2>/dev/null || true
if _zday_env_exists "${_ZDAY_ENV_NAME}"; then
echo -e " ${_GREEN}✓${_NC} Activating conda environment: ${_ZDAY_ENV_NAME}"
else
echo -e " ${_CYAN}→${_NC} Creating conda environment: ${_ZDAY_ENV_NAME}"
conda env create -n "${_ZDAY_ENV_NAME}" -f "${_ZDAY_ENV_FILE}" || return 1
_ZDAY_CREATED_ENV=1
fi
conda activate "${_ZDAY_ENV_NAME}" || return 1
if [[ "${CONDA_DEFAULT_ENV:-}" != "${_ZDAY_ENV_NAME}" ]]; then
echo -e " ${_YELLOW}✗${_NC} Failed to activate ${_ZDAY_ENV_NAME}"
return 1
fi
if [[ -n "${CONDA_PREFIX:-}" ]] && [[ -d "${CONDA_PREFIX}/bin" ]]; then
export PATH="${CONDA_PREFIX}/bin:$PATH"
_ZDAY_PYTHON="${CONDA_PREFIX}/bin/python"
else
_ZDAY_PYTHON="python"
fi
if [[ ! -x "${_ZDAY_PYTHON}" ]]; then
echo -e " ${_YELLOW}✗${_NC} Missing python interpreter in ${CONDA_PREFIX:-<unset>}"
return 1
fi
if [[ "${_ZDAY_CREATED_ENV:-0}" == "1" ]]; then
echo -e " ${_CYAN}→${_NC} Installing zebra_day editable checkout"
"${_ZDAY_PYTHON}" -m pip install -e "${ZEBRA_DAY_PROJECT_ROOT}" -q || return 1
fi
hash -r 2>/dev/null || true
echo -e " ${_GREEN}✓${_NC} Deployment env: ${_ZDAY_ENV_NAME}"
echo -e " ${_GREEN}✓${_NC} CONDA_DEFAULT_ENV=${CONDA_DEFAULT_ENV}"
echo
echo " Next:"
echo " zday config init"
echo " zday config status"
echo " zday gui start"
unset _zday_deployment_code
unset _ZDAY_ENV_NAME
unset _ZDAY_PYTHON
unset _ZDAY_CREATED_ENV