blob: 0821dad0b54bc7a3724208940ce06dfd2ed0b2b8 [file]
# Copyright 2023 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Defines SHAC checks for JSON and JSON5 files."""
load("./common.star", "FORMATTER_MSG", "cipd_platform_name", "compiled_tool_path", "get_fuchsia_dir", "os_exec")
def _json5_format(ctx):
"""Runs `formatjson5` on .json5 files.
Args:
ctx: A ctx instance.
"""
json5_files = ctx.scm.affected_files(glob = [
"*.json5",
"*.persist",
"*.triage",
])
if not json5_files:
return
exe = compiled_tool_path(ctx, "formatjson5")
procs = []
for f in json5_files:
procs.append((f, os_exec(ctx, [exe, f])))
for f, proc in procs:
formatted = proc.wait().stdout
original = str(ctx.io.read_file(f))
if formatted != original:
ctx.emit.finding(
level = "warning",
message = FORMATTER_MSG,
filepath = f,
replacements = [formatted],
)
def _json_format(ctx):
"""Runs `json_format.py` on .json and .config files.
Args:
ctx: A ctx instance.
"""
json_files = ctx.scm.affected_files(glob = [
"*.json",
"*.config",
"!/third_party/**",
"!*.golden.json",
"!*_pb2.json",
"!*.sh.config",
])
if not json_files:
return
fuchsia_dir = get_fuchsia_dir(ctx)
platform = cipd_platform_name(ctx)
python_bin = "%s/prebuilt/third_party/python3/%s/bin/python3" % (
fuchsia_dir,
platform,
)
formatter_script = "%s/scripts/shac/json_format.py" % fuchsia_dir
# The results can exceed shac's limit on a subprocess's stdout size.
output = ctx.io.tempfile("")
os_exec(
ctx,
[
python_bin,
formatter_script,
"--check",
"--no-sort-keys",
"--output",
output,
] + list(json_files),
).wait()
for item in json.decode(str(ctx.io.read_file(output))):
if "replacements" in item:
ctx.emit.finding(
level = "warning",
message = FORMATTER_MSG,
filepath = item["filepath"],
replacements = item["replacements"],
)
else:
ctx.emit.finding(
level = "warning",
message = "Could not format %s: invalid JSON syntax" % item["filepath"],
filepath = item["filepath"],
)
def register_json_checks():
shac.register_check(shac.check(_json5_format, formatter = True))
shac.register_check(shac.check(_json_format, formatter = True))