Skip to content

Commit

Permalink
Merge pull request openshift-helm-charts#272 from komish/py-codestyle…
Browse files Browse the repository at this point in the history
…-tooling

Add Python Code Style Enforcement to GitHub Actions
  • Loading branch information
komish authored Oct 13, 2023
2 parents 4de4d38 + a207421 commit 7ae469f
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/python-style.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Python Style

on:
pull_request:
paths:
# Only trigger on core script changes
- 'scripts/**.py'

jobs:
enforce:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python 3.x Part 1
uses: actions/setup-python@v4
with:
python-version: "3.9"
- name: Install style tooling
working-directory: scripts
run: make venv.codestyle
- name: Run formatter
working-directory: scripts
run: make ci.format
# Temporarily auto-pass linting until we are able to manually review and
# address.
- name: Run linter
working-directory: scripts
run: make lint || true
31 changes: 31 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,34 @@ __pycache__
sanity-check.py
.cr-release-packages/*.tgz
oc

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# Virtual Envs
venv.*/
56 changes: 56 additions & 0 deletions scripts/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
PY_BIN ?= python3

# The virtualenv containing code style tools.
VENV_CODESTYLE = venv.codestyle
VENV_CODESTYLE_BIN = $(VENV_CODESTYLE)/bin

# The virtualenv containing our CI scripts
VENV_TOOLS = venv.tools
VENV_TOOLS_BIN = $(VENV_TOOLS)/bin

# This is what we pass to git ls-files.
LS_FILES_INPUT_STR ?= 'src/*.py'

.PHONY: default
default: format lint

# The same as format, but will throw a non-zero exit code
# if the formatter had to make changes.
.PHONY: ci.format
ci.format: format
git diff --exit-code

venv.codestyle:
$(MAKE) venv.codestyle.always-reinstall

# This target will always install the codestyle venv.
# Useful for development cases.
.PHONY: venv.codestyle.always-reinstall
venv.codestyle.always-reinstall:
$(PY_BIN) -m venv $(VENV_CODESTYLE)
./$(VENV_CODESTYLE_BIN)/pip install --upgrade \
black \
ruff

.PHONY: format
format: venv.codestyle
./$(VENV_CODESTYLE_BIN)/black \
--verbose \
$$(git ls-files $(LS_FILES_INPUT_STR))

.PHONY: lint
lint: venv.codestyle
./$(VENV_CODESTYLE_BIN)/ruff \
check \
$$(git ls-files $(LS_FILES_INPUT_STR))

venv.tools:
$(MAKE) venv.tools.always-reinstall

# This target will always install the tools at the venv.
# Useful for development cases.
.PHONY: venv.tools.always-reinstall
venv.tools.always-reinstall:
$(PY_BIN) -m venv $(VENV_TOOLS)
./$(VENV_TOOLS_BIN)/pip install -r requirements.txt
./$(VENV_TOOLS_BIN)/python setup.py install
4 changes: 4 additions & 0 deletions scripts/ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ignore = [
"E402", # import ordering (komish): import ordering isn't handled by Black so we need to handle this manually.
"E501", # line length (komish): line length is not enforced by Black so we need to handle these manually.
]

0 comments on commit 7ae469f

Please sign in to comment.