Skip to content

Commit 4e2526c

Browse files
committed
Add new build_spdx_license_expression() function
This new function returns an SPDX license expression string from a ScanCode license_expression string. Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
1 parent 7500b64 commit 4e2526c

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/licensedcode/cache.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,19 @@ def get_spdx_symbols(licenses_db=None, check_consistency=SCANCODE_DEV_MODE):
363363
if licenses_db:
364364
return build_spdx_symbols(licenses_db)
365365
return get_cache(check_consistency=check_consistency).spdx_symbols
366+
367+
368+
def build_spdx_license_expression(license_expression, licensing=None):
369+
"""
370+
Return an SPDX license expression from a ScanCode ``license_expression``
371+
string.
372+
373+
For example::
374+
>>> exp = "mit OR gpl-2.0 with generic-exception"
375+
>>> spdx = "MIT OR GPL-2.0-only WITH LicenseRef-scancode-generic-exception"
376+
>>> assert build_spdx_license_expression(exp) == spdx
377+
"""
378+
if not licensing:
379+
licensing = get_licensing()
380+
parsed = licensing.parse(license_expression)
381+
return parsed.render(template='{symbol.wrapped.spdx_license_key}')

src/licensedcode/models.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -991,11 +991,8 @@ def licensing_contains(self, other):
991991
)
992992

993993
def spdx_license_expression(self, licensing=None):
994-
if not licensing:
995-
from licensedcode.cache import get_licensing
996-
licensing = get_licensing()
997-
parsed = licensing.parse(self.license_expression)
998-
return parsed.render(template='{symbol.spdx_license_key}')
994+
from licensedcode.cache import build_spdx_license_expression
995+
return build_spdx_license_expression(self, licensing=licensing)
999996

1000997
def get_length(self, unique=False):
1001998
return self.length_unique if unique else self.length

0 commit comments

Comments
 (0)