2024-07-25 13:20:16 +08:00
#!/usr/bin/env bash
2024-07-24 12:47:07 +08:00
2024-08-02 04:44:57 +08:00
if [ [ ! " ${ BASH_SOURCE [0] } " = " ${ 0 } " ] ] ; then
echo "Invalid invocation! This script must not be sourced."
echo "Run 'op.sh' directly or check your .bashrc for a valid alias"
return 0
fi
2024-08-01 10:37:06 +08:00
set -e
2024-07-24 12:47:07 +08:00
RED = '\033[0;31m'
GREEN = '\033[0;32m'
2024-07-25 05:49:35 +08:00
UNDERLINE = '\033[4m'
BOLD = '\033[1m'
2024-07-24 12:47:07 +08:00
NC = '\033[0m'
2024-08-09 07:32:02 +08:00
SHELL_NAME = " $( basename ${ SHELL } ) "
2024-08-01 10:37:06 +08:00
RC_FILE = " ${ HOME } /. $( basename ${ SHELL } ) rc "
if [ " $( uname) " = = "Darwin" ] && [ $SHELL = = "/bin/bash" ] ; then
RC_FILE = " $HOME /.bash_profile "
fi
2024-07-26 07:57:43 +08:00
function op_install( ) {
2024-07-24 12:47:07 +08:00
echo "Installing op system-wide..."
2024-08-01 10:37:06 +08:00
CMD = "\nalias op='" $( cd " $( dirname " ${ BASH_SOURCE [0] } " ) " >/dev/null && pwd ) /op.sh" \"\$@\"'\n"
2024-07-27 07:10:00 +08:00
grep "alias op=" " $RC_FILE " & > /dev/null || printf " $CMD " >> $RC_FILE
2024-08-14 06:15:19 +08:00
echo -e " ↳ [ ${ GREEN } ✔ ${ NC } ] op installed successfully. Open a new shell to use it. "
2024-07-24 12:47:07 +08:00
}
2024-08-06 05:07:57 +08:00
function loge( ) {
if [ [ -f " $LOG_FILE " ] ] ; then
# error type
echo " $1 " >> $LOG_FILE
# error log
echo " $2 " >> $LOG_FILE
fi
}
2024-07-25 13:20:16 +08:00
function op_run_command( ) {
CMD = " $@ "
2024-08-14 06:15:19 +08:00
echo -e " ${ BOLD } Running command → ${ NC } $CMD │ "
for ( ( i = 0; i<$(( 19 + ${# CMD } )) ; i++) ) ; do
echo -n "─"
done
echo -e "┘\n"
2024-07-25 13:20:16 +08:00
if [ [ -z " $DRY " ] ] ; then
2024-07-25 14:11:23 +08:00
eval " $CMD "
2024-07-25 13:20:16 +08:00
fi
}
2024-07-24 12:47:07 +08:00
# be default, assume openpilot dir is in current directory
OPENPILOT_ROOT = $( pwd )
2024-07-26 07:57:43 +08:00
function op_get_openpilot_dir( ) {
2024-07-25 05:49:35 +08:00
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
2024-07-26 07:57:43 +08:00
}
2024-08-24 06:19:21 +08:00
function op_install_post_commit( ) {
op_get_openpilot_dir
if [ [ ! -d $OPENPILOT_ROOT /.git/hooks/post-commit.d ] ] ; then
mkdir $OPENPILOT_ROOT /.git/hooks/post-commit.d
mv $OPENPILOT_ROOT /.git/hooks/post-commit $OPENPILOT_ROOT /.git/hooks/post-commit.d 2>/dev/null || true
fi
cd $OPENPILOT_ROOT /.git/hooks
ln -sf ../../scripts/post-commit post-commit
}
2024-07-26 07:57:43 +08:00
function op_check_openpilot_dir( ) {
echo "Checking for openpilot directory..."
if [ [ -f " $OPENPILOT_ROOT /launch_openpilot.sh " ] ] ; then
2024-08-14 06:15:19 +08:00
echo -e " ↳ [ ${ GREEN } ✔ ${ NC } ] openpilot found. "
2024-07-26 07:57:43 +08:00
return 0
fi
2024-07-24 12:47:07 +08:00
2024-07-25 13:20:16 +08:00
echo -e " ↳ [ ${ RED } ✗ ${ NC } ] openpilot directory not found! Make sure that you are "
echo " inside the openpilot directory or specify one with the"
echo " --dir option!"
2024-07-25 05:49:35 +08:00
return 1
}
2024-07-24 12:47:07 +08:00
function op_check_git( ) {
echo "Checking for git..."
if ! command -v "git" > /dev/null 2>& 1; then
2024-07-26 07:57:43 +08:00
echo -e " ↳ [ ${ RED } ✗ ${ NC } ] git not found on your system! "
2024-07-24 12:47:07 +08:00
return 1
else
2024-07-26 07:57:43 +08:00
echo -e " ↳ [ ${ GREEN } ✔ ${ NC } ] git found. "
2024-07-24 12:47:07 +08:00
fi
echo "Checking for git lfs files..."
2024-07-26 07:57:43 +08:00
if [ [ $( file -b $OPENPILOT_ROOT /selfdrive/modeld/models/supercombo.onnx) = = "data" ] ] ; then
echo -e " ↳ [ ${ GREEN } ✔ ${ NC } ] git lfs files found. "
2024-07-24 12:47:07 +08:00
else
2024-07-26 07:57:43 +08:00
echo -e " ↳ [ ${ RED } ✗ ${ NC } ] git lfs files not found! Run 'git lfs pull' "
return 1
2024-07-24 12:47:07 +08:00
fi
echo "Checking for git submodules..."
2024-07-27 06:28:13 +08:00
for name in $( git config --file .gitmodules --get-regexp path | awk '{ print $2 }' | tr '\n' ' ' ) ; do
2024-07-25 13:20:16 +08:00
if [ [ -z $( ls $OPENPILOT_ROOT /$name ) ] ] ; then
2024-07-26 07:57:43 +08:00
echo -e " ↳ [ ${ RED } ✗ ${ NC } ] git submodule $name not found! Run 'git submodule update --init --recursive' "
2024-07-25 13:20:16 +08:00
return 1
fi
done
2024-07-26 07:57:43 +08:00
echo -e " ↳ [ ${ GREEN } ✔ ${ NC } ] git submodules found. "
2024-07-24 12:47:07 +08:00
}
function op_check_os( ) {
echo "Checking for compatible os version..."
if [ [ " $OSTYPE " = = "linux-gnu" * ] ] ; then
if [ -f "/etc/os-release" ] ; then
source /etc/os-release
case " $VERSION_CODENAME " in
"jammy" | "kinetic" | "noble" | "focal" )
2024-07-26 07:57:43 +08:00
echo -e " ↳ [ ${ GREEN } ✔ ${ NC } ] Ubuntu $VERSION_CODENAME detected. "
2024-07-24 12:47:07 +08:00
; ;
* )
2024-07-26 07:57:43 +08:00
echo -e " ↳ [ ${ RED } ✗ ${ NC } ] Incompatible Ubuntu version $VERSION_CODENAME detected! "
2024-08-06 05:07:57 +08:00
loge "ERROR_INCOMPATIBLE_UBUNTU" " $VERSION_CODENAME "
2024-07-24 12:47:07 +08:00
return 1
; ;
esac
else
2024-07-26 07:57:43 +08:00
echo -e " ↳ [ ${ RED } ✗ ${ NC } ] No /etc/os-release on your system. Make sure you're running on Ubuntu, or similar! "
2024-08-06 05:07:57 +08:00
loge "ERROR_UNKNOWN_UBUNTU"
2024-07-24 12:47:07 +08:00
return 1
fi
elif [ [ " $OSTYPE " = = "darwin" * ] ] ; then
2024-08-14 06:15:19 +08:00
echo -e " ↳ [ ${ GREEN } ✔ ${ NC } ] macOS detected. "
2024-07-24 12:47:07 +08:00
else
2024-07-26 07:57:43 +08:00
echo -e " ↳ [ ${ RED } ✗ ${ NC } ] OS type $OSTYPE not supported! "
2024-08-06 05:07:57 +08:00
loge "ERROR_UNKNOWN_OS" " $OSTYPE "
2024-07-24 12:47:07 +08:00
return 1
fi
}
function op_check_python( ) {
echo "Checking for compatible python version..."
2024-07-25 13:20:16 +08:00
REQUIRED_PYTHON_VERSION = $( grep "requires-python" $OPENPILOT_ROOT /pyproject.toml)
INSTALLED_PYTHON_VERSION = $( python3 --version 2> /dev/null || true )
if [ [ -z $INSTALLED_PYTHON_VERSION ] ] ; then
2024-07-26 07:57:43 +08:00
echo -e " ↳ [ ${ RED } ✗ ${ NC } ] python3 not found on your system. You need python version at least $( echo $REQUIRED_PYTHON_VERSION | tr -d -c '[0-9.]' ) to continue! "
2024-08-06 05:07:57 +08:00
loge "ERROR_PYTHON_NOT_FOUND"
2024-07-24 12:47:07 +08:00
return 1
2024-08-01 12:40:48 +08:00
elif [ [ $( echo $INSTALLED_PYTHON_VERSION | grep -o '[0-9]\+\.[0-9]\+' | tr -d -c '[0-9]' ) -ge $( echo $REQUIRED_PYTHON_VERSION | tr -d -c '[0-9]' ) ] ] ; then
2024-07-26 07:57:43 +08:00
echo -e " ↳ [ ${ GREEN } ✔ ${ NC } ] $INSTALLED_PYTHON_VERSION detected. "
2024-07-24 12:47:07 +08:00
else
2024-07-26 07:57:43 +08:00
echo -e " ↳ [ ${ RED } ✗ ${ NC } ] You need python version at least $( echo $REQUIRED_PYTHON_VERSION | tr -d -c '[0-9.]' ) to continue! "
2024-08-06 05:07:57 +08:00
loge "ERROR_PYTHON_VERSION" " $INSTALLED_PYTHON_VERSION "
2024-07-25 13:20:16 +08:00
return 1
2024-07-24 12:47:07 +08:00
fi
}
2024-07-25 13:20:16 +08:00
function op_check_venv( ) {
echo "Checking for venv..."
2024-08-09 07:32:02 +08:00
if [ [ -f $OPENPILOT_ROOT /.venv/bin/activate ] ] ; then
2024-07-26 07:57:43 +08:00
echo -e " ↳ [ ${ GREEN } ✔ ${ NC } ] venv detected. "
2024-07-25 13:20:16 +08:00
else
2024-07-26 07:57:43 +08:00
echo -e " ↳ [ ${ RED } ✗ ${ NC } ] Can't activate venv in $OPENPILOT_ROOT . Assuming global env! "
2024-07-25 13:20:16 +08:00
fi
2024-07-24 12:47:07 +08:00
}
2024-07-25 13:20:16 +08:00
function op_before_cmd( ) {
if [ [ ! -z " $NO_VERIFY " ] ] ; then
return 0
fi
2024-07-24 12:47:07 +08:00
2024-07-26 07:57:43 +08:00
op_get_openpilot_dir
2024-07-24 12:47:07 +08:00
cd $OPENPILOT_ROOT
2024-07-26 07:57:43 +08:00
result = " $(( op_check_openpilot_dir ) 2 >& 1 ) " || (echo -e " $result " && return 1)
result = " ${ result } \n $(( op_check_git ) 2 >& 1 ) " || (echo -e " $result " && return 1)
result = " ${ result } \n $(( op_check_os ) 2 >& 1 ) " || (echo -e " $result " && return 1)
result = " ${ result } \n $(( op_check_venv ) 2 >& 1 ) " || (echo -e " $result " && return 1)
op_activate_venv
result = " ${ result } \n $(( op_check_python ) 2 >& 1 ) " || (echo -e " $result " && return 1)
if [ [ -z $VERBOSE ] ] ; then
2024-08-14 06:15:19 +08:00
echo -e " ${ BOLD } Checking system → ${ NC } [ ${ GREEN } ✔ ${ NC } ] "
2024-07-26 07:57:43 +08:00
else
echo -e " $result "
fi
2024-07-24 12:47:07 +08:00
}
2024-07-26 07:57:43 +08:00
function op_setup( ) {
op_get_openpilot_dir
2024-07-24 12:47:07 +08:00
cd $OPENPILOT_ROOT
2024-07-26 07:57:43 +08:00
op_check_openpilot_dir
2024-07-25 13:20:16 +08:00
op_check_os
2024-07-25 05:49:35 +08:00
2024-07-26 05:28:41 +08:00
echo "Installing dependencies..."
2024-08-01 01:42:08 +08:00
st = " $( date +%s) "
2024-07-25 13:20:16 +08:00
if [ [ " $OSTYPE " = = "linux-gnu" * ] ] ; then
2024-08-06 05:07:57 +08:00
SETUP_SCRIPT = "tools/ubuntu_setup.sh"
2024-07-25 13:20:16 +08:00
elif [ [ " $OSTYPE " = = "darwin" * ] ] ; then
2024-08-06 05:07:57 +08:00
SETUP_SCRIPT = "tools/mac_setup.sh"
fi
2024-08-14 06:15:19 +08:00
if ! $OPENPILOT_ROOT /$SETUP_SCRIPT ; then
2024-08-06 05:07:57 +08:00
echo -e " ↳ [ ${ RED } ✗ ${ NC } ] Dependencies installation failed! "
loge "ERROR_DEPENDENCIES_INSTALLATION"
return 1
2024-07-25 13:20:16 +08:00
fi
2024-08-01 01:42:08 +08:00
et = " $( date +%s) "
2024-08-14 06:15:19 +08:00
echo -e " ↳ [ ${ GREEN } ✔ ${ NC } ] Dependencies installed successfully in $(( et - st)) seconds. "
2024-07-25 13:20:16 +08:00
2024-07-26 05:28:41 +08:00
echo "Getting git submodules..."
2024-08-01 01:42:08 +08:00
st = " $( date +%s) "
2024-08-14 06:15:19 +08:00
if ! git submodule update --filter= blob:none --jobs 4 --init --recursive; then
2024-08-06 05:07:57 +08:00
echo -e " ↳ [ ${ RED } ✗ ${ NC } ] Getting git submodules failed! "
loge "ERROR_GIT_SUBMODULES"
return 1
fi
2024-08-01 01:42:08 +08:00
et = " $( date +%s) "
2024-08-14 06:15:19 +08:00
echo -e " ↳ [ ${ GREEN } ✔ ${ NC } ] Submodules installed successfully in $(( et - st)) seconds. "
2024-07-26 05:28:41 +08:00
echo "Pulling git lfs files..."
2024-08-01 01:42:08 +08:00
st = " $( date +%s) "
2024-08-14 06:15:19 +08:00
if ! git lfs pull; then
2024-08-06 05:07:57 +08:00
echo -e " ↳ [ ${ RED } ✗ ${ NC } ] Pulling git lfs files failed! "
loge "ERROR_GIT_LFS"
return 1
fi
2024-08-01 01:42:08 +08:00
et = " $( date +%s) "
2024-08-14 06:15:19 +08:00
echo -e " ↳ [ ${ GREEN } ✔ ${ NC } ] Files pulled successfully in $(( et - st)) seconds. "
2024-07-26 05:28:41 +08:00
op_check
2024-07-24 12:47:07 +08:00
}
2024-07-26 07:57:43 +08:00
function op_activate_venv( ) {
2024-08-09 07:32:02 +08:00
# bash 3.2 can't handle this without the 'set +e'
set +e
2024-07-26 07:57:43 +08:00
source $OPENPILOT_ROOT /.venv/bin/activate & > /dev/null || true
2024-08-09 07:32:02 +08:00
set -e
2024-07-26 07:57:43 +08:00
}
2024-07-25 13:20:16 +08:00
function op_venv( ) {
op_before_cmd
2024-08-09 07:32:02 +08:00
if [ [ ! -f $OPENPILOT_ROOT /.venv/bin/activate ] ] ; then
echo -e " No venv found in $OPENPILOT_ROOT "
return 1
fi
case $SHELL_NAME in
"zsh" )
ZSHRC_DIR = $( mktemp -d 2>/dev/null || mktemp -d -t 'tmp_zsh' )
echo " source $RC_FILE ; source $OPENPILOT_ROOT /.venv/bin/activate " >> $ZSHRC_DIR /.zshrc
ZDOTDIR = $ZSHRC_DIR zsh ; ;
*)
bash --rcfile <( echo " source $RC_FILE ; source $OPENPILOT_ROOT /.venv/bin/activate " ) ; ;
esac
2024-07-25 13:20:16 +08:00
}
2024-07-24 12:47:07 +08:00
2024-07-25 13:20:16 +08:00
function op_check( ) {
2024-07-26 07:57:43 +08:00
VERBOSE = 1
2024-07-25 13:20:16 +08:00
op_before_cmd
2024-07-26 07:57:43 +08:00
unset VERBOSE
2024-07-24 12:47:07 +08:00
}
2024-07-25 13:20:16 +08:00
function op_build( ) {
2024-07-27 07:10:00 +08:00
CDIR = $( pwd )
2024-07-25 13:20:16 +08:00
op_before_cmd
2024-07-27 07:10:00 +08:00
cd " $CDIR "
2024-07-25 05:49:35 +08:00
op_run_command scons $@
2024-07-24 12:47:07 +08:00
}
function op_juggle( ) {
2024-07-25 13:20:16 +08:00
op_before_cmd
2024-07-26 07:57:43 +08:00
op_run_command tools/plotjuggler/juggle.py $@
2024-07-25 05:49:35 +08:00
}
2024-07-26 09:21:42 +08:00
function op_lint( ) {
2024-07-25 13:20:16 +08:00
op_before_cmd
2024-08-14 12:47:20 +08:00
op_run_command scripts/lint/lint.sh $@
2024-07-25 05:49:35 +08:00
}
2024-07-27 02:34:36 +08:00
function op_test( ) {
op_before_cmd
op_run_command pytest $@
}
2024-07-25 05:49:35 +08:00
function op_replay( ) {
2024-07-25 13:20:16 +08:00
op_before_cmd
2024-07-26 07:57:43 +08:00
op_run_command tools/replay/replay $@
2024-07-24 12:47:07 +08:00
}
2024-07-25 13:20:16 +08:00
function op_cabana( ) {
op_before_cmd
2024-07-26 07:57:43 +08:00
op_run_command tools/cabana/cabana $@
2024-07-25 13:20:16 +08:00
}
2024-07-26 05:28:41 +08:00
function op_sim( ) {
op_before_cmd
2024-07-26 07:57:43 +08:00
op_run_command exec tools/sim/run_bridge.py &
op_run_command exec tools/sim/launch_openpilot.sh
2024-07-26 05:28:41 +08:00
}
2024-07-24 12:47:07 +08:00
function op_default( ) {
echo "An openpilot helper"
echo ""
2024-07-25 05:49:35 +08:00
echo -e " ${ BOLD } ${ UNDERLINE } Description: ${ NC } "
echo " op is your entry point for all things related to openpilot development."
2024-07-26 09:21:42 +08:00
echo " op is only a wrapper for existing scripts, tools, and commands."
2024-07-25 05:49:35 +08:00
echo " op will always show you what it will run on your system."
echo ""
echo " op will try to find your openpilot directory in the following order:"
echo " 1: use the directory specified with the --dir option"
echo " 2: use the current working directory"
echo " 3: go up the file tree non-recursively"
echo ""
echo -e " ${ BOLD } ${ UNDERLINE } Usage: ${ NC } op [OPTIONS] <COMMAND> "
2024-07-24 12:47:07 +08:00
echo ""
2024-08-15 07:51:14 +08:00
echo -e " ${ BOLD } ${ UNDERLINE } Commands [System]: ${ NC } "
2024-08-24 06:19:21 +08:00
echo -e " ${ BOLD } check ${ NC } Check the development environment (git, os, python) to start using openpilot "
echo -e " ${ BOLD } venv ${ NC } Activate the python virtual environment "
echo -e " ${ BOLD } setup ${ NC } Install openpilot dependencies "
echo -e " ${ BOLD } build ${ NC } Run the openpilot build system in the current working directory "
echo -e " ${ BOLD } install ${ NC } Install the 'op' tool system wide "
2024-08-15 07:51:14 +08:00
echo ""
echo -e " ${ BOLD } ${ UNDERLINE } Commands [Tooling]: ${ NC } "
2024-08-24 06:19:21 +08:00
echo -e " ${ BOLD } juggle ${ NC } Run PlotJuggler "
echo -e " ${ BOLD } replay ${ NC } Run Replay "
echo -e " ${ BOLD } cabana ${ NC } Run Cabana "
2024-08-15 07:51:14 +08:00
echo ""
echo -e " ${ BOLD } ${ UNDERLINE } Commands [Testing]: ${ NC } "
2024-08-24 06:19:21 +08:00
echo -e " ${ BOLD } sim ${ NC } Run openpilot in a simulator "
echo -e " ${ BOLD } lint ${ NC } Run the linter "
echo -e " ${ BOLD } post-commit ${ NC } Install the linter as a post-commit hook "
echo -e " ${ BOLD } test ${ NC } Run all unit tests from pytest "
2024-07-24 12:47:07 +08:00
echo ""
2024-07-25 05:49:35 +08:00
echo -e " ${ BOLD } ${ UNDERLINE } Options: ${ NC } "
echo -e " ${ BOLD } -d, --dir ${ NC } "
2024-07-24 12:47:07 +08:00
echo " Specify the openpilot directory you want to use"
2024-07-25 05:49:35 +08:00
echo -e " ${ BOLD } --dry ${ NC } "
2024-07-26 09:21:42 +08:00
echo " Don't actually run anything, just print what would be run"
2024-07-25 13:20:16 +08:00
echo -e " ${ BOLD } -n, --no-verify ${ NC } "
2024-07-26 09:21:42 +08:00
echo " Skip environment check before running commands"
2024-07-24 12:47:07 +08:00
echo ""
2024-07-25 05:49:35 +08:00
echo -e " ${ BOLD } ${ UNDERLINE } Examples: ${ NC } "
2024-08-05 02:07:08 +08:00
echo " op setup"
echo " Run the setup script to install"
echo " openpilot's dependencies."
2024-07-24 12:47:07 +08:00
echo ""
2024-08-05 02:07:08 +08:00
echo " op build -j4"
echo " Compile openpilot using 4 cores"
2024-07-24 12:47:07 +08:00
echo ""
2024-08-05 02:07:08 +08:00
echo " op juggle --demo"
echo " Run PlotJuggler on the demo route"
2024-07-24 12:47:07 +08:00
}
function _op( ) {
# parse Options
case $1 in
2024-07-25 13:20:16 +08:00
-d | --dir ) shift 1; OPENPILOT_ROOT = " $1 " ; shift 1 ; ;
--dry ) shift 1; DRY = "1" ; ;
-n | --no-verify ) shift 1; NO_VERIFY = "1" ; ;
2024-08-06 05:07:57 +08:00
-l | --log ) shift 1; LOG_FILE = " $1 " ; shift 1 ; ;
2024-07-24 12:47:07 +08:00
esac
# parse Commands
case $1 in
2024-08-24 06:19:21 +08:00
venv ) shift 1; op_venv " $@ " ; ;
check ) shift 1; op_check " $@ " ; ;
setup ) shift 1; op_setup " $@ " ; ;
build ) shift 1; op_build " $@ " ; ;
juggle ) shift 1; op_juggle " $@ " ; ;
cabana ) shift 1; op_cabana " $@ " ; ;
lint ) shift 1; op_lint " $@ " ; ;
test ) shift 1; op_test " $@ " ; ;
replay ) shift 1; op_replay " $@ " ; ;
sim ) shift 1; op_sim " $@ " ; ;
install ) shift 1; op_install " $@ " ; ;
post-commit ) shift 1; op_install_post_commit " $@ " ; ;
2024-07-24 12:47:07 +08:00
* ) op_default " $@ " ; ;
esac
}
_op $@