Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
venv/
.venv/
__pycache__/
.ci_cache
.env
.clang-format
Expand Down
1 change: 1 addition & 0 deletions tools/install_ubuntu_dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function install_ubuntu_common_requirements() {
locales \
git \
git-lfs \
fswatch \
xvfb

# TODO: vendor the rest of these in third_party/
Expand Down
1 change: 1 addition & 0 deletions tools/mac_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ brew "zeromq"
cask "gcc-arm-embedded"
brew "portaudio"
brew "gcc@13"
brew "fswatch"
EOS

echo "[ ] finished brew install t=$SECONDS"
Expand Down
28 changes: 12 additions & 16 deletions tools/op.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ RC_FILE="${HOME}/.$(basename ${SHELL})rc"
if [ "$(uname)" == "Darwin" ] && [ $SHELL == "/bin/bash" ]; then
RC_FILE="$HOME/.bash_profile"
fi

OP_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
source "$OP_DIR/scripts/lib/common.sh"

function op_install() {
echo "Installing op system-wide..."
CMD="\nalias op='"$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )/op.sh" \"\$@\"'\n"
Expand Down Expand Up @@ -52,22 +56,7 @@ function op_run_command() {
# be default, assume openpilot dir is in current directory
OPENPILOT_ROOT=$(pwd)
function op_get_openpilot_dir() {
# First try traversing up the directory tree
while [[ "$OPENPILOT_ROOT" != '/' ]];
do
if find "$OPENPILOT_ROOT/launch_openpilot.sh" -maxdepth 1 -mindepth 1 &> /dev/null; then
return 0
fi
OPENPILOT_ROOT="$(readlink -f "$OPENPILOT_ROOT/"..)"
done

# Fallback to hardcoded directories if not found
for dir in "$HOME/openpilot" "/data/openpilot"; do
if [[ -f "$dir/launch_openpilot.sh" ]]; then
OPENPILOT_ROOT="$dir"
return 0
fi
done
OPENPILOT_ROOT="$(resolve_root_dir)"
}

function op_install_post_commit() {
Expand Down Expand Up @@ -352,6 +341,11 @@ function op_clip() {
op_run_command tools/clip/run.py $@
}

function op_hot() {
op_before_cmd
op_run_command tools/scripts/hot.sh $@
}

function op_switch() {
REMOTE="origin"
if [ "$#" -gt 1 ]; then
Expand Down Expand Up @@ -419,6 +413,7 @@ function op_default() {
echo -e " ${BOLD}replay${NC} Run Replay"
echo -e " ${BOLD}cabana${NC} Run Cabana"
echo -e " ${BOLD}clip${NC} Run clip (linux only)"
echo -e " ${BOLD}hot${NC} Hot reload a command when files in the openpilot directory change"
echo -e " ${BOLD}adb${NC} Run adb shell"
echo -e " ${BOLD}ssh${NC} comma prime SSH helper"
echo ""
Expand Down Expand Up @@ -472,6 +467,7 @@ function _op() {
test ) shift 1; op_test "$@" ;;
replay ) shift 1; op_replay "$@" ;;
clip ) shift 1; op_clip "$@" ;;
hot ) shift 1; op_hot "$@" ;;
sim ) shift 1; op_sim "$@" ;;
install ) shift 1; op_install "$@" ;;
switch ) shift 1; op_switch "$@" ;;
Expand Down
65 changes: 65 additions & 0 deletions tools/scripts/hot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env bash

# usage: hot.sh -- COMMAND [ARGS...]

set -Eeuo pipefail

TOOLS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
source "$TOOLS_DIR/lib/common.sh"

command_exists() { command -v "$1" >/dev/null 2>&1; }

if ! command_exists fswatch; then
echo "error: fswatch not found. did you run setup?" >&2
exit 1
fi

CMD=( )
if [[ $# -eq 0 || "$1" != "--" ]]; then
echo "usage: $(basename "$0") -- COMMAND [ARGS...]" >&2
exit 2
fi
shift # drop --
if [[ $# -eq 0 ]]; then
echo "error: missing command to run" >&2
exit 2
fi
CMD=("$@")

ROOT_DIR="$(resolve_root_dir)"

echo "watching: $ROOT_DIR" >&2
echo "command: ${CMD[*]}" >&2

child_pid=""
cleanup() {
if [[ -n "${child_pid:-}" ]] && kill -0 "$child_pid" 2>/dev/null; then
kill -TERM -"$child_pid" 2>/dev/null || true
wait "$child_pid" 2>/dev/null || true
fi
}

trap cleanup INT TERM EXIT

start_child() {
# start in its own process group so we can kill descendants
setsid "${CMD[@]}" &
child_pid=$!
}

exclude_flags=""
if [[ -f "$ROOT_DIR/.gitignore" ]]; then
exclude_flags=$(cd "$ROOT_DIR" && grep -v '^[[:space:]]*#' .gitignore | grep -v '^[[:space:]]*$' | grep -v '^!' | sed 's|/$||' | sed 's|^|-e |')
fi

while true; do
start_child

echo "watching: $ROOT_DIR" >&2
fswatch -1 --latency=1 --event Updated --event Removed --event Renamed -x -Lr openpilot/ | while read event; do echo "$event"; done

if kill -0 "$child_pid" 2>/dev/null; then
kill -TERM -"$child_pid" 2>/dev/null || true
wait "$child_pid" 2>/dev/null || true
fi
done
25 changes: 25 additions & 0 deletions tools/scripts/lib/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash

resolve_root_dir() {
local root_dir
root_dir="$(pwd -P)"

# First try traversing up the directory tree
while [[ "$root_dir" != "/" ]]; do
if [[ -f "$root_dir/launch_openpilot.sh" ]]; then
echo "$root_dir"
return 0
fi
root_dir="$(readlink -f "$root_dir/..")"
done

# Fallback to hardcoded directories if not found
for dir in "$HOME/openpilot" "/data/openpilot"; do
if [[ -f "$dir/launch_openpilot.sh" ]]; then
echo "$dir"
return 0
fi
done

pwd -P
}