diff --git a/.flake8 b/.flake8 deleted file mode 100644 index 7da1f96..0000000 --- a/.flake8 +++ /dev/null @@ -1,2 +0,0 @@ -[flake8] -max-line-length = 100 diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..d38eb4d --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,13 @@ +name: Lint + +on: + push: + branches: + - master + pull_request: + +jobs: + lint: + uses: trailofbits/.github/.github/workflows/lint.yml@v0.0.2 + with: + python-version: "3.7" diff --git a/.github/workflows/ci.yml b/.github/workflows/tests.yml similarity index 68% rename from .github/workflows/ci.yml rename to .github/workflows/tests.yml index b0470c5..5c6cfab 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/tests.yml @@ -7,19 +7,6 @@ on: pull_request: jobs: - lint: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - - uses: actions/setup-python@v4 - with: - python-version: "3.7" - - - run: make dev - - - run: make lint - test: strategy: matrix: diff --git a/Makefile b/Makefile index 73e872f..f51b6b2 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,17 @@ +# Optionally overriden by the user, if they're using a virtual environment manager. +VENV ?= env +VENV_EXISTS := $(VENV)/pyvenv.cfg + +# On Windows, venv scripts/shims are under `Scripts` instead of `bin`. +VENV_BIN := $(VENV)/bin +ifeq ($(OS),Windows_NT) + VENV_BIN := $(VENV)/Scripts +endif + +# Optionally overridden by the user/CI, to limit the installation to a specific +# subset of development dependencies. +BLIGHT_EXTRA := dev + ALL_PY_SRCS := $(shell find src -name '*.py') \ $(shell find test -name '*.py') @@ -5,36 +19,42 @@ ALL_PY_SRCS := $(shell find src -name '*.py') \ all: @echo "Run my targets individually!" -.PHONY: dev -dev: - test -d env || python -m venv env - . env/bin/activate && \ +$(VENV)/pyvenv.cfg: pyproject.toml + python -m venv env + . $(VENV_BIN)/activate && \ pip install --upgrade pip setuptools && \ - pip install -e .[dev] + pip install -e .[$(BLIGHT_EXTRA)] + +.PHONY: dev +dev: $(VENV)/pyvenv.cfg .PHONY: lint -lint: - . env/bin/activate && \ - black $(ALL_PY_SRCS) && \ - isort $(ALL_PY_SRCS) && \ - flake8 $(ALL_PY_SRCS) && \ - mypy src && \ - git diff --exit-code +lint: $(VENV_EXISTS) + . $(VENV_BIN)/activate && \ + black --check $(ALL_PY_SRCS) && \ + ruff $(ALL_PY_SRCS) && \ + mypy src + +.PHONY: reformat +reformat: $(VENV_EXISTS) + . $(VENV_BIN)/activate && \ + ruff --fix $(ALL_PY_SRCS) && \ + black $(ALL_PY_SRCS) .PHONY: test -test: - . env/bin/activate && \ +test: $(VENV_EXISTS) + . $(VENV_BIN)/activate && \ pytest --cov=blight test/ && \ python -m coverage report -m --fail-under 100 .PHONY: doc -doc: - . env/bin/activate && \ +doc: $(VENV_EXISTS) + . $(VENV_BIN)/activate && \ PYTHONWARNINGS='error::UserWarning' pdoc --force --html blight .PHONY: package -package: - . env/bin/activate && \ +package: $(VENV_EXISTS) + . $(VENV_BIN)/activate && \ python -m build && \ twine upload --repository pypi dist/* diff --git a/pyproject.toml b/pyproject.toml index 4383b0e..7234040 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,9 +26,7 @@ requires-python = ">=3.7" [project.optional-dependencies] dev = [ - "flake8", "black", - "isort", "pytest", "pytest-cov", "pretend", @@ -36,6 +34,7 @@ dev = [ "twine", "pdoc3", "mypy", + "ruff", ] [project.scripts] @@ -56,14 +55,14 @@ Documentation = "https://trailofbits.github.io/blight/" Issues = "https://github.com/trailofbits/blight/issues" Source = "https://github.com/trailofbits/blight" -[tool.isort] -line_length = 100 -multi_line_output = 3 -known_first_party = "blight" - [tool.black] line-length = 100 [tool.coverage.run] # don't attempt code coverage for the CLI entrypoints omit = ["src/blight/cli.py"] + +[tool.ruff] +line-length = 100 +select = ["E", "F", "W", "UP", "I", "N", "YTT", "BLE", "C4", "SIM"] +target-version = "py37" diff --git a/src/blight/action.py b/src/blight/action.py index d70fe95..c81824c 100644 --- a/src/blight/action.py +++ b/src/blight/action.py @@ -82,7 +82,7 @@ class CompilerAction(CCAction, CXXAction): """ def _should_run_on(self, tool: Tool) -> bool: - return isinstance(tool, blight.tool.CC) or isinstance(tool, blight.tool.CXX) + return isinstance(tool, (blight.tool.CC, blight.tool.CXX)) class CPPAction(Action): diff --git a/src/blight/exceptions.py b/src/blight/exceptions.py index b1db2d6..b69d175 100644 --- a/src/blight/exceptions.py +++ b/src/blight/exceptions.py @@ -19,7 +19,7 @@ class BuildError(BlightError): pass -class SkipRun(BlightError): +class SkipRun(BlightError): # noqa: N818 """ A special error that `before_run` actions can raise to tell the underlying tool not to actually run. diff --git a/src/blight/tool.py b/src/blight/tool.py index 3f69777..38afa1b 100644 --- a/src/blight/tool.py +++ b/src/blight/tool.py @@ -22,7 +22,7 @@ CompilerStage, Lang, OptLevel, - Std + Std, ) from blight.exceptions import BlightError, BuildError, SkipRun from blight.protocols import CanonicalizedArgsProtocol, IndexedUndefinesProtocol, LangProtocol @@ -580,10 +580,7 @@ def indexed_undefines(self: IndexedUndefinesProtocol) -> Dict[str, int]: continue # Both `-Uname` and `-U name` work in GCC and Clang. - if arg == "-U": - undefine = self.canonicalized_args[idx + 1] - else: - undefine = arg[2:] + undefine = self.canonicalized_args[idx + 1] if arg == "-U" else arg[2:] indexed_undefines[undefine] = idx @@ -604,10 +601,7 @@ def defines(self: IndexedUndefinesProtocol) -> List[Tuple[str, str]]: continue # Both `-Dname[=value]` and `-D name[=value]` work in GCC and Clang. - if arg == "-D": - define = self.canonicalized_args[idx + 1] - else: - define = arg[2:] + define = self.canonicalized_args[idx + 1] if arg == "-D" else arg[2:] components = define.split("=", 1) name = components[0] diff --git a/test/actions/test_find_outputs.py b/test/actions/test_find_outputs.py index c43015a..3f5c01a 100644 --- a/test/actions/test_find_outputs.py +++ b/test/actions/test_find_outputs.py @@ -2,7 +2,6 @@ import json import pytest - from blight.actions import FindOutputs from blight.actions.find_outputs import OutputKind from blight.tool import CC, INSTALL diff --git a/test/actions/test_skip_strip.py b/test/actions/test_skip_strip.py index 2986a01..8b67e6d 100644 --- a/test/actions/test_skip_strip.py +++ b/test/actions/test_skip_strip.py @@ -1,5 +1,4 @@ import pytest - from blight.actions import SkipStrip from blight.exceptions import SkipRun from blight.tool import CC, STRIP diff --git a/test/test_action.py b/test/test_action.py index 0afb51f..019d103 100644 --- a/test/test_action.py +++ b/test/test_action.py @@ -1,5 +1,4 @@ import pytest - from blight import action, tool diff --git a/test/test_tool.py b/test/test_tool.py index 5cac4fa..a622b7a 100644 --- a/test/test_tool.py +++ b/test/test_tool.py @@ -7,7 +7,6 @@ import pretend import pytest - from blight import tool, util from blight.enums import CodeModel, CompilerFamily, CompilerStage, Lang, OptLevel, Std from blight.exceptions import BlightError, BuildError diff --git a/test/test_util.py b/test/test_util.py index 6411c5f..655105c 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -3,7 +3,6 @@ import pretend import pytest - from blight import util from blight.actions import Record from blight.exceptions import BlightError