From 4e3cd4a15322a12159d532a92c4d93ad19c65487 Mon Sep 17 00:00:00 2001 From: Ignas Baranauskas Date: Tue, 30 Jul 2024 14:18:57 +0100 Subject: [PATCH 1/2] Add new make target to lint and format python code Signed-off-by: Ignas Baranauskas --- Makefile | 3 +++ hack/verify-python.sh | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100755 hack/verify-python.sh diff --git a/Makefile b/Makefile index 1727392003..80fc14be52 100644 --- a/Makefile +++ b/Makefile @@ -116,3 +116,6 @@ controller-gen: ## Download controller-gen locally if necessary. KUSTOMIZE = $(shell pwd)/bin/kustomize kustomize: ## Download kustomize locally if necessary. GOBIN=$(PROJECT_DIR)/bin go install sigs.k8s.io/kustomize/kustomize/v4@v4.5.7 + +fmt-python: + hack/verify-python.sh diff --git a/hack/verify-python.sh b/hack/verify-python.sh new file mode 100755 index 0000000000..a51892eaf2 --- /dev/null +++ b/hack/verify-python.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +set -o errexit +set -o nounset +set -o pipefail + +SCRIPT_ROOT=$(dirname "${BASH_SOURCE}")/.. +DIFFROOT="${SCRIPT_ROOT}/sdk" +TMP_DIFFROOT="${SCRIPT_ROOT}/_tmp/sdk" +_tmp="${SCRIPT_ROOT}/_tmp" + +cleanup() { + rm -rf "${_tmp}" +} +trap "cleanup" EXIT SIGINT + +cleanup + +mkdir -p "${TMP_DIFFROOT}" +cp -a "${DIFFROOT}"/* "${TMP_DIFFROOT}" + +black "${DIFFROOT}" --exclude '/*kubeflow_org_v1*|__init__.py|api_client.py|configuration.py|exceptions.py|rest.py' + +echo "Comparing ${DIFFROOT} with ${TMP_DIFFROOT}..." +ret=0 +diff -Naupr "${DIFFROOT}" "${TMP_DIFFROOT}" || ret=$? + +cp -a "${TMP_DIFFROOT}"/* "${DIFFROOT}" + +if [[ $ret -eq 0 ]]; then + echo "Code is properly formatted." +else + echo "Code is not properly formatted. Please run 'make fmt-python'" + exit 1 +fi + +if ! flake8 "${DIFFROOT}" --exclude='/*kubeflow_org_v1*,__init__.py,api_client.py,configuration.py,exceptions.py,rest.py'; then + echo "Code linting failed. Please fix the issues reported by flake8." + exit 1 +fi From 62c6c15c8ad33f6e83c015d282c88ded2ed49690 Mon Sep 17 00:00:00 2001 From: Ignas Baranauskas Date: Tue, 30 Jul 2024 16:16:42 +0100 Subject: [PATCH 2/2] Update action with a script for linting and formatting Signed-off-by: Ignas Baranauskas --- .github/workflows/test-python.yaml | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/.github/workflows/test-python.yaml b/.github/workflows/test-python.yaml index 3d6faae951..b78b4ee130 100644 --- a/.github/workflows/test-python.yaml +++ b/.github/workflows/test-python.yaml @@ -26,15 +26,10 @@ jobs: with: python-version: ${{ matrix.python-version }} - # TODO (andreyvelich): We need to replace this action with script to do - # linting and formatting for Training Operator SDK. - - name: Check Python code with Black - uses: psf/black@stable - with: - version: 24.2.0 - options: --check --exclude '/*kubeflow_org_v1*|__init__.py|api_client.py|configuration.py|exceptions.py|rest.py' - src: sdk/ - + - name: Check Python code with Black and Flake8 + run: | + pip install black Flake8 + make fmt-python - name: Install dependencies run: | pip install pytest python-dateutil urllib3 kubernetes