Skip to content

Commit

Permalink
refactor: move ext_ref relpath helper to CycloneDxSupport
Browse files Browse the repository at this point in the history
  • Loading branch information
gernot-h committed Aug 5, 2023
1 parent 0e453ad commit 2a2c2bf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
25 changes: 8 additions & 17 deletions capycli/bom/download_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions capycli/common/capycli_bom_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import json
import os
import tempfile
import pathlib
import uuid
from datetime import datetime
from enum import Enum
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 2a2c2bf

Please sign in to comment.