-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.envrc
88 lines (72 loc) · 2.49 KB
/
.envrc
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
# This is the .envrc for openthot, for use with direnv (https://direnv.net/)
# It's responsible for enforcing a standard dev environment by checking as much state as possible, and either performing
# initialization (e.g. activating the venv) or giving recommendations on how to reach the desired state.
# It also sets useful environment variables.
# If you'd like to override or set any custom environment variables, this .envrc will read a .env file at the end.
set -e
# we like some colors
bold="$(tput bold)"
red="$(tput setaf 1)"
green="$(tput setaf 2)"
orange="$(tput setaf 3)"
reset="$(tput sgr0)"
info () {
cat <<EOF
${bold}direnv: ${1}${reset}
EOF
}
warning () {
cat <<EOF
${orange}${bold}direnv: ${1}${reset}
EOF
}
die () {
>&2 cat <<EOF
${red}${bold}direnv FATAL: ${1}
${reset}
EOF
return 1
}
layout_poetry() {
local VENV=$( poetry show -v|grep "Using virtualenv:"|cut -f 3 -d " " 2>/dev/null)
export VIRTUAL_ENV=$VENV
PATH_add "$VIRTUAL_ENV/bin"
}
export PYTHONDONTWRITEBYTECODE=1
export PIP_DISABLE_PIP_VERSION_CHECK=on
info "Checking virtualenv..."
# direnv set -u's, so we need to do this.
VIRTUAL_ENV="${VIRTUAL_ENV:-}"
if [ -n "$VIRTUAL_ENV" ]; then
# we're enforcing that virtualenv be in .venv
if [ "$VIRTUAL_ENV" != "${PWD}/.venv" ]; then
info "You're in a virtualenv, but it's not in the expected location (${PWD}/.venv)"
layout_poetry
fi
else
if [ ! -f ".venv/bin/activate" ]; then
info "You don't seem to have a virtualenv. Creating it for you..."
python -m venv .venv
layout_poetry
fi
fi
info "Activating $(python -V) virtualenv"
source .venv/bin/activate
# ideally, direnv is able to export PS1 as modified by sourcing venvs
# but we'd have to patch direnv, and ".venv" isn't descriptive anyways
unset PS1
[ "$(command -v python)" != "${PWD}/.venv/bin/python" ] && die "Failed to activate virtualenv."
# check that openthot is installed
if [ "$(command -v openthot)" != "${PWD}/.venv/bin/openthot" ]; then
info "Your .venv is activated, but «openthot» CLI doesn't seem to be installed. Let's install it with dependencies."
poetry install --only main,dev,cli --no-cache
warning "Now you can:"
warning " 1. run \`cp .env.example .env\` and edit .env to set ASR__ENGINE"
warning " 2. run \`poetry install --only \${ASR__ENGINE} --no-cache\`"
warning " 3. run \`openthot standalone\`"
fi
# read .env file if found
if [ -f '.env' ]; then
info ".env found. Reading it..."
dotenv .env
fi