Skip to content

Commit

Permalink
Add lint scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
nvatuan committed Jan 13, 2024
1 parent bea1a6d commit dc26680
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 63 deletions.
9 changes: 5 additions & 4 deletions scripts/lint/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ FROM python:3.8-slim-buster

#
RUN apt-get update && apt-get install -y \
libpq-dev \
python3-dev \
build-essential
build-essential \
git

RUN pip install pylint==2.15.9 pylint-django==2.5.3 pylint-plugin-utils==0.7
# Match the version of Django in your project

RUN pip install backports.zoneinfo
RUN pip install Django==4.0.4
COPY app/requirements.txt /tmp/requirements.txt
RUN pip install -r /tmp/requirements.txt
55 changes: 9 additions & 46 deletions scripts/lint/lint.sh
Original file line number Diff line number Diff line change
@@ -1,57 +1,20 @@
#!/bin/bash
# Loads necessaries
set -e # Exit on error

source ./scripts/utils/prettyecho.sh
source ./scripts/lint/setupenv.sh

VERBOSE=false
KEEP_CONTAINER=false
# Parse options
while getopts "v" opt; do
case $opt in
v)
VERBOSE=true
;;
d)
KEEP_CONTAINER=true
;;
\?)
efatal "Lint" "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done

# Tearing down
spin_up
if [ $? -ne 0 ]; then
efatal 0 "Lint" "Failed to spin up container."
exit 1
fi

# Code
einfo 0 "Lint" "ERRORS ONLY MODE. Ignoring warnings and bad conventions.."
CLNORMAL=$(tput sgr0)
CLCYAN=$(tput setaf 6)

component=$1
if [ component = "" ]; then
component="app"
else
component="app/$component"
fi
einfo 1 "Lint" "Linting $component..."

docker exec -t $TMP_LINTER_NAME pylint --output-format=colorized \
--django-settings-module=bkdnoj.settings \
--load-plugins pylint_django \
--ignore=migrations \
--errors-only \
$component
echo "${CLCYAN} * [ LINT ] Linting $component...${CLNORMAL}"

# Tearing down
if [ "$KEEP_CONTAINER" = "false" ]; then
einfo 0 "Lint" "Tearing down temporary container for lint..."
tear_down
else
einfo 0 "Lint" "Keeping container."
fi
pylint --output-format=colorized \
--django-settings-module=bkdnoj.settings \
--load-plugins pylint_django \
--ignore=migrations \
--errors-only \
$component
59 changes: 59 additions & 0 deletions scripts/lint/lintdock.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash
# Loads necessaries
set -e # Exit on error

source ./scripts/utils/prettyecho.sh

source ./scripts/lint/setupenv.sh
# Defines
# # $TMP_LINTER_NAME
# # $TMP_LINTER_IMAGE_NAME
# # $CONTAINER_WORKDIR


VERBOSE=false
KEEP_CONTAINER=false
LINT_TARGET=""
# Parse options
while getopts "vdt" opt; do
case $opt in
v)
VERBOSE=true
;;
d)
KEEP_CONTAINER=true
;;
t)
LINT_TARGET=$OPTARG
;;
\?)
efatal "Lint" "Invalid option: -$OPTARG" >&2
einfo "Usage $0 [-v] [-d]"
echo " -v: To enable verbose"
echo " -d: To keep container after linting"
exit 1
;;
esac
done

einfo 0 "LINT OPTIONS" "VERBOSE=$VERBOSE, KEEP_CONTAINER=$KEEP_CONTAINER, LINT_TARGET='$LINT_TARGET'"

# Tearing down
spin_up
if [ $? -ne 0 ]; then
efatal 0 "Lint" "Failed to spin up container."
exit 1
fi

# Code
einfo 0 "Lint" "ERRORS ONLY MODE. Ignoring warnings and bad conventions.."

docker exec -t $TMP_LINTER_NAME bash $CONTAINER_WORKDIR/scripts/lint/lint.sh $LINT_TARGET

# Tearing down
if [ "$KEEP_CONTAINER" = "false" ]; then
einfo 0 "Lint" "Tearing down temporary container for lint..."
tear_down
else
einfo 0 "Lint" "Keeping container."
fi
24 changes: 11 additions & 13 deletions scripts/lint/setupenv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,37 @@ TMP_LINTER_IMAGE_NAME="bkdnoj-lint-image:local"
CONTAINER_WORKDIR="/usr/app"

spin_up() {
set +e

echo ""
einfo 1 "Lint | Setup" "Spinning up temporary container for lint..."
einfo 1 "SETUP" "Spinning up temporary container for lint..."

# Check if container is already running
einfo 2 "Lint | Setup" "Checking if container is already running..."
einfo 2 "SETUP" "Checking if container is already running..."

local msg=$(docker inspect $TMP_LINTER_NAME 2> /dev/null);
if [ "$msg" != "[]" ]; then
ewarn 2 "Lint | Setup" "Container is already running. Skipping..."
ewarn 2 "SETUP" "Container is already running. Skipping..."
return 0
else
einfo 2 "Lint | Setup" "Container is not running."
einfo 2 "SETUP" "Container is not running."
fi

# Container is not up, spin up a new one
einfo 2 "Lint | Setup" "Building image..."
docker build -t $TMP_LINTER_IMAGE_NAME -f ./scripts/lint/Dockerfile .
einfo 2 "SETUP" "Building image..."
set -e
docker build --progress=plain -t $TMP_LINTER_IMAGE_NAME -f ./scripts/lint/Dockerfile .
set +e

einfo 2 "Lint | Setup" "Running container in the background..."
einfo 2 "SETUP" "Running container in the background..."
docker run -t -d --name $TMP_LINTER_NAME -v ".:$CONTAINER_WORKDIR" -w $CONTAINER_WORKDIR \
$TMP_LINTER_IMAGE_NAME bash

esucceed 2 "Lint | Setup" "Container is ready."
esucceed 2 "SETUP" "Container is ready."

echo ""
set -e
}

tear_down() {
echo ""
einfo 1 "Lint" "Tearing down temporary container for lint..."
einfo 1 "SETUP" "Tearing down temporary container for lint..."
docker rm -f $TMP_LINTER_NAME
echo ""
}

0 comments on commit dc26680

Please sign in to comment.