tools/codeformat.py: Add formatter using uncrustify for C, black for Py.
This commit adds a tool, codeformat.py, which will reformat C and Python
code to fit a certain style. By default the tool will reformat (almost)
all the original (ie not 3rd-party) .c, .h and .py files in this
repository. Passing filenames on the command-line to codeformat.py will
reformat only those. Reformatting is done in-place.
uncrustify is used for C reformatting, which is available for many
platforms and can be easily built from source, see
https://github.com/uncrustify/uncrustify. The configuration for uncrustify
is also added in this commit and values are chosen to best match the
existing code style. A small post-processing stage on .c and .h files is
done by codeformat.py (after running uncrustify) to fix up some minor
items:
- space inserted after * when used as multiplication with sizeof
- #if/ifdef/ifndef/elif/else/endif are dedented by one level when they are
configuring if-blocks and case-blocks.
For Python code, the formatter used is black, which can be pip-installed;
see https://github.com/psf/black. The defaults are used, except for line-
length which is set at 99 characters to match the "about 100" line-length
limit used in C code.
The formatting tools used and their configuration were chosen to strike a
balance between keeping existing style and not changing too many lines of
code, and enforcing a relatively strict style (especially for Python code).
This should help to keep the code consistent across everything, and reduce
cognitive load when writing new code to match the style.
2020-02-14 00:42:44 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
#
|
|
|
|
# The MIT License (MIT)
|
|
|
|
#
|
|
|
|
# Copyright (c) 2020 Damien P. George
|
|
|
|
# Copyright (c) 2020 Jim Mussared
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
# THE SOFTWARE.
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import glob
|
|
|
|
import itertools
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
# Relative to top-level repo dir.
|
|
|
|
PATHS = [
|
2021-08-15 17:51:15 +01:00
|
|
|
"drivers/ninaw10/*.[ch]",
|
tools/codeformat.py: Add formatter using uncrustify for C, black for Py.
This commit adds a tool, codeformat.py, which will reformat C and Python
code to fit a certain style. By default the tool will reformat (almost)
all the original (ie not 3rd-party) .c, .h and .py files in this
repository. Passing filenames on the command-line to codeformat.py will
reformat only those. Reformatting is done in-place.
uncrustify is used for C reformatting, which is available for many
platforms and can be easily built from source, see
https://github.com/uncrustify/uncrustify. The configuration for uncrustify
is also added in this commit and values are chosen to best match the
existing code style. A small post-processing stage on .c and .h files is
done by codeformat.py (after running uncrustify) to fix up some minor
items:
- space inserted after * when used as multiplication with sizeof
- #if/ifdef/ifndef/elif/else/endif are dedented by one level when they are
configuring if-blocks and case-blocks.
For Python code, the formatter used is black, which can be pip-installed;
see https://github.com/psf/black. The defaults are used, except for line-
length which is set at 99 characters to match the "about 100" line-length
limit used in C code.
The formatting tools used and their configuration were chosen to strike a
balance between keeping existing style and not changing too many lines of
code, and enforcing a relatively strict style (especially for Python code).
This should help to keep the code consistent across everything, and reduce
cognitive load when writing new code to match the style.
2020-02-14 00:42:44 +00:00
|
|
|
"extmod/*.[ch]",
|
|
|
|
"extmod/btstack/*.[ch]",
|
|
|
|
"extmod/nimble/*.[ch]",
|
2020-05-29 21:05:47 +01:00
|
|
|
"lib/mbedtls_errors/tester.c",
|
2021-07-09 05:19:15 +01:00
|
|
|
"shared/netutils/*.[ch]",
|
|
|
|
"shared/timeutils/*.[ch]",
|
|
|
|
"shared/runtime/*.[ch]",
|
2022-10-04 00:25:56 +01:00
|
|
|
"shared/tinyusb/*.[ch]",
|
tools/codeformat.py: Add formatter using uncrustify for C, black for Py.
This commit adds a tool, codeformat.py, which will reformat C and Python
code to fit a certain style. By default the tool will reformat (almost)
all the original (ie not 3rd-party) .c, .h and .py files in this
repository. Passing filenames on the command-line to codeformat.py will
reformat only those. Reformatting is done in-place.
uncrustify is used for C reformatting, which is available for many
platforms and can be easily built from source, see
https://github.com/uncrustify/uncrustify. The configuration for uncrustify
is also added in this commit and values are chosen to best match the
existing code style. A small post-processing stage on .c and .h files is
done by codeformat.py (after running uncrustify) to fix up some minor
items:
- space inserted after * when used as multiplication with sizeof
- #if/ifdef/ifndef/elif/else/endif are dedented by one level when they are
configuring if-blocks and case-blocks.
For Python code, the formatter used is black, which can be pip-installed;
see https://github.com/psf/black. The defaults are used, except for line-
length which is set at 99 characters to match the "about 100" line-length
limit used in C code.
The formatting tools used and their configuration were chosen to strike a
balance between keeping existing style and not changing too many lines of
code, and enforcing a relatively strict style (especially for Python code).
This should help to keep the code consistent across everything, and reduce
cognitive load when writing new code to match the style.
2020-02-14 00:42:44 +00:00
|
|
|
"mpy-cross/*.[ch]",
|
2022-04-19 02:36:07 +01:00
|
|
|
"ports/**/*.[ch]",
|
tools/codeformat.py: Add formatter using uncrustify for C, black for Py.
This commit adds a tool, codeformat.py, which will reformat C and Python
code to fit a certain style. By default the tool will reformat (almost)
all the original (ie not 3rd-party) .c, .h and .py files in this
repository. Passing filenames on the command-line to codeformat.py will
reformat only those. Reformatting is done in-place.
uncrustify is used for C reformatting, which is available for many
platforms and can be easily built from source, see
https://github.com/uncrustify/uncrustify. The configuration for uncrustify
is also added in this commit and values are chosen to best match the
existing code style. A small post-processing stage on .c and .h files is
done by codeformat.py (after running uncrustify) to fix up some minor
items:
- space inserted after * when used as multiplication with sizeof
- #if/ifdef/ifndef/elif/else/endif are dedented by one level when they are
configuring if-blocks and case-blocks.
For Python code, the formatter used is black, which can be pip-installed;
see https://github.com/psf/black. The defaults are used, except for line-
length which is set at 99 characters to match the "about 100" line-length
limit used in C code.
The formatting tools used and their configuration were chosen to strike a
balance between keeping existing style and not changing too many lines of
code, and enforcing a relatively strict style (especially for Python code).
This should help to keep the code consistent across everything, and reduce
cognitive load when writing new code to match the style.
2020-02-14 00:42:44 +00:00
|
|
|
"py/*.[ch]",
|
|
|
|
]
|
|
|
|
|
|
|
|
EXCLUSIONS = [
|
2022-04-19 02:36:07 +01:00
|
|
|
# The cc3200 port is not fully formatted yet.
|
|
|
|
"ports/cc3200/*/*.[ch]",
|
2023-08-09 11:50:24 +01:00
|
|
|
# ESP-IDF downloads 3rd party code.
|
|
|
|
"ports/esp32/managed_components/*",
|
2022-04-19 02:36:07 +01:00
|
|
|
# The nrf port is not fully formatted yet.
|
|
|
|
"ports/nrf/boards/*.[ch]",
|
|
|
|
"ports/nrf/device/*.[ch]",
|
|
|
|
"ports/nrf/drivers/*.[ch]",
|
|
|
|
"ports/nrf/modules/ble/*.[ch]",
|
|
|
|
"ports/nrf/modules/board/*.[ch]",
|
|
|
|
"ports/nrf/modules/machine/*.[ch]",
|
|
|
|
"ports/nrf/modules/music/*.[ch]",
|
|
|
|
"ports/nrf/modules/ubluepy/*.[ch]",
|
2022-08-18 07:24:27 +01:00
|
|
|
"ports/nrf/modules/os/*.[ch]",
|
|
|
|
"ports/nrf/modules/time/*.[ch]",
|
2022-04-19 02:36:07 +01:00
|
|
|
# STM32 USB dev/host code is mostly 3rd party.
|
|
|
|
"ports/stm32/usbdev/**/*.[ch]",
|
|
|
|
"ports/stm32/usbhost/**/*.[ch]",
|
tools/codeformat.py: Add formatter using uncrustify for C, black for Py.
This commit adds a tool, codeformat.py, which will reformat C and Python
code to fit a certain style. By default the tool will reformat (almost)
all the original (ie not 3rd-party) .c, .h and .py files in this
repository. Passing filenames on the command-line to codeformat.py will
reformat only those. Reformatting is done in-place.
uncrustify is used for C reformatting, which is available for many
platforms and can be easily built from source, see
https://github.com/uncrustify/uncrustify. The configuration for uncrustify
is also added in this commit and values are chosen to best match the
existing code style. A small post-processing stage on .c and .h files is
done by codeformat.py (after running uncrustify) to fix up some minor
items:
- space inserted after * when used as multiplication with sizeof
- #if/ifdef/ifndef/elif/else/endif are dedented by one level when they are
configuring if-blocks and case-blocks.
For Python code, the formatter used is black, which can be pip-installed;
see https://github.com/psf/black. The defaults are used, except for line-
length which is set at 99 characters to match the "about 100" line-length
limit used in C code.
The formatting tools used and their configuration were chosen to strike a
balance between keeping existing style and not changing too many lines of
code, and enforcing a relatively strict style (especially for Python code).
This should help to keep the code consistent across everything, and reduce
cognitive load when writing new code to match the style.
2020-02-14 00:42:44 +00:00
|
|
|
# STM32 build includes generated Python code.
|
|
|
|
"ports/*/build*",
|
|
|
|
]
|
|
|
|
|
|
|
|
# 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")
|
|
|
|
|
|
|
|
|
|
|
|
def list_files(paths, exclusions=None, prefix=""):
|
|
|
|
files = set()
|
|
|
|
for pattern in paths:
|
|
|
|
files.update(glob.glob(os.path.join(prefix, pattern), recursive=True))
|
|
|
|
for pattern in exclusions or []:
|
|
|
|
files.difference_update(glob.fnmatch.filter(files, os.path.join(prefix, pattern)))
|
|
|
|
return sorted(files)
|
|
|
|
|
|
|
|
|
|
|
|
def fixup_c(filename):
|
|
|
|
# Read file.
|
|
|
|
with open(filename) as f:
|
|
|
|
lines = f.readlines()
|
|
|
|
|
|
|
|
# Write out file with fixups.
|
|
|
|
with open(filename, "w", newline="") as f:
|
|
|
|
dedent_stack = []
|
|
|
|
while lines:
|
|
|
|
# Get next line.
|
|
|
|
l = lines.pop(0)
|
|
|
|
|
|
|
|
# Dedent #'s to match indent of following line (not previous line).
|
|
|
|
m = re.match(r"( +)#(if |ifdef |ifndef |elif |else|endif)", l)
|
|
|
|
if m:
|
|
|
|
indent = len(m.group(1))
|
|
|
|
directive = m.group(2)
|
|
|
|
if directive in ("if ", "ifdef ", "ifndef "):
|
|
|
|
l_next = lines[0]
|
|
|
|
indent_next = len(re.match(r"( *)", l_next).group(1))
|
|
|
|
if indent - 4 == indent_next and re.match(r" +(} else |case )", l_next):
|
|
|
|
# This #-line (and all associated ones) needs dedenting by 4 spaces.
|
|
|
|
l = l[4:]
|
|
|
|
dedent_stack.append(indent - 4)
|
|
|
|
else:
|
|
|
|
# This #-line does not need dedenting.
|
|
|
|
dedent_stack.append(-1)
|
|
|
|
else:
|
|
|
|
if dedent_stack[-1] >= 0:
|
|
|
|
# This associated #-line needs dedenting to match the #if.
|
|
|
|
indent_diff = indent - dedent_stack[-1]
|
|
|
|
assert indent_diff >= 0
|
|
|
|
l = l[indent_diff:]
|
|
|
|
if directive == "endif":
|
|
|
|
dedent_stack.pop()
|
|
|
|
|
|
|
|
# Write out line.
|
|
|
|
f.write(l)
|
|
|
|
|
|
|
|
assert not dedent_stack, filename
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
cmd_parser = argparse.ArgumentParser(description="Auto-format C and Python files.")
|
|
|
|
cmd_parser.add_argument("-c", action="store_true", help="Format C code only")
|
|
|
|
cmd_parser.add_argument("-p", action="store_true", help="Format Python code only")
|
2020-05-27 17:10:40 +01:00
|
|
|
cmd_parser.add_argument("-v", action="store_true", help="Enable verbose output")
|
2022-08-29 08:30:14 +01:00
|
|
|
cmd_parser.add_argument(
|
|
|
|
"-f",
|
|
|
|
action="store_true",
|
|
|
|
help="Filter files provided on the command line against the default list of files to check.",
|
|
|
|
)
|
tools/codeformat.py: Add formatter using uncrustify for C, black for Py.
This commit adds a tool, codeformat.py, which will reformat C and Python
code to fit a certain style. By default the tool will reformat (almost)
all the original (ie not 3rd-party) .c, .h and .py files in this
repository. Passing filenames on the command-line to codeformat.py will
reformat only those. Reformatting is done in-place.
uncrustify is used for C reformatting, which is available for many
platforms and can be easily built from source, see
https://github.com/uncrustify/uncrustify. The configuration for uncrustify
is also added in this commit and values are chosen to best match the
existing code style. A small post-processing stage on .c and .h files is
done by codeformat.py (after running uncrustify) to fix up some minor
items:
- space inserted after * when used as multiplication with sizeof
- #if/ifdef/ifndef/elif/else/endif are dedented by one level when they are
configuring if-blocks and case-blocks.
For Python code, the formatter used is black, which can be pip-installed;
see https://github.com/psf/black. The defaults are used, except for line-
length which is set at 99 characters to match the "about 100" line-length
limit used in C code.
The formatting tools used and their configuration were chosen to strike a
balance between keeping existing style and not changing too many lines of
code, and enforcing a relatively strict style (especially for Python code).
This should help to keep the code consistent across everything, and reduce
cognitive load when writing new code to match the style.
2020-02-14 00:42:44 +00:00
|
|
|
cmd_parser.add_argument("files", nargs="*", help="Run on specific globs")
|
|
|
|
args = cmd_parser.parse_args()
|
|
|
|
|
|
|
|
# Setting only one of -c or -p disables the other. If both or neither are set, then do both.
|
|
|
|
format_c = args.c or not args.p
|
|
|
|
format_py = args.p or not args.c
|
|
|
|
|
|
|
|
# Expand the globs passed on the command line, or use the default globs above.
|
|
|
|
files = []
|
|
|
|
if args.files:
|
|
|
|
files = list_files(args.files)
|
2022-08-29 08:30:14 +01:00
|
|
|
if args.f:
|
|
|
|
# Filter against the default list of files. This is a little fiddly
|
|
|
|
# because we need to apply both the inclusion globs given in PATHS
|
|
|
|
# as well as the EXCLUSIONS, and use absolute paths
|
|
|
|
files = set(os.path.abspath(f) for f in files)
|
|
|
|
all_files = set(list_files(PATHS, EXCLUSIONS, TOP))
|
|
|
|
if args.v: # In verbose mode, log any files we're skipping
|
|
|
|
for f in files - all_files:
|
|
|
|
print("Not checking: {}".format(f))
|
|
|
|
files = list(files & all_files)
|
tools/codeformat.py: Add formatter using uncrustify for C, black for Py.
This commit adds a tool, codeformat.py, which will reformat C and Python
code to fit a certain style. By default the tool will reformat (almost)
all the original (ie not 3rd-party) .c, .h and .py files in this
repository. Passing filenames on the command-line to codeformat.py will
reformat only those. Reformatting is done in-place.
uncrustify is used for C reformatting, which is available for many
platforms and can be easily built from source, see
https://github.com/uncrustify/uncrustify. The configuration for uncrustify
is also added in this commit and values are chosen to best match the
existing code style. A small post-processing stage on .c and .h files is
done by codeformat.py (after running uncrustify) to fix up some minor
items:
- space inserted after * when used as multiplication with sizeof
- #if/ifdef/ifndef/elif/else/endif are dedented by one level when they are
configuring if-blocks and case-blocks.
For Python code, the formatter used is black, which can be pip-installed;
see https://github.com/psf/black. The defaults are used, except for line-
length which is set at 99 characters to match the "about 100" line-length
limit used in C code.
The formatting tools used and their configuration were chosen to strike a
balance between keeping existing style and not changing too many lines of
code, and enforcing a relatively strict style (especially for Python code).
This should help to keep the code consistent across everything, and reduce
cognitive load when writing new code to match the style.
2020-02-14 00:42:44 +00:00
|
|
|
else:
|
|
|
|
files = list_files(PATHS, EXCLUSIONS, TOP)
|
|
|
|
|
|
|
|
# Run tool on N files at a time (to avoid making the command line too long).
|
2023-10-21 07:40:27 +01:00
|
|
|
def batch(cmd, N=200):
|
|
|
|
files_iter = iter(files)
|
tools/codeformat.py: Add formatter using uncrustify for C, black for Py.
This commit adds a tool, codeformat.py, which will reformat C and Python
code to fit a certain style. By default the tool will reformat (almost)
all the original (ie not 3rd-party) .c, .h and .py files in this
repository. Passing filenames on the command-line to codeformat.py will
reformat only those. Reformatting is done in-place.
uncrustify is used for C reformatting, which is available for many
platforms and can be easily built from source, see
https://github.com/uncrustify/uncrustify. The configuration for uncrustify
is also added in this commit and values are chosen to best match the
existing code style. A small post-processing stage on .c and .h files is
done by codeformat.py (after running uncrustify) to fix up some minor
items:
- space inserted after * when used as multiplication with sizeof
- #if/ifdef/ifndef/elif/else/endif are dedented by one level when they are
configuring if-blocks and case-blocks.
For Python code, the formatter used is black, which can be pip-installed;
see https://github.com/psf/black. The defaults are used, except for line-
length which is set at 99 characters to match the "about 100" line-length
limit used in C code.
The formatting tools used and their configuration were chosen to strike a
balance between keeping existing style and not changing too many lines of
code, and enforcing a relatively strict style (especially for Python code).
This should help to keep the code consistent across everything, and reduce
cognitive load when writing new code to match the style.
2020-02-14 00:42:44 +00:00
|
|
|
while True:
|
2023-10-21 07:40:27 +01:00
|
|
|
file_args = list(itertools.islice(files_iter, N))
|
tools/codeformat.py: Add formatter using uncrustify for C, black for Py.
This commit adds a tool, codeformat.py, which will reformat C and Python
code to fit a certain style. By default the tool will reformat (almost)
all the original (ie not 3rd-party) .c, .h and .py files in this
repository. Passing filenames on the command-line to codeformat.py will
reformat only those. Reformatting is done in-place.
uncrustify is used for C reformatting, which is available for many
platforms and can be easily built from source, see
https://github.com/uncrustify/uncrustify. The configuration for uncrustify
is also added in this commit and values are chosen to best match the
existing code style. A small post-processing stage on .c and .h files is
done by codeformat.py (after running uncrustify) to fix up some minor
items:
- space inserted after * when used as multiplication with sizeof
- #if/ifdef/ifndef/elif/else/endif are dedented by one level when they are
configuring if-blocks and case-blocks.
For Python code, the formatter used is black, which can be pip-installed;
see https://github.com/psf/black. The defaults are used, except for line-
length which is set at 99 characters to match the "about 100" line-length
limit used in C code.
The formatting tools used and their configuration were chosen to strike a
balance between keeping existing style and not changing too many lines of
code, and enforcing a relatively strict style (especially for Python code).
This should help to keep the code consistent across everything, and reduce
cognitive load when writing new code to match the style.
2020-02-14 00:42:44 +00:00
|
|
|
if not file_args:
|
|
|
|
break
|
|
|
|
subprocess.check_call(cmd + file_args)
|
|
|
|
|
|
|
|
# Format C files with uncrustify.
|
|
|
|
if format_c:
|
2020-05-27 17:10:40 +01:00
|
|
|
command = ["uncrustify", "-c", UNCRUSTIFY_CFG, "-lC", "--no-backup"]
|
|
|
|
if not args.v:
|
|
|
|
command.append("-q")
|
2023-10-21 07:40:27 +01:00
|
|
|
batch(command)
|
|
|
|
for file in files:
|
tools/codeformat.py: Add formatter using uncrustify for C, black for Py.
This commit adds a tool, codeformat.py, which will reformat C and Python
code to fit a certain style. By default the tool will reformat (almost)
all the original (ie not 3rd-party) .c, .h and .py files in this
repository. Passing filenames on the command-line to codeformat.py will
reformat only those. Reformatting is done in-place.
uncrustify is used for C reformatting, which is available for many
platforms and can be easily built from source, see
https://github.com/uncrustify/uncrustify. The configuration for uncrustify
is also added in this commit and values are chosen to best match the
existing code style. A small post-processing stage on .c and .h files is
done by codeformat.py (after running uncrustify) to fix up some minor
items:
- space inserted after * when used as multiplication with sizeof
- #if/ifdef/ifndef/elif/else/endif are dedented by one level when they are
configuring if-blocks and case-blocks.
For Python code, the formatter used is black, which can be pip-installed;
see https://github.com/psf/black. The defaults are used, except for line-
length which is set at 99 characters to match the "about 100" line-length
limit used in C code.
The formatting tools used and their configuration were chosen to strike a
balance between keeping existing style and not changing too many lines of
code, and enforcing a relatively strict style (especially for Python code).
This should help to keep the code consistent across everything, and reduce
cognitive load when writing new code to match the style.
2020-02-14 00:42:44 +00:00
|
|
|
fixup_c(file)
|
|
|
|
|
2023-10-21 07:40:27 +01:00
|
|
|
# Format Python files with "ruff format" (using config in pyproject.toml).
|
tools/codeformat.py: Add formatter using uncrustify for C, black for Py.
This commit adds a tool, codeformat.py, which will reformat C and Python
code to fit a certain style. By default the tool will reformat (almost)
all the original (ie not 3rd-party) .c, .h and .py files in this
repository. Passing filenames on the command-line to codeformat.py will
reformat only those. Reformatting is done in-place.
uncrustify is used for C reformatting, which is available for many
platforms and can be easily built from source, see
https://github.com/uncrustify/uncrustify. The configuration for uncrustify
is also added in this commit and values are chosen to best match the
existing code style. A small post-processing stage on .c and .h files is
done by codeformat.py (after running uncrustify) to fix up some minor
items:
- space inserted after * when used as multiplication with sizeof
- #if/ifdef/ifndef/elif/else/endif are dedented by one level when they are
configuring if-blocks and case-blocks.
For Python code, the formatter used is black, which can be pip-installed;
see https://github.com/psf/black. The defaults are used, except for line-
length which is set at 99 characters to match the "about 100" line-length
limit used in C code.
The formatting tools used and their configuration were chosen to strike a
balance between keeping existing style and not changing too many lines of
code, and enforcing a relatively strict style (especially for Python code).
This should help to keep the code consistent across everything, and reduce
cognitive load when writing new code to match the style.
2020-02-14 00:42:44 +00:00
|
|
|
if format_py:
|
2023-10-21 07:40:27 +01:00
|
|
|
command = ["ruff", "format"]
|
2020-05-27 17:10:40 +01:00
|
|
|
if args.v:
|
|
|
|
command.append("-v")
|
|
|
|
else:
|
|
|
|
command.append("-q")
|
2023-10-21 07:40:27 +01:00
|
|
|
command.append(".")
|
|
|
|
subprocess.check_call(command, cwd=TOP)
|
tools/codeformat.py: Add formatter using uncrustify for C, black for Py.
This commit adds a tool, codeformat.py, which will reformat C and Python
code to fit a certain style. By default the tool will reformat (almost)
all the original (ie not 3rd-party) .c, .h and .py files in this
repository. Passing filenames on the command-line to codeformat.py will
reformat only those. Reformatting is done in-place.
uncrustify is used for C reformatting, which is available for many
platforms and can be easily built from source, see
https://github.com/uncrustify/uncrustify. The configuration for uncrustify
is also added in this commit and values are chosen to best match the
existing code style. A small post-processing stage on .c and .h files is
done by codeformat.py (after running uncrustify) to fix up some minor
items:
- space inserted after * when used as multiplication with sizeof
- #if/ifdef/ifndef/elif/else/endif are dedented by one level when they are
configuring if-blocks and case-blocks.
For Python code, the formatter used is black, which can be pip-installed;
see https://github.com/psf/black. The defaults are used, except for line-
length which is set at 99 characters to match the "about 100" line-length
limit used in C code.
The formatting tools used and their configuration were chosen to strike a
balance between keeping existing style and not changing too many lines of
code, and enforcing a relatively strict style (especially for Python code).
This should help to keep the code consistent across everything, and reduce
cognitive load when writing new code to match the style.
2020-02-14 00:42:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|