aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Mohr <mohrr@google.com>2023-01-26 04:18:30 +0000
committerCQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com>2023-01-26 04:18:30 +0000
commita19b9fd3a8b35d5d206a1793f19be6398d49c90f (patch)
tree67043a44fbc73856db947008f15d472f1ca0dac1
parent352fe28de3f34675ed30553629590aa329d66f98 (diff)
downloadpigweed-a19b9fd3a8b35d5d206a1793f19be6398d49c90f.tar.gz
pw_presubmit: Allow customizing black
Allow customizing the executable used for black. Also, explicitly specify a config file for black if one is present in a couple locations. It now looks at the following locations, in order: * $PW_PROJECT_ROOT/.black.toml * $PW_PROJECT_ROOT/pyproject.toml * $PW_ROOT/.black.toml * $PW_ROOT/pyproject.toml Bug: b/264578594 Change-Id: Ic1ea174ca286b6f7ffd262da3dea4d83bff27c99 Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/126211 Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com> Reviewed-by: Wyatt Hepler <hepler@google.com> Pigweed-Auto-Submit: Rob Mohr <mohrr@google.com>
-rw-r--r--.black.toml (renamed from pyproject.toml)2
-rw-r--r--pw_presubmit/docs.rst2
-rwxr-xr-xpw_presubmit/py/pw_presubmit/format_code.py46
-rwxr-xr-xpw_presubmit/py/pw_presubmit/pigweed_presubmit.py2
4 files changed, 37 insertions, 15 deletions
diff --git a/pyproject.toml b/.black.toml
index 8bc1b1ef7..c9c0c31b3 100644
--- a/pyproject.toml
+++ b/.black.toml
@@ -1,4 +1,4 @@
-# Copyright 2022 The Pigweed Authors
+# Copyright 2023 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
diff --git a/pw_presubmit/docs.rst b/pw_presubmit/docs.rst
index 2fcde5bd6..f86a13211 100644
--- a/pw_presubmit/docs.rst
+++ b/pw_presubmit/docs.rst
@@ -171,7 +171,7 @@ Formatting checks for a variety of languages are available from
``pw_presubmit.format_code``. These include C/C++, Java, Go, Python, GN, and
others. All of these checks can be included by adding
``pw_presubmit.format_code.presubmit_checks()`` to a presubmit program. These
-all use language-specific formatters like clang-format or yapf.
+all use language-specific formatters like clang-format or black.
These will suggest fixes using ``pw format --fix``.
diff --git a/pw_presubmit/py/pw_presubmit/format_code.py b/pw_presubmit/py/pw_presubmit/format_code.py
index eacac2343..596808c06 100755
--- a/pw_presubmit/py/pw_presubmit/format_code.py
+++ b/pw_presubmit/py/pw_presubmit/format_code.py
@@ -39,6 +39,7 @@ from typing import (
NamedTuple,
Optional,
Pattern,
+ Sequence,
TextIO,
Tuple,
Union,
@@ -283,22 +284,37 @@ def fix_py_format_yapf(ctx: _Context) -> Dict[Path, str]:
return {}
-_BLACK_OPTS = (
- '--skip-string-normalization',
- '--line-length',
- '80',
- '--target-version',
- 'py310',
- '--include',
- r'\.pyi?$',
-)
+BLACK = 'black'
+
+
+def _enumerate_black_configs() -> Iterable[Path]:
+ if directory := os.environ.get('PW_PROJECT_ROOT'):
+ yield Path(directory, '.black.toml')
+ yield Path(directory, 'pyproject.toml')
+
+ if directory := os.environ.get('PW_ROOT'):
+ yield Path(directory, '.black.toml')
+ yield Path(directory, 'pyproject.toml')
+
+
+def _black_config_args() -> Sequence[Union[str, Path]]:
+ config = None
+ for config_location in _enumerate_black_configs():
+ if config_location.is_file():
+ config = config_location
+ break
+
+ config_args: Sequence[Union[str, Path]] = ()
+ if config:
+ config_args = ('--config', config)
+ return config_args
def _black_multiple_files(ctx: _Context) -> Tuple[str, ...]:
changed_paths: List[str] = []
for line in (
log_run(
- ['black', '--check', *_BLACK_OPTS, *ctx.paths],
+ [BLACK, '--check', *_black_config_args(), *ctx.paths],
capture_output=True,
)
.stderr.decode()
@@ -325,7 +341,10 @@ def check_py_format_black(ctx: _Context) -> Dict[Path, str]:
build = Path(temp) / os.path.basename(path)
build.write_bytes(data)
- proc = log_run(['black', *_BLACK_OPTS, build], capture_output=True)
+ proc = log_run(
+ [BLACK, *_black_config_args(), build],
+ capture_output=True,
+ )
if proc.returncode:
stderr = proc.stderr.decode(errors='replace')
stderr = stderr.replace(str(build), str(path))
@@ -352,7 +371,10 @@ def fix_py_format_black(ctx: _Context) -> Dict[Path, str]:
if not str(path).endswith(paths):
continue
- proc = log_run(['black', *_BLACK_OPTS, path], capture_output=True)
+ proc = log_run(
+ [BLACK, *_black_config_args(), path],
+ capture_output=True,
+ )
if proc.returncode:
errors[path] = proc.stderr.decode()
return errors
diff --git a/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py b/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py
index be287d1ad..fc1394344 100755
--- a/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py
+++ b/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py
@@ -647,7 +647,7 @@ _GN_SOURCES_IN_BUILD = (
SOURCE_FILES_FILTER = presubmit.FileFilter(
endswith=_GN_SOURCES_IN_BUILD,
suffix=('.bazel', '.bzl', '.gn', '.gni'),
- exclude=(r'zephyr.*/', r'android.*/', r'^pyproject.toml'),
+ exclude=(r'zephyr.*/', r'android.*/', r'^(.black|pyproject).toml'),
)