-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinstall
executable file
·239 lines (210 loc) · 6.15 KB
/
install
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env bash
# If the 'CI' environment variable is set, this runs
# like it would on the CI. To try and test that locally,
# can do: CI_SKIP_INSTALL=1 CI=1 ./install
set -o pipefail
ci() {
[[ -n "${CI}" ]]
}
ci && set -x
# script to setup HPI
# - installs karlicoss/HPI as an editable namespace package,
# - installs this repo
# - installs additional python packages for modules
# - checks for any required external commands
# cd to base directory
BASE_DIR="$(dirname "${BASH_SOURCE[0]}")"
cd "${BASE_DIR}" || exit 1
printf 'In: %s\n' "$(pwd)"
# function to verify an external command is installed
havecmd() {
local BINARY ERRMSG script_name
script_name='HPI'
# error if first argument isn't provided
BINARY="${1:?Must provide command to check}"
# the command exists, exit with 0 (success!)
if command -v "${BINARY}" >/dev/null 2>&1; then
return 0
else
# construct error message
ERRMSG="'${script_name}' requires '${BINARY}', could not find that on your \$PATH"
if [[ -n "$2" ]]; then
ERRMSG="$ERRMSG. $2"
fi
printf '%s\n' "$ERRMSG" 1>&2
return 1
fi
} && export -f havecmd
maybe_boxes() {
# Print a fancy box, if boxes is installed
# http://boxes.thomasjensen.com/
if havecmd boxes >/dev/null 2>&1; then
boxes -dshell -pv1h2
else
cat
fi
} && export -f maybe_boxes
prompt_if_fails() {
ci && return 1
printf "Command failed... Hit Enter to continue, Ctrl+C to exit"
read -r
} && export -f prompt_if_fails
pipm() {
python3 -m pip "$@"
} && export -f pipm
pip_install() {
local -a ARGS=()
# only use --user when not in a virtual environment
[[ -z "$VIRTUAL_ENV" ]] && ARGS+=("--user")
ARGS+=("$@")
pipm install "${ARGS[@]}"
} && export -f pip_install
# install dependencies (with pip) for this module
# this works by traversing the AST/looking for
# a 'REQUIRES' global variable in the
# corresponding file
hpi_module() {
local -a ARGS=()
[[ -z "$VIRTUAL_ENV" ]] && ARGS+=("--user")
ARGS+=("$@")
python3 -m my.core module install "${ARGS[@]}"
} && export -f hpi_module
ci_install_deps() {
if [[ -n "${CI}" && -z "${CI_SKIP_INSTALL}" ]]; then
# install OS specific stuff here
if [[ "$OSTYPE" == "darwin"* ]]; then
# macos ci
brew install coreutils || return $?
brew install boxes || return $?
else
# ubuntu ci
sudo apt update || return $?
sudo apt install boxes || return $?
fi
pip_install -U pip wheel setuptools || return $?
fi
}
install_dependencies() {
echo 'Installing dependencies...' | maybe_boxes
if ci; then
ci_install_deps || return $?
fi
}
required_commands() {
echo 'Checking if required commands are installed...' | maybe_boxes
havecmd realpath || return $?
havecmd git || return $?
}
ci_realpath() {
if ci; then
if [[ "$OSTYPE" == "darwin"* ]]; then
# need to use g-prefixed things on CI
grealpath "$@"
return $?
fi
fi
realpath "$@"
}
setup_fork() {
local FORK_ABBREV UPSTREAM_URL UPSTREAM_DIR
echo 'Setting up upstream fork...' | maybe_boxes
FORK_ABBREV="${HPI_UPSTREAM_FOLDER_NAME:-HPI-karlicoss}"
UPSTREAM_URL='https://github.com/karlicoss/HPI'
UPSTREAM_DIR="$(ci_realpath "../${FORK_ABBREV}")"
# clone my fork one level up from here if it does not exist
if [[ ! -e "${UPSTREAM_DIR}" ]]; then
git clone "${UPSTREAM_URL}" "${UPSTREAM_DIR}"
else
echo "Path already exists, skipping clone..."
fi
# install upstream/core HPI
cd "${UPSTREAM_DIR}" || return $?
if ci; then
pip_install -e '.[optional,testing]' || return $?
else
pip_install -e '.' || return $?
fi
# cd back to here, to install this
cd "${BASE_DIR}" || return $?
pip_install -e '.' || return $?
}
module_dependencies() {
if ! ci; then
printf 'Install all module dependencies? [y/N] '
read -r || return $?
case "${REPLY}" in
y | Y) ;;
*)
return 0
;;
esac
fi
echo "Installing module dependencies" | maybe_boxes
hpi_module my.activitywatch.active_window || return $?
hpi_module my.discord.data_export || return $?
hpi_module my.todotxt.git_history || return $?
hpi_module my.ip.all || return $?
hpi_module my.linkedin.privacy_export || return $?
hpi_module my.chess.export || return $?
hpi_module my.mpv.history_daemon || return $?
hpi_module my.league.export || return $?
hpi_module my.scramble.history || return $?
hpi_module my.trakt.export || return $?
hpi_module my.mail.all || return $?
hpi_module my.piazza.scraper || return $?
hpi_module my.apple.privacy_export || return $?
hpi_module my.grouvee.export || return $?
hpi_module my.offline.listens || return $?
hpi_module my.mal.export || return $?
hpi_module my.listenbrainz.export || return $?
hpi_module my.skype.gdpr || return $?
}
ci_config() {
CONF="$(ci_realpath "${BASE_DIR}/tests/my")" || return $?
MY_CONFIG="${CONF}" python3 -m my.core config check 1>&2 || return $?
echo "${CONF}"
}
ci_tests() {
echo 'Running tests' | maybe_boxes
ci && unset HPI_TESTS_PURA
python3 -m pytest ./tests || return $?
}
ci_mypy() {
echo 'Checking types' | maybe_boxes
pip_install 'git+https://github.com/python/mypy'
pip_install types-urllib3 types-simplejson types-setuptools types-PyYAML types-pytz types-dateparser types-requests lxml-stubs
local mypy_args=(-p tests --txt-report .coverage.mypy)
while read -r pkg; do
mypy_args+=(-p "${pkg}")
done < <(find my -name '*.py' | grep -v '__' | sed -e 's|\.\/||' -e 's|/|.|g' -e 's/\.py$//g' | sort)
python3 -m mypy "${mypy_args[@]}" || return $?
}
ci_lint() {
pip_install flake8 || return $?
echo 'Linting...' | maybe_boxes
python3 -m flake8 ./my || return $?
}
ci_run() {
ci || return 0
CONF="$(ci_config)" || return $?
MY_CONFIG="${CONF}" ci_tests || return $?
MY_CONFIG="${CONF}" ci_mypy || return $?
MY_CONFIG="${CONF}" ci_lint || return $?
}
main() {
install_dependencies || prompt_if_fails || return $?
required_commands || prompt_if_fails || return $?
# use realpath to make sure BASE_DIR is set properly
BASE_DIR="$(ci_realpath "${BASE_DIR}")" || return $?
(setup_fork) || prompt_if_fails || return $?
module_dependencies || prompt_if_fails || return $?
ci_run || return $?
}
# if user isn't running this directly
# source the exported functions into the current bash environment
if [[ "${BASH_SOURCE[0]}" != "$0" ]]; then
:
else
# otherwise, run main as usual
main "$@" || exit $?
fi