Skip to content

Commit 7862bd9

Browse files
committed
feat(bom downloadAttachments): read attachment id from control file
1 parent 5efb39a commit 7862bd9

File tree

3 files changed

+86
-142
lines changed

3 files changed

+86
-142
lines changed

capycli/bom/download_attachments.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from capycli.common.capycli_bom_support import CaPyCliBom, CycloneDxSupport, SbomWriter
2020
from capycli.common.print import print_red, print_text, print_yellow
2121
from capycli.common.script_support import ScriptSupport
22+
from capycli.common.json_support import load_json_file
2223
from capycli.main.result_codes import ResultCode
2324

2425
LOG = capycli.get_logger(__name__)
@@ -29,7 +30,7 @@ class BomDownloadAttachments(capycli.common.script_base.ScriptBase):
2930
Download SW360 attachments as specified in the SBOM.
3031
"""
3132

32-
def download_attachments(self, sbom: Bom, source_folder: str, bompath: str = None,
33+
def download_attachments(self, sbom: Bom, control_components: list, source_folder: str, bompath: str = None,
3334
attachment_types: Tuple[str] = ("COMPONENT_LICENSE_INFO_XML", "CLEARING_REPORT")) -> Bom:
3435

3536
for component in sbom.components:
@@ -46,27 +47,25 @@ def download_attachments(self, sbom: Bom, source_folder: str, bompath: str = Non
4647
if not found:
4748
continue
4849

49-
attachment_id = ext_ref.comment.split(", sw360Id: ")
50-
if len(attachment_id) != 2:
51-
print_red(" No sw360Id for attachment!")
52-
continue
53-
attachment_id = attachment_id[1]
54-
5550
release_id = CycloneDxSupport.get_property_value(component, CycloneDxSupport.CDX_PROP_SW360ID)
5651
if not release_id:
5752
print_red(" No sw360Id for release!")
5853
continue
59-
print(" ", ext_ref.url, release_id, attachment_id)
60-
filename = os.path.join(source_folder, ext_ref.url)
54+
url = str(ext_ref.url)
55+
filename = os.path.join(source_folder, url)
56+
57+
details = [e for e in control_components
58+
if e["Sw360Id"] == release_id and (
59+
e.get("CliFile", "") == url
60+
or e.get("ReportFile", "") == url)]
61+
if len(details) != 1:
62+
print_red(" ERROR: Found", len(details), "entries for attachment",
63+
ext_ref.url, "of", item_name, "in control file!")
64+
continue
65+
attachment_id = details[0]["Sw360AttachmentId"]
6166

6267
print_text(" Downloading file " + filename)
6368
try:
64-
at_info = self.client.get_attachment(attachment_id)
65-
at_info = {k: v for k, v in at_info.items()
66-
if k.startswith("check")
67-
or k.startswith("created")}
68-
print(at_info)
69-
7069
self.client.download_release_attachment(filename, release_id, attachment_id)
7170
ext_ref.url = filename
7271
try:
@@ -104,6 +103,7 @@ def run(self, args):
104103
print("optional arguments:")
105104
print(" -h, --help show this help message and exit")
106105
print(" -i INPUTFILE, input SBOM to read from, e.g. created by \"project CreateBom\"")
106+
print(" -ct CONTROLFILE, control file to read from as created by \"project CreateBom\"")
107107
print(" -source SOURCE source folder or additional source file")
108108
print(" -o OUTPUTFILE output file to write to")
109109
print(" -v be verbose")
@@ -113,6 +113,10 @@ def run(self, args):
113113
print_red("No input file specified!")
114114
sys.exit(ResultCode.RESULT_COMMAND_ERROR)
115115

116+
if not args.controlfile:
117+
print_red("No control file specified!")
118+
sys.exit(ResultCode.RESULT_COMMAND_ERROR)
119+
116120
if not os.path.isfile(args.inputfile):
117121
print_red("Input file not found!")
118122
sys.exit(ResultCode.RESULT_FILE_NOT_FOUND)
@@ -127,6 +131,16 @@ def run(self, args):
127131
if args.verbose:
128132
print_text(" " + str(len(bom.components)) + "components read from SBOM file")
129133

134+
print_text("Loading control file " + args.controlfile)
135+
try:
136+
control = load_json_file(args.controlfile)
137+
except Exception as ex:
138+
print_red("JSON error reading control file: " + repr(ex))
139+
sys.exit(ResultCode.RESULT_ERROR_READING_BOM)
140+
if "Components" not in control:
141+
print_red("missing Components in control file")
142+
sys.exit(ResultCode.RESULT_ERROR_READING_BOM)
143+
130144
source_folder = "./"
131145
if args.source:
132146
source_folder = args.source
@@ -144,7 +158,7 @@ def run(self, args):
144158

145159
print_text("Downloading source files to folder " + source_folder + " ...")
146160

147-
self.download_attachments(bom, source_folder, os.path.dirname(args.outputfile))
161+
self.download_attachments(bom, control["Components"], source_folder, os.path.dirname(args.outputfile))
148162

149163
if args.outputfile:
150164
print_text("Updating path information")
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"ProjectName": "CaPyCLI, 2.0.0-dev1",
3+
"Components": [
4+
{
5+
"ComponentName": "certifi 2022.12.7",
6+
"Sw360Id": "ae8c7ed",
7+
"Sw360AttachmentId": "794446",
8+
"CreatedBy": "[email protected]",
9+
"CreatedTeam": "AA",
10+
"CreatedOn": "2020-10-23",
11+
"CheckStatus": "ACCEPTED",
12+
"CheckedBy": "[email protected]",
13+
"CheckedTeam": "BB",
14+
"CheckedOn": "2020-10-30",
15+
"CliFile": "CLIXML_certifi-2022.12.7.xml"
16+
},
17+
{
18+
"ComponentName": "certifi 2022.12.7",
19+
"Sw360Id": "ae8c7ed",
20+
"Sw360AttachmentId": "63b368",
21+
"ReportFile": "certifi-2022.12.7_clearing_report.docx"
22+
}
23+
]
24+
}

0 commit comments

Comments
 (0)