Skip to content

Commit

Permalink
Fix script in Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
noliveleger committed Sep 10, 2024
1 parent 5ce6eab commit f9846a0
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions format-python.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@

BASE_REVISION=$1

if [ -n "${UWSGI_USER}" ] && [ "${DEBIAN_FRONTEND}" == "noninteractive" ] && [ "${TMP_DIR}" == "/srv/tmp" ]; then
INSIDE_CONTAINER=1
else
INSIDE_CONTAINER=0
fi

if [ -z "$BASE_REVISION" ]; then
BASE_REVISION="origin/beta"
elif [ "$BASE_REVISION" == "-l" ] || [ "$BASE_REVISION" == "--last" ]; then
BASE_REVISION=$(git log --oneline| head -n 1 | awk '{ print $1}')
if [ "$INSIDE_CONTAINER" == "1" ]; then
BASE_REVISION=$(gosu "$UWSGI_USER" git log --oneline| head -n 1 | awk '{ print $1}')
else
BASE_REVISION=$(git log --oneline| head -n 1 | awk '{ print $1}')
fi
fi

# First, do not touch formatting but fix:
Expand All @@ -17,15 +27,29 @@ fi
# - Unnecessary parentheses after class definition (--select UP039)
# - Indentation warning (--select W1)
# - No newline at end of file (--select W292)
PYTHON_CHANGES=$(git diff --name-only "$BASE_REVISION" | grep '\.py')
if [ "$INSIDE_CONTAINER" == "1" ]; then
PYTHON_CHANGES=$(gosu "$UWSGI_USER" git diff --name-only "$BASE_REVISION" | grep '\.py')
else
PYTHON_CHANGES=$(git diff --name-only "$BASE_REVISION" | grep '\.py')
fi

if [ -n "$PYTHON_CHANGES" ]; then
echo "Using ruff..."
git diff --name-only "$BASE_REVISION" | grep '\.py' | xargs ruff check --select Q --select I --select F401 --select UP026 --select UP034 --select UP039 --select W292 --fix
if [ "$INSIDE_CONTAINER" == "1" ]; then
gosu "$UWSGI_USER" git diff --name-only "$BASE_REVISION" | grep '\.py' | xargs ruff check --select Q --select I --select F401 --select UP026 --select UP034 --select UP039 --select W292 --fix
else
git diff --name-only "$BASE_REVISION" | grep '\.py' | xargs ruff check --select Q --select I --select F401 --select UP026 --select UP034 --select UP039 --select W292 --fix
fi

# Applying Black format on changes
# Applying Black format with `darker` with options:
# --isort: Using isort
# --revision: Compare changes with revision $BASE_REVISION
echo "Using darker..."
# Use `isort` and compare changes with revision $BASE_REVISION
darker --isort --revision "$BASE_REVISION"
if [ "$INSIDE_CONTAINER" == "1" ]; then
gosu "$UWSGI_USER" darker --isort --revision "$BASE_REVISION"
else
darker --isort --revision "$BASE_REVISION"
fi
else
echo "No Python changes detected!"
fi

0 comments on commit f9846a0

Please sign in to comment.