Skip to content

Commit 039d012

Browse files
Merge pull request #20 from brainelectronics/feature/add-pre-commit-hooks
Add pre commit hooks
2 parents e329cf1 + a1db6cb commit 039d012

11 files changed

+281
-22
lines changed

.github/workflows/test.yml

+16-10
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,17 @@ jobs:
4040
- name: Lint with yamllint
4141
run: |
4242
yamllint .
43-
- name: Install deploy dependencies
44-
run: |
45-
python -m pip install --upgrade pip
46-
if [ -f requirements-deploy.txt ]; then pip install -r requirements-deploy.txt; fi
47-
- name: Build package
43+
- name: Validate package version file
44+
# the package version file has to be always up to date as mip is using
45+
# the file directly from the repo. On a PyPi package the version file
46+
# is updated and then packed
4847
run: |
4948
changelog2version \
5049
--changelog_file changelog.md \
5150
--version_file be_upy_blink/version.py \
5251
--version_file_type py \
52+
--validate \
5353
--debug
54-
python setup.py sdist
55-
rm dist/*.orig
56-
- name: Test built package
57-
run: |
58-
twine check dist/*
5954
- name: Validate mip package file
6055
run: |
6156
upy-package \
@@ -66,3 +61,14 @@ jobs:
6661
# use --ignore-version to skip version validation
6762
# use --ignore-deps to skip dependency validation
6863
# use --ignore-boot-main to skip boot.py and main.py files in URLs
64+
- name: Install deploy dependencies
65+
run: |
66+
python -m pip install --upgrade pip
67+
if [ -f requirements-deploy.txt ]; then pip install -r requirements-deploy.txt; fi
68+
- name: Build package
69+
run: |
70+
python setup.py sdist
71+
rm dist/*.orig
72+
- name: Test built package
73+
run: |
74+
twine check dist/*

.pre-commit-config.yaml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
3+
# To install pre-commit hooks, install `pre-commit` and activate it here:
4+
# pip3 install pre-commit
5+
# pre-commit install
6+
#
7+
default_stages:
8+
- commit
9+
- push
10+
- manual
11+
12+
repos:
13+
- repo: https://github.com/pre-commit/pre-commit-hooks
14+
rev: v2.3.0
15+
hooks:
16+
- id: check-yaml
17+
- id: trailing-whitespace
18+
args: [--markdown-linebreak-ext=md]
19+
- repo: https://github.com/PyCQA/flake8
20+
rev: 5.0.4
21+
hooks:
22+
- id: flake8
23+
- repo: https://github.com/brainelectronics/micropython-package-validation
24+
rev: 0.5.0
25+
hooks:
26+
- id: upy-package
27+
args:
28+
- "--setup_file=setup.py"
29+
- "--package_changelog_file=changelog.md"
30+
- "--package_file=package.json"
31+
- "--validate"
32+
- repo: https://github.com/brainelectronics/changelog2version
33+
rev: 0.10.0
34+
hooks:
35+
- id: changelog2version
36+
args:
37+
- "--changelog_file=changelog.md"
38+
- "--version_file=be_upy_blink/version.py"
39+
- "--validate"

README.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ MicroPython PyPi package template with GitHub Action based testing and deploy
2323
- [Installation](#installation)
2424
- [Install required tools](#install-required-tools)
2525
- [Setup](#setup)
26-
- [Install package with upip](#install-package-with-upip)
26+
- [Install package](#install-package)
2727
- [General](#general)
2828
- [Specific version](#specific-version)
2929
- [Test version](#test-version)
@@ -65,7 +65,7 @@ pip install -r requirements.txt
6565

6666
## Setup
6767

68-
### Install package with upip
68+
### Install package
6969

7070
Connect the MicroPython device to a network (if possible)
7171

@@ -229,6 +229,11 @@ twine upload dist/micropython-package-template-*.tar.gz -u PYPI_USERNAME -p PYPI
229229
Run the unittests locally with the following command after installing this
230230
package in a virtual environment
231231

232+
```bash
233+
# create a report directory where all generated report files are placed
234+
python create_report_dirs.py
235+
```
236+
232237
```bash
233238
# run all tests
234239
nose2 --config tests/unittest.cfg
@@ -240,7 +245,6 @@ nose2 tests.test_blink.TestBlink.test_flash_led
240245
Generate the coverage files with
241246

242247
```bash
243-
python create_report_dirs.py
244248
coverage html
245249
```
246250

@@ -258,6 +262,7 @@ should be done and changes to these file being made
258262
| `.github/workflows/release.yml` | Path to `version.py` file | Use package version file to set changelog based version |
259263
| `.github/workflows/test-release.yml` | Path to `version.py` file | Use package version file to set changelog based version |
260264
| `.github/workflows/test.yml` | Path to `version.py` file | Use package version file to set changelog based version |
265+
| `.pre-commit-config.yaml` | Path to `version.py` file | Use package version file for validation against latest changelog based version |
261266
| `README.md` | Links in header section and installation instructions | |
262267
| `changelog.md` | Cleanup changelog from informations of template | Keep usage of SemVer |
263268
| `docs/DOCUMENTATION.md` | Kink to ReadTheDocs | |

be_upy_blink/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python3
22
# -*- coding: UTF-8 -*-
33

4-
__version_info__ = ("0", "0", "0")
4+
__version_info__ = ("0", "9", "0")
55
__version__ = '.'.join(__version_info__)

changelog.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ r"^\#\# \[\d{1,}[.]\d{1,}[.]\d{1,}\] \- \d{4}\-\d{2}-\d{2}$"
1717
-->
1818

1919
## Released
20+
## [0.9.0] - 2023-07-11
21+
### Added
22+
- Precommit hooks for `package.json` and package version file validation, yaml style, flake8 and trailing whitespace checks
23+
- Contribution guideline
24+
- Package version file validation step in test workflow
25+
26+
### Fixed
27+
- Added missing empty line in several files
28+
2029
## [0.8.1] - 2023-06-12
2130
### Fixed
2231
- Usage documentation with more comments and WiFi instructions in root README
@@ -121,8 +130,9 @@ r"^\#\# \[\d{1,}[.]\d{1,}[.]\d{1,}\] \- \d{4}\-\d{2}-\d{2}$"
121130
- [`setup.py`](setup.py) and [`sdist_upip.py`](sdist_upip.py) file
122131

123132
<!-- Links -->
124-
[Unreleased]: https://github.com/brainelectronics/micropython-package-template/compare/0.8.1...main
133+
[Unreleased]: https://github.com/brainelectronics/micropython-package-template/compare/0.9.0...main
125134

135+
[0.9.0]: https://github.com/brainelectronics/micropython-package-template/tree/0.9.0
126136
[0.8.1]: https://github.com/brainelectronics/micropython-package-template/tree/0.8.1
127137
[0.8.0]: https://github.com/brainelectronics/micropython-package-template/tree/0.8.0
128138
[0.7.0]: https://github.com/brainelectronics/micropython-package-template/tree/0.7.0

docs/CONTRIBUTING.md

+198-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,201 @@ Guideline to contribute to this package
44

55
---------------
66

7-
## TBD
7+
## General
8+
9+
You're welcome to contribute to this package with or without raising an issue
10+
before creating a PR.
11+
12+
Please follow the following guideline covering all necessary steps and hints
13+
for a smooth review and contribution process.
14+
15+
## Code
16+
17+
To test and verify your changes it is recommended to run all checks locally in
18+
a virtual environment. Use the following command to setup and install all
19+
tools.
20+
21+
```bash
22+
python3 -m venv .venv
23+
source .venv/bin/activate
24+
25+
pip install -r requirements-test.txt
26+
```
27+
28+
For very old systems it might be necessary to use an older version of
29+
`pre-commit`, an "always" working version is `1.18.3` with the drawback of not
30+
having `flake8` and maybe other checks in place.
31+
32+
### Format
33+
34+
The Python code format is checked by `flake8` with the default line length
35+
limit of 79. Further configuration can be found in the `.flake8` file in the
36+
repo root.
37+
38+
The YAML code format is checked by `yamllint` with some small adjustments as
39+
defined in the `.yamllint` file in the repo root.
40+
41+
Use the following commands (inside the virtual environment) to run the Python
42+
and YAML checks
43+
44+
```bash
45+
# check Python
46+
flake8 .
47+
48+
# check YAML
49+
yamllint .
50+
```
51+
52+
### Tests
53+
54+
Every code should be covered by a unittest. This can be achieved for
55+
MicroPython up to some degree, as hardware specific stuff can't be always
56+
tested by a unittest.
57+
58+
For now `nose2` is used as tool of choice. The configuration is defined in the
59+
`tests/unittest.cfg` file.
60+
61+
All reports are placed inside the `reports` folder or a subfolder of it. It
62+
can be created with
63+
64+
```bash
65+
python create_report_dirs.py
66+
```
67+
68+
After this either all or a specific unittest can be executed, see the following
69+
commands as an example
70+
71+
```bash
72+
# run all tests
73+
nose2 --config tests/unittest.cfg
74+
75+
# run only one specific tests
76+
nose2 tests.test_blink.TestBlink.test_flash_led
77+
```
78+
79+
As last step the coverage report can be generated. There might be a minimum
80+
coverage limit set up for the project. Check the value of `target` in the
81+
`codecov.yaml` file, located in the repo root.
82+
83+
```bash
84+
coverage html
85+
```
86+
87+
The coverage report is placed at `reports/coverage/html/index.html`
88+
89+
### Precommit hooks
90+
91+
This repo is equipped with a `.pre-commit-config.yaml` file to combine most of
92+
the previously mentioned checks plus the changelog validation, see next
93+
section, into one handy command. It additionally allows to automatically run
94+
the checks on every commit.
95+
96+
In order to run this repo's pre commit hooks, perform the following steps
97+
98+
```bash
99+
# install pre-commit to run before each commit, optionally
100+
pre-commit install
101+
102+
pre-commit run --all-files
103+
```
104+
105+
## Changelog
106+
107+
The changelog format is based on [Keep a Changelog][ref-keep-a-changelog], and
108+
this project adheres to [Semantic Versioning][ref-semantic-versioning].
109+
110+
Please add a changelog entry for every PR you contribute. The versions are
111+
seperated into `MAJOR.MINOR.PATCH`:
112+
113+
- Increment the major version by 1 in case you created a breaking, non
114+
backwards compatible change which requires the user to perform additional
115+
tasks, adopt his currently running code or in general can't be used as is anymore.
116+
- Increment the minor version by 1 on new "features" which can be used or are
117+
optional, but in either case do not require any changes by the user to keep
118+
the system running after upgrading.
119+
- Increment the patch version by 1 on bugfixes which fix an issue but can be
120+
used out of the box, like features, without any changes by the user. In some
121+
cases bugfixes can be breaking changes of course.
122+
123+
Please add the date the change has been made as well to the changelog
124+
following the format `## [MAJOR.MINOR.PATCH] - YYYY-MM-DD`. It is not
125+
necessary to keep this date up to date, it is just used as meta data.
126+
127+
The changelog entry shall be short but meaningful and can of course contain
128+
links and references to other issues or PRs. New lines are only allowed for a
129+
new bulletpoint entry. Usage examples or other code snippets should be placed
130+
in the code documentation, README or the docs folder.
131+
132+
### General
133+
134+
The package version file, located at `<PACKAGE_NAME>/version.py` contains the
135+
latest changelog version.
136+
137+
To avoid a manual sync of the changelog version and the package version file
138+
content, the `changelog2version` package is used. It parses the changelog,
139+
extracts the latest version and updates the version file.
140+
141+
The package version file can be generated with the following command consuming
142+
the latest changelog entry
143+
144+
```bash
145+
changelog2version \
146+
--changelog_file changelog.md \
147+
--version_file <PACKAGE_NAME>/version.py \
148+
--version_file_type py \
149+
--debug
150+
```
151+
152+
To validate the existing package version file against the latest changelog
153+
entry use this command
154+
155+
```bash
156+
changelog2version \
157+
--changelog_file=changelog.md \
158+
--version_file=<PACKAGE_NAME>/version.py \
159+
--validate
160+
```
161+
162+
### MicroPython
163+
164+
On MicroPython the `mip` package is used to install packages instead of `pip`
165+
at MicroPython version 1.20.0 and newer. This utilizes a `package.json` file
166+
in the repo root to define all files and dependencies of a package to be
167+
downloaded by [`mip`][ref-mip-docs].
168+
169+
To avoid a manual sync of the changelog version and the MicroPython package
170+
file content, the `setup2upypackage` package is used. It parses the changelog,
171+
extracts the latest version and updates the package file version entry. It
172+
additionally parses the `setup.py` file and adds entries for all files
173+
contained in the package to the `urls` section and all other external
174+
dependencies to the `deps` section.
175+
176+
The MicroPython package file can be generated with the following command based
177+
on the latest changelog entry and `setup` file.
178+
179+
```bash
180+
upy-package \
181+
--setup_file setup.py \
182+
--package_changelog_file changelog.md \
183+
--package_file package.json
184+
```
185+
186+
To validate the existing package file against the latest changelog entry and
187+
setup file content use this command
188+
189+
```bash
190+
upy-package \
191+
--setup_file setup.py \
192+
--package_changelog_file changelog.md \
193+
--package_file package.json \
194+
--validate
195+
```
196+
197+
## Documentation
198+
199+
Please check the `docs/DOCUMENTATION.md` file for further details.
200+
201+
<!-- Links -->
202+
[ref-keep-a-changelog]: https://keepachangelog.com/en/1.0.0/
203+
[ref-semantic-versioning]: https://semver.org/spec/v2.0.0.html
204+
[ref-mip-docs]: https://docs.micropython.org/en/v1.20.0/reference/packages.html

docs/changelog_link.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11

22
.. include:: ../changelog.md
3-
:parser: myst_parser.sphinx_
3+
:parser: myst_parser.sphinx_

docs/readme_link.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11

22
.. include:: ../README.md
3-
:parser: myst_parser.sphinx_
3+
:parser: myst_parser.sphinx_

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
]
1515
],
1616
"deps": [],
17-
"version": "0.8.1"
17+
"version": "0.9.0"
1818
}

requirements-deploy.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
# Avoid fixed versions
33
# # to upload package to PyPi or other package hosts
44
twine>=4.0.1,<5
5-
changelog2version>=0.5.0,<1
5+
changelog2version>=0.5.0,<1

0 commit comments

Comments
 (0)