Skip to content

Commit

Permalink
Migrate to zigpy CI and pre-commit hooks (NabuCasa#53)
Browse files Browse the repository at this point in the history
* Migrate to zigpy CI

* Migrate to zigpy pre-commit config

* Fix existing formatting

* Move workflows into the correct folder

* Fix CI for 3.8
  • Loading branch information
puddly authored Dec 22, 2023
1 parent 68e1448 commit 3dbfc51
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 215 deletions.
174 changes: 9 additions & 165 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,172 +1,16 @@
name: CI

# yamllint disable-line rule:truthy
on:
push:
pull_request: ~

env:
CACHE_VERSION: 1
DEFAULT_PYTHON: 3.9.15
PRE_COMMIT_HOME: ~/.cache/pre-commit
PYTHON_MODULE_NAME: universal_silabs_flasher

jobs:
prepare-base:
name: Prepare base dependencies
runs-on: ubuntu-latest
steps:
- name: Check out code from GitHub
uses: actions/checkout@v2

- name: Set up Python ${{ env.DEFAULT_PYTHON }}
id: python
uses: actions/setup-python@v4
with:
python-version: ${{ env.DEFAULT_PYTHON }}

- name: Restore base Python virtual environment
id: cache-venv
uses: actions/cache@v3
with:
path: venv
key: |
base-${{ env.CACHE_VERSION }}-${{ runner.os }}-${{ steps.python.outputs.python-version }}-${{ hashFiles('pyproject.toml') }}
- name: Create Python virtual environment
if: steps.cache-venv.outputs.cache-hit != 'true'
run: |
python -m venv venv
. venv/bin/activate
pip install -U pip wheel setuptools pre-commit
pip install -e '.[testing]'
pre-commit:
name: Run pre-commit
runs-on: ubuntu-latest
needs: prepare-base
steps:
- name: Check out code from GitHub
uses: actions/checkout@v2

- name: Set up Python ${{ env.DEFAULT_PYTHON }}
id: python
uses: actions/setup-python@v4
with:
python-version: ${{ env.DEFAULT_PYTHON }}

- name: Restore base Python virtual environment
id: cache-venv
uses: actions/cache@v3
with:
path: ${{ env.PRE_COMMIT_HOME }}
key: |
base-${{ env.CACHE_VERSION }}-${{ runner.os }}-${{ steps.python.outputs.python-version }}-${{ hashFiles('.pre-commit-config.yaml') }}
- name: Create Python virtual environment
if: steps.cache-venv.outputs.cache-hit != 'true'
run: |
python -m venv venv
. venv/bin/activate
pip install -U pip wheel setuptools pre-commit
pip install -e '.[testing]'
- name: Install pre-commit dependencies
if: steps.cache-precommit.outputs.cache-hit != 'true'
run: |
. venv/bin/activate
pre-commit install-hooks
- name: Run pre-commit
run: |
. venv/bin/activate
pre-commit run --show-diff-on-failure --color=always
pytest:
runs-on: ubuntu-latest
needs: prepare-base
name: Run tests
steps:
- name: Check out code from GitHub
uses: actions/checkout@v2

- name: Set up Python ${{ env.DEFAULT_PYTHON }}
id: python
uses: actions/setup-python@v4
with:
python-version: ${{ env.DEFAULT_PYTHON }}

- name: Restore base Python virtual environment
id: cache-venv
uses: actions/cache@v3
with:
path: venv
key: |
base-${{ env.CACHE_VERSION }}-${{ runner.os }}-${{ steps.python.outputs.python-version }}-${{ hashFiles('pyproject.toml') }}
- name: Fail job if Python cache restore failed
if: steps.cache-venv.outputs.cache-hit != 'true'
run: |
echo "Failed to restore Python virtual environment from cache"
exit 1
- name: Register Python problem matcher
run: |
echo "::add-matcher::.github/workflows/matchers/python.json"
- name: Run pytest
run: |
. venv/bin/activate
pytest \
-qq \
--durations=10 \
--cov ${{ env.PYTHON_MODULE_NAME }} \
-o console_output_style=count \
-p no:sugar \
tests
- name: Upload coverage artifact
uses: actions/upload-artifact@v3
with:
name: coverage-${{ env.DEFAULT_PYTHON }}
path: .coverage

coverage:
name: Process test coverage
runs-on: ubuntu-latest
needs: pytest
steps:
- name: Check out code from GitHub
uses: actions/checkout@v2

- name: Set up Python ${{ env.DEFAULT_PYTHON }}
id: python
uses: actions/setup-python@v4
with:
python-version: ${{ env.DEFAULT_PYTHON }}

- name: Restore base Python virtual environment
id: cache-venv
uses: actions/cache@v3
with:
path: venv
key: |
base-${{ env.CACHE_VERSION }}-${{ runner.os }}-${{ steps.python.outputs.python-version }}-${{ hashFiles('pyproject.toml') }}
- name: Fail job if Python cache restore failed
if: steps.cache-venv.outputs.cache-hit != 'true'
run: |
echo "Failed to restore Python virtual environment from cache"
exit 1
- name: Download all coverage artifacts
uses: actions/download-artifact@v3

- name: Combine coverage results
run: |
. venv/bin/activate
coverage combine coverage*/.coverage*
coverage report
coverage xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
shared-ci:
uses: zigpy/workflows/.github/workflows/ci.yml@main
with:
CODE_FOLDER: universal_silabs_flasher
CACHE_VERSION: 2
PYTHON_VERSION_DEFAULT: 3.8.14
PRE_COMMIT_CACHE_PATH: ~/.cache/pre-commit
MINIMUM_COVERAGE_PERCENTAGE: 40
16 changes: 16 additions & 0 deletions .github/workflows/matchers/codespell.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"problemMatcher": [
{
"owner": "codespell",
"severity": "warning",
"pattern": [
{
"regexp": "^(.+):(\\d+):\\s(.+)$",
"file": 1,
"line": 2,
"message": 3
}
]
}
]
}
30 changes: 30 additions & 0 deletions .github/workflows/matchers/flake8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"problemMatcher": [
{
"owner": "flake8-error",
"severity": "error",
"pattern": [
{
"regexp": "^(.*):(\\d+):(\\d+):\\s([EF]\\d{3}\\s.*)$",
"file": 1,
"line": 2,
"column": 3,
"message": 4
}
]
},
{
"owner": "flake8-warning",
"severity": "warning",
"pattern": [
{
"regexp": "^(.*):(\\d+):(\\d+):\\s([CDNW]\\d{3}\\s.*)$",
"file": 1,
"line": 2,
"column": 3,
"message": 4
}
]
}
]
}
12 changes: 12 additions & 0 deletions .github/workflows/publish-to-pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Publish distributions to PyPI

on:
release:
types:
- published

jobs:
shared-build-and-publish:
uses: zigpy/workflows/.github/workflows/publish-to-pypi.yml@main
secrets:
PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
30 changes: 0 additions & 30 deletions .github/workflows/pypi.yml

This file was deleted.

55 changes: 46 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,53 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
hooks:
- id: debug-statements
- id: pyupgrade
args: [--py38-plus]

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: 'v0.0.272'
- repo: https://github.com/PyCQA/autoflake
rev: v2.0.2
hooks:
- id: ruff
args: ["--fix"]
- id: autoflake

- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black
args:
- --quiet

- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
additional_dependencies:
- Flake8-pyproject==1.2.3

- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort

- repo: https://github.com/codespell-project/codespell
rev: v2.2.4
hooks:
- id: codespell
args:
- --ignore-words-list=zigpy,hass
- --skip="./.*"
- --quiet-level=2

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.1.1
rev: v1.3.0
hooks:
- id: mypy
- id: mypy
additional_dependencies:
- zigpy

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.261
hooks:
- id: ruff
args:
- --fix
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ $ universal-silabs-flasher \

The IEEE address can also be specified without colons: `--ieee 003c84fffe92bb2c`.

If the current device's IEEE address already matches the provided one, the command will not write it unnecesarily.
If the current device's IEEE address already matches the provided one, the command will not write it unnecessarily.
26 changes: 20 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,10 @@ select = [
"F",
# Pycodestyle
"E",
"W",
# isort
"I001"
"W"
]
src = ["universal_silabs_flasher", "tests"]

[tool.ruff.isort]
known-first-party = ["universal_silabs_flasher", "tests"]

[tool.pytest.ini_options]
addopts = "--showlocals --verbose"
testpaths = ["tests"]
Expand All @@ -75,6 +70,25 @@ disable_error_code = [
"arg-type",
]

[tool.isort]
profile = "black"
# will group `import x` and `from x import` of the same module.
force_sort_within_sections = true
known_first_party = ["universal_silabs_flasher", "tests"]
forced_separate = "tests"
combine_as_imports = true

[tool.flake8]
exclude = [".venv", ".git", ".tox", "docs", "venv", "bin", "lib", "deps", "build"]
# To work with Black
max-line-length = 88
# W503: Line break occurred before a binary operator
# E203: Whitespace before ':'
# E501: line too long
# D202 No blank lines allowed after function docstring
ignore = ["W503", "E203", "E501", "D202"]
per-file-ignores = ["tests/*:F811,F401,F403"]

[tool.coverage.run]
source = ["universal_silabs_flasher"]

Expand Down
Loading

0 comments on commit 3dbfc51

Please sign in to comment.