diff options
author | Jim Mussared <jim.mussared@gmail.com> | 2023-10-21 17:40:27 +1100 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2023-11-03 13:30:38 +1100 |
commit | 303ccca7c62bd48b9f6a519549caf0d0f3d2624e (patch) | |
tree | 3957164f749abc13527add3647afd3439c50a04e /tools/codeformat.py | |
parent | 7ad84e0422a521fe97ce5d742df280c6b8a3ff87 (diff) |
all: Replace "black" with "ruff format".
- Add config for [tool.ruff.format] to pyproject.toml.
- Update pre-commit to run both ruff and ruff-format (and only check C
files when running codeformat.py)
- Update CI.
- Simplify codeformat.py to remove all the Python-specific logic (just run
"ruff format" directly).
This work was funded through GitHub Sponsors.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
Diffstat (limited to 'tools/codeformat.py')
-rwxr-xr-x | tools/codeformat.py | 42 |
1 files changed, 9 insertions, 33 deletions
diff --git a/tools/codeformat.py b/tools/codeformat.py index aee989434..bd37aec46 100755 --- a/tools/codeformat.py +++ b/tools/codeformat.py @@ -34,7 +34,6 @@ import subprocess # Relative to top-level repo dir. PATHS = [ - # C "drivers/ninaw10/*.[ch]", "extmod/*.[ch]", "extmod/btstack/*.[ch]", @@ -47,14 +46,6 @@ PATHS = [ "mpy-cross/*.[ch]", "ports/**/*.[ch]", "py/*.[ch]", - # Python - "drivers/**/*.py", - "examples/**/*.py", - "extmod/**/*.py", - "ports/**/*.py", - "py/**/*.py", - "tools/**/*.py", - "tests/**/*.py", ] EXCLUSIONS = [ @@ -78,23 +69,12 @@ EXCLUSIONS = [ "ports/stm32/usbhost/**/*.[ch]", # STM32 build includes generated Python code. "ports/*/build*", - # not real python files - "tests/**/repl_*.py", - # needs careful attention before applying automatic formatting - "tests/basics/*.py", ] # Path to repo top-level dir. TOP = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) UNCRUSTIFY_CFG = os.path.join(TOP, "tools/uncrustify.cfg") -PYPROJECT_TOML = os.path.join(TOP, "pyproject.toml") - -C_EXTS = ( - ".c", - ".h", -) -PY_EXTS = (".py",) def list_files(paths, exclusions=None, prefix=""): @@ -182,16 +162,11 @@ def main(): else: files = list_files(PATHS, EXCLUSIONS, TOP) - # Extract files matching a specific language. - def lang_files(exts): - for file in files: - if os.path.splitext(file)[1].lower() in exts: - yield file - # Run tool on N files at a time (to avoid making the command line too long). - def batch(cmd, files, N=200): + def batch(cmd, N=200): + files_iter = iter(files) while True: - file_args = list(itertools.islice(files, N)) + file_args = list(itertools.islice(files_iter, N)) if not file_args: break subprocess.check_call(cmd + file_args) @@ -201,18 +176,19 @@ def main(): command = ["uncrustify", "-c", UNCRUSTIFY_CFG, "-lC", "--no-backup"] if not args.v: command.append("-q") - batch(command, lang_files(C_EXTS)) - for file in lang_files(C_EXTS): + batch(command) + for file in files: fixup_c(file) - # Format Python files with black. + # Format Python files with "ruff format" (using config in pyproject.toml). if format_py: - command = ["black", "--fast", "--config={}".format(PYPROJECT_TOML)] + command = ["ruff", "format"] if args.v: command.append("-v") else: command.append("-q") - batch(command, lang_files(PY_EXTS)) + command.append(".") + subprocess.check_call(command, cwd=TOP) if __name__ == "__main__": |