Skip to content

Commit 545f7ea

Browse files
committed
Add template workflow to lint and check formatting of Python files
On every push and pull request that affects relevant files, and periodically, run flake8 to check the Python files of the repository for issues and black to check formatting. The .flake8 file is used to configure flake8: https://flake8.pycqa.org/en/latest/user/configuration.html
1 parent 4a4af4c commit 545f7ea

File tree

5 files changed

+287
-0
lines changed

5 files changed

+287
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# See: https://taskfile.dev/#/usage
2+
version: "3"
3+
4+
tasks:
5+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-python-task/Taskfile.yml
6+
python:lint:
7+
desc: Lint Python code
8+
deps:
9+
- task: poetry:install-deps
10+
cmds:
11+
- poetry run flake8 --show-source
12+
13+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-python-task/Taskfile.yml
14+
python:format:
15+
desc: Format Python files
16+
deps:
17+
- task: poetry:install-deps
18+
cmds:
19+
- poetry run black .
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-python/.flake8
2+
# See: https://flake8.pycqa.org/en/latest/user/configuration.html
3+
# The code style defined in this file is the official standardized style to be used in all Arduino tooling projects and
4+
# should not be modified.
5+
6+
[flake8]
7+
doctests = True
8+
# W503 and W504 are mutually exclusive. PEP 8 recommends line break before.
9+
ignore = W503
10+
max-complexity = 10
11+
max-line-length = 120
12+
select = E,W,F,C,N
+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# "Check Python" workflow (Task)
2+
3+
Workflow file: [check-python-task.yml](check-python-task.yml)
4+
5+
Run [flake8](https://flake8.pycqa.org/) and [black](https://github.com/psf/black) on the Python files of the repository.
6+
7+
This is the version of the workflow for projects using the [Task](https://taskfile.dev/#/) task runner tool.
8+
9+
## Installation
10+
11+
### 1. Add configuration files
12+
13+
Copy the configuration files listed in the [**Assets**](#assets) section below into the project's repository.
14+
15+
If the project already contains a [`pyproject.toml`](https://www.python.org/dev/peps/pep-0518/) file, then merge them.
16+
17+
### 2. Install tool dependencies
18+
19+
The tool dependencies of this workflow are managed by [Poetry](https://python-poetry.org/).
20+
21+
Install Poetry by following these instructions:<br />
22+
https://python-poetry.org/docs/#installation
23+
24+
If your project does not already use Poetry, you can initialize the [`pyproject.toml`](https://python-poetry.org/docs/pyproject/) file using these commands:
25+
26+
```
27+
poetry init --python="^3.9" --dev-dependency="black@^21.5b0" --dev-dependency="flake8@^3.9.2" --dev-dependency="pep8-naming@^0.11.1"
28+
poetry install
29+
```
30+
31+
If already using Poetry, add the tool using this command:
32+
33+
```
34+
poetry add --dev "black@^21.5b0" "flake8@^3.9.2" "pep8-naming@^0.11.1"
35+
```
36+
37+
Make sure to commit the resulting `pyproject.toml` and `poetry.lock` files.
38+
39+
### 3. Configuration
40+
41+
Add the following to `pyproject.toml`:
42+
43+
```toml
44+
[tool.black]
45+
line-length = 120
46+
```
47+
48+
## Assets
49+
50+
- [`.flake8`](assets/check-python/.flake8) - flake8 configuration file.
51+
- Install to: repository root
52+
- [`Taskfile.yml`](assets/check-python-task/Taskfile.yml] - Python linting and formatting tasks.
53+
- Install to: repository root (or add the tasks into the existing `Taskfile.yml`)
54+
- [`Taskfile.yml`](assets/shared/Taskfile.yml] - Installation task.
55+
- Add the `poetry:install-deps` task into the existing `Taskfile.yml`
56+
57+
The code style defined in `pyproject.toml` and `.flake8` is the official standardized style to be used in all Arduino tooling projects and should not be modified.
58+
59+
## Readme badge
60+
61+
Markdown badge:
62+
63+
```markdown
64+
[![Check Python status](https://github.com/REPO_OWNER/REPO_NAME/actions/workflows/check-python-task.yml/badge.svg)](https://github.com/REPO_OWNER/REPO_NAME/actions/workflows/check-python-task.yml)
65+
```
66+
67+
Replace the `REPO_OWNER` and `REPO_NAME` placeholders in the URLs with the final repository owner and name ([example](https://raw.githubusercontent.com/arduino-libraries/ArduinoIoTCloud/master/README.md)).
68+
69+
---
70+
71+
Asciidoc badge:
72+
73+
```adoc
74+
image:https://github.com/{repository-owner}/{repository-name}/actions/workflows/check-python-task.yml/badge.svg["Check Python status", link="https://github.com/{repository-owner}/{repository-name}/actions/workflows/check-python-task.yml"]
75+
```
76+
77+
Define the `{repository-owner}` and `{repository-name}` attributes and use them throughout the readme ([example](https://raw.githubusercontent.com/arduino-libraries/WiFiNINA/master/README.adoc)).
78+
79+
## Commit message
80+
81+
```
82+
Add CI workflow to lint and check formatting of Python files
83+
84+
On every push and pull request that affects relevant files, and periodically, run flake8 to check the Python files of
85+
the repository for issues and black to check formatting.
86+
87+
The .flake8 file is used to configure flake8:
88+
https://flake8.pycqa.org/en/latest/user/configuration.html
89+
```
90+
91+
## PR message
92+
93+
```markdown
94+
On every push and pull request that affects relevant files, and periodically, run [`flake8`](https://flake8.pycqa.org/) to check the Python files of the repository for issues and [black](https://github.com/psf/black) to check formatting.
95+
96+
The `.flake8` file is used to configure `flake8`:
97+
https://flake8.pycqa.org/en/latest/user/configuration.html
98+
```
+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/check-python-task.md
2+
name: Check Python
3+
4+
# See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows
5+
on:
6+
push:
7+
paths:
8+
- ".github/workflows/check-python-task.ya?ml"
9+
- "**/.flake8"
10+
- "**/poetry.lock"
11+
- "**/pyproject.toml"
12+
- "**/setup.cfg"
13+
- "Taskfile.ya?ml"
14+
- "**/tox.ini"
15+
- "**.py"
16+
pull_request:
17+
paths:
18+
- ".github/workflows/check-python-task.ya?ml"
19+
- "**/.flake8"
20+
- "**/poetry.lock"
21+
- "**/pyproject.toml"
22+
- "**/setup.cfg"
23+
- "Taskfile.ya?ml"
24+
- "**/tox.ini"
25+
- "**.py"
26+
workflow_dispatch:
27+
repository_dispatch:
28+
29+
jobs:
30+
lint:
31+
runs-on: ubuntu-latest
32+
33+
steps:
34+
- name: Checkout repository
35+
uses: actions/checkout@v2
36+
37+
- name: Install Python
38+
uses: actions/setup-python@v2
39+
with:
40+
python-version: "3.9"
41+
42+
- name: Install Poetry
43+
run: pip install poetry
44+
45+
- name: Install Task
46+
uses: arduino/setup-task@v1
47+
with:
48+
repo-token: ${{ secrets.GITHUB_TOKEN }}
49+
version: 3.x
50+
51+
- name: Run flake8
52+
run: task python:lint
53+
54+
formatting:
55+
runs-on: ubuntu-latest
56+
57+
steps:
58+
- name: Checkout repository
59+
uses: actions/checkout@v2
60+
61+
- name: Install Python
62+
uses: actions/setup-python@v2
63+
with:
64+
python-version: "3.9"
65+
66+
- name: Install Poetry
67+
run: pip install poetry
68+
69+
- name: Install Task
70+
uses: arduino/setup-task@v1
71+
with:
72+
repo-token: ${{ secrets.GITHUB_TOKEN }}
73+
version: 3.x
74+
75+
- name: Format Python code
76+
run: task python:format
77+
78+
- name: Check formatting
79+
run: git diff --color --exit-code
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/check-python-task.md
2+
name: Check Python
3+
4+
# See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows
5+
on:
6+
push:
7+
paths:
8+
- ".github/workflows/check-python-task.ya?ml"
9+
- "**/.flake8"
10+
- "**/poetry.lock"
11+
- "**/pyproject.toml"
12+
- "**/setup.cfg"
13+
- "Taskfile.ya?ml"
14+
- "**/tox.ini"
15+
- "**.py"
16+
pull_request:
17+
paths:
18+
- ".github/workflows/check-python-task.ya?ml"
19+
- "**/.flake8"
20+
- "**/poetry.lock"
21+
- "**/pyproject.toml"
22+
- "**/setup.cfg"
23+
- "Taskfile.ya?ml"
24+
- "**/tox.ini"
25+
- "**.py"
26+
workflow_dispatch:
27+
repository_dispatch:
28+
29+
jobs:
30+
lint:
31+
runs-on: ubuntu-latest
32+
33+
steps:
34+
- name: Checkout repository
35+
uses: actions/checkout@v2
36+
37+
- name: Install Python
38+
uses: actions/setup-python@v2
39+
with:
40+
python-version: "3.9"
41+
42+
- name: Install Poetry
43+
run: pip install poetry
44+
45+
- name: Install Task
46+
uses: arduino/setup-task@v1
47+
with:
48+
repo-token: ${{ secrets.GITHUB_TOKEN }}
49+
version: 3.x
50+
51+
- name: Run flake8
52+
run: task python:lint
53+
54+
formatting:
55+
runs-on: ubuntu-latest
56+
57+
steps:
58+
- name: Checkout repository
59+
uses: actions/checkout@v2
60+
61+
- name: Install Python
62+
uses: actions/setup-python@v2
63+
with:
64+
python-version: "3.9"
65+
66+
- name: Install Poetry
67+
run: pip install poetry
68+
69+
- name: Install Task
70+
uses: arduino/setup-task@v1
71+
with:
72+
repo-token: ${{ secrets.GITHUB_TOKEN }}
73+
version: 3.x
74+
75+
- name: Format Python code
76+
run: task python:format
77+
78+
- name: Check formatting
79+
run: git diff --color --exit-code

0 commit comments

Comments
 (0)