diff --git a/.github/workflows/python-style.yml b/.github/workflows/python-style.yml new file mode 100644 index 00000000..4a7c34b2 --- /dev/null +++ b/.github/workflows/python-style.yml @@ -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 \ No newline at end of file diff --git a/.gitignore b/.gitignore index 37f20ed8..666a69ab 100644 --- a/.gitignore +++ b/.gitignore @@ -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.*/ \ No newline at end of file diff --git a/scripts/Makefile b/scripts/Makefile new file mode 100644 index 00000000..eed6497c --- /dev/null +++ b/scripts/Makefile @@ -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 diff --git a/scripts/ruff.toml b/scripts/ruff.toml new file mode 100644 index 00000000..af87f9db --- /dev/null +++ b/scripts/ruff.toml @@ -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. +] \ No newline at end of file