Skip to content

Commit

Permalink
Improve lint
Browse files Browse the repository at this point in the history
  • Loading branch information
nvatuan committed Dec 31, 2023
1 parent fca002c commit 4834c27
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 31 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Virtual envs
venv/

# User-custom files
uwsgi.ini

Expand Down
23 changes: 0 additions & 23 deletions scripts/lint.sh

This file was deleted.

12 changes: 12 additions & 0 deletions scripts/lint/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM python:3.8-slim-buster

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

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
57 changes: 57 additions & 0 deletions scripts/lint/lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/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.."

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

# 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
45 changes: 45 additions & 0 deletions scripts/lint/setupenv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash

source ./scripts/utils/prettyecho.sh

TMP_LINTER_NAME="bkdnoj-lint-tmp"
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..."

# Check if container is already running
einfo 2 "Lint | 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..."
return 0
else
einfo 2 "Lint | 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 "Lint | 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."

echo ""
set -e
}

tear_down() {
echo ""
einfo 1 "Lint" "Tearing down temporary container for lint..."
docker rm -f $TMP_LINTER_NAME
echo ""
}
2 changes: 2 additions & 0 deletions scripts/setup/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash

17 changes: 17 additions & 0 deletions scripts/utils/colors.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

CLBLACK=$(tput setaf 0)
CLRED=$(tput setaf 1)
CLGREEN=$(tput setaf 2)
CLYELLOW=$(tput setaf 3)
CLLIME_YELLOW=$(tput setaf 190)
CLPOWDER_BLUE=$(tput setaf 153)
CLBLUE=$(tput setaf 4)
CLMAGENTA=$(tput setaf 5)
CLCYAN=$(tput setaf 6)
CLWHITE=$(tput setaf 7)
CLBRIGHT=$(tput bold)
CLNORMAL=$(tput sgr0)
CLBLINK=$(tput blink)
CLREVERSE=$(tput smso)
CLUNDERLINE=$(tput smul)
53 changes: 45 additions & 8 deletions scripts/utils/prettyecho.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,54 @@
#!/bin/bash

echo_red() {
echo -e "---- \e[1;31m[$1]\e[0m $2"
source ./scripts/utils/colors.sh

ecolor() {
# Check for VERBOSE env var
if [ "$VERBOSE" = "false" ]; then
return 0
fi

local color=$1
local depth=$2
local headmsg=$3
local mainmsg=$4

if [[ -z "$color" || -z "$depth" || -z "$headmsg" || -z "$mainmsg" ]]; then
echo "Usage: ecolor <color> <depth> <headmsg> <mainmsg>"
echo " - <color> refers to the color of the head message. Check ./scripts/utils/colors.sh"
echo " - <depth> indent the message by <depth> spaces, must be a number."
echo " - <headmsg> is message to be wrapped in [HEADMSG]"
echo " - <mainmsg> is message after [HEADMSG]"
return 1
fi

if [[ ! $depth =~ ^[0-9]+$ ]]; then
echo "Usage: ecolor <color> <depth> <headmsg> <mainmsg>"
echo " - error: depth must be a number"
return 1
fi

local indent="${color}$(printf "%${depth}s" | tr ' ' '-')"
echo -e "${indent}[${headmsg}]${CLNORMAL} ${mainmsg}"
return 0
}

efatal() {
ecolor "$CLRED" "$1" "$2" "$3"
}

ewarn() {
ecolor "$CLYELLOW" "$1" "$2" "$3"
}

echo_green() {
echo -e "---- \e[1;32m[$1]\e[0m $2"
esucceed() {
ecolor "$CLGREEN" "$1" "$2" "$3"
}

echo_cyan() {
echo -e "---- \e[1;34m[$1]\e[0m $2"
einfo() {
ecolor "$CLCYAN" "$1" "$2" "$3"
}

echo_blue() {
echo -e "---- \e[1;36m[$1]\e[0m $2"
edebug() {
ecolor "$CLBLUE" "$1" "$2" "$3"
}

0 comments on commit 4834c27

Please sign in to comment.