Skip to content

Commit 6c2999e

Browse files
committed
feat: created standard way to get project root; fixes #304
Signed-off-by: Morgan Rockett <[email protected]>
1 parent cf9060e commit 6c2999e

12 files changed

+83
-45
lines changed

.dockerignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ charts/
1313

1414
# Prevent copying scripts that are unused in docker
1515
scripts/
16-
!scripts/test.sh
1716
!scripts/activate-venv.sh
17+
!scripts/get-root.sh
1818
!scripts/install-build-tools.sh
1919
!scripts/setup-dependencies.sh
20+
!scripts/test.sh
2021
!scripts/test-transaction.sh
2122
!scripts/wait-for-it.sh
2223

Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ RUN mkdir -p /opt/tx-processor/scripts
1616

1717
COPY requirements.txt /opt/tx-processor/requirements.txt
1818
COPY scripts/activate-venv.sh /opt/tx-processor/scripts/activate-venv.sh
19+
COPY scripts/get-root.sh /opt/tx-processor/scripts/get-root.sh
1920
COPY scripts/install-build-tools.sh /opt/tx-processor/scripts/install-build-tools.sh
2021
COPY scripts/setup-dependencies.sh /opt/tx-processor/scripts/setup-dependencies.sh
2122

scripts/benchmarks.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ done
6262
# environment variable, assume it's named 'build' and is located in the
6363
# top-level directory of the repo. By defining the top-level directory relative
6464
# to the location of this script, the user can run this script from any folder.
65-
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
66-
REPO_TOP_DIR="${SCRIPT_DIR}/.."
65+
SCRIPT_DIR="$( cd -- "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
66+
REPO_TOP_DIR="$( "$SCRIPT_DIR"/get-root.sh )"
6767
if [[ -z "${BUILD_DIR+x}" ]]
6868
then
6969
BUILD_DIR="${REPO_TOP_DIR}/build"

scripts/build-docker.sh

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
set -e
44

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

1415
# Build docker image
15-
docker build --target base -t $DOCKER_IMAGE_TAG_BASE -f $SCRIPT_DIR/../Dockerfile $SCRIPT_DIR/..
16-
docker build --target builder --build-arg BASE_IMAGE=base -t $DOCKER_IMAGE_TAG_BUILDER -f $SCRIPT_DIR/../Dockerfile $SCRIPT_DIR/..
17-
docker build --target twophase --build-arg BASE_IMAGE=base -t $DOCKER_IMAGE_TAG_TWOPHASE -f $SCRIPT_DIR/../Dockerfile $SCRIPT_DIR/..
18-
docker build --target atomizer --build-arg BASE_IMAGE=base -t $DOCKER_IMAGE_TAG_ATOMIZER -f $SCRIPT_DIR/../Dockerfile $SCRIPT_DIR/..
16+
docker build --target base -t $DOCKER_IMAGE_TAG_BASE -f "$ROOT"/Dockerfile "$ROOT"
17+
docker build --target builder --build-arg BASE_IMAGE=base -t $DOCKER_IMAGE_TAG_BUILDER -f "$ROOT"/Dockerfile "$ROOT"
18+
docker build --target twophase --build-arg BASE_IMAGE=base -t $DOCKER_IMAGE_TAG_TWOPHASE -f "$ROOT"/Dockerfile "$ROOT"
19+
docker build --target atomizer --build-arg BASE_IMAGE=base -t $DOCKER_IMAGE_TAG_ATOMIZER -f "$ROOT"/Dockerfile "$ROOT"

scripts/build.sh

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ fi
3333

3434
echo "Building..."
3535

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

3941
if [ -z ${BUILD_DIR+x} ]; then
4042
export BUILD_DIR=build

scripts/get-root.sh

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
# calling scripts can use this script to capture the project ROOT dir
5+
# SCRIPT_DIR="$( cd -- "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
6+
# ROOT="$( "$SCRIPT_DIR"/get-root.sh )"
7+
8+
# returns the fullpath to the ROOT dir without a slash at the end
9+
ROOT=
10+
# if we are in a git repository
11+
if git rev-parse --show-toplevel >/dev/null 2>&1; then
12+
ROOT="$( git rev-parse --show-toplevel )"
13+
else
14+
# $BASH_SOURCE[0] is the full path of the script being executed
15+
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd )"
16+
# scripts dir will always be one level below ROOT level: "$ROOT"/scripts
17+
ROOT="$( cd -- "$SCRIPT_DIR"/.. && pwd )"
18+
fi
19+
20+
# check for directory existence
21+
if [[ ! -d "$ROOT" ]]; then
22+
echo "Error: ROOT '${ROOT}' not found."
23+
exit 1
24+
fi
25+
26+
# use other scripts in the script dir to capture the ROOT fullpath
27+
printf "%s\n" "$ROOT"
28+

scripts/install-build-tools.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ for PY_VERS in "${PYTHON_VERSIONS[@]}"; do
3030
fi
3131
done
3232

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

3637
# make a virtual environement to install python packages

scripts/native-system-benchmark.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ DBG="${DBG:-$GDB}"
1010
DURATION=30
1111
CWD=$(pwd)
1212
COMMIT=$(git rev-parse --short HEAD)
13-
TL=$(git rev-parse --show-toplevel)
14-
RT="${TL:-$CWD}"
13+
SCRIPT_DIR="$( cd -- "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
14+
RT="$( "$SCRIPT_DIR"/get-root.sh )"
1515
BLD="$RT"/build
1616
SEEDDIR="$BLD"/preseeds
1717
TESTDIR="$BLD"/test-$(date +"%s")

scripts/pylint.sh

+29-28
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,51 @@
11
#!/usr/bin/env bash
22

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

78
get_code_score() {
8-
if [ -n "$1" ]; then
9-
# set minimum quality to user input (int/float) if provided and (5.0 <= input <= 10.0)
10-
if [[ $1 =~ ^([0-9]+)*([\.][0-9])?$ ]]; then
11-
if (( $(echo "$1 >= 5.0" | bc -l) )) && (( $(echo "$1 <= 10.0" | bc -l) )); then
12-
MIN_CODE_QUALITY=$1
13-
else
14-
# In the future, we want code quality to be at minimum 8.0/10.0
15-
echo "Code quality score must be between 5.0 and 10.0, inclusive."
16-
echo "Recommended code quality score is >= 8.0."
17-
exit 1
18-
fi
19-
else
20-
echo "Code quality score must be an integer or floating point number."
21-
exit 1
9+
if [ -n "$1" ]; then
10+
# set minimum quality to user input (int/float) if provided and (5.0 <= input <= 10.0)
11+
if [[ $1 =~ ^([0-9]+)*([\.][0-9])?$ ]]; then
12+
if (( $(echo "$1 >= 5.0" | bc -l) )) && (( $(echo "$1 <= 10.0" | bc -l) )); then
13+
MIN_CODE_QUALITY=$1
14+
else
15+
# In the future, we want code quality to be at minimum 8.0/10.0
16+
echo "Code quality score must be between 5.0 and 10.0, inclusive."
17+
echo "Recommended code quality score is >= 8.0."
18+
exit 1
19+
fi
20+
else
21+
echo "Code quality score must be an integer or floating point number."
22+
exit 1
23+
fi
2224
fi
23-
fi
24-
echo "Linting Python code with minimum quality of $MIN_CODE_QUALITY/10.0..."
25+
echo "Linting Python code with minimum quality of $MIN_CODE_QUALITY/10.0..."
2526
}
2627

2728
check_pylint() {
28-
if ! command -v pylint &>/dev/null; then
29-
echo "pylint is not installed."
30-
echo "Run 'sudo ./scripts/install-build-tools.sh' to install pylint."
31-
exit 1
32-
fi
29+
if ! command -v pylint &>/dev/null; then
30+
echo "pylint is not installed."
31+
echo "Run 'sudo ./scripts/install-build-tools.sh' to install pylint."
32+
exit 1
33+
fi
3334
}
3435

3536
get_code_score $1
3637
if source "${ROOT}/scripts/activate-venv.sh"; then
37-
echo "Virtual environment activated."
38+
echo "Virtual environment activated."
3839
else
39-
echo "Failed to activate virtual environment."
40-
exit 1
40+
echo "Failed to activate virtual environment."
41+
exit 1
4142
fi
4243

4344
check_pylint
4445
if ! pylint scripts src tests tools --rcfile=.pylintrc \
45-
--fail-under=$MIN_CODE_QUALITY $(git ls-files '*.py'); then
46+
--fail-under=$MIN_CODE_QUALITY $(git ls-files '*.py'); then
4647
echo "Linting failed, please fix the issues and rerun."
47-
exit 1
48+
exit 1
4849
else
49-
echo "Linting passed."
50+
echo "Linting passed."
5051
fi

scripts/setup-dependencies.sh

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ set -e
1010

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

scripts/test-e2e-minikube.sh

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/usr/bin/env bash
22
set -e
33

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

@@ -36,13 +37,13 @@ if [[ $BUILD_DOCKER -eq 1 ]]; then
3637
fi
3738

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

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

scripts/test.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ done
114114
# top-level directory of the repo. By defining the top-level directory relative
115115
# to the location of this script, the user can run this script from any folder.
116116
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
117-
REPO_TOP_DIR="${SCRIPT_DIR}/.."
117+
REPO_TOP_DIR="$( "$SCRIPT_DIR"/get-root.sh )"
118118
if [[ -z "${BUILD_DIR+x}" ]]
119119
then
120120
BUILD_DIR="${REPO_TOP_DIR}/build"

0 commit comments

Comments
 (0)