From 8b53e787596408c7bd449cfdd8c87b7e6b8efc79 Mon Sep 17 00:00:00 2001 From: Olivier Leger Date: Mon, 9 Sep 2024 17:01:52 -0400 Subject: [PATCH] Utility to format Python code before pushing new commits --- format-python.sh | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100755 format-python.sh diff --git a/format-python.sh b/format-python.sh new file mode 100755 index 0000000000..17da96ef1e --- /dev/null +++ b/format-python.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +BASE_REVISION=$1 + +echo $BASE_REVISION + + +if [ -z "$BASE_REVISION" ]; then + BASE_REVISION="beta" +elif [ "$BASE_REVISION" == "-l" ] || [ "$BASE_REVISION" == "--last" ]; then + BASE_REVISION=$(git log --oneline| head -n 1 | awk '{ print $1}') +fi + +# First, do not touch formatting but fix: +# - single/double quotes (--select Q) +# - sort imports (--select I) +# - unused imports (--select F401) +# - deprecated `mock` (--select UP026) +# - extraneous-parentheses (--select UP034) +# - Unnecessary parentheses after class definition (--select UP039) +# - Indentation warning (--select W1) +# - No newline at end of file (--select W292) +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 + +# Applying Black format on changes (since $BASE_REVISION) +echo "Using darker..." +darker -i -r "$BASE_REVISION"