From 4834c272b9060677b95d086211c37d6edce05de0 Mon Sep 17 00:00:00 2001 From: "Touya (nvatuan)" Date: Sun, 31 Dec 2023 17:01:16 +0900 Subject: [PATCH] Improve lint --- .gitignore | 3 ++ scripts/lint.sh | 23 --------------- scripts/lint/Dockerfile | 12 ++++++++ scripts/lint/lint.sh | 57 +++++++++++++++++++++++++++++++++++++ scripts/lint/setupenv.sh | 45 +++++++++++++++++++++++++++++ scripts/setup/setup.sh | 2 ++ scripts/utils/colors.sh | 17 +++++++++++ scripts/utils/prettyecho.sh | 53 ++++++++++++++++++++++++++++------ 8 files changed, 181 insertions(+), 31 deletions(-) delete mode 100755 scripts/lint.sh create mode 100644 scripts/lint/Dockerfile create mode 100755 scripts/lint/lint.sh create mode 100755 scripts/lint/setupenv.sh create mode 100644 scripts/setup/setup.sh create mode 100644 scripts/utils/colors.sh diff --git a/.gitignore b/.gitignore index 227c29a..9f8c0da 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# Virtual envs +venv/ + # User-custom files uwsgi.ini diff --git a/scripts/lint.sh b/scripts/lint.sh deleted file mode 100755 index 02e27a3..0000000 --- a/scripts/lint.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/bash -# Loads necessaries -set -e # Exit on error -source ./scripts/utils/prettyecho.sh - -# Code - -component=$1 -if [ component = "" ]; then - component="app" -else - component="app/$component" -fi - -echo_cyan "LINTING" "ERRORS ONLY MODE. Ignoring warnings and bad conventions.." -echo_cyan "LINTING" "bkdnOJ.v2 $component..." - -pylint --output-format=colorized \ - --django-settings-module=bkdnoj.settings \ - --load-plugins pylint_django \ - --ignore=migrations \ - --errors-only \ - $component diff --git a/scripts/lint/Dockerfile b/scripts/lint/Dockerfile new file mode 100644 index 0000000..d1457a9 --- /dev/null +++ b/scripts/lint/Dockerfile @@ -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 diff --git a/scripts/lint/lint.sh b/scripts/lint/lint.sh new file mode 100755 index 0000000..eba7efa --- /dev/null +++ b/scripts/lint/lint.sh @@ -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 \ No newline at end of file diff --git a/scripts/lint/setupenv.sh b/scripts/lint/setupenv.sh new file mode 100755 index 0000000..79ecd67 --- /dev/null +++ b/scripts/lint/setupenv.sh @@ -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 "" +} diff --git a/scripts/setup/setup.sh b/scripts/setup/setup.sh new file mode 100644 index 0000000..05a7907 --- /dev/null +++ b/scripts/setup/setup.sh @@ -0,0 +1,2 @@ +#!/bin/bash + diff --git a/scripts/utils/colors.sh b/scripts/utils/colors.sh new file mode 100644 index 0000000..4c942c3 --- /dev/null +++ b/scripts/utils/colors.sh @@ -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) \ No newline at end of file diff --git a/scripts/utils/prettyecho.sh b/scripts/utils/prettyecho.sh index bf73938..9525968 100644 --- a/scripts/utils/prettyecho.sh +++ b/scripts/utils/prettyecho.sh @@ -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 " + echo " - refers to the color of the head message. Check ./scripts/utils/colors.sh" + echo " - indent the message by spaces, must be a number." + echo " - is message to be wrapped in [HEADMSG]" + echo " - is message after [HEADMSG]" + return 1 + fi + + if [[ ! $depth =~ ^[0-9]+$ ]]; then + echo "Usage: ecolor " + 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" } \ No newline at end of file