From 5faed5a72c6150e0add58fe694de720304931704 Mon Sep 17 00:00:00 2001 From: Armando Montanez Date: Thu, 3 Oct 2024 17:08:47 -0700 Subject: [PATCH] Improve Bazel presubmit formatting --- tools/bazel_build.py | 5 +++-- tools/bazel_common.py | 11 ++++++++--- tools/compare_build_systems.py | 20 ++++++++++++-------- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/tools/bazel_build.py b/tools/bazel_build.py index b303e3402..546eb7d9e 100755 --- a/tools/bazel_build.py +++ b/tools/bazel_build.py @@ -15,6 +15,7 @@ override_picotool_arg, parse_common_args, print_framed_string, + print_to_stderr, run_bazel, setup_logging, ) @@ -196,12 +197,12 @@ def build_all_configurations(picotool_dir): ) if result.returncode != 0: failed_builds.append(config["name"]) - print() + print_to_stderr() if failed_builds: print_framed_string("ERROR: One or more builds failed.") for build in failed_builds: - print(f" * FAILED: {build} build") + print_to_stderr(f" * FAILED: {build} build") return 1 print_framed_string("All builds successfully passed!") diff --git a/tools/bazel_common.py b/tools/bazel_common.py index 487044a46..409bc96f3 100644 --- a/tools/bazel_common.py +++ b/tools/bazel_common.py @@ -13,6 +13,7 @@ import shlex import shutil import subprocess +import sys _LOG = logging.getLogger(__file__) @@ -93,12 +94,16 @@ def run_bazel(args, check=False, **kwargs): return proc +def print_to_stderr(*args, **kwargs): + print(*args, file=sys.stderr, **kwargs) + + def print_framed_string(s): """Frames a string of text and prints it to highlight script steps.""" header_spacer = "#" * (len(s) + 12) - print(header_spacer) - print("### " + s + " ###") - print(header_spacer) + print_to_stderr(header_spacer) + print_to_stderr("### " + s + " ###") + print_to_stderr(header_spacer) def setup_logging(): diff --git a/tools/compare_build_systems.py b/tools/compare_build_systems.py index c5762817b..d3cb7a408 100755 --- a/tools/compare_build_systems.py +++ b/tools/compare_build_systems.py @@ -14,6 +14,7 @@ from dataclasses import dataclass import glob +import logging import os import re import sys @@ -21,6 +22,8 @@ from bazel_common import SDK_ROOT +_LOG = logging.getLogger(__file__) + CMAKE_FILE_TYPES = ( "**/CMakeLists.txt", "**/*.cmake", @@ -182,17 +185,17 @@ def OptionsAreEqual(bazel_option, cmake_option): if bazel_option is None: if cmake_option.name in CMAKE_ONLY_ALLOWLIST: return True - print(f" {cmake_option.name} does not exist in Bazel") + _LOG.warning(f" {cmake_option.name} does not exist in Bazel") return False elif cmake_option is None: if bazel_option.name in BAZEL_ONLY_ALLOWLIST: return True - print(f" {bazel_option.name} does not exist in CMake") + _LOG.warning(f" {bazel_option.name} does not exist in CMake") return False elif not bazel_option.matches(cmake_option): - print(" Bazel and CMAKE definitions do not match:") - print(f" [CMAKE] {bazel_option}") - print(f" [BAZEL] {cmake_option}") + _LOG.error(" Bazel and CMAKE definitions do not match:") + _LOG.error(f" [CMAKE] {bazel_option}") + _LOG.error(f" [BAZEL] {cmake_option}") return False return True @@ -224,22 +227,23 @@ def compare_build_systems(): for f in glob.glob(os.path.join(SDK_ROOT, p), recursive=True) ] - print("[1/2] Checking build system configuration flags...") + _LOG.info("[1/2] Checking build system configuration flags...") build_options_ok = CompareOptions( "PICO_BAZEL_CONFIG", bazel_files, "PICO_CMAKE_CONFIG", cmake_files ) - print("[2/2] Checking build system defines...") + _LOG.info("[2/2] Checking build system defines...") build_defines_ok = CompareOptions( "PICO_BUILD_DEFINE", bazel_files, "PICO_BUILD_DEFINE", cmake_files ) if build_options_ok and build_defines_ok: - print("OK") + _LOG.info("OK") return 0 return 1 if __name__ == "__main__": + setup_logging() sys.exit(compare_build_systems())