Skip to content

Commit

Permalink
feat: created standard way to get project root; fixes mit-dci#304
Browse files Browse the repository at this point in the history
  • Loading branch information
rockett-m committed Oct 7, 2024
1 parent cf9060e commit 5c9ee8c
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 44 deletions.
4 changes: 2 additions & 2 deletions scripts/benchmarks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ done
# environment variable, assume it's named 'build' and is located in the
# top-level directory of the repo. By defining the top-level directory relative
# to the location of this script, the user can run this script from any folder.
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
REPO_TOP_DIR="${SCRIPT_DIR}/.."
SCRIPT_DIR="$( cd -- "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
REPO_TOP_DIR="$( "$SCRIPT_DIR"/get-root.sh )"
if [[ -z "${BUILD_DIR+x}" ]]
then
BUILD_DIR="${REPO_TOP_DIR}/build"
Expand Down
11 changes: 6 additions & 5 deletions scripts/build-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

set -e

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
SCRIPT_DIR="$( cd -- "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
ROOT="$( "$SCRIPT_DIR"/get-root.sh )"
DOCKER_IMAGE_TAG_BASE=${DOCKER_IMAGE_TAG:-opencbdc-tx-base}
DOCKER_IMAGE_TAG_BUILDER=${DOCKER_IMAGE_TAG:-opencbdc-tx-builder}
DOCKER_IMAGE_TAG_ATOMIZER=${DOCKER_IMAGE_TAG:-opencbdc-tx-atomizer}
Expand All @@ -12,7 +13,7 @@ DOCKER_IMAGE_TAG_TWOPHASE=${DOCKER_IMAGE_TAG:-opencbdc-tx-twophase}
git submodule init && git submodule update

# Build docker image
docker build --target base -t $DOCKER_IMAGE_TAG_BASE -f $SCRIPT_DIR/../Dockerfile $SCRIPT_DIR/..
docker build --target builder --build-arg BASE_IMAGE=base -t $DOCKER_IMAGE_TAG_BUILDER -f $SCRIPT_DIR/../Dockerfile $SCRIPT_DIR/..
docker build --target twophase --build-arg BASE_IMAGE=base -t $DOCKER_IMAGE_TAG_TWOPHASE -f $SCRIPT_DIR/../Dockerfile $SCRIPT_DIR/..
docker build --target atomizer --build-arg BASE_IMAGE=base -t $DOCKER_IMAGE_TAG_ATOMIZER -f $SCRIPT_DIR/../Dockerfile $SCRIPT_DIR/..
docker build --target base -t $DOCKER_IMAGE_TAG_BASE -f "$ROOT"/Dockerfile "$ROOT"
docker build --target builder --build-arg BASE_IMAGE=base -t $DOCKER_IMAGE_TAG_BUILDER -f "$ROOT"/Dockerfile "$ROOT"
docker build --target twophase --build-arg BASE_IMAGE=base -t $DOCKER_IMAGE_TAG_TWOPHASE -f "$ROOT"/Dockerfile "$ROOT"
docker build --target atomizer --build-arg BASE_IMAGE=base -t $DOCKER_IMAGE_TAG_ATOMIZER -f "$ROOT"/Dockerfile "$ROOT"
4 changes: 3 additions & 1 deletion scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ fi

echo "Building..."

SCRIPT_DIR="$( cd -- "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
ROOT="$( "$SCRIPT_DIR"/get-root.sh )"
# see PREFIX in ./scripts/setup-dependencies.sh
PREFIX="$(cd "$(dirname "$0")"/.. && pwd)/prefix"
PREFIX=""$ROOT"/prefix"

if [ -z ${BUILD_DIR+x} ]; then
export BUILD_DIR=build
Expand Down
28 changes: 28 additions & 0 deletions scripts/get-root.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash
set -e

# calling scripts can use this script to capture the project ROOT dir
# SCRIPT_DIR="$( cd -- "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# ROOT="$( "$SCRIPT_DIR"/get-root.sh )"

# returns the fullpath to the ROOT dir without a slash at the end
ROOT=
# if we are in a git repository
if git rev-parse --show-toplevel >/dev/null 2>&1; then
ROOT="$( git rev-parse --show-toplevel )"
else
# $BASH_SOURCE[0] is the full path of the script being executed
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd )"
# scripts dir will always be one level below ROOT level: "$ROOT"/scripts
ROOT="$( cd -- "$SCRIPT_DIR"/.. && pwd )"
fi

# check for directory existence
if [[ ! -d "$ROOT" ]]; then
echo "Error: ROOT '${ROOT}' not found."
exit 1
fi

# use other scripts in the script dir to capture the ROOT fullpath
printf "%s\n" "$ROOT"

3 changes: 2 additions & 1 deletion scripts/install-build-tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ for PY_VERS in "${PYTHON_VERSIONS[@]}"; do
fi
done

ROOT="$(cd "$(dirname "$0")"/.. && pwd)"
SCRIPT_DIR="$( cd -- "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
ROOT="$( "$SCRIPT_DIR"/get-root.sh )"
ENV_NAME=".py_venv"

# make a virtual environement to install python packages
Expand Down
4 changes: 2 additions & 2 deletions scripts/native-system-benchmark.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ DBG="${DBG:-$GDB}"
DURATION=30
CWD=$(pwd)
COMMIT=$(git rev-parse --short HEAD)
TL=$(git rev-parse --show-toplevel)
RT="${TL:-$CWD}"
SCRIPT_DIR="$( cd -- "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
RT="$( "$SCRIPT_DIR"/get-root.sh )"
BLD="$RT"/build
SEEDDIR="$BLD"/preseeds
TESTDIR="$BLD"/test-$(date +"%s")
Expand Down
57 changes: 29 additions & 28 deletions scripts/pylint.sh
Original file line number Diff line number Diff line change
@@ -1,50 +1,51 @@
#!/usr/bin/env bash

ROOT="$(cd "$(dirname "$0")"/.. && pwd)"
SCRIPT_DIR="$( cd -- "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
ROOT="$( "$SCRIPT_DIR"/get-root.sh )"
PREFIX="${ROOT}"/prefix
MIN_CODE_QUALITY=8.0

get_code_score() {
if [ -n "$1" ]; then
# set minimum quality to user input (int/float) if provided and (5.0 <= input <= 10.0)
if [[ $1 =~ ^([0-9]+)*([\.][0-9])?$ ]]; then
if (( $(echo "$1 >= 5.0" | bc -l) )) && (( $(echo "$1 <= 10.0" | bc -l) )); then
MIN_CODE_QUALITY=$1
else
# In the future, we want code quality to be at minimum 8.0/10.0
echo "Code quality score must be between 5.0 and 10.0, inclusive."
echo "Recommended code quality score is >= 8.0."
exit 1
fi
else
echo "Code quality score must be an integer or floating point number."
exit 1
if [ -n "$1" ]; then
# set minimum quality to user input (int/float) if provided and (5.0 <= input <= 10.0)
if [[ $1 =~ ^([0-9]+)*([\.][0-9])?$ ]]; then
if (( $(echo "$1 >= 5.0" | bc -l) )) && (( $(echo "$1 <= 10.0" | bc -l) )); then
MIN_CODE_QUALITY=$1
else
# In the future, we want code quality to be at minimum 8.0/10.0
echo "Code quality score must be between 5.0 and 10.0, inclusive."
echo "Recommended code quality score is >= 8.0."
exit 1
fi
else
echo "Code quality score must be an integer or floating point number."
exit 1
fi
fi
fi
echo "Linting Python code with minimum quality of $MIN_CODE_QUALITY/10.0..."
echo "Linting Python code with minimum quality of $MIN_CODE_QUALITY/10.0..."
}

check_pylint() {
if ! command -v pylint &>/dev/null; then
echo "pylint is not installed."
echo "Run 'sudo ./scripts/install-build-tools.sh' to install pylint."
exit 1
fi
if ! command -v pylint &>/dev/null; then
echo "pylint is not installed."
echo "Run 'sudo ./scripts/install-build-tools.sh' to install pylint."
exit 1
fi
}

get_code_score $1
if source "${ROOT}/scripts/activate-venv.sh"; then
echo "Virtual environment activated."
echo "Virtual environment activated."
else
echo "Failed to activate virtual environment."
exit 1
echo "Failed to activate virtual environment."
exit 1
fi

check_pylint
if ! pylint scripts src tests tools --rcfile=.pylintrc \
--fail-under=$MIN_CODE_QUALITY $(git ls-files '*.py'); then
--fail-under=$MIN_CODE_QUALITY $(git ls-files '*.py'); then
echo "Linting failed, please fix the issues and rerun."
exit 1
exit 1
else
echo "Linting passed."
echo "Linting passed."
fi
4 changes: 3 additions & 1 deletion scripts/setup-dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ set -e

# install in a custom prefix rather than /usr/local. by default, this
# chooses "prefix" directory alongside "scripts" directory.
PREFIX="$(cd "$(dirname "$0")"/.. && pwd)/prefix"
SCRIPT_DIR="$( cd -- "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
ROOT="$( "$SCRIPT_DIR"/get-root.sh )"
PREFIX="$ROOT"/prefix
echo "Will install local dependencies in the following prefix: $PREFIX"
mkdir -p "$PREFIX"/{lib,include}

Expand Down
7 changes: 4 additions & 3 deletions scripts/test-e2e-minikube.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env bash
set -e

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
SCRIPT_DIR="$( cd -- "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
ROOT="$( "$SCRIPT_DIR"/get-root.sh )"
MINIKUBE_PROFILE=${MINIKUBE_PROFILE:-opencbdc}
BUILD_DOCKER=${TESTRUN_BUILD_DOCKER:-1}

Expand Down Expand Up @@ -36,13 +37,13 @@ if [[ $BUILD_DOCKER -eq 1 ]]; then
fi

# Change to the test directory and fetch dependencies
cd $SCRIPT_DIR/../charts/tests
cd "$ROOT"/charts/tests
echo "🔄 Downloading Go dependencies for testing..."
go mod download -x

# Set testrun_id and path to store logs from testrun and containers
TESTRUN_ID=${TESTRUN_ID:-$(uuidgen)}
TESTRUN_PATH=$SCRIPT_DIR/../testruns/$TESTRUN_ID
TESTRUN_PATH="$ROOT"/testruns/$TESTRUN_ID
TESTRUN_LOG_PATH="$TESTRUN_PATH/testrun.log"
mkdir -p $TESTRUN_PATH

Expand Down
2 changes: 1 addition & 1 deletion scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ done
# top-level directory of the repo. By defining the top-level directory relative
# to the location of this script, the user can run this script from any folder.
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
REPO_TOP_DIR="${SCRIPT_DIR}/.."
REPO_TOP_DIR="$( "$SCRIPT_DIR"/get-root.sh )"
if [[ -z "${BUILD_DIR+x}" ]]
then
BUILD_DIR="${REPO_TOP_DIR}/build"
Expand Down

0 comments on commit 5c9ee8c

Please sign in to comment.