Skip to content

Commit 7799a50

Browse files
Mahesh SaripalliCQ Bot
Mahesh Saripalli
authored and
CQ Bot
committed
[shac] integrate with python import sortter isort
Integrate shac with the isort formatter, enabling presubmit blocking checks and local checks. Bug: 352796213 Change-Id: I2ced567cc5005de1e41746f3466f7d5917f71bc7 Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/1082095 Reviewed-by: Oliver Newman <[email protected]> Reviewed-by: Kevin Cho <[email protected]> Commit-Queue: Mahesh Saripalli <[email protected]>
1 parent 9f2104e commit 7799a50

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

pyproject.toml

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ exclude = '''
2222
)
2323
'''
2424

25+
26+
[tool.isort]
27+
profile = "black"
28+
line_length = 80
29+
default_section="FIRSTPARTY"
30+
skip_glob = ["**/.venvs/*", "**/third_party/*", "**/*.pyz", "out/*"]
31+
32+
2533
[tool.mypy]
2634
strict = true
2735
pretty = true

scripts/shac/python.star

+28-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
load("./common.star", "FORMATTER_MSG", "cipd_platform_name", "get_fuchsia_dir", "os_exec")
66

77
def _pyfmt(ctx):
8-
"""Runs python formatter black on a Python code base.
8+
"""Formats Python code using Black and sorts imports using isort on a Python code base.
99
1010
Args:
1111
ctx: A ctx instance.
@@ -18,21 +18,39 @@ def _pyfmt(ctx):
1818
if not py_files:
1919
return
2020

21-
procs = []
22-
base_cmd = [
21+
# Isort and black make conflicting code style changes. To ensure consistent formatting:
22+
# 1. Run isort to sort imports and apply its formatting rules.
23+
# 2. Run black on the formatted code to enforce its code style guidelines.
24+
fuchsia_dir = get_fuchsia_dir(ctx)
25+
26+
isort_cmd = [
27+
"%s/prebuilt/third_party/python3/%s/bin/python3" % (
28+
fuchsia_dir,
29+
cipd_platform_name(ctx),
30+
),
31+
"%s/third_party/pylibs/isort/main.py" % fuchsia_dir,
32+
"--stdout",
33+
]
34+
isort_procs = [(f, os_exec(ctx, isort_cmd + [f])) for f in py_files]
35+
36+
black_cmd = [
2337
"%s/prebuilt/third_party/black/%s/black" % (
24-
get_fuchsia_dir(ctx),
38+
fuchsia_dir,
2539
cipd_platform_name(ctx),
2640
),
2741
"--config",
28-
"%s/pyproject.toml" % get_fuchsia_dir(ctx),
42+
"%s/pyproject.toml" % fuchsia_dir,
43+
"-",
2944
]
30-
for filepath in py_files:
31-
original = str(ctx.io.read_file(filepath))
32-
procs.append(
33-
(filepath, original, os_exec(ctx, base_cmd + ["-"], stdin = original)),
45+
black_procs = []
46+
for filepath, proc in isort_procs:
47+
isort_formatted = proc.wait().stdout
48+
black_procs.append(
49+
(filepath, os_exec(ctx, black_cmd, stdin = isort_formatted)),
3450
)
35-
for filepath, original, proc in procs:
51+
52+
for filepath, proc in black_procs:
53+
original = str(ctx.io.read_file(filepath))
3654
formatted = proc.wait().stdout
3755
if formatted != original:
3856
ctx.emit.finding(

0 commit comments

Comments
 (0)