Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GitHub Actions Workflow for Python Code Formatting and Linting #2191

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions .github/workflows/test-python.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]

fmt-python:
hack/verify-python.sh
40 changes: 40 additions & 0 deletions hack/verify-python.sh
Original file line number Diff line number Diff line change
@@ -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
Loading