From 1e28c401717b06dc733c56d2502a677ffb672017 Mon Sep 17 00:00:00 2001 From: Maxime Desroches Date: Tue, 23 Jul 2024 16:49:16 -0700 Subject: [PATCH 1/9] op --- tools/op.sh | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100755 tools/op.sh diff --git a/tools/op.sh b/tools/op.sh new file mode 100755 index 00000000000000..4531b4baf3164a --- /dev/null +++ b/tools/op.sh @@ -0,0 +1,162 @@ +#!/bin/bash + +set -e + +RED='\033[0;31m' +GREEN='\033[0;32m' +NC='\033[0m' + +# be default, assume openpilot dir is in current directory +OPENPILOT_ROOT=$(pwd) +function op_check_openpilot_dir() { + if [ ! -f "$OPENPILOT_ROOT/launch_openpilot.sh" ]; then + echo "openpilot directory not found!" + return 1 + fi +} + +function op_check_git() { + 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!" + return 1 + else + echo -e " ↳ [${GREEN}✔${NC}] git found on your system.\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" + return 1 + else + echo -e " ↳ [${GREEN}✔${NC}] git lfs files found on your system.\n" + fi +} + +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") + OS_VERSION="Ubuntu" + echo -e " ↳ [${GREEN}✔${NC}] Ubuntu $VERSION_CODENAME detected.\n" + ;; + * ) + echo -e " ↳ [${RED}✗${NC}] Incompatible Ubuntu version $VERSION_CODENAME detected!" + return 1 + ;; + esac + else + echo -e " ↳ [${RED}✗${NC}] No /etc/os-release on your system. Make sure you're running on Ubuntu, or similar!" + return 1 + fi + + elif [[ "$OSTYPE" == "darwin"* ]]; then + echo -e " ↳ [${GREEN}✔${NC}] macos detected.\n" + OS_VERSION="Darwin" + else + echo -e " ↳ [${RED}✗${NC}] OS type $OSTYPE not supported!" + return 1 + fi +} + +function op_check_python() { + 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!" + return 1 + 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 + fi +} + +function op_venv() { + op_check_openpilot_dir + . $OPENPILOT_ROOT/.venv/bin/activate 2&> /dev/null || (echo "Can't activate venv. Have you ran 'op install' ?" && return 1) +} + +function op_check() { + op_check_openpilot_dir + cd $OPENPILOT_ROOT + op_check_git + op_check_os + op_check_python +} + +function op_run() { + op_venv + cd $OPENPILOT_ROOT + $OPENPILOT_ROOT/launch_openpilot.sh +} + +function op_install() { + op_check_openpilot_dir + cd $OPENPILOT_ROOT + + op_check_os + op_check_python + + case "$OS_VERSION" in + "Ubuntu") + $OPENPILOT_ROOT/tools/ubuntu_setup.sh + ;; + "Darwin") + $OPENPILOT_ROOT/tools/mac_setup.sh + ;; + esac + + git lfs pull +} + +function op_build() { + op_venv + cd $OPENPILOT_ROOT + + scons -j$(nproc || sysctl -n hw.logicalcpu) +} + +function op_juggle() { + op_venv + cd $OPENPILOT_ROOT + + $OPENPILOT_ROOT/tools/plotjuggler/juggle.py +} + +function op_default() { + echo "An openpilot helper" + echo "" + echo -e "\e[4mUsage:\e[0m op " + echo "" + echo -e "\e[4mCommands:\e[0m" + 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 Plotjuggler" + echo " linter Run the linter" + echo " help Show this message" +} + +function op() { + case $1 in + check ) shift 1; op_check "$@" ;; + install ) shift 1; op_install "$@" ;; + build ) shift 1; op_build "$@" ;; + run ) shift 1; op_run "$@" ;; + juggle ) shift 1; op_juggle "$@" ;; + * ) op_default "$@" ;; + esac +} + +op $@ From b453b96c3d91bf4a4031765f57a62ef25c1f0552 Mon Sep 17 00:00:00 2001 From: Maxime Desroches Date: Tue, 23 Jul 2024 16:51:38 -0700 Subject: [PATCH 2/9] change this --- tools/op.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/op.sh b/tools/op.sh index 4531b4baf3164a..9ee369f8b3686d 100755 --- a/tools/op.sh +++ b/tools/op.sh @@ -83,7 +83,7 @@ function op_check_python() { function op_venv() { op_check_openpilot_dir - . $OPENPILOT_ROOT/.venv/bin/activate 2&> /dev/null || (echo "Can't activate venv. Have you ran 'op install' ?" && return 1) + . $OPENPILOT_ROOT/.venv/bin/activate || (echo -e "\nCan't activate venv. Have you ran 'op install' ?" && return 1) } function op_check() { From b930871bd2287277cd6e44f7c0d871a9aa1c683b Mon Sep 17 00:00:00 2001 From: Maxime Desroches Date: Tue, 23 Jul 2024 17:01:27 -0700 Subject: [PATCH 3/9] juggler --- tools/op.sh | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/tools/op.sh b/tools/op.sh index 9ee369f8b3686d..61db8d57ec8f43 100755 --- a/tools/op.sh +++ b/tools/op.sh @@ -130,7 +130,7 @@ function op_juggle() { op_venv cd $OPENPILOT_ROOT - $OPENPILOT_ROOT/tools/plotjuggler/juggle.py + $OPENPILOT_ROOT/tools/plotjuggler/juggle.py $@ } function op_default() { @@ -143,18 +143,17 @@ function op_default() { echo " install Install requirements to use openpilot" echo " build Build openpilot" echo " run Run openpilot" - echo " juggle Plotjuggler" - echo " linter Run the linter" + echo " juggle Run Plotjuggler" echo " help Show this message" } function op() { case $1 in - check ) shift 1; op_check "$@" ;; - install ) shift 1; op_install "$@" ;; - build ) shift 1; op_build "$@" ;; - run ) shift 1; op_run "$@" ;; - juggle ) shift 1; op_juggle "$@" ;; + check ) shift 1; op_check "$@" ;; + install ) shift 1; op_install "$@" ;; + build ) shift 1; op_build "$@" ;; + run ) shift 1; op_run "$@" ;; + juggle ) shift 1; op_juggle "$@" ;; * ) op_default "$@" ;; esac } From 199b3cacb6cdc6ca4043ad39b824b97ff2d70244 Mon Sep 17 00:00:00 2001 From: Maxime Desroches Date: Tue, 23 Jul 2024 17:11:46 -0700 Subject: [PATCH 4/9] options --- tools/op.sh | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tools/op.sh b/tools/op.sh index 61db8d57ec8f43..8c1156eea6adb2 100755 --- a/tools/op.sh +++ b/tools/op.sh @@ -136,7 +136,7 @@ function op_juggle() { function op_default() { echo "An openpilot helper" echo "" - echo -e "\e[4mUsage:\e[0m op " + echo -e "\e[4mUsage:\e[0m op [OPTIONS] " echo "" echo -e "\e[4mCommands:\e[0m" echo " check Check system requirements (git, os, python) to start using openpilot" @@ -145,9 +145,25 @@ function op_default() { echo " run Run openpilot" echo " juggle Run Plotjuggler" echo " help Show this message" + echo "" + echo -e "\e[4mOptions:\e[0m" + echo " -d, --dir" + echo " Specify the openpilot directory you want to use" + echo " Default to the current working directory" + echo "" + echo -e "\e[4mExamples:\e[0m" + echo " op --dir /tmp/openpilot check" + echo " Run the check command on openpilot located in /tmp/openpilot" + echo "" + echo " op build" + echo " Build openpilot located in your current working directory" } function op() { + case $1 in + -d | --dir ) shift 1; OPENPILOT_ROOT="$1"; shift 1 ;; + esac + case $1 in check ) shift 1; op_check "$@" ;; install ) shift 1; op_install "$@" ;; From 4b292d030f898b25d14596c724e473ff80f9e170 Mon Sep 17 00:00:00 2001 From: Maxime Desroches Date: Tue, 23 Jul 2024 17:22:14 -0700 Subject: [PATCH 5/9] fix --- tools/op.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/op.sh b/tools/op.sh index 8c1156eea6adb2..b6eb1225fb4282 100755 --- a/tools/op.sh +++ b/tools/op.sh @@ -116,6 +116,7 @@ function op_install() { ;; esac + git submodules update --init --recursive git lfs pull } @@ -123,7 +124,7 @@ function op_build() { op_venv cd $OPENPILOT_ROOT - scons -j$(nproc || sysctl -n hw.logicalcpu) + scons -j$(nproc || sysctl -n hw.logicalcpu) || echo -e "\nBuild failed. Have you ran 'op install' ?" } function op_juggle() { From 1071c70cf33128bd3a77b0f4e2aa5d2264fda483 Mon Sep 17 00:00:00 2001 From: Maxime Desroches Date: Tue, 23 Jul 2024 17:32:38 -0700 Subject: [PATCH 6/9] submodules --- tools/op.sh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tools/op.sh b/tools/op.sh index b6eb1225fb4282..86f283011f3f4f 100755 --- a/tools/op.sh +++ b/tools/op.sh @@ -33,6 +33,14 @@ function op_check_git() { else echo -e " ↳ [${GREEN}✔${NC}] git lfs files found on your system.\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 submodules update --init --recursive'" + return 1 + else + echo -e " ↳ [${GREEN}✔${NC}] git submodules found on your system.\n" + fi } function op_check_os() { From 9be65e7fb435c4067fc65ee0ace0d2176fcca93c Mon Sep 17 00:00:00 2001 From: Maxime Desroches Date: Tue, 23 Jul 2024 17:35:00 -0700 Subject: [PATCH 7/9] typo --- tools/op.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/op.sh b/tools/op.sh index 86f283011f3f4f..0898d5ad3eb71a 100755 --- a/tools/op.sh +++ b/tools/op.sh @@ -36,7 +36,7 @@ function op_check_git() { 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 submodules update --init --recursive'" + 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" From 469257153d14ca741d67274410c6201875a914b7 Mon Sep 17 00:00:00 2001 From: Maxime Desroches Date: Tue, 23 Jul 2024 20:58:22 -0700 Subject: [PATCH 8/9] venv --- tools/op.sh | 86 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 67 insertions(+), 19 deletions(-) diff --git a/tools/op.sh b/tools/op.sh index 0898d5ad3eb71a..d6c345dc67fbc8 100755 --- a/tools/op.sh +++ b/tools/op.sh @@ -1,7 +1,5 @@ #!/bin/bash -set -e - RED='\033[0;31m' GREEN='\033[0;32m' NC='\033[0m' @@ -9,13 +7,19 @@ NC='\033[0m' # be default, assume openpilot dir is in current directory OPENPILOT_ROOT=$(pwd) function op_check_openpilot_dir() { + (set -e + if [ ! -f "$OPENPILOT_ROOT/launch_openpilot.sh" ]; then echo "openpilot directory not found!" return 1 fi + + ) } function op_check_git() { + (set -e + cd $OPENPILOT_ROOT echo "Checking for git..." @@ -41,9 +45,13 @@ function op_check_git() { else echo -e " ↳ [${GREEN}✔${NC}] git submodules found on your system.\n" fi + + ) } function op_check_os() { + (set -e + echo "Checking for compatible os version..." if [[ "$OSTYPE" == "linux-gnu"* ]]; then @@ -71,9 +79,13 @@ function op_check_os() { echo -e " ↳ [${RED}✗${NC}] OS type $OSTYPE not supported!" return 1 fi + + ) } 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 @@ -87,28 +99,41 @@ function op_check_python() { return 1 fi fi + + ) } +# this must be run in the same shell as the user calling "op" function op_venv() { - op_check_openpilot_dir - . $OPENPILOT_ROOT/.venv/bin/activate || (echo -e "\nCan't activate venv. Have you ran 'op install' ?" && return 1) + 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) } function op_check() { + (set -e + op_check_openpilot_dir cd $OPENPILOT_ROOT op_check_git op_check_os op_check_python + + ) } function op_run() { + (set -e + op_venv cd $OPENPILOT_ROOT $OPENPILOT_ROOT/launch_openpilot.sh + + ) } function op_install() { + (set -e + op_check_openpilot_dir cd $OPENPILOT_ROOT @@ -126,20 +151,30 @@ function op_install() { git submodules update --init --recursive git lfs pull + + ) } function op_build() { + (set -e + op_venv cd $OPENPILOT_ROOT scons -j$(nproc || sysctl -n hw.logicalcpu) || echo -e "\nBuild failed. Have you ran 'op install' ?" + + ) } function op_juggle() { + (set -e + op_venv cd $OPENPILOT_ROOT $OPENPILOT_ROOT/tools/plotjuggler/juggle.py $@ + + ) } function op_default() { @@ -148,6 +183,7 @@ function op_default() { echo -e "\e[4mUsage:\e[0m op [OPTIONS] " 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" @@ -168,19 +204,31 @@ function op_default() { echo " Build openpilot located in your current working directory" } -function op() { - case $1 in - -d | --dir ) shift 1; OPENPILOT_ROOT="$1"; shift 1 ;; - esac - - case $1 in - check ) shift 1; op_check "$@" ;; - install ) shift 1; op_install "$@" ;; - build ) shift 1; op_build "$@" ;; - run ) shift 1; op_run "$@" ;; - juggle ) shift 1; op_juggle "$@" ;; - * ) op_default "$@" ;; - esac -} -op $@ +# parse Options +case $1 in + -d | --dir ) shift 1; OPENPILOT_ROOT="$1"; shift 1 ;; +esac + +# parse Commands +case $1 in + venv ) shift 1; op_venv "$@" ;; + check ) shift 1; op_check "$@" ;; + install ) shift 1; op_install "$@" ;; + build ) shift 1; op_build "$@" ;; + run ) shift 1; op_run "$@" ;; + juggle ) shift 1; op_juggle "$@" ;; + * ) op_default "$@" ;; +esac + +# remove from env +unset -f op_check +unset -f op_install +unset -f op_build +unset -f op_run +unset -f op_juggle +unset -f op_venv +unset -f op_check_openpilot_dir +unset -f op_check_git +unset -f op_check_python +unset -f op_check_os From a58f5e0b3001a9665a1fa580101a5c303afe096e Mon Sep 17 00:00:00 2001 From: Maxime Desroches Date: Tue, 23 Jul 2024 21:41:46 -0700 Subject: [PATCH 9/9] clean + install --- tools/op.sh | 76 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 24 deletions(-) diff --git a/tools/op.sh b/tools/op.sh index d6c345dc67fbc8..d8a3f4b809f196 100755 --- a/tools/op.sh +++ b/tools/op.sh @@ -4,6 +4,20 @@ RED='\033[0;31m' GREEN='\033[0;32m' NC='\033[0m' +function op_first_install() { + (set -e + + echo "Installing op system-wide..." + RC_FILE="${HOME}/.$(basename ${SHELL})rc" + 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 + echo -e " ↳ [${GREEN}✔${NC}] op installed successfully. Open a new shell to use it.\n" + + ) +} + # be default, assume openpilot dir is in current directory OPENPILOT_ROOT=$(pwd) function op_check_openpilot_dir() { @@ -161,7 +175,7 @@ function op_build() { op_venv cd $OPENPILOT_ROOT - scons -j$(nproc || sysctl -n hw.logicalcpu) || echo -e "\nBuild failed. Have you ran 'op install' ?" + scons $@ || echo -e "\nBuild failed. Have you ran 'op install' ?" ) } @@ -183,13 +197,14 @@ function op_default() { echo -e "\e[4mUsage:\e[0m op [OPTIONS] " 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 " 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 "" echo -e "\e[4mOptions:\e[0m" echo " -d, --dir" @@ -200,28 +215,39 @@ function op_default() { echo " op --dir /tmp/openpilot check" echo " Run the check command on openpilot located in /tmp/openpilot" echo "" - echo " op build" - echo " Build openpilot located in your current working directory" + echo " op juggle --install" + echo " Install plotjuggler in the openpilot located in your current" + echo " working directory" + echo "" + echo " op --dir /tmp/openpilot build -j4" + echo " Run the build command on openpilot located in /tmp/openpilot" + echo " on 4 cores" } -# parse Options -case $1 in - -d | --dir ) shift 1; OPENPILOT_ROOT="$1"; shift 1 ;; -esac +function _op() { + # parse Options + case $1 in + -d | --dir ) shift 1; OPENPILOT_ROOT="$1"; shift 1 ;; + esac + + # parse Commands + case $1 in + venv ) shift 1; op_venv "$@" ;; + check ) shift 1; op_check "$@" ;; + install ) shift 1; op_install "$@" ;; + build ) shift 1; op_build "$@" ;; + run ) shift 1; op_run "$@" ;; + juggle ) shift 1; op_juggle "$@" ;; + --install ) shift 1; op_first_install "$@" ;; + * ) op_default "$@" ;; + esac +} -# parse Commands -case $1 in - venv ) shift 1; op_venv "$@" ;; - check ) shift 1; op_check "$@" ;; - install ) shift 1; op_install "$@" ;; - build ) shift 1; op_build "$@" ;; - run ) shift 1; op_run "$@" ;; - juggle ) shift 1; op_juggle "$@" ;; - * ) op_default "$@" ;; -esac +_op $@ # remove from env +unset -f _op unset -f op_check unset -f op_install unset -f op_build @@ -232,3 +258,5 @@ unset -f op_check_openpilot_dir unset -f op_check_git unset -f op_check_python unset -f op_check_os +unset -f op_first_install +unset -f op_default