1212from io import BytesIO
1313from io import StringIO
1414
15- from spdx .checksum import Algorithm
15+ from spdx .checksum import Checksum , ChecksumAlgorithm
1616from spdx .creationinfo import Tool
1717from spdx .document import ExtractedLicense
1818from spdx .document import Document
19- from spdx .document import License
19+ from spdx .license import License
2020from spdx .file import File
2121from spdx .package import Package
22+ from spdx .relationship import Relationship
23+ from spdx .utils import calc_verif_code
2224from spdx .utils import NoAssert
2325from spdx .utils import SPDXNone
2426from spdx .version import Version
@@ -281,9 +283,8 @@ def write_spdx(
281283 name = './' + file_data .get ('path' )
282284 file_entry = File (
283285 spdx_id = f'SPDXRef-{ sid } ' ,
284- name = name ,
285- chk_sum = Algorithm ('SHA1' , file_data .get ('sha1' ) or '' )
286- )
286+ name = name )
287+ file_entry .set_checksum (Checksum (ChecksumAlgorithm .SHA1 , file_data .get ('sha1' ) or '' ))
287288
288289 file_license_detections = file_data .get ('license_detections' )
289290 license_matches = get_matches_from_detection_mappings (file_license_detections )
@@ -362,9 +363,11 @@ def write_spdx(
362363 else :
363364 file_entry .copyright = SPDXNone ()
364365
365- package .add_file (file_entry )
366+ doc .add_file (file_entry )
367+ relationship = Relationship (package .spdx_id + " CONTAINS " + file_entry .spdx_id )
368+ doc .add_relationship (relationship )
366369
367- if len (package .files ) == 0 :
370+ if len (doc .files ) == 0 :
368371 if as_tagvalue :
369372 msg = "# No results for package '{}'.\n " .format (package .name )
370373 else :
@@ -397,7 +400,7 @@ def write_spdx(
397400 # statements for the package.
398401 package .cr_text = '\n ' .join (sorted (package .cr_text )) + '\n '
399402
400- package .verif_code = doc . package . calc_verif_code ()
403+ package .verif_code = calc_verif_code (doc . files )
401404 package .license_declared = NoAssert ()
402405 package .conc_lics = NoAssert ()
403406
@@ -409,7 +412,7 @@ def write_spdx(
409412 # one case we do need to deal with bytes and decode before writing (rdf) and
410413 # in the other case we deal with text all the way.
411414
412- if package .files :
415+ if doc .files :
413416
414417 if as_tagvalue :
415418 from spdx .writers .tagvalue import write_document # NOQA
0 commit comments