This repository was archived by the owner on Feb 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun
More file actions
executable file
·433 lines (361 loc) · 11.7 KB
/
run
File metadata and controls
executable file
·433 lines (361 loc) · 11.7 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#!/usr/bin/env bash
#
# usage: ./run.sh command [argument ...]
#
# Commands used during development / CI.
# Also, executable documentation for project dev practices.
#
# See https://death.andgravity.com/run-sh
# for an explanation of how it works and why it's useful.
################################################################################
# Core Development Commands
function install {
#@ Install project dependencies and create virtual environment
#@ Category: Core Development
uv sync
uv lock
}
function sync {
#@ Install dependencies from lock file
#@ Category: Core Development
uv sync
}
function lint {
#@ Run linting (ruff check/format + mypy type checking)
#@ Category: Core Development
uv run ruff check src
uv run ruff format --check src
mypy # Call mypy function
}
function format {
#@ Auto-format code with ruff
#@ Category: Core Development
uv run ruff check --fix src
uv run ruff format src
}
function mypy {
#@ Run type checking only
#@ Category: Core Development
uv run mypy --config-file pyproject.toml src
}
function test {
#@ Run all tests with pytest
#@ Category: Core Development
if [ $# -eq 0 ]; then
# If no arguments provided, run tests only in tests/ directory to avoid config conflicts
uv run pytest tests/
else
# If arguments provided, pass them through
uv run pytest "$@"
fi
}
function clean {
#@ Remove temporary files and caches
#@ Category: Core Development
pycache-remove
dsstore-remove
mypycache-remove
ipynbcheckpoints-remove
pytestcache-remove
build-remove
}
function version {
#@ Show current project version
#@ Category: Core Development
cat VERSION
}
function bumpversion {
#@ Bump version using bumpversion tool
#@ Usage: bumpversion [patch|minor|major] or bumpversion --new-version X.Y.Z patch
#@ Category: Core Development
command bumpversion "$@"
}
################################################################################
# Package Management Commands
function build-deb {
#@ Build Debian package (native)
#@ Category: Package Management
echo "🏗️ Building Debian package..."
# Set up environment
dev-env
export DEBEMAIL="info@hatlabs.fi"
export DEBFULLNAME="Hat Labs CI"
export PACKAGE_VERSION=$(release-version)
DEB_VERSION=$(echo "$PACKAGE_VERSION" | sed 's/-\([a-zA-Z]\)/~\1/g')
# Create a new changelog entry
dch --newversion "$DEB_VERSION" \
--distribution stable \
--force-distribution \
"Automated release $PACKAGE_VERSION. See GitHub for details."
dpkg-buildpackage -us -uc -b
echo "✅ Debian package built successfully"
}
function docker-build-deb {
#@ Build Debian package using Docker container
#@ Category: Package Management
echo "🐳 Building Debian package using Docker..."
# Use Docker container for Debian packaging
echo "🐳 Running Debian packaging in Docker container..."
export DIR_NAME=$(basename "$PWD")
debtools ./run build-deb
echo "✅ Docker-based Debian package built successfully"
}
function build-docker-tools {
#@ Build Docker tools image
#@ Category: Package Management
export DIR_NAME=$(basename "$PWD")
docker compose -f docker/docker-compose.debtools.yml build debtools "$@"
}
################################################################################
# Testing/CI Commands
function ci-check {
#@ Run CI verification checks (lint + test)
#@ Category: Testing/CI
echo "🔍 Running CI verification checks..."
lint
test
echo "✅ CI checks passed"
}
function ci-build {
#@ Full CI build pipeline (lint + test + package)
#@ Category: Testing/CI
echo "🚀 Running full CI build pipeline..."
clean
ci-check
docker-build-deb
echo "✅ CI build pipeline completed successfully"
}
################################################################################
# Development Utilities Commands
function update-dev-deps {
#@ Update development dependencies to latest versions
#@ Category: Development Utilities
uv add --dev ruff mypy mypy-extensions
}
function update-bindings {
#@ Update I2C bindings from firmware repo
#@ Category: Development Utilities
rm -rf src/halpi2_fw_i2c_postcard
pushd ../HALPI2-firmware || { echo "Error: ../HALPI2-firmware not found"; exit 1; }
./run generate-bindings
popd
cp -a ../HALPI2-firmware/halpi2-fw-i2c-postcard/src/halpi2_fw_i2c_postcard src/
}
function push {
#@ Push code to remote development server
#@ Category: Development Utilities
rsync -avP --delete --exclude-from='.gitignore' \
--exclude='.venv' . halpi2:src/halpid/
}
function pull {
#@ Pull code from remote development server
#@ Category: Development Utilities
rsync -avP --delete --exclude-from='.gitignore' \
--exclude='.venv' halpi2:src/halpid/ .
}
function dev-env {
#@ Show/check development environment
#@ Category: Development Utilities
_env
}
function debtools {
#@ Run a command inside the debtools Docker container
#@ Usage: debtools <command> [args]
#@ Category: Development Utilities
export DIR_NAME=$(basename "$PWD")
docker compose -f docker/docker-compose.debtools.yml run --rm debtools "$@"
}
function bumpversion-dry-run {
#@ Preview version change without applying
#@ Usage: bumpversion-dry-run <version>
#@ Category: Development Utilities
local new_version="${1:-}"
if [ -z "$new_version" ]; then
echo "Error: Version required. Usage: bumpversion-dry-run <new_version>"
echo "Current version: $(version)"
echo "Example: ./run bumpversion-dry-run 3.2.0"
exit 1
fi
echo "Current version: $(version)"
echo "Preview of changes for version $new_version:"
bump2version --new-version "$new_version" patch --dry-run --verbose
}
################################################################################
# Docker Commands
function build-docker {
#@ Build the Docker image
#@ Category: Docker
echo Building docker ${DOCKER_IMAGE}:${DOCKER_IMAGE_VERSION} ...
docker build -t ${DOCKER_IMAGE}:${DOCKER_IMAGE_VERSION} . \
-f ./docker/Dockerfile --no-cache
}
function remove-docker {
#@ Remove the Docker image
#@ Category: Docker
echo Removing docker ${DOCKER_IMAGE}:${DOCKER_IMAGE_VERSION} ...
docker rmi -f ${DOCKER_IMAGE}:${DOCKER_IMAGE_VERSION}
}
################################################################################
# Setup/Installation Commands
function install-uv {
#@ Download and install the latest version of uv
#@ Category: Setup/Installation
curl -LsSf https://astral.sh/uv/install.sh | sh
}
################################################################################
# Internal cleanup functions (not shown in main help)
function pycache-remove {
find . -type d -name __pycache__ -exec rm -r {} + 2>/dev/null || true
}
function dsstore-remove {
find . -type f -name .DS_Store -exec rm {} + 2>/dev/null || true
}
function mypycache-remove {
find . -type d -name .mypy_cache -exec rm -r {} + 2>/dev/null || true
}
function ipynbcheckpoints-remove {
find . -type d -name .ipynb_checkpoints -exec rm -r {} + 2>/dev/null || true
}
function pytestcache-remove {
find . -type d -name .pytest_cache -exec rm -r {} + 2>/dev/null || true
}
function build-remove {
rm -rf build/ 2>/dev/null || true
}
################################################################################
# Project-specific commands end.
# First, set up the environment.
# (Check the notes at the end when changing this.)
set -o nounset
set -o pipefail
set -o errexit
# Enable this to echo commands as they are executed.
#set -o xtrace
# Change the current directory to the project root.
PROJECT_ROOT=${0%/*}
if [[ $0 != $PROJECT_ROOT && $PROJECT_ROOT != "" ]]; then
cd "$PROJECT_ROOT"
fi
readonly PROJECT_ROOT=$(pwd)
# Some useful variables.
DOCKER_IMAGE="halpid"
DOCKER_IMAGE_VERSION="latest"
# Store the absolute path to this script (useful for recursion).
readonly SCRIPT="$PROJECT_ROOT/$(basename "$0")"
################################################################################
# Meta-commands and utilities follow.
function help {
#@ Show help for all available commands (auto-generated)
#@ Category: Meta
printf "%s <command> [args]\n\n" "${0}"
# Extract help information from function comments
# This automatically generates help from #@ comments in functions
local -A categories
local -A descriptions
local -A usages
# Parse all functions and their help comments
while IFS= read -r line; do
if [[ "$line" =~ ^function[[:space:]]+([^[:space:]{}]+) ]]; then
current_func="${BASH_REMATCH[1]}"
elif [[ "$line" =~ ^[[:space:]]*#@[[:space:]]*Category:[[:space:]]*(.+) ]]; then
categories["$current_func"]="${BASH_REMATCH[1]}"
elif [[ "$line" =~ ^[[:space:]]*#@[[:space:]]*Usage:[[:space:]]*(.+) ]]; then
usages["$current_func"]="${BASH_REMATCH[1]}"
elif [[ "$line" =~ ^[[:space:]]*#@[[:space:]]*(.+) ]] && [[ -z "${descriptions[$current_func]:-}" ]]; then
descriptions["$current_func"]="${BASH_REMATCH[1]}"
fi
done < "$0"
# Group functions by category and display
local -A category_funcs
for func in "${!categories[@]}"; do
local cat="${categories[$func]}"
category_funcs["$cat"]+="$func "
done
# Display in order of preference
local ordered_categories=("Core Development" "Testing/CI" "Package Management" "Development Utilities" "Docker" "Setup/Installation" "Meta")
for category in "${ordered_categories[@]}"; do
if [[ -n "${category_funcs[$category]:-}" ]]; then
echo "${category} Commands:"
for func in ${category_funcs[$category]}; do
local usage="${usages[$func]:-$func}"
local desc="${descriptions[$func]:-No description}"
printf " %-30s %s\n" "$usage" "$desc"
done
echo ""
fi
done
# Handle uncategorized functions
for func in "${!descriptions[@]}"; do
if [[ -z "${categories[$func]:-}" ]]; then
local usage="${usages[$func]:-$func}"
local desc="${descriptions[$func]}"
printf " %-30s %s\n" "$usage" "$desc"
fi
done
}
once_hash_array=()
function _once {
# Run a command only once during the execution of this script, even if it's
# called multiple times.
#
# Usage:
# _once <command> [argument ...]
#
# Example:
# _once echo "Hello"
# _once echo "Hello" # won't be executed
local command="$*"
local hash=$(echo "$command" | shasum | cut -d' ' -f1)
if [[ ! " ${once_hash_array[@]} " =~ " ${hash} " ]]; then
once_hash_array+=("$hash")
eval "$command"
fi
}
compose_flags=""
function _dc {
docker compose $compose_flags "$@"
}
function _env {
echo "🗝️ Setting environment from .env and .env.defaults"
# Go through the files and export all variables not already present in
# the environment. First file has precedence!
if [ -f .env ]; then
_export_unset .env
else
# Make sure a .env file exists, otherwise docker-compose will complain
cp .env.defaults .env
fi
if [ -f .env.defaults ]; then
_export_unset .env.defaults
fi
}
function _export_unset {
local file="$1"
# Need to use a temp file to avoid a subshell
local tmpfile=$(mktemp)
grep -v '^#' $file >$tmpfile
while read -r line; do
if [[ ! "$line" =~ ^[[:space:]]*$ ]]; then
varname=$(echo "$line" | cut -d= -f1)
if [[ -z "${!varname:-}" ]]; then
eval $line
export $varname
fi
fi
done <$tmpfile
rm $tmpfile
}
# Dispatch to command. A simpler version would be just "$@" (with the quotes!).
TIMEFORMAT=$'\nTask completed in %3lR'
time "${@:-help}"
# Some dev notes for this script.
#
# The commands *require*:
#
# * The current working directory is the project root.
# * The shell options and globals are set as they are.
#
# Inspired by the following:
# - https://death.andgravity.com/run-sh
# - http://www.oilshell.org/blog/2020/02/good-parts-sketch.html
# - https://www.youtube.com/watch?v=SdmYd5hJISM&t=7s