feat: Use ruff instead of flake8 and black both in pre-commit and CI

This commit is contained in:
Jan Beran
2024-01-22 12:49:59 +01:00
parent 2381711ebb
commit 1d5fcb31ab
9 changed files with 78 additions and 51 deletions

View File

@@ -128,12 +128,12 @@ check_python_style:
codequality: code_quality_report.json
when: on_failure
script:
# This step installs any 'dev' dependencies (ie flake8, Black)
# This step installs any 'dev' dependencies (ie ruff)
# The runner should cache the downloads, so still quite fast.
- pip install -e .[dev] --prefer-binary
- python -m flake8 --exit-zero --format gl-codeclimate --output-file code_quality_report.json
- python -m flake8
- python -m black --check --diff .
- python -m ruff check --exit-zero --output-format=gitlab --output-file=code_quality_report.json
- python -m ruff check
- python -m ruff format
.run_esptool: &run_esptool |
esptool.py --help

View File

@@ -1,13 +1,10 @@
repos:
- repo: https://github.com/PyCQA/flake8
rev: 6.1.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.14
hooks:
- id: flake8
additional_dependencies: [flake8-import-order]
- repo: https://github.com/psf/black
rev: 23.11.0
hooks:
- id: black
- id: ruff # Runs ruff linter (replaces flake8)
args: [--fix, --exit-non-zero-on-fix] # --fix for fixing errors
- id: ruff-format
- repo: https://github.com/espressif/conventional-precommit-linter
rev: v1.4.0
hooks:

59
.ruff.toml Normal file
View File

@@ -0,0 +1,59 @@
# https://docs.astral.sh/ruff/settings/
# Exclude a variety of commonly ignored directories.
exclude = [
".eggs",
".git",
"__pycache__"
]
line-length = 88
select = ['E', 'F', 'W']
ignore = ["E203"]
# Assume Python 3.7
target-version = "py37"
[per-file-ignores]
# tests often manipulate sys.path before importing the main tools, so ignore import order violations
"test/*.py" = ["E402"]
# multiple spaces after ',' and long lines - used for visual layout of eFuse data
"espefuse/efuse/*/mem_definition.py" = ["E241", "E501"]
"espefuse/efuse/*/operations.py" = ["E241", "E501", "F401"]
"espefuse/efuse/*/fields.py" = ["E241", "E501"]
# ignore long lines - used for RS encoding pairs
"test/test_modules.py" = ["E501"]
# don't check for unused imports in __init__.py files
"__init__.py" = ["F401"]
# allow definition from star imports in docs config
"docs/conf_common.py" = ["F405"]
[lint]
# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default.
# Unlike Flake8, Ruff doesn't enable pycodestyle warnings (`W`) or
# McCabe complexity (`C901`) by default.
select = ["E4", "E7", "E9", "F"]
ignore = []
# Allow fix for all enabled rules (when `--fix`) is provided.
fixable = ["ALL"]
unfixable = []
# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
# ruff-format hook configuration
[format]
quote-style = "double"
indent-style = "space"
docstring-code-format = true

View File

@@ -58,8 +58,9 @@ class CheckArgValue(object):
elif efuse.efuse_type.startswith("bytes"):
if new_value is None:
raise esptool.FatalError(
"New value required for efuse '{}' "
"(given None)".format(efuse.name)
"New value required for efuse '{}' (given None)".format(
efuse.name
)
)
if len(new_value) * 8 != efuse.bitarray.len:
raise esptool.FatalError(

View File

@@ -368,15 +368,14 @@ class EfuseMacField(EfuseField):
class EfuseKeyPurposeField(EfuseField):
KEY_PURPOSES = [
# fmt: off
KEY_PURPOSES = [
("USER", 0, None), # User purposes (software-only use)
("XTS_AES_128_KEY", 1, None), # (whole 256bits) flash/PSRAM encryption
("XTS_AES_128_KEY_DERIVED_FROM_128_EFUSE_BITS", 2, None), # (lo 128bits) flash/PSRAM encryption
("SECURE_BOOT_DIGEST", 3, "DIGEST"),
# (hi 128bits) Secure Boot key digest
# fmt: on
]
] # fmt: on
KEY_PURPOSES_NAME = [name[0] for name in KEY_PURPOSES]
DIGEST_KEY_PURPOSES = [name[0] for name in KEY_PURPOSES if name[2] == "DIGEST"]

View File

@@ -1252,7 +1252,7 @@ class ELFFile(object):
nobits_secitons = [s for s in all_sections if s[1] == ELFFile.SEC_TYPE_NOBITS]
# search for the string table section
if not (shstrndx * self.LEN_SEC_HEADER) in section_header_offsets:
if (shstrndx * self.LEN_SEC_HEADER) not in section_header_offsets:
raise FatalError("ELF file has no STRTAB section at shstrndx %d" % shstrndx)
_, sec_type, _, sec_size, sec_offs = read_section_header(
shstrndx * self.LEN_SEC_HEADER

View File

@@ -892,8 +892,9 @@ def image_info(args):
ESP8266V2FirmwareImage.IMAGE_V2_MAGIC,
]:
raise FatalError(
"This is not a valid image "
"(invalid magic number: {:#x})".format(magic)
"This is not a valid image " "(invalid magic number: {:#x})".format(
magic
)
)
if args.chip == "auto":

View File

@@ -1,27 +0,0 @@
[flake8]
exclude = .git,__pycache__,.eggs,build
ignore =
# multiple spaces before operator - used for visual indent of constants in some files
E221,
per-file-ignores =
# tests often manipulate sys.path before importing the main tools, so ignore import order violations
test/*.py: E402,
# multiple spaces after ',' and long lines - used for visual layout of eFuse data
espefuse/efuse/*/mem_definition.py: E241, E501,
espefuse/efuse/*/operations.py: E241, E501, F401,
espefuse/efuse/*/fields.py: E241, E501,
# ignore long lines - used for RS encoding pairs
test/test_modules.py: E501,
# don't check for unused imports in __init__.py files
__init__.py: F401,
# allow definition from star imports in docs config
docs/conf_common.py: F405,
# Compatibility with Black
max-line-length = 88
extend-ignore = E203, W503,

View File

@@ -108,12 +108,9 @@ setup(
setup_requires=(["wheel"] if "bdist_wheel" in sys.argv else []),
extras_require={
"dev": [
"flake8>=3.2.0",
"flake8-import-order",
"flake8-gl-codeclimate",
"ruff>=0.1.14",
"pyelftools",
"coverage~=6.0",
"black",
"pre-commit",
"pytest",
"pytest-rerunfailures",