-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarvain_activate
More file actions
133 lines (118 loc) · 5.06 KB
/
marvain_activate
File metadata and controls
133 lines (118 loc) · 5.06 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
#!/bin/sh
# shellcheck shell=sh
# Source this file from the repo root to add marvain's ./bin to PATH and enable completion.
#
# Usage (from repo root):
# . ./marvain_activate
_marvain_log() {
level="$1"; shift
# minimal color (works on most terminals; safe to ignore)
case "$level" in
INFO) printf '\033[1m[marvain]\033[0m %s\n' "$*";;
WARN) printf '\033[1m[marvain]\033[0m \033[33mWARN\033[0m %s\n' "$*";;
ERROR) printf '\033[1m[marvain]\033[0m \033[31mERROR\033[0m %s\n' "$*";;
*) printf '[marvain] %s\n' "$*";;
esac
}
_MARVAIN_ROOT=$(pwd -P)
if [ ! -x "$_MARVAIN_ROOT/bin/marvain" ]; then
_marvain_log ERROR "Could not find executable: $_MARVAIN_ROOT/bin/marvain"
_marvain_log ERROR "Please cd to the marvain repo root and source again: . ./marvain_activate"
return 1 2>/dev/null || exit 1
fi
case ":${PATH:-}:" in
*":$_MARVAIN_ROOT/bin:"*) :;;
*) PATH="$_MARVAIN_ROOT/bin${PATH:+:$PATH}";;
esac
export PATH
# --- Conda activation (primary env manager) ---
# Escape hatch for exceptional cases (CI/venv debugging).
if [ "${MARVAIN_ALLOW_VENV:-}" = "1" ]; then
_marvain_log WARN "MARVAIN_ALLOW_VENV=1 set; skipping conda activation"
else
_MARVAIN_CONDA_ENV="marvain"
_MARVAIN_CONDA_FILE="$_MARVAIN_ROOT/config/marvain_conda.yaml"
if [ ! -f "$_MARVAIN_CONDA_FILE" ]; then
_marvain_log ERROR "Missing conda env spec: $_MARVAIN_CONDA_FILE"
return 1 2>/dev/null || exit 1
fi
if ! command -v conda >/dev/null 2>&1; then
_marvain_log ERROR "Conda not found on PATH. Install Miniconda/Mambaforge, then create env: conda env create -f $_MARVAIN_CONDA_FILE"
return 1 2>/dev/null || exit 1
fi
# Initialize conda for the current shell (required for `conda activate`).
# shellcheck disable=SC1090
if [ -n "${ZSH_VERSION:-}" ]; then
_MARVAIN_CONDA_HOOK="$(conda shell.zsh hook 2>/dev/null)"
elif [ -n "${BASH_VERSION:-}" ]; then
_MARVAIN_CONDA_HOOK="$(conda shell.bash hook 2>/dev/null)"
else
_MARVAIN_CONDA_HOOK="$(conda shell.posix hook 2>/dev/null)"
fi
if ! eval "$_MARVAIN_CONDA_HOOK"; then
_marvain_log ERROR "Failed to initialize conda shell hook. Try running: conda init (for your shell), then re-source: . ./marvain_activate"
return 1 2>/dev/null || exit 1
fi
unset _MARVAIN_CONDA_HOOK
if ! conda info --envs 2>/dev/null | awk 'NF{print $1}' | sed 's/*//' | grep -Fxq "$_MARVAIN_CONDA_ENV"; then
_marvain_log ERROR "Conda env '$_MARVAIN_CONDA_ENV' not found. Create it with: conda env create -f $_MARVAIN_CONDA_FILE"
return 1 2>/dev/null || exit 1
fi
if [ "${CONDA_DEFAULT_ENV:-}" != "$_MARVAIN_CONDA_ENV" ]; then
_marvain_log INFO "Activating conda env: $_MARVAIN_CONDA_ENV"
if ! conda activate "$_MARVAIN_CONDA_ENV" >/dev/null 2>&1; then
_marvain_log ERROR "Failed to activate conda env '$_MARVAIN_CONDA_ENV'. Try: conda activate $_MARVAIN_CONDA_ENV"
return 1 2>/dev/null || exit 1
fi
else
_marvain_log INFO "Conda env already active: $_MARVAIN_CONDA_ENV"
fi
fi
_MARVAIN_REPO_CFG="$_MARVAIN_ROOT/marvain.yaml"
_MARVAIN_XDG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
# Primary canonical config location
_MARVAIN_USER_CFG="$_MARVAIN_XDG_HOME/marvain/marvain-config.yaml"
# Legacy locations (backward compatibility)
_MARVAIN_USER_CFG_OLD="$_MARVAIN_XDG_HOME/marvain/marvain.yaml"
_MARVAIN_USER_CFG_LEGACY="$HOME/.marvain/config.yaml"
if [ -f "$_MARVAIN_REPO_CFG" ]; then
_marvain_log INFO "Found repo config: $_MARVAIN_REPO_CFG"
elif [ -f "$_MARVAIN_USER_CFG" ]; then
_marvain_log INFO "Found user config: $_MARVAIN_USER_CFG"
elif [ -f "$_MARVAIN_USER_CFG_OLD" ]; then
_marvain_log INFO "Found user config (legacy): $_MARVAIN_USER_CFG_OLD"
_marvain_log WARN "Consider migrating to: $_MARVAIN_USER_CFG"
elif [ -f "$_MARVAIN_USER_CFG_LEGACY" ]; then
_marvain_log INFO "Found user config (legacy): $_MARVAIN_USER_CFG_LEGACY"
_marvain_log WARN "Consider migrating to: $_MARVAIN_USER_CFG"
else
_marvain_log INFO "No config found. Create one via: marvain config init"
fi
# Enable Typer/Click completion when available.
# Note: Only works if CLI uses Typer/Click. Argparse CLIs output help text instead,
# which would cause zsh glob errors if eval'd. We check for valid completion output.
if command -v marvain >/dev/null 2>&1; then
if [ -n "${ZSH_VERSION:-}" ]; then
_MARVAIN_COMP_OUT="$( _MARVAIN_COMPLETE=zsh_source marvain 2>/dev/null )"
# Only eval if it looks like completion code (starts with #compdef or _marvain)
case "$_MARVAIN_COMP_OUT" in
'#compdef'*|'_marvain'*) eval "$_MARVAIN_COMP_OUT" ;;
esac
unset _MARVAIN_COMP_OUT
elif [ -n "${BASH_VERSION:-}" ]; then
_MARVAIN_COMP_OUT="$( _MARVAIN_COMPLETE=bash_source marvain 2>/dev/null )"
# Only eval if it looks like completion code (contains complete -o or _marvain)
case "$_MARVAIN_COMP_OUT" in
*'complete -'*|*'_marvain'*) eval "$_MARVAIN_COMP_OUT" ;;
esac
unset _MARVAIN_COMP_OUT
fi
fi
unset _MARVAIN_ROOT
unset _MARVAIN_CONDA_ENV
unset _MARVAIN_CONDA_FILE
unset _MARVAIN_REPO_CFG
unset _MARVAIN_XDG_HOME
unset _MARVAIN_USER_CFG
unset _MARVAIN_USER_CFG_OLD
unset _MARVAIN_USER_CFG_LEGACY