Skip to content

Commit 2a2c2bf

Browse files
committed
refactor: move ext_ref relpath helper to CycloneDxSupport
1 parent 0e453ad commit 2a2c2bf

2 files changed

Lines changed: 17 additions & 17 deletions

File tree

capycli/bom/download_sources.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -121,29 +121,20 @@ def download_sources(self, sbom: Bom, source_folder: str) -> None:
121121
if new:
122122
component.external_references.add(ext_ref)
123123

124-
def have_relative_ext_ref_path(self, ext_ref: ExternalReference, bompath: str):
125-
bip = pathlib.PurePath(ext_ref.url)
126-
try:
127-
file = bip.as_posix()
128-
if os.path.isfile(file):
129-
ext_ref.url = "file://" + bip.relative_to(bompath).as_posix()
130-
except ValueError:
131-
print_yellow(
132-
" SBOM file is not relative to source file " + ext_ref.url)
133-
134-
return bip.name
135-
136124
def update_local_path(self, sbom: Bom, bomfile: str):
137125
bompath = pathlib.Path(bomfile).parent
138126
for component in sbom.components:
139127
ext_ref = CycloneDxSupport.get_ext_ref(
140128
component, ExternalReferenceType.DISTRIBUTION, CaPyCliBom.SOURCE_FILE_COMMENT)
141129
if ext_ref:
142-
name = self.have_relative_ext_ref_path(ext_ref, bompath)
143-
CycloneDxSupport.update_or_set_property(
144-
component,
145-
CycloneDxSupport.CDX_PROP_FILENAME,
146-
name)
130+
try:
131+
name = CycloneDxSupport.have_relative_ext_ref_path(ext_ref, bompath)
132+
CycloneDxSupport.update_or_set_property(
133+
component,
134+
CycloneDxSupport.CDX_PROP_FILENAME,
135+
name)
136+
except ValueError:
137+
print_yellow(" SBOM file is not relative to source file " + ext_ref.url)
147138

148139
def run(self, args):
149140
"""Main method

capycli/common/capycli_bom_support.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import json
1010
import os
1111
import tempfile
12+
import pathlib
1213
import uuid
1314
from datetime import datetime
1415
from enum import Enum
@@ -370,6 +371,14 @@ def update_or_set_ext_ref(comp: Component, type: ExternalReferenceType, comment:
370371
else:
371372
CycloneDxSupport.set_ext_ref(comp, type, comment, value)
372373

374+
@staticmethod
375+
def have_relative_ext_ref_path(ext_ref: ExternalReference, rel_to: str):
376+
bip = pathlib.PurePath(ext_ref.url)
377+
file = bip.as_posix()
378+
if os.path.isfile(file):
379+
ext_ref.url = "file://" + bip.relative_to(rel_to).as_posix()
380+
return bip.name
381+
373382
@staticmethod
374383
def get_ext_ref_by_comment(comp: Component, comment: str) -> Any:
375384
for ext_ref in comp.external_references:

0 commit comments

Comments
 (0)