Skip to content

Commit f335ec0

Browse files
authored
refactor: log relative paths for file (#1032)
Signed-off-by: Aman Sharma <[email protected]>
1 parent 89fe3bb commit f335ec0

File tree

10 files changed

+55
-30
lines changed

10 files changed

+55
-30
lines changed

src/macaron/__main__.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,21 @@ def verify_policy(verify_policy_args: argparse.Namespace) -> int:
206206
vsa = generate_vsa(policy_content=policy_content, policy_result=result)
207207
if vsa is not None:
208208
vsa_filepath = os.path.join(global_config.output_path, "vsa.intoto.jsonl")
209-
logger.info("Generating the Verification Summary Attestation (VSA) to %s.", vsa_filepath)
209+
logger.info(
210+
"Generating the Verification Summary Attestation (VSA) to %s.",
211+
os.path.relpath(vsa_filepath, os.getcwd()),
212+
)
210213
logger.info(
211214
"To decode and inspect the payload, run `cat %s | jq -r '.payload' | base64 -d | jq`.",
212-
vsa_filepath,
215+
os.path.relpath(vsa_filepath, os.getcwd()),
213216
)
214217
try:
215218
with open(vsa_filepath, mode="w", encoding="utf-8") as file:
216219
file.write(json.dumps(vsa))
217220
except OSError as err:
218-
logger.error("Could not generate the VSA to %s. Error: %s", vsa_filepath, err)
221+
logger.error(
222+
"Could not generate the VSA to %s. Error: %s", os.path.relpath(vsa_filepath, os.getcwd()), err
223+
)
219224

220225
policy_reporter = PolicyReporter()
221226
policy_reporter.generate(global_config.output_path, result)
@@ -544,9 +549,9 @@ def main(argv: list[str] | None = None) -> None:
544549
sys.exit(os.EX_USAGE)
545550

546551
if os.path.isdir(args.output_dir):
547-
logger.info("Setting the output directory to %s", args.output_dir)
552+
logger.info("Setting the output directory to %s", os.path.relpath(args.output_dir, os.getcwd()))
548553
else:
549-
logger.info("No directory at %s. Creating one ...", args.output_dir)
554+
logger.info("No directory at %s. Creating one ...", os.path.relpath(args.output_dir, os.getcwd()))
550555
os.makedirs(args.output_dir)
551556

552557
# Add file handler to the root logger. Remove stream handler from the

src/macaron/config/global_config.py

Lines changed: 7 additions & 4 deletions
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 contains the GlobalConfig class to be used globally."""
@@ -97,10 +97,10 @@ def load_expectation_files(self, exp_path: str) -> None:
9797
policy_file_path = os.path.join(exp_path, policy_path)
9898
if os.path.isfile(policy_file_path):
9999
exp_files.append(policy_file_path)
100-
logger.info("Added provenance expectation file %s", policy_file_path)
100+
logger.info("Added provenance expectation file %s", os.path.relpath(policy_file_path, os.getcwd()))
101101
elif os.path.isfile(exp_path):
102102
exp_files.append(exp_path)
103-
logger.info("Added provenance expectation file %s", exp_path)
103+
logger.info("Added provenance expectation file %s", os.path.relpath(exp_path, os.getcwd()))
104104

105105
self.expectation_paths = exp_files
106106

@@ -114,7 +114,10 @@ def load_python_venv(self, venv_path: str) -> None:
114114
The path to the Python virtual environment of the target software component.
115115
"""
116116
if os.path.isdir(venv_path):
117-
logger.info("Found Python virtual environment for the analysis target at %s", venv_path)
117+
logger.info(
118+
"Found Python virtual environment for the analysis target at %s",
119+
os.path.relpath(venv_path, os.getcwd()),
120+
)
118121

119122
self.python_venv_path = str(os.path.abspath(venv_path))
120123

src/macaron/dependency_analyzer/cyclonedx.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,9 @@ def resolve_dependencies(main_ctx: Any, sbom_path: str, recursive: bool = False)
381381
continue
382382

383383
if sbom_path:
384-
logger.info("Getting the dependencies from the SBOM defined at %s.", sbom_path)
384+
logger.info(
385+
"Getting the dependencies from the SBOM defined at %s.", os.path.relpath(sbom_path, os.getcwd())
386+
)
385387

386388
deps_resolved = dep_analyzer.get_deps_from_sbom(
387389
sbom_path,
@@ -406,7 +408,7 @@ def resolve_dependencies(main_ctx: Any, sbom_path: str, recursive: bool = False)
406408
"Running %s version %s dependency analyzer on %s",
407409
dep_analyzer.tool_name,
408410
dep_analyzer.tool_version,
409-
main_ctx.component.repository.fs_path,
411+
os.path.relpath(main_ctx.component.repository.fs_path, os.getcwd()),
410412
)
411413

412414
log_path = os.path.join(
@@ -452,7 +454,11 @@ def resolve_dependencies(main_ctx: Any, sbom_path: str, recursive: bool = False)
452454
recursive=recursive,
453455
)
454456

455-
logger.info("Stored dependency resolver log for %s to %s.", dep_analyzer.tool_name, log_path)
457+
logger.info(
458+
"Stored dependency resolver log for %s to %s.",
459+
dep_analyzer.tool_name,
460+
os.path.relpath(log_path, os.getcwd()),
461+
)
456462

457463
# Use repo finder to find more repositories to analyze.
458464
if defaults.getboolean("repofinder", "find_repos"):

src/macaron/output_reporter/reporter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2022 - 2023, 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 contains reporter classes for creating reports of Macaron analyzed results."""
@@ -60,11 +60,11 @@ def write_file(self, file_path: str, data: str) -> bool:
6060
"""
6161
try:
6262
with open(file_path, mode=self.mode, encoding=self.encoding) as file:
63-
logger.info("Writing to file %s", file_path)
63+
logger.info("Writing to file %s", os.path.relpath(file_path, os.getcwd()))
6464
file.write(data)
6565
return True
6666
except OSError as error:
67-
logger.error("Cannot write to %s. Error: %s", file_path, error)
67+
logger.error("Cannot write to %s. Error: %s", os.path.relpath(file_path, os.getcwd()), error)
6868
return False
6969

7070
@abc.abstractmethod

src/macaron/parsers/bashparser.py

Lines changed: 3 additions & 3 deletions
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 is a Python wrapper for the compiled bashparser binary.
@@ -96,10 +96,10 @@ def parse_file(file_path: str, macaron_path: str | None = None) -> dict:
9696
macaron_path = global_config.macaron_path
9797
try:
9898
with open(file_path, encoding="utf8") as file:
99-
logger.info("Parsing %s.", file_path)
99+
logger.info("Parsing %s.", os.path.relpath(file_path, os.getcwd()))
100100
return parse(file.read(), macaron_path)
101101
except OSError as error:
102-
raise ParseError(f"Could not load the bash script file: {file_path}.") from error
102+
raise ParseError(f"Could not load the bash script file: {os.path.relpath(file_path, os.getcwd())}.") from error
103103
except ParseError as error:
104104
raise error
105105

src/macaron/provenance/provenance_verifier.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ def _verify_slsa(
327327
verified = "PASSED: SLSA verification passed" in output
328328
log_path = os.path.join(global_config.build_log_path, f"{os.path.basename(source_path)}.slsa_verifier.log")
329329
with open(log_path, mode="a", encoding="utf-8") as log_file:
330-
logger.info("Storing SLSA verifier output for %s to %s", asset_name, log_path)
330+
logger.info("Storing SLSA verifier output for %s to %s", asset_name, os.path.relpath(log_path, os.getcwd()))
331331
log_file.writelines(
332332
[f"SLSA verifier output for cmd: {' '.join(cmd)}\n", output, "--------------------------------\n"]
333333
)
@@ -346,7 +346,9 @@ def _verify_slsa(
346346
global_config.build_log_path, f"{os.path.basename(source_path)}.slsa_verifier.errors"
347347
)
348348
with open(error_log_path, mode="a", encoding="utf-8") as log_file:
349-
logger.info("Storing SLSA verifier log for%s to %s", asset_name, error_log_path)
349+
logger.info(
350+
"Storing SLSA verifier log for%s to %s", asset_name, os.path.relpath(error_log_path, os.getcwd())
351+
)
350352
log_file.write(f"SLSA verifier output for cmd: {' '.join(cmd)}\n")
351353
log_file.writelines(errors)
352354
log_file.write("--------------------------------\n")

src/macaron/repo_finder/repo_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def generate_report(purl: str, commit: str, repo: str, target_dir: str) -> bool:
7575
fullpath = f"{target_dir}/{filename}"
7676

7777
os.makedirs(os.path.dirname(fullpath), exist_ok=True)
78-
logger.info("Writing report to: %s", fullpath)
78+
logger.info("Writing report to: %s", os.path.relpath(fullpath, os.getcwd()))
7979

8080
try:
8181
with open(fullpath, "w", encoding="utf-8") as file:
@@ -84,7 +84,7 @@ def generate_report(purl: str, commit: str, repo: str, target_dir: str) -> bool:
8484
logger.debug("Failed to write report to file: %s", error)
8585
return False
8686

87-
logger.info("Report written to: %s", fullpath)
87+
logger.info("Report written to: %s", os.path.relpath(fullpath, os.getcwd()))
8888

8989
return True
9090

src/macaron/slsa_analyzer/ci_service/base_ci_service.py

Lines changed: 4 additions & 2 deletions
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 contains the BaseCIService class to be inherited by a CI service."""
@@ -147,7 +147,9 @@ def has_kws_in_config(self, kws: list, build_tool_name: str, repo_path: str) ->
147147
line.strip(),
148148
)
149149
return keyword, config
150-
logger.info("No build command found for %s in %s", build_tool_name, file_path)
150+
logger.info(
151+
"No build command found for %s in %s", build_tool_name, os.path.relpath(file_path, os.getcwd())
152+
)
151153
return "", ""
152154
except FileNotFoundError as error:
153155
logger.debug(error)

src/macaron/slsa_analyzer/provenance/expectations/cue/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2023 - 2024, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2023 - 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 CUE expectation implementations.
@@ -10,6 +10,7 @@
1010

1111
import hashlib
1212
import logging
13+
import os
1314
from typing import Self
1415

1516
from sqlalchemy import ForeignKey
@@ -52,7 +53,7 @@ def make_expectation(cls, expectation_path: str) -> Self | None:
5253
Self
5354
The instantiated expectation object.
5455
"""
55-
logger.info("Generating an expectation from file %s", expectation_path)
56+
logger.info("Generating an expectation from file %s", os.path.relpath(expectation_path, os.getcwd()))
5657
expectation: CUEExpectation = CUEExpectation(
5758
description="CUE expectation",
5859
path=expectation_path,

src/macaron/slsa_analyzer/provenance/expectations/expectation_registry.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2023 - 2024, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2023 - 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
"""The provenance expectation module manages expectations that will be provided to checks."""
@@ -37,11 +37,17 @@ def __init__(self, expectation_paths: list[str]) -> None:
3737
expectation = CUEExpectation.make_expectation(expectation_path)
3838
if expectation and expectation.target:
3939
self.expectations[expectation.target] = expectation
40-
logger.info("Found target %s for expectation %s.", expectation.target, expectation_path)
40+
logger.info(
41+
"Found target %s for expectation %s.",
42+
expectation.target,
43+
os.path.relpath(expectation_path, os.getcwd()),
44+
)
4145
else:
42-
logger.error("Unable to find target for expectation %s.", expectation_path)
46+
logger.error(
47+
"Unable to find target for expectation %s.", os.path.relpath(expectation_path, os.getcwd())
48+
)
4349
else:
44-
logger.error("Unsupported expectation format: %s", expectation_path)
50+
logger.error("Unsupported expectation format: %s", os.path.relpath(expectation_path, os.getcwd()))
4551

4652
def get_expectation_for_target(self, repo_complete_name: str) -> Expectation | None:
4753
"""

0 commit comments

Comments
 (0)