Skip to content

Commit

Permalink
Update infrastructure for checking for problems with Python code
Browse files Browse the repository at this point in the history
Arduino tooling projects use a standardized infrastructure. A centralized collection of reusable infrastructure assets
is maintained in a dedicated repository:

https://github.com/arduino/tooling-project-assets

Since the time this project's infrastructure was installed, some advancements have been made in the upstream "template"
assets. The project's infrastructure is hereby brought up to date with the state of the art upstream assets.

The significant changes:

- Migration to the use of the Task task runner to allow contributors to easily run the same operations locally as is
  done by the CI system on GitHub.
- The project will now be formatted using the Black code formatter tool, the standard for Arduino Tooling projects.
  • Loading branch information
per1234 committed Mar 25, 2023
1 parent 4525fab commit 71b4fd2
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 75 deletions.
12 changes: 12 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-python/.flake8
# See: https://flake8.pycqa.org/en/latest/user/configuration.html
# The code style defined in this file is the official standardized style to be used in all Arduino tooling projects and
# should not be modified.

[flake8]
doctests = True
# W503 and W504 are mutually exclusive. PEP 8 recommends line break before.
ignore = W503
max-complexity = 10
max-line-length = 120
select = E,W,F,C,N
121 changes: 121 additions & 0 deletions .github/workflows/check-python-task.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/check-python-task.md
name: Check Python

# See: https://docs.github.com/actions/using-workflows/events-that-trigger-workflows
on:
create:
push:
paths:
- ".github/workflows/check-python-task.ya?ml"
- "**/.flake8"
- "**/poetry.lock"
- "**/pyproject.toml"
- "**/setup.cfg"
- ".python-version"
- "Taskfile.ya?ml"
- "**/tox.ini"
- "**.py"
pull_request:
paths:
- ".github/workflows/check-python-task.ya?ml"
- "**/.flake8"
- "**/poetry.lock"
- "**/pyproject.toml"
- "**/setup.cfg"
- ".python-version"
- "Taskfile.ya?ml"
- "**/tox.ini"
- "**.py"
schedule:
# Run periodically to catch breakage caused by external changes.
- cron: "0 8 * * WED"
workflow_dispatch:
repository_dispatch:

jobs:
run-determination:
runs-on: ubuntu-latest
outputs:
result: ${{ steps.determination.outputs.result }}
steps:
- name: Determine if the rest of the workflow should run
id: determination
run: |
RELEASE_BRANCH_REGEX="refs/heads/[0-9]+.[0-9]+.x"
# The `create` event trigger doesn't support `branches` filters, so it's necessary to use Bash instead.
if [[
"${{ github.event_name }}" != "create" ||
"${{ github.ref }}" =~ $RELEASE_BRANCH_REGEX
]]; then
# Run the other jobs.
RESULT="true"
else
# There is no need to run the other jobs.
RESULT="false"
fi
echo "result=$RESULT" >> $GITHUB_OUTPUT
lint:
needs: run-determination
if: needs.run-determination.outputs.result == 'true'
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Install Python
uses: actions/setup-python@v4
with:
python-version-file: .python-version

- name: Install Poetry
run: |
pipx install \
--python "$(which python)" \
poetry
- name: Install Task
uses: arduino/setup-task@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
version: 3.x

- name: Run flake8
uses: liskin/gh-problem-matcher-wrap@v2
with:
linters: flake8
run: task python:lint

formatting:
needs: run-determination
if: needs.run-determination.outputs.result == 'true'
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Install Python
uses: actions/setup-python@v4
with:
python-version-file: .python-version

- name: Install Poetry
run: |
pipx install \
--python "$(which python)" \
poetry
- name: Install Task
uses: arduino/setup-task@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
version: 3.x

- name: Format Python code
run: task python:format

- name: Check formatting
run: git diff --color --exit-code
66 changes: 0 additions & 66 deletions .github/workflows/lint-python.yml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Check Action Metadata status](https://github.com/arduino/compile-sketches/actions/workflows/check-action-metadata-task.yml/badge.svg)](https://github.com/arduino/compile-sketches/actions/workflows/check-action-metadata-task.yml)
[![Tests](https://github.com/arduino/compile-sketches/workflows/Test%20Python%20code/badge.svg)](https://github.com/arduino/compile-sketches/actions?workflow=Test+Python+code)
[![Lint](https://github.com/arduino/compile-sketches/workflows/Lint%20Python%20code/badge.svg)](https://github.com/arduino/compile-sketches/actions?workflow=Lint+Python+code)
[![Check Python status](https://github.com/arduino/compile-sketches/actions/workflows/check-python-task.yml/badge.svg)](https://github.com/arduino/compile-sketches/actions/workflows/check-python-task.yml)
[![Spell Check](https://github.com/arduino/compile-sketches/workflows/Spell%20Check/badge.svg)](https://github.com/arduino/compile-sketches/actions?workflow=Spell+Check)
[![codecov](https://codecov.io/gh/arduino/compile-sketches/branch/main/graph/badge.svg?token=Uv6f1ebMZ4)](https://codecov.io/gh/arduino/compile-sketches)

Expand Down
36 changes: 36 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ tasks:
desc: Check for problems with the project
deps:
- task: action:validate
- task: python:lint

fix:
desc: Make automated corrections to the project's files
deps:
- task: python:format

# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-action-metadata-task/Taskfile.yml
action:validate:
Expand Down Expand Up @@ -36,6 +42,36 @@ tasks:
cmds:
- npm install

# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/poetry-task/Taskfile.yml
poetry:install-deps:
desc: Install dependencies managed by Poetry
run: when_changed
cmds:
- |
poetry install \
--no-root \
{{if .POETRY_GROUPS}} --only {{.POETRY_GROUPS}} {{end}}
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-python-task/Taskfile.yml
python:format:
desc: Format Python files
deps:
- task: poetry:install-deps
vars:
POETRY_GROUPS: dev
cmds:
- poetry run black .

# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-python-task/Taskfile.yml
python:lint:
desc: Lint Python code
deps:
- task: poetry:install-deps
vars:
POETRY_GROUPS: dev
cmds:
- poetry run flake8 --show-source

# Make a temporary file named according to the passed TEMPLATE variable and print the path passed to stdout
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/windows-task/Taskfile.yml
utility:mktemp-file:
Expand Down
7 changes: 0 additions & 7 deletions compilesketches/.flake8

This file was deleted.

Loading

0 comments on commit 71b4fd2

Please sign in to comment.