Skip to content

Commit c5b5f30

Browse files
committed
refactor: improve console logging for gen-build-spec command
Signed-off-by: Demolus13 <parth.govale@oracle.com>
1 parent ca41f80 commit c5b5f30

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

src/macaron/__main__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,8 @@ def perform_action(action_args: argparse.Namespace) -> None:
351351
find_source(action_args)
352352

353353
case "gen-build-spec":
354+
if not action_args.disable_rich_output:
355+
rich_handler.start("gen-build-spec")
354356
sys.exit(gen_build_spec(action_args))
355357

356358
case _:

src/macaron/build_spec_generator/build_spec_generator.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from macaron.build_spec_generator.build_command_patcher import PatchCommandBuildTool, PatchValueType
1616
from macaron.build_spec_generator.reproducible_central.reproducible_central import gen_reproducible_central_build_spec
17+
from macaron.console import access_handler
1718
from macaron.path_utils.purl_based_path import get_purl_based_dir
1819

1920
logger: logging.Logger = logging.getLogger(__name__)
@@ -131,6 +132,8 @@ def gen_build_spec_for_purl(
131132
build_spec_format.value,
132133
os.path.relpath(build_spec_filepath, os.getcwd()),
133134
)
135+
rich_handler = access_handler.get_handler()
136+
rich_handler.update_gen_build_spec("Build Spec Path:", os.path.relpath(build_spec_filepath, os.getcwd()))
134137
try:
135138
with open(build_spec_filepath, mode="w", encoding="utf-8") as file:
136139
file.write(build_spec_content)

src/macaron/build_spec_generator/reproducible_central/reproducible_central.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
lookup_build_tools_check,
2424
lookup_latest_component,
2525
)
26+
from macaron.console import access_handler
2627
from macaron.errors import QueryMacaronDatabaseError
2728
from macaron.slsa_analyzer.checks.build_tool_check import BuildToolFacts
2829

@@ -253,6 +254,11 @@ def get_rc_build_tool_name(
253254
BuildToolFacts.__tablename__,
254255
[(fact.build_tool_name, fact.language) for fact in build_tool_facts],
255256
)
257+
rich_handler = access_handler.get_handler()
258+
rich_handler.update_gen_build_spec(
259+
"Build Tools:",
260+
"\n".join([f"{fact.build_tool_name} ({fact.language})" for fact in build_tool_facts]),
261+
)
256262

257263
return _get_rc_build_tool_name_from_build_facts(build_tool_facts)
258264

@@ -351,6 +357,11 @@ def gen_reproducible_central_build_spec(
351357
version = purl.version
352358
if group is None or version is None:
353359
logger.error("Missing group and/or version for purl %s.", purl.to_string())
360+
rich_handler = access_handler.get_handler()
361+
rich_handler.update_gen_build_spec("Repository PURL:", "[red]FAILED[/]")
362+
rich_handler.update_gen_build_spec("Repository URL:", "[red]FAILED[/]")
363+
rich_handler.update_gen_build_spec("Commit Hash:", "[red]FAILED[/]")
364+
rich_handler.update_gen_build_spec("Build Tools:", "[red]FAILED[/]")
354365
return None
355366

356367
try:
@@ -386,6 +397,10 @@ def gen_reproducible_central_build_spec(
386397
latest_component_repository.remote_path,
387398
latest_component_repository.commit_sha,
388399
)
400+
rich_handler = access_handler.get_handler()
401+
rich_handler.update_gen_build_spec("Repository PURL:", purl.to_string())
402+
rich_handler.update_gen_build_spec("Repository URL:", latest_component_repository.remote_path)
403+
rich_handler.update_gen_build_spec("Commit Hash:", latest_component_repository.commit_sha)
389404

390405
# Getting the RC build tool name from the build tool check facts.
391406
rc_build_tool_name = get_rc_build_tool_name(

src/macaron/console.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ def __init__(self, *args: Any, verbose: bool = False, **kwargs: Any) -> None:
7979
for key, value in self.find_source_content.items():
8080
self.find_source_table.add_row(key, value)
8181
self.dump_defaults: str | Status = Status("[green]Generating[/]")
82+
self.gen_build_spec: dict[str, str | Status] = {
83+
"Repository PURL:": Status("[green]Processing[/]"),
84+
"Repository URL:": Status("[green]Processing[/]"),
85+
"Commit Hash:": Status("[green]Processing[/]"),
86+
"Build Tools:": Status("[green]Processing[/]"),
87+
"Build Spec Path:": "Not Generated",
88+
}
89+
self.gen_build_spec_table = Table(show_header=False, box=None)
90+
for key, value in self.gen_build_spec.items():
91+
self.gen_build_spec_table.add_row(key, value)
8292
self.verbose = verbose
8393
self.verbose_panel = Panel(
8494
"\n".join(self.logs),
@@ -257,6 +267,16 @@ def update_dump_defaults(self, value: str | Status) -> None:
257267
"""Update the dump defaults status."""
258268
self.dump_defaults = value
259269

270+
def update_gen_build_spec(self, key: str, value: str | Status) -> None:
271+
"""Add or update a key-value pair in the generate build spec table."""
272+
self.gen_build_spec[key] = value
273+
gen_build_spec_table = Table(show_header=False, box=None)
274+
gen_build_spec_table.add_column("Details", justify="left")
275+
gen_build_spec_table.add_column("Value", justify="left")
276+
for field, content in self.gen_build_spec.items():
277+
gen_build_spec_table.add_row(field, content)
278+
self.gen_build_spec_table = gen_build_spec_table
279+
260280
def make_layout(self) -> Group:
261281
"""Create the overall layout for the console output."""
262282
layout: list[RenderableType] = []
@@ -333,7 +353,9 @@ def make_layout(self) -> Group:
333353
dump_defaults_table.add_column("Value", justify="left")
334354
dump_defaults_table.add_row("Dump Defaults", self.dump_defaults)
335355
layout = layout + [dump_defaults_table]
336-
356+
elif self.command == "gen-build-spec":
357+
if self.gen_build_spec_table.row_count > 0:
358+
layout = layout + [self.gen_build_spec_table]
337359
if self.verbose:
338360
layout = layout + ["", self.verbose_panel]
339361
return Group(*layout)
@@ -353,12 +375,11 @@ class AccessHandler:
353375
"""A class to manage access to the RichConsoleHandler instance."""
354376

355377
def __init__(self) -> None:
356-
self.verbose = False
357378
self.rich_handler = RichConsoleHandler()
358379

359380
def set_handler(self, verbose: bool) -> RichConsoleHandler:
360381
"""Set the verbosity and create a new RichConsoleHandler instance."""
361-
self.rich_handler = RichConsoleHandler(verbose)
382+
self.rich_handler = RichConsoleHandler(verbose=verbose)
362383
return self.rich_handler
363384

364385
def get_handler(self) -> RichConsoleHandler:

0 commit comments

Comments
 (0)