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
@@ -280,9 +282,8 @@ def write_spdx(
280282 name = './' + file_data .get ('path' )
281283 file_entry = File (
282284 spdx_id = f'SPDXRef-{ sid } ' ,
283- name = name ,
284- chk_sum = Algorithm ('SHA1' , file_data .get ('sha1' ) or '' )
285- )
285+ name = name )
286+ file_entry .set_checksum (Checksum (ChecksumAlgorithm .SHA1 , file_data .get ('sha1' ) or '' ))
286287
287288 file_license_detections = file_data .get ('license_detections' )
288289 license_matches = get_matches_from_detection_mappings (file_license_detections )
@@ -357,9 +358,11 @@ def write_spdx(
357358 else :
358359 file_entry .copyright = SPDXNone ()
359360
360- package .add_file (file_entry )
361+ doc .add_file (file_entry )
362+ relationship = Relationship (package .spdx_id + " CONTAINS " + file_entry .spdx_id )
363+ doc .add_relationship (relationship )
361364
362- if len (package .files ) == 0 :
365+ if len (doc .files ) == 0 :
363366 if as_tagvalue :
364367 msg = "# No results for package '{}'.\n " .format (package .name )
365368 else :
@@ -392,7 +395,7 @@ def write_spdx(
392395 # statements for the package.
393396 package .cr_text = '\n ' .join (sorted (package .cr_text )) + '\n '
394397
395- package .verif_code = doc . package . calc_verif_code ()
398+ package .verif_code = calc_verif_code (doc . files )
396399 package .license_declared = NoAssert ()
397400 package .conc_lics = NoAssert ()
398401
@@ -404,7 +407,7 @@ def write_spdx(
404407 # one case we do need to deal with bytes and decode before writing (rdf) and
405408 # in the other case we deal with text all the way.
406409
407- if package .files :
410+ if doc .files :
408411
409412 if as_tagvalue :
410413 from spdx .writers .tagvalue import write_document # NOQA
0 commit comments