2323from spdx .utils import SPDXNone
2424from spdx .version import Version
2525
26+ from license_expression import Licensing
2627from commoncode .cliutils import OUTPUT_GROUP
2728from commoncode .cliutils import PluggableCommandLineOption
2829from commoncode .fileutils import file_name
2930from commoncode .fileutils import parent_directory
3031from commoncode .text import python_safe_name
3132from formattedcode import FileOptionType
3233from licensedcode .detection import get_matches_from_detection_mappings
34+ from licensedcode .licenses_reference import get_matched_text_from_reference_data
3335from plugincode .output import output_impl
3436from plugincode .output import OutputPlugin
3537import scancode_config
@@ -170,6 +172,7 @@ def _process_codebase(
170172 package_name = build_package_name (input_path )
171173
172174 write_spdx (
175+ codebase = codebase ,
173176 output_file = output_file ,
174177 files = files ,
175178 tool_name = tool_name ,
@@ -208,6 +211,7 @@ def check_sha1(codebase):
208211
209212
210213def write_spdx (
214+ codebase ,
211215 output_file ,
212216 files ,
213217 tool_name ,
@@ -229,6 +233,10 @@ def write_spdx(
229233 producing this SPDX document.
230234 Use ``package_name`` as a Package name and as a namespace prefix base.
231235 """
236+ from licensedcode import cache
237+ licenses = cache .get_licenses_db ()
238+ licensing = Licensing ()
239+
232240 as_rdf = not as_tagvalue
233241 _patch_license_list ()
234242
@@ -282,11 +290,20 @@ def write_spdx(
282290 if license_matches :
283291 all_files_have_no_license = False
284292 for match in license_matches :
285- file_licenses = match ["licenses" ]
286- for file_license in file_licenses :
287- license_key = file_license .get ('key' )
288-
289- spdx_id = file_license .get ('spdx_license_key' )
293+ file_license_expression = match ["license_expression" ]
294+ file_license_keys = licensing .license_keys (
295+ expression = file_license_expression ,
296+ unique = True
297+ )
298+ matched_text = get_matched_text_from_reference_data (
299+ codebase = codebase ,
300+ rule_identifier = match ["rule_identifier" ],
301+ )
302+ for license_key in file_license_keys :
303+ file_license = licenses .get (license_key )
304+ license_key = file_license .key
305+
306+ spdx_id = file_license .spdx_license_key
290307 if not spdx_id :
291308 spdx_id = f'LicenseRef-scancode-{ license_key } '
292309 is_license_ref = spdx_id .lower ().startswith ('licenseref-' )
@@ -295,7 +312,7 @@ def write_spdx(
295312 spdx_license = License .from_identifier (spdx_id )
296313 else :
297314 spdx_license = ExtractedLicense (spdx_id )
298- spdx_license .name = file_license .get ( ' short_name' )
315+ spdx_license .name = file_license .short_name
299316 # FIXME: replace this with the licensedb URL
300317 comment = (
301318 f'See details at https://github.com/nexB/scancode-toolkit'
0 commit comments