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 Python Code Style Enforcement to GitHub Actions #272

Merged
merged 3 commits into from
Oct 13, 2023
Merged
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
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:
OchiengEd marked this conversation as resolved.
Show resolved Hide resolved
$(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.
]
Loading