From 9993a6b40f0a2bbe0de01479de5234df92d6ef5d Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Mon, 17 Mar 2025 08:00:22 +0100 Subject: [PATCH] Initial commit. --- install.sh | 4 +++ lintplom | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100755 install.sh create mode 100755 lintplom diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..576ceef --- /dev/null +++ b/install.sh @@ -0,0 +1,4 @@ +#!/bin/sh +PATH_LOCAL_BIN="${HOME}/.local/bin" +mkdir -p "${PATH_LOCAL_BIN}" +cp lintplom "${PATH_LOCAL_BIN}/" diff --git a/lintplom b/lintplom new file mode 100755 index 0000000..9a4799c --- /dev/null +++ b/lintplom @@ -0,0 +1,75 @@ +#!/bin/sh +set -e + +# check existence of file, ensure absoluteness of path +if [ "$?" -ne 1 ]; then + echo "usage: lintplom FILEPATH" + echo "" + echo "arguments:" + echo " FILEPATH - source file to run Python linters against" + exit 1 +fi + +# check existence of file, ensure absoluteness of path +TARGET="$1" +if [ ! -f "$1" ]; then + echo "Must provide valid file path as argument." + exit 1 +fi +if [ $(echo "${TARGET}" | cut -c1) != '/' ]; then + TARGET="$(pwd)/${TARGET}" +fi + +# find closest parent directory with requirements listing, limit search to +# directories below user directory +if [ $(echo "${TARGET}" | cut -d'/' -f'1-3') != "${HOME}" ]; then + echo "Target outside user home directory." + exit 1 +fi +FILE_REQS=requirements.txt +DIR_REQS= +cd "$(dirname ${TARGET})" +while true; do + if [ "$(pwd)" = "${HOME}" ]; then + break + elif [ -f "${FILE_REQS}" ]; then + DIR_REQS="$(pwd)" + break + fi + cd .. +done + +# decide venv directory specific to directory found to hold requirements +# listing, or if none found specific to user's home directory +DIR_VENV_BASE=/tmp/venv_lintplom +if [ -z "${DIR_REQS}" ]; then + DIR_VENV="${DIR_VENV_BASE}/_any/${HOME}" +else + DIR_VENV="${DIR_VENV_BASE}${DIR_REQS}" +fi + +# give some basic feedback +echo "Linting ${TARGET} using venv environment at ${DIR_VENV} – but first, checking for and installing missing dependencies." +if [ -z "${DIR_REQS}" ]; then + echo "(No ${FILE_REQS} found, so nothing to provide besides linters themselves.)" +fi + +# ensure venv environment at result of previous decision, install reqs +if [ ! -d "${DIR_VENV}" ]; then + python3 -m venv "${DIR_VENV}" +fi +. "${DIR_VENV}/bin/activate" +pip3 -q install flake8 pylint mypy +. "${DIR_VENV}/bin/activate" +if [ ! -z "${DIR_REQS}" ]; then + pip3 -q install -r "${DIR_REQS}/${FILE_REQS}" +fi + +# run actual tests +set +e +echo "\n\n============================[ FLAKE results ]==================================\n" +flake8 "${TARGET}" +echo "\n\n============================[ PYLINT results ]=================================\n" +pylint "${TARGET}" +echo "\n\n============================[ MYPY results ]===================================\n" +mypy "${TARGET}" -- 2.30.2