Skip to content

Commit b499acc

Browse files
Add Python autoformatter (no actual formatting yet)
1 parent fbfa2d0 commit b499acc

File tree

3 files changed

+67
-18
lines changed

3 files changed

+67
-18
lines changed

.github/workflows/basic_checks.yml

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,26 +86,20 @@ jobs:
8686
8787
style-check:
8888
runs-on: ubuntu-latest
89-
container:
90-
image: ghcr.io/armmbed/mbed-os-env:master-latest
91-
9289
steps:
9390

9491
- name: Checkout repo
95-
uses: actions/checkout@v4
92+
uses: actions/checkout@v5
9693
with:
9794
fetch-depth: 0
9895

99-
-
100-
name: UTF-8 Check
96+
- name: UTF-8 Check
10197
run: |
10298
git config --global --add safe.directory "$GITHUB_WORKSPACE"
10399
# Make sure we're not introducing any text which is not UTF-8 encoded
104100
git diff origin/${GITHUB_BASE_REF} -U0 | ( grep -a '^+' || true ) | ( ! grep -axv '.*' )
105-
106101
107-
-
108-
name: astyle checks
102+
- name: astyle checks
109103
run: |
110104
set -x
111105
git config --global --add safe.directory "$GITHUB_WORKSPACE"
@@ -134,11 +128,28 @@ jobs:
134128
tee BUILD/badlibs |
135129
sed -e "s/^/Bad library name found: /" && [ ! -s BUILD/badlibs ]
136130
# Assert that all assembler files are named correctly
137-
# The strange command below asserts that there are exactly 0 libraries
131+
# The strange command below asserts that there are exactly 0 files
138132
# that do end with .s
139133
find -name "*.s" | tee BUILD/badasm |
140134
sed -e "s/^/Bad Assembler file name found: /" && [ ! -s BUILD/badasm ]
141135
136+
# Set up the oldest python version that's currently still supported by Python (as of Fall 2025)
137+
- uses: actions/setup-python@v6
138+
with:
139+
python-version: '3.9'
140+
141+
- name: Install Python packages
142+
run: |
143+
python3 -m venv venv
144+
source venv/bin/activate
145+
pip install -e ./tools[linters]
146+
147+
- name: Check Python Formatting
148+
run: |
149+
cd tools
150+
ruff format --diff
151+
152+
142153
docs-check:
143154
runs-on: ubuntu-latest
144155

@@ -162,14 +173,13 @@ jobs:
162173
python-tests:
163174
runs-on: ubuntu-latest
164175
steps:
165-
-
166-
name: Checkout repo
167-
uses: actions/checkout@v4
168-
169-
- name: Install python3-venv
170-
run: |
171-
sudo apt-get update
172-
sudo apt-get install -y python3-venv
176+
- name: Checkout repo
177+
uses: actions/checkout@v5
178+
179+
# Set up the oldest python version that's currently still supported by Python (as of Fall 2025)
180+
- uses: actions/setup-python@v6
181+
with:
182+
python-version: '3.9'
173183

174184
- name: Install Python packages
175185
run: |

tools/pyproject.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ unit-tests = [
7171
"beautifulsoup4",
7272
"lxml"
7373
]
74+
linters = [
75+
"ruff"
76+
]
7477
greentea = [
7578
## Additional requirements to install into the Mbed environment when running Greentea tests
7679
# For USB Device host tests
@@ -103,3 +106,8 @@ mbedls = "mbed_lstools.main:mbedls_main"
103106
mbed-tools = "mbed_tools.cli.main:cli"
104107
memap = "memap.memap:main"
105108
ambiq_svl = "ambiq_svl.svl:cli"
109+
110+
[tool.ruff]
111+
line-length = 120
112+
show-fixes = true
113+
src = ['python']

tools/run_python_linters.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash -e
2+
3+
#
4+
# Copyright (c) 2025 Jamie Smith.
5+
# SPDX-License-Identifier: Apache-2.0
6+
#
7+
8+
# Script which runs Python formatters and linters
9+
# This is executed by the GitHub Actions CI build but also can be run locally.
10+
11+
# Go to script directory
12+
cd "$(dirname $0)"
13+
14+
# Activate Mbed OS virtualenv
15+
if command -v mbedhtrun >/dev/null 2>&1; then
16+
echo "Mbed OS python environment appears to already be activated."
17+
elif [ -e "../venv/Scripts/activate" ]; then
18+
source "../venv/Scripts/activate"
19+
elif [ -e "../venv/bin/activate" ]; then
20+
source "../venv/bin/activate"
21+
else
22+
echo "Failed to find Mbed OS virtualenv in ../venv and Python packages not installed to global environment."
23+
exit 1
24+
fi
25+
26+
if ! command -v ruff >/dev/null 2>&1; then
27+
echo "Linters optional dependency of Mbed not installed. Please run 'mbed-os/venv/bin/pip install mbed-os/tools[linters]'."
28+
fi
29+
30+
echo ">> Formatting with Ruff..."
31+
ruff format

0 commit comments

Comments
 (0)