Skip to content

Commit 0444d7a

Browse files
committed
linters: Add black
Add black python linter both as an action and as a Makefile rule. Signed-off-by: Alex Apostolescu <[email protected]>
1 parent 3017d5b commit 0444d7a

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

.github/workflows/actions.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,30 @@ jobs:
7676
if [[ -n "${{ env.FILES }}" ]]; then
7777
cpplint --recursive --quiet ${{ env.FILES }}
7878
fi
79+
80+
black:
81+
runs-on: ubuntu-latest
82+
steps:
83+
- uses: actions/checkout@v4
84+
with:
85+
fetch-depth: 0
86+
87+
- name: Get changed Python files
88+
uses: tj-actions/changed-files@v45
89+
id: changed-files
90+
with:
91+
files: "**/*.py"
92+
separator: " "
93+
94+
- uses: actions/setup-python@v5
95+
with:
96+
python-version: 3.12
97+
98+
- uses: psf/black@stable
99+
env:
100+
# Black will fail if no arguments are provided.
101+
# In that case check a directory without any python files.
102+
FILES: ${{ steps.changed-files.outputs.all_changed_files || '.github' }}
103+
with:
104+
options: "--check --diff"
105+
src: ${{ env.FILES }}

Makefile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ clean: stop_bash
4040
cleanall: clean
4141
-docker inspect --type=image $(IMAGE_NAME) > /dev/null 2>&1 && docker image rm $(IMAGE_NAME)
4242

43-
.PHONY: lint markdownlint cpplint
44-
lint: markdownlint cpplint
43+
.PHONY: lint markdownlint cpplint black
44+
lint: markdownlint cpplint black
4545

4646
CHANGED_MD_FILES = $(call get_changed_files,\.md$$)
4747
markdownlint:
@@ -60,6 +60,14 @@ cpplint: # Runs locally, requires cpplint installed
6060
cpplint --recursive --quiet $(CHANGED_CPP_FILES); \
6161
fi
6262

63+
CHANGED_PY_FILES = $(call get_changed_files,\.py$$)
64+
black:
65+
@echo "Checking Python files ..."
66+
@if [ "$(CHANGED_PY_FILES)" ]; then \
67+
docker run --rm -v $(PWD):/data --workdir=/data pyfound/black:latest \
68+
black --check --diff $(CHANGED_PY_FILES); \
69+
fi
70+
6371
# Get a list of files that changed in the current branch and match the given pattern
6472
define get_changed_files # $(1): pattern
6573
$(shell git diff --name-only HEAD $(shell git merge-base HEAD main) | grep "$(1)" | tr '\n' ' ')

0 commit comments

Comments
 (0)