Skip to content

Commit

Permalink
feat: Use ruff instead of flake8 and black both in pre-commit and CI
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Beran committed Jan 31, 2024
1 parent 2381711 commit 1d5fcb3
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 51 deletions.
8 changes: 4 additions & 4 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 5 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
59 changes: 59 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
@@ -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

5 changes: 3 additions & 2 deletions espefuse/efuse/base_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ def check_arg_value(efuse, new_value):
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(
Expand Down
5 changes: 2 additions & 3 deletions espefuse/efuse/esp32c2/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,15 +368,14 @@ def print_field(e, new_value):


class EfuseKeyPurposeField(EfuseField):
# fmt: off
KEY_PURPOSES = [
# fmt: off
("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"]
2 changes: 1 addition & 1 deletion esptool/bin_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,7 @@ def read_section_header(offs):
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
Expand Down
5 changes: 3 additions & 2 deletions esptool/cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,8 +892,9 @@ def get_key_from_value(dict, val):
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":
Expand Down
27 changes: 0 additions & 27 deletions setup.cfg

This file was deleted.

5 changes: 1 addition & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,9 @@ def find_version(*file_paths):
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",
Expand Down

0 comments on commit 1d5fcb3

Please sign in to comment.