Skip to content

Commit 005ae35

Browse files
authored
workflows, Makefile, src: experimental linting (#43456)
* workflows, Makefile, src: experimental linting Signed-off-by: William Woodruff <william@trailofbits.com> * pyproject: fixup isort config Signed-off-by: William Woodruff <william@trailofbits.com> * Makefile: clean up, add a `format` target Signed-off-by: William Woodruff <william@trailofbits.com> * Makefile: more genericisms Signed-off-by: William Woodruff <william@trailofbits.com> * treewide: use ruff over flake8/isort Signed-off-by: William Woodruff <william@trailofbits.com> * lint: experimenting Signed-off-by: William Woodruff <william@trailofbits.com> * lint, Makefile: more experimenting Signed-off-by: William Woodruff <william@trailofbits.com> * Makefile: undo Signed-off-by: William Woodruff <william@trailofbits.com> * pyproject: enable some more ruff lints Signed-off-by: William Woodruff <william@trailofbits.com> * exceptions: ignore N818 This is named intentionally. Signed-off-by: William Woodruff <william@trailofbits.com> * workflows: ci -> tests Signed-off-by: William Woodruff <william@trailofbits.com> * pyproject: add ruff `SIM` rules Signed-off-by: William Woodruff <william@trailofbits.com> * Makefile: put `ruff` before `black` for formatting ...on the theory that `black` provides our fixpoint. Signed-off-by: William Woodruff <william@trailofbits.com> * Makefile: `format` -> `reformat` Signed-off-by: William Woodruff <william@trailofbits.com> * blight: `ruff --fix` Signed-off-by: William Woodruff <william@trailofbits.com> * lint: update workflow ref Signed-off-by: William Woodruff <william@trailofbits.com> * lint: fix ref again Signed-off-by: William Woodruff <william@trailofbits.com> * lint: typo Signed-off-by: William Woodruff <william@trailofbits.com> Signed-off-by: William Woodruff <william@trailofbits.com>
1 parent 53a0238 commit 005ae35

13 files changed

Lines changed: 62 additions & 56 deletions

File tree

.flake8

Lines changed: 0 additions & 2 deletions
This file was deleted.

.github/workflows/lint.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: Lint
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
9+
jobs:
10+
lint:
11+
uses: trailofbits/.github/.github/workflows/lint.yml@v0.0.2
12+
with:
13+
python-version: "3.7"
Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,6 @@ on:
77
pull_request:
88

99
jobs:
10-
lint:
11-
runs-on: ubuntu-latest
12-
steps:
13-
- uses: actions/checkout@v3
14-
15-
- uses: actions/setup-python@v4
16-
with:
17-
python-version: "3.7"
18-
19-
- run: make dev
20-
21-
- run: make lint
22-
2310
test:
2411
strategy:
2512
matrix:

Makefile

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,60 @@
1+
# Optionally overriden by the user, if they're using a virtual environment manager.
2+
VENV ?= env
3+
VENV_EXISTS := $(VENV)/pyvenv.cfg
4+
5+
# On Windows, venv scripts/shims are under `Scripts` instead of `bin`.
6+
VENV_BIN := $(VENV)/bin
7+
ifeq ($(OS),Windows_NT)
8+
VENV_BIN := $(VENV)/Scripts
9+
endif
10+
11+
# Optionally overridden by the user/CI, to limit the installation to a specific
12+
# subset of development dependencies.
13+
BLIGHT_EXTRA := dev
14+
115
ALL_PY_SRCS := $(shell find src -name '*.py') \
216
$(shell find test -name '*.py')
317

418
.PHONY: all
519
all:
620
@echo "Run my targets individually!"
721

8-
.PHONY: dev
9-
dev:
10-
test -d env || python -m venv env
11-
. env/bin/activate && \
22+
$(VENV)/pyvenv.cfg: pyproject.toml
23+
python -m venv env
24+
. $(VENV_BIN)/activate && \
1225
pip install --upgrade pip setuptools && \
13-
pip install -e .[dev]
26+
pip install -e .[$(BLIGHT_EXTRA)]
27+
28+
.PHONY: dev
29+
dev: $(VENV)/pyvenv.cfg
1430

1531
.PHONY: lint
16-
lint:
17-
. env/bin/activate && \
18-
black $(ALL_PY_SRCS) && \
19-
isort $(ALL_PY_SRCS) && \
20-
flake8 $(ALL_PY_SRCS) && \
21-
mypy src && \
22-
git diff --exit-code
32+
lint: $(VENV_EXISTS)
33+
. $(VENV_BIN)/activate && \
34+
black --check $(ALL_PY_SRCS) && \
35+
ruff $(ALL_PY_SRCS) && \
36+
mypy src
37+
38+
.PHONY: reformat
39+
reformat: $(VENV_EXISTS)
40+
. $(VENV_BIN)/activate && \
41+
ruff --fix $(ALL_PY_SRCS) && \
42+
black $(ALL_PY_SRCS)
2343

2444
.PHONY: test
25-
test:
26-
. env/bin/activate && \
45+
test: $(VENV_EXISTS)
46+
. $(VENV_BIN)/activate && \
2747
pytest --cov=blight test/ && \
2848
python -m coverage report -m --fail-under 100
2949

3050
.PHONY: doc
31-
doc:
32-
. env/bin/activate && \
51+
doc: $(VENV_EXISTS)
52+
. $(VENV_BIN)/activate && \
3353
PYTHONWARNINGS='error::UserWarning' pdoc --force --html blight
3454

3555
.PHONY: package
36-
package:
37-
. env/bin/activate && \
56+
package: $(VENV_EXISTS)
57+
. $(VENV_BIN)/activate && \
3858
python -m build && \
3959
twine upload --repository pypi dist/*
4060

pyproject.toml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,15 @@ requires-python = ">=3.7"
2626

2727
[project.optional-dependencies]
2828
dev = [
29-
"flake8",
3029
"black",
31-
"isort",
3230
"pytest",
3331
"pytest-cov",
3432
"pretend",
3533
"coverage[toml]",
3634
"twine",
3735
"pdoc3",
3836
"mypy",
37+
"ruff",
3938
]
4039

4140
[project.scripts]
@@ -56,14 +55,14 @@ Documentation = "https://trailofbits.github.io/blight/"
5655
Issues = "https://github.com/trailofbits/blight/issues"
5756
Source = "https://github.com/trailofbits/blight"
5857

59-
[tool.isort]
60-
line_length = 100
61-
multi_line_output = 3
62-
known_first_party = "blight"
63-
6458
[tool.black]
6559
line-length = 100
6660

6761
[tool.coverage.run]
6862
# don't attempt code coverage for the CLI entrypoints
6963
omit = ["src/blight/cli.py"]
64+
65+
[tool.ruff]
66+
line-length = 100
67+
select = ["E", "F", "W", "UP", "I", "N", "YTT", "BLE", "C4", "SIM"]
68+
target-version = "py37"

src/blight/action.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class CompilerAction(CCAction, CXXAction):
8282
"""
8383

8484
def _should_run_on(self, tool: Tool) -> bool:
85-
return isinstance(tool, blight.tool.CC) or isinstance(tool, blight.tool.CXX)
85+
return isinstance(tool, (blight.tool.CC, blight.tool.CXX))
8686

8787

8888
class CPPAction(Action):

src/blight/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class BuildError(BlightError):
1919
pass
2020

2121

22-
class SkipRun(BlightError):
22+
class SkipRun(BlightError): # noqa: N818
2323
"""
2424
A special error that `before_run` actions can raise to tell the underlying
2525
tool not to actually run.

src/blight/tool.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
CompilerStage,
2323
Lang,
2424
OptLevel,
25-
Std
25+
Std,
2626
)
2727
from blight.exceptions import BlightError, BuildError, SkipRun
2828
from blight.protocols import CanonicalizedArgsProtocol, IndexedUndefinesProtocol, LangProtocol
@@ -580,10 +580,7 @@ def indexed_undefines(self: IndexedUndefinesProtocol) -> Dict[str, int]:
580580
continue
581581

582582
# Both `-Uname` and `-U name` work in GCC and Clang.
583-
if arg == "-U":
584-
undefine = self.canonicalized_args[idx + 1]
585-
else:
586-
undefine = arg[2:]
583+
undefine = self.canonicalized_args[idx + 1] if arg == "-U" else arg[2:]
587584

588585
indexed_undefines[undefine] = idx
589586

@@ -604,10 +601,7 @@ def defines(self: IndexedUndefinesProtocol) -> List[Tuple[str, str]]:
604601
continue
605602

606603
# Both `-Dname[=value]` and `-D name[=value]` work in GCC and Clang.
607-
if arg == "-D":
608-
define = self.canonicalized_args[idx + 1]
609-
else:
610-
define = arg[2:]
604+
define = self.canonicalized_args[idx + 1] if arg == "-D" else arg[2:]
611605

612606
components = define.split("=", 1)
613607
name = components[0]

test/actions/test_find_outputs.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import json
33

44
import pytest
5-
65
from blight.actions import FindOutputs
76
from blight.actions.find_outputs import OutputKind
87
from blight.tool import CC, INSTALL

test/actions/test_skip_strip.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import pytest
2-
32
from blight.actions import SkipStrip
43
from blight.exceptions import SkipRun
54
from blight.tool import CC, STRIP

0 commit comments

Comments
 (0)