From 2a2c2bf5c2bcdef4b152c651e1e2e2c0277f71c7 Mon Sep 17 00:00:00 2001 From: Gernot Hillier Date: Sat, 5 Aug 2023 08:24:05 +0200 Subject: [PATCH] refactor: move ext_ref relpath helper to CycloneDxSupport --- capycli/bom/download_sources.py | 25 ++++++++----------------- capycli/common/capycli_bom_support.py | 9 +++++++++ 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/capycli/bom/download_sources.py b/capycli/bom/download_sources.py index f9f9639..0fee855 100644 --- a/capycli/bom/download_sources.py +++ b/capycli/bom/download_sources.py @@ -121,29 +121,20 @@ def download_sources(self, sbom: Bom, source_folder: str) -> None: if new: component.external_references.add(ext_ref) - def have_relative_ext_ref_path(self, ext_ref: ExternalReference, bompath: str): - bip = pathlib.PurePath(ext_ref.url) - try: - file = bip.as_posix() - if os.path.isfile(file): - ext_ref.url = "file://" + bip.relative_to(bompath).as_posix() - except ValueError: - print_yellow( - " SBOM file is not relative to source file " + ext_ref.url) - - return bip.name - def update_local_path(self, sbom: Bom, bomfile: str): bompath = pathlib.Path(bomfile).parent for component in sbom.components: ext_ref = CycloneDxSupport.get_ext_ref( component, ExternalReferenceType.DISTRIBUTION, CaPyCliBom.SOURCE_FILE_COMMENT) if ext_ref: - name = self.have_relative_ext_ref_path(ext_ref, bompath) - CycloneDxSupport.update_or_set_property( - component, - CycloneDxSupport.CDX_PROP_FILENAME, - name) + try: + name = CycloneDxSupport.have_relative_ext_ref_path(ext_ref, bompath) + CycloneDxSupport.update_or_set_property( + component, + CycloneDxSupport.CDX_PROP_FILENAME, + name) + except ValueError: + print_yellow(" SBOM file is not relative to source file " + ext_ref.url) def run(self, args): """Main method diff --git a/capycli/common/capycli_bom_support.py b/capycli/common/capycli_bom_support.py index c5b8e8d..751a761 100644 --- a/capycli/common/capycli_bom_support.py +++ b/capycli/common/capycli_bom_support.py @@ -9,6 +9,7 @@ import json import os import tempfile +import pathlib import uuid from datetime import datetime from enum import Enum @@ -370,6 +371,14 @@ def update_or_set_ext_ref(comp: Component, type: ExternalReferenceType, comment: else: CycloneDxSupport.set_ext_ref(comp, type, comment, value) + @staticmethod + def have_relative_ext_ref_path(ext_ref: ExternalReference, rel_to: str): + bip = pathlib.PurePath(ext_ref.url) + file = bip.as_posix() + if os.path.isfile(file): + ext_ref.url = "file://" + bip.relative_to(rel_to).as_posix() + return bip.name + @staticmethod def get_ext_ref_by_comment(comp: Component, comment: str) -> Any: for ext_ref in comp.external_references: