Skip to content

Commit

Permalink
op.sh: general improvements (commaai#33062)
Browse files Browse the repository at this point in the history
* color

* format

* dry

* unset

* venv

* search for openpilot

* linter

* replay
  • Loading branch information
maxime-desroches authored Jul 24, 2024
1 parent add958b commit 71ad5a8
Showing 1 changed file with 81 additions and 25 deletions.
106 changes: 81 additions & 25 deletions tools/op.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

RED='\033[0;31m'
GREEN='\033[0;32m'
UNDERLINE='\033[4m'
BOLD='\033[1m'
NC='\033[0m'

function op_first_install() {
Expand All @@ -12,7 +14,7 @@ function op_first_install() {
if [ "$(uname)" == "Darwin" ] && [ $SHELL == "/bin/bash" ]; then
RC_FILE="$HOME/.bash_profile"
fi
printf "\nalias op='source "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )/op.sh" \"\$@\"'\n" >> $RC_FILE
op_run_command printf "\nalias op='source "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )/op.sh" \"\$@\"'\n" >> $RC_FILE
echo -e " ↳ [${GREEN}${NC}] op installed successfully. Open a new shell to use it.\n"

)
Expand All @@ -21,14 +23,25 @@ function op_first_install() {
# be default, assume openpilot dir is in current directory
OPENPILOT_ROOT=$(pwd)
function op_check_openpilot_dir() {
(set -e
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

if [ ! -f "$OPENPILOT_ROOT/launch_openpilot.sh" ]; then
echo "openpilot directory not found!"
return 1
fi
echo "openpilot directory not found! Make sure that you are inside openpilot"
echo "directory or specify one with the --dir option!"
return 1
}

)
function op_run_command() {
CMD="$@"
echo -e "${BOLD}Running:${NC} $CMD"
if [[ -z "$DRY" ]]; then
$CMD
fi
}

function op_check_git() {
Expand Down Expand Up @@ -118,7 +131,7 @@ function op_check_python() {
# this must be run in the same shell as the user calling "op"
function op_venv() {
op_check_openpilot_dir || return 1
source $OPENPILOT_ROOT/.venv/bin/activate || (echo -e "\nCan't activate venv. Have you ran 'op install' ?" && return 1)
op_run_command source $OPENPILOT_ROOT/.venv/bin/activate || (echo -e "\nCan't activate venv. Have you ran 'op install' ?" && return 1)
}

function op_check() {
Expand All @@ -138,7 +151,8 @@ function op_run() {

op_venv
cd $OPENPILOT_ROOT
$OPENPILOT_ROOT/launch_openpilot.sh

op_run_command $OPENPILOT_ROOT/launch_openpilot.sh

)
}
Expand Down Expand Up @@ -170,7 +184,7 @@ function op_build() {
op_venv
cd $OPENPILOT_ROOT

scons $@ || echo -e "\nBuild failed. Have you ran 'op install' ?"
op_run_command scons $@

)
}
Expand All @@ -181,32 +195,66 @@ function op_juggle() {
op_venv
cd $OPENPILOT_ROOT

$OPENPILOT_ROOT/tools/plotjuggler/juggle.py $@
op_run_command $OPENPILOT_ROOT/tools/plotjuggler/juggle.py $@

)
}

function op_linter() {
(set -e

op_venv
cd $OPENPILOT_ROOT

op_run_command pre-commit run --all $@

)
}

function op_replay() {
(set -e
op_check_openpilot_dir
cd $OPENPILOT_ROOT

op_run_command $OPENPILOT_ROOT/tools/replay/replay $@

)
}

function op_default() {
echo "An openpilot helper"
echo ""
echo -e "\e[4mUsage:\e[0m op [OPTIONS] <COMMAND>"
echo -e "${BOLD}${UNDERLINE}Description:${NC}"
echo " op is your entry point for all things related to openpilot development."
echo " op is only a wrapper for scripts, tools and commands already existing."
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>"
echo ""
echo -e "\e[4mCommands:\e[0m"
echo " venv Activate the virtual environment"
echo " check Check system requirements (git, os, python) to start using openpilot"
echo " install Install requirements to use openpilot"
echo " build Build openpilot"
echo " run Run openpilot"
echo " juggle Run Plotjuggler"
echo " help Show this message"
echo " --install Install this tool system wide"
echo -e "${BOLD}${UNDERLINE}Commands:${NC}"
echo -e " ${BOLD}venv${NC} Activate the virtual environment"
echo -e " ${BOLD}check${NC} Check system requirements (git, os, python) to start using openpilot"
echo -e " ${BOLD}install${NC} Install requirements to use openpilot"
echo -e " ${BOLD}build${NC} Build openpilot"
echo -e " ${BOLD}run${NC} Run openpilot"
echo -e " ${BOLD}juggle${NC} Run Plotjuggler"
echo -e " ${BOLD}replay${NC} Run replay"
echo -e " ${BOLD}linter${NC} Run all the pre-commit checks"
echo -e " ${BOLD}help${NC} Show this message"
echo -e " ${BOLD}--install${NC} Install this tool system wide"
echo ""
echo -e "\e[4mOptions:\e[0m"
echo " -d, --dir"
echo -e "${BOLD}${UNDERLINE}Options:${NC}"
echo -e " ${BOLD}-d, --dir${NC}"
echo " Specify the openpilot directory you want to use"
echo " Default to the current working directory"
echo -e " ${BOLD}--dry${NC}"
echo " Don't actually run anything, just print what would be"
echo ""
echo -e "\e[4mExamples:\e[0m"
echo -e "${BOLD}${UNDERLINE}Examples:${NC}"
echo " op --dir /tmp/openpilot check"
echo " Run the check command on openpilot located in /tmp/openpilot"
echo ""
Expand All @@ -224,6 +272,7 @@ function _op() {
# parse Options
case $1 in
-d | --dir ) shift 1; OPENPILOT_ROOT="$1"; shift 1 ;;
--dry ) shift 1; DRY="1" ;;
esac

# parse Commands
Expand All @@ -234,6 +283,8 @@ function _op() {
build ) shift 1; op_build "$@" ;;
run ) shift 1; op_run "$@" ;;
juggle ) shift 1; op_juggle "$@" ;;
linter ) shift 1; op_linter "$@" ;;
replay ) shift 1; op_replay "$@" ;;
--install ) shift 1; op_first_install "$@" ;;
* ) op_default "$@" ;;
esac
Expand All @@ -255,3 +306,8 @@ unset -f op_check_python
unset -f op_check_os
unset -f op_first_install
unset -f op_default
unset -f op_run_command
unset -f op_linter
unset -f op_replay
unset DRY
unset OPENPILOT_ROOT

0 comments on commit 71ad5a8

Please sign in to comment.