Skip to content

Commit d0c0736

Browse files
committed
Add infrastructure to lint and check formatting of Python files
Tasks and a GitHub Actions workflow are added for linting and formatting of the project's Python files. On every push and pull request that affects relevant files, run flake8 to check the Python files of the repository for issues and black to check formatting.
1 parent 31305f2 commit d0c0736

File tree

6 files changed

+362
-1
lines changed

6 files changed

+362
-1
lines changed

.flake8

Lines changed: 12 additions & 0 deletions
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
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/check-python-task.md
2+
name: Check Python
3+
4+
env:
5+
# See: https://github.com/actions/setup-python/tree/main#available-versions-of-python
6+
PYTHON_VERSION: "3.9"
7+
8+
# See: https://docs.github.com/actions/using-workflows/events-that-trigger-workflows
9+
on:
10+
create:
11+
push:
12+
paths:
13+
- ".github/workflows/check-python-task.ya?ml"
14+
- "**/.flake8"
15+
- "**/poetry.lock"
16+
- "**/pyproject.toml"
17+
- "**/setup.cfg"
18+
- "Taskfile.ya?ml"
19+
- "**/tox.ini"
20+
- "**.py"
21+
pull_request:
22+
paths:
23+
- ".github/workflows/check-python-task.ya?ml"
24+
- "**/.flake8"
25+
- "**/poetry.lock"
26+
- "**/pyproject.toml"
27+
- "**/setup.cfg"
28+
- "Taskfile.ya?ml"
29+
- "**/tox.ini"
30+
- "**.py"
31+
schedule:
32+
# Run periodically to catch breakage caused by external changes.
33+
- cron: "0 8 * * WED"
34+
workflow_dispatch:
35+
repository_dispatch:
36+
37+
jobs:
38+
run-determination:
39+
runs-on: ubuntu-latest
40+
outputs:
41+
result: ${{ steps.determination.outputs.result }}
42+
steps:
43+
- name: Determine if the rest of the workflow should run
44+
id: determination
45+
run: |
46+
RELEASE_BRANCH_REGEX="refs/heads/[0-9]+.[0-9]+.x"
47+
# The `create` event trigger doesn't support `branches` filters, so it's necessary to use Bash instead.
48+
if [[
49+
"${{ github.event_name }}" != "create" ||
50+
"${{ github.ref }}" =~ $RELEASE_BRANCH_REGEX
51+
]]; then
52+
# Run the other jobs.
53+
RESULT="true"
54+
else
55+
# There is no need to run the other jobs.
56+
RESULT="false"
57+
fi
58+
59+
echo "result=$RESULT" >> $GITHUB_OUTPUT
60+
61+
lint:
62+
needs: run-determination
63+
if: needs.run-determination.outputs.result == 'true'
64+
runs-on: ubuntu-latest
65+
66+
steps:
67+
- name: Checkout repository
68+
uses: actions/checkout@v3
69+
70+
- name: Install Python
71+
uses: actions/setup-python@v4
72+
with:
73+
python-version: ${{ env.PYTHON_VERSION }}
74+
75+
- name: Install Poetry
76+
run: pip install poetry
77+
78+
- name: Install Task
79+
uses: arduino/setup-task@v1
80+
with:
81+
repo-token: ${{ secrets.GITHUB_TOKEN }}
82+
version: 3.x
83+
84+
- name: Run flake8
85+
uses: liskin/gh-problem-matcher-wrap@v2
86+
with:
87+
linters: flake8
88+
run: task python:lint
89+
90+
formatting:
91+
needs: run-determination
92+
if: needs.run-determination.outputs.result == 'true'
93+
runs-on: ubuntu-latest
94+
95+
steps:
96+
- name: Checkout repository
97+
uses: actions/checkout@v3
98+
99+
- name: Install Python
100+
uses: actions/setup-python@v4
101+
with:
102+
python-version: ${{ env.PYTHON_VERSION }}
103+
104+
- name: Install Poetry
105+
run: pip install poetry
106+
107+
- name: Install Task
108+
uses: arduino/setup-task@v1
109+
with:
110+
repo-token: ${{ secrets.GITHUB_TOKEN }}
111+
version: 3.x
112+
113+
- name: Format Python code
114+
run: task python:format
115+
116+
- name: Check formatting
117+
run: git diff --color --exit-code

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
[![Check License status](https://github.com/arduino/forum-assets/actions/workflows/check-license.yml/badge.svg)](https://github.com/arduino/forum-assets/actions/workflows/check-license.yml)
77
[![Check Markdown status](https://github.com/arduino/forum-assets/actions/workflows/check-markdown-task.yml/badge.svg)](https://github.com/arduino/forum-assets/actions/workflows/check-markdown-task.yml)
88
[![Check Prettier Formatting status](https://github.com/arduino/forum-assets/actions/workflows/check-prettier-formatting-task.yml/badge.svg)](https://github.com/arduino/forum-assets/actions/workflows/check-prettier-formatting-task.yml)
9+
[![Check Python status](https://github.com/arduino/forum-assets/actions/workflows/check-python-task.yml/badge.svg)](https://github.com/arduino/forum-assets/actions/workflows/check-python-task.yml)
910
[![Check Taskfiles status](https://github.com/arduino/forum-assets/actions/workflows/check-taskfiles.yml/badge.svg)](https://github.com/arduino/forum-assets/actions/workflows/check-taskfiles.yml)
1011
[![Check ToC status](https://github.com/arduino/forum-assets/actions/workflows/check-toc-task.yml/badge.svg)](https://github.com/arduino/forum-assets/actions/workflows/check-toc-task.yml)
1112
[![Check Workflows status](https://github.com/arduino/forum-assets/actions/workflows/check-workflows-task.yml/badge.svg)](https://github.com/arduino/forum-assets/actions/workflows/check-workflows-task.yml)

Taskfile.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,22 @@ tasks:
302302
cmds:
303303
- poetry install --no-root
304304

305+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-python-task/Taskfile.yml
306+
python:format:
307+
desc: Format Python files
308+
deps:
309+
- task: poetry:install-deps
310+
cmds:
311+
- poetry run black .
312+
313+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-python-task/Taskfile.yml
314+
python:lint:
315+
desc: Lint Python code
316+
deps:
317+
- task: poetry:install-deps
318+
cmds:
319+
- poetry run flake8 --show-source
320+
305321
# Make a temporary file named according to the passed TEMPLATE variable and print the path passed to stdout
306322
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/windows-task/Taskfile.yml
307323
utility:mktemp-file:

0 commit comments

Comments
 (0)