Skip to content

Commit 3b04952

Browse files
committed
refactor: improve console logging for find-source and dump-defaults
Signed-off-by: Demolus13 <parth.govale@oracle.com>
1 parent f19785a commit 3b04952

File tree

5 files changed

+54
-11
lines changed

5 files changed

+54
-11
lines changed

src/macaron/__main__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def verify_policy(verify_policy_args: argparse.Namespace) -> int:
217217
rich_handler = access_handler.get_handler()
218218
if vsa is not None:
219219
vsa_filepath = os.path.join(global_config.output_path, "vsa.intoto.jsonl")
220-
rich_handler.update_vsa(vsa_filepath)
220+
rich_handler.update_vsa(os.path.relpath(vsa_filepath, os.getcwd()))
221221
logger.info(
222222
"Generating the Verification Summary Attestation (VSA) to %s.",
223223
os.path.relpath(vsa_filepath, os.getcwd()),
@@ -416,7 +416,6 @@ def main(argv: list[str] | None = None) -> None:
416416
)
417417

418418
main_parser.add_argument(
419-
"-dro",
420419
"--disable-rich-output",
421420
default=False,
422421
help="Disable Rich UI output",

src/macaron/config/defaults.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2022 - 2024, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2022 - 2025, Oracle and/or its affiliates. All rights reserved.
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
33

44
"""This module provides functions to manage default values."""
@@ -9,6 +9,8 @@
99
import pathlib
1010
import shutil
1111

12+
from macaron.console import access_handler
13+
1214
logger: logging.Logger = logging.getLogger(__name__)
1315

1416

@@ -162,14 +164,17 @@ def create_defaults(output_path: str, cwd_path: str) -> bool:
162164
# Since we have only one defaults.ini file and ConfigParser.write does not
163165
# preserve the comments, copy the file directly.
164166
dest_path = os.path.join(output_path, "defaults.ini")
167+
rich_handler = access_handler.get_handler()
165168
try:
166169
shutil.copy2(src_path, dest_path)
167170
logger.info(
168171
"Dumped the default values in %s.",
169172
os.path.relpath(os.path.join(output_path, "defaults.ini"), cwd_path),
170173
)
174+
rich_handler.update_dump_defaults(os.path.relpath(dest_path, cwd_path))
171175
return True
172176
# We catch OSError to support errors on different platforms.
173177
except OSError as error:
174178
logger.error("Failed to create %s: %s.", os.path.relpath(dest_path, cwd_path), error)
179+
rich_handler.update_dump_defaults("[bold red]Failed[/]")
175180
return False

src/macaron/console.py

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
from rich.status import Status
1717
from rich.table import Table
1818

19-
from macaron.slsa_analyzer.checks.check_result import CheckResultType
20-
2119

2220
class Check:
2321
"""Class to represent a check with its status and target."""
@@ -72,6 +70,15 @@ def __init__(self, *args: Any, verbose: bool = False, **kwargs: Any) -> None:
7270
"Policy Report": Status("[green]Generating[/]"),
7371
}
7472
self.verification_summary_attestation: str | None = None
73+
self.find_source_table = Table(show_header=False, box=None)
74+
self.find_source_content: dict[str, str | Status] = {
75+
"Repository PURL:": Status("[green]Processing[/]"),
76+
"Commit Hash:": Status("[green]Processing[/]"),
77+
"JSON Report:": "Not Generated",
78+
}
79+
for key, value in self.find_source_content.items():
80+
self.find_source_table.add_row(key, value)
81+
self.dump_defaults: str | Status = Status("[green]Generating[/]")
7582
self.verbose = verbose
7683
self.verbose_panel = Panel(
7784
"\n".join(self.logs),
@@ -137,7 +144,7 @@ def update_checks_summary(self, checks_summary: dict, total_checks: int) -> None
137144
failed_checks_table.add_column("Check ID", justify="left")
138145
failed_checks_table.add_column("Description", justify="left")
139146

140-
failed_checks = checks_summary[CheckResultType.FAILED]
147+
failed_checks = checks_summary["FAILED"]
141148
for check in failed_checks:
142149
failed_checks_table.add_row(
143150
"[bold red]FAILED[/]",
@@ -153,15 +160,15 @@ def update_checks_summary(self, checks_summary: dict, total_checks: int) -> None
153160
summary_table.add_row("Total Checks", str(total_checks), style="white")
154161

155162
for check_result_type, checks in checks_summary.items():
156-
if check_result_type == CheckResultType.PASSED:
163+
if check_result_type == "PASSED":
157164
summary_table.add_row("PASSED", str(len(checks)), style="green")
158-
if check_result_type == CheckResultType.FAILED:
165+
if check_result_type == "FAILED":
159166
summary_table.add_row("FAILED", str(len(checks)), style="red")
160-
if check_result_type == CheckResultType.SKIPPED:
167+
if check_result_type == "SKIPPED":
161168
summary_table.add_row("SKIPPED", str(len(checks)), style="yellow")
162-
if check_result_type == CheckResultType.DISABLED:
169+
if check_result_type == "DISABLED":
163170
summary_table.add_row("DISABLED", str(len(checks)), style="bright_blue")
164-
if check_result_type == CheckResultType.UNKNOWN:
171+
if check_result_type == "UNKNOWN":
165172
summary_table.add_row("UNKNOWN", str(len(checks)), style="white")
166173

167174
self.summary_table = summary_table
@@ -236,6 +243,20 @@ def update_policy_engine(self, results: dict) -> None:
236243

237244
self.generate_policy_summary_table()
238245

246+
def update_find_source_table(self, key: str, value: str | Status) -> None:
247+
"""Add or update a key-value pair in the find source table."""
248+
self.find_source_content[key] = value
249+
find_source_table = Table(show_header=False, box=None)
250+
find_source_table.add_column("Details", justify="left")
251+
find_source_table.add_column("Value", justify="left")
252+
for field, content in self.find_source_content.items():
253+
find_source_table.add_row(field, content)
254+
self.find_source_table = find_source_table
255+
256+
def update_dump_defaults(self, value: str | Status) -> None:
257+
"""Update the dump defaults status."""
258+
self.dump_defaults = value
259+
239260
def make_layout(self) -> Group:
240261
"""Create the overall layout for the console output."""
241262
layout: list[RenderableType] = []
@@ -303,6 +324,15 @@ def make_layout(self) -> Group:
303324
)
304325

305326
layout = layout + [vsa_table]
327+
elif self.command == "find-source":
328+
if self.find_source_table.row_count > 0:
329+
layout = layout + [self.find_source_table]
330+
elif self.command == "dump-defaults":
331+
dump_defaults_table = Table(show_header=False, box=None)
332+
dump_defaults_table.add_column("Detail", justify="left")
333+
dump_defaults_table.add_column("Value", justify="left")
334+
dump_defaults_table.add_row("Dump Defaults", self.dump_defaults)
335+
layout = layout + [dump_defaults_table]
306336

307337
if self.verbose:
308338
layout = layout + ["", self.verbose_panel]

src/macaron/repo_finder/repo_finder.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,15 @@ def find_source(purl_string: str, input_repo: str | None, latest_version_fallbac
324324

325325
return find_source(str(purl), latest_repo, False)
326326

327+
rich_handler = access_handler.get_handler()
327328
if not input_repo:
328329
logger.info("Found repository for PURL: %s", found_repo)
330+
rich_handler.update_find_source_table("Repository PURL:", found_repo)
331+
else:
332+
rich_handler.update_find_source_table("Repository PURL:", input_repo)
329333

330334
logger.info("Found commit for PURL: %s", digest)
335+
rich_handler.update_find_source_table("Commit Hash:", digest)
331336

332337
if not generate_report(
333338
purl_string,

src/macaron/repo_finder/repo_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from pydriller import Git
1414

1515
from macaron.config.global_config import global_config
16+
from macaron.console import access_handler
1617
from macaron.slsa_analyzer.git_service import GIT_SERVICES, BaseGitService
1718
from macaron.slsa_analyzer.git_service.base_git_service import NoneGitService
1819
from macaron.slsa_analyzer.git_url import GIT_REPOS_DIR, decode_git_tags, parse_git_tags
@@ -88,6 +89,9 @@ def generate_report(purl: str, commit: str, repo: str, target_dir: str) -> bool:
8889

8990
logger.info("Report written to: %s", os.path.relpath(fullpath, os.getcwd()))
9091

92+
rich_handler = access_handler.get_handler()
93+
rich_handler.update_find_source_table("JSON Report:", os.path.relpath(fullpath, os.getcwd()))
94+
9195
return True
9296

9397

0 commit comments

Comments
 (0)