Skip to content

Commit

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

* pre-check

* cd

* change this

* fix

* fix install
  • Loading branch information
maxime-desroches authored Jul 25, 2024
1 parent 71ad5a8 commit 4ac938a
Showing 1 changed file with 110 additions and 73 deletions.
183 changes: 110 additions & 73 deletions tools/op.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

RED='\033[0;31m'
GREEN='\033[0;32m'
Expand All @@ -14,64 +14,66 @@ function op_first_install() {
if [ "$(uname)" == "Darwin" ] && [ $SHELL == "/bin/bash" ]; then
RC_FILE="$HOME/.bash_profile"
fi
op_run_command printf "\nalias op='source "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )/op.sh" \"\$@\"'\n" >> $RC_FILE
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"

)
}

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

# be default, assume openpilot dir is in current directory
OPENPILOT_ROOT=$(pwd)
function op_check_openpilot_dir() {
echo "Checking for openpilot directory..."
while [[ "$OPENPILOT_ROOT" != '/' ]];
do
if find "$OPENPILOT_ROOT/launch_openpilot.sh" -maxdepth 1 -mindepth 1 &> /dev/null; then
echo -e " ↳ [${GREEN}${NC}] openpilot found.\n"
return 0
fi
OPENPILOT_ROOT="$(readlink -f "$OPENPILOT_ROOT/"..)"
done

echo "openpilot directory not found! Make sure that you are inside openpilot"
echo "directory or specify one with the --dir option!"
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!"
return 1
}

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

function op_check_git() {
(set -e

cd $OPENPILOT_ROOT

echo "Checking for git..."
if ! command -v "git" > /dev/null 2>&1; then
echo -e " ↳ [${RED}${NC}] git not found on your system!"
echo -e " ↳ [${RED}${NC}] git not found on your system!\n"
return 1
else
echo -e " ↳ [${GREEN}${NC}] git found on your system.\n"
echo -e " ↳ [${GREEN}${NC}] git found.\n"
fi

echo "Checking for git lfs files..."
if [[ $(file -b $(git lfs ls-files -n | grep "\.so" | head -n 1)) == "ASCII text" ]]; then
echo -e " ↳ [${RED}${NC}] git lfs files not found! Run git lfs pull"
if [[ $(file -b $OPENPILOT_ROOT/selfdrive/modeld/models/supercombo.onnx) == "ASCII text" ]]; then
echo -e " ↳ [${RED}${NC}] git lfs files not found! Run 'git lfs pull'\n"
return 1
else
echo -e " ↳ [${GREEN}${NC}] git lfs files found on your system.\n"
echo -e " ↳ [${GREEN}${NC}] git lfs files found.\n"
fi

echo "Checking for git submodules..."
if $(git submodule foreach --quiet --recursive 'return 1' 2&> /dev/null); then
echo -e " ↳ [${RED}${NC}] git submodules not found! Run 'git submodule update --init --recursive'"
return 1
else
echo -e " ↳ [${GREEN}${NC}] git submodules found on your system.\n"
fi
for name in body msgq_repo opendbc panda rednose_repo tinygrad_repo; do
if [[ -z $(ls $OPENPILOT_ROOT/$name) ]]; then
echo -e " ↳ [${RED}${NC}] git submodule $name not found! Run 'git submodule update --init --recursive'\n"
return 1
fi
done
echo -e " ↳ [${GREEN}${NC}] git submodules found.\n"

)
}
Expand All @@ -89,19 +91,19 @@ function op_check_os() {
echo -e " ↳ [${GREEN}${NC}] Ubuntu $VERSION_CODENAME detected.\n"
;;
* )
echo -e " ↳ [${RED}${NC}] Incompatible Ubuntu version $VERSION_CODENAME detected!"
echo -e " ↳ [${RED}${NC}] Incompatible Ubuntu version $VERSION_CODENAME detected!\n"
return 1
;;
esac
else
echo -e " ↳ [${RED}${NC}] No /etc/os-release on your system. Make sure you're running on Ubuntu, or similar!"
echo -e " ↳ [${RED}${NC}] No /etc/os-release on your system. Make sure you're running on Ubuntu, or similar!\n"
return 1
fi

elif [[ "$OSTYPE" == "darwin"* ]]; then
echo -e " ↳ [${GREEN}${NC}] macos detected.\n"
else
echo -e " ↳ [${RED}${NC}] OS type $OSTYPE not supported!"
echo -e " ↳ [${RED}${NC}] OS type $OSTYPE not supported!\n"
return 1
fi

Expand All @@ -112,78 +114,100 @@ function op_check_python() {
(set -e

echo "Checking for compatible python version..."
export REQUIRED_PYTHON_VERSION=$(grep "requires-python" pyproject.toml | cut -d= -f3- | tr -d '"' | tr -d ' ')
if ! command -v "python3" > /dev/null 2>&1; then
echo -e " ↳ [${RED}${NC}] python3 not found on your system. You need python version at least $REQUIRED_PYTHON_VERSION to continue!"
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
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!\n"
return 1
elif [[ $(echo $INSTALLED_PYTHON_VERSION | tr -d -c '[0-9]') -ge $(($(echo $REQUIRED_PYTHON_VERSION | tr -d -c '[0-9]') * 10)) ]]; then
echo -e " ↳ [${GREEN}${NC}] $INSTALLED_PYTHON_VERSION detected.\n"
else
if $(python3 -c "import sys; quit(not sys.version_info >= tuple(map(int, \"$REQUIRED_PYTHON_VERSION\".split('.'))))"); then
echo -e " ↳ [${GREEN}${NC}] $(python3 --version) detected.\n"
else
echo -e " ↳ [${RED}${NC}] You need python version at least $REQUIRED_PYTHON_VERSION to continue!"
return 1
fi
echo -e " ↳ [${RED}${NC}] You need python version at least $(echo $REQUIRED_PYTHON_VERSION | tr -d -c '[0-9.]') to continue!\n"
return 1
fi

)
}

# this must be run in the same shell as the user calling "op"
function op_venv() {
op_check_openpilot_dir || 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_venv() {
echo "Checking for venv..."
if source $OPENPILOT_ROOT/.venv/bin/activate; then
echo -e " ↳ [${GREEN}${NC}] venv detected.\n"
else
echo -e " ↳ [${RED}${NC}] Can't activate venv in $OPENPILOT_ROOT. Assuming global env!\n"
fi
}

function op_check() {
(set -e
function op_before_cmd() {
if [[ ! -z "$NO_VERIFY" ]]; then
return 0
fi

op_check_openpilot_dir
cd $OPENPILOT_ROOT
op_check_git
op_check_os
op_check_venv
op_check_python

)
echo -e "-----------------------------\n"
}

function op_run() {
function op_install() {
(set -e

op_venv
op_check_openpilot_dir
cd $OPENPILOT_ROOT
op_check_os
op_check_python

op_run_command $OPENPILOT_ROOT/launch_openpilot.sh
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
op_run_command $OPENPILOT_ROOT/tools/ubuntu_setup.sh
elif [[ "$OSTYPE" == "darwin"* ]]; then
op_run_command $OPENPILOT_ROOT/tools/mac_setup.sh
fi

op_run_command git submodule update --init --recursive
op_run_command git lfs pull

)
}

function op_install() {
(set -e
function op_venv() {
( set -e

op_check_openpilot_dir
cd $OPENPILOT_ROOT
op_before_cmd

op_check_os
op_check_python
)

if [[ "$OSTYPE" == "linux-gnu"* ]]; then
$OPENPILOT_ROOT/tools/ubuntu_setup.sh
elif [[ "$OSTYPE" == "darwin"* ]]; then
$OPENPILOT_ROOT/tools/mac_setup.sh
if [[ "$?" -eq 0 ]]; then
# this must be run in the same shell as the user calling "op"
op_check_openpilot_dir > /dev/null
op_run_command source $OPENPILOT_ROOT/.venv/bin/activate
fi
}

git submodule update --init --recursive
git lfs pull
function op_check() {
(set -e

op_before_cmd

)
}

function op_build() {
function op_run() {
(set -e

op_venv
cd $OPENPILOT_ROOT
op_before_cmd
op_run_command $OPENPILOT_ROOT/launch_openpilot.sh

)
}

function op_build() {
(set -e

op_before_cmd
op_run_command scons $@

)
Expand All @@ -192,9 +216,7 @@ function op_build() {
function op_juggle() {
(set -e

op_venv
cd $OPENPILOT_ROOT

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

)
Expand All @@ -203,24 +225,30 @@ function op_juggle() {
function op_linter() {
(set -e

op_venv
cd $OPENPILOT_ROOT

op_before_cmd
op_run_command pre-commit run --all $@

)
}

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

op_before_cmd
op_run_command $OPENPILOT_ROOT/tools/replay/replay $@

)
}

function op_cabana() {
(set -e

op_before_cmd
op_run_command $OPENPILOT_ROOT/tools/cabana/cabana $@

)
}

function op_default() {
echo "An openpilot helper"
echo ""
Expand All @@ -244,6 +272,7 @@ function op_default() {
echo -e " ${BOLD}run${NC} Run openpilot"
echo -e " ${BOLD}juggle${NC} Run Plotjuggler"
echo -e " ${BOLD}replay${NC} Run replay"
echo -e " ${BOLD}cabana${NC} Run cabana"
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"
Expand All @@ -253,6 +282,8 @@ function op_default() {
echo " Specify the openpilot directory you want to use"
echo -e " ${BOLD}--dry${NC}"
echo " Don't actually run anything, just print what would be"
echo -e " ${BOLD}-n, --no-verify${NC}"
echo " Don't run checks before running a command"
echo ""
echo -e "${BOLD}${UNDERLINE}Examples:${NC}"
echo " op --dir /tmp/openpilot check"
Expand All @@ -271,8 +302,9 @@ function op_default() {
function _op() {
# parse Options
case $1 in
-d | --dir ) shift 1; OPENPILOT_ROOT="$1"; shift 1 ;;
--dry ) shift 1; DRY="1" ;;
-d | --dir ) shift 1; OPENPILOT_ROOT="$1"; shift 1 ;;
--dry ) shift 1; DRY="1" ;;
-n | --no-verify ) shift 1; NO_VERIFY="1" ;;
esac

# parse Commands
Expand All @@ -283,6 +315,7 @@ function _op() {
build ) shift 1; op_build "$@" ;;
run ) shift 1; op_run "$@" ;;
juggle ) shift 1; op_juggle "$@" ;;
cabana ) shift 1; op_cabana "$@" ;;
linter ) shift 1; op_linter "$@" ;;
replay ) shift 1; op_replay "$@" ;;
--install ) shift 1; op_first_install "$@" ;;
Expand All @@ -309,5 +342,9 @@ unset -f op_default
unset -f op_run_command
unset -f op_linter
unset -f op_replay
unset -f op_cabana
unset -f op_check_venv
unset -f op_before_cmd
unset DRY
unset OPENPILOT_ROOT
unset NO_VERIFY

0 comments on commit 4ac938a

Please sign in to comment.