Skip to content

Commit

Permalink
lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hesa committed Sep 3, 2023
1 parent a7f12da commit 8d063da
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 65 deletions.
6 changes: 0 additions & 6 deletions python/flame/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,6 @@ def format_compat(self, compat, verbose):
def format_compatibilities(self, compats, verbose):
ret = []
ret.append(f'{compats["compat_license"]}')
#import json
#print("---- compats: " + json.dumps(compats, indent=4))
#print("---- compats: " + str(len(compats)))
if verbose:
for compat in compats['compatibilities']:
ret.append(f' * "{compat["queried_name"]}" -> "{compat["name"]}" via "{compat["identified_via"]}"')
Expand All @@ -161,9 +158,6 @@ def format_expression(self, expression, verbose):
ret.append(f'{id_lic}')
if verbose:
for identification in expression['identifications']:
#print(f' * identifcation: {identification}')
#id_elem = identification['identified_element']
#print(f' * id_elem: {id_elem}')
ret.append(f' * "{identification["queried_name"]}" -> "{identification["name"]} via "{identification["identified_via"]}"')
return '\n'.join(ret)

Expand Down
27 changes: 8 additions & 19 deletions python/flame/license_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,12 @@ def __init_license_db(self, check=False):
compats[data['spdxid']] = data[COMPATIBILITY_AS_TAG]

self.license_expression = license_expression.get_spdx_licensing()
# self.license_expression = None
self.license_db[LICENSES_TAG] = licenses
self.license_db[COMPATS_TAG] = compats
self.license_db[ALIASES_TAG] = aliases
self.license_db[SCANCODE_KEYS_TAG] = scancode_keys
self.license_db[SCANCODE_KEYS_TAG] = scancode_keys
self.license_db[LICENSE_OPERATORS_TAG] = self.__read_json(LICENSE_OPERATORS_FILE)['operators']
# regular expression for splitting expression in to parts
# re_list = [re.escape(op) for op in self.license_db[LICENSE_OPERATORS_TAG]]
# make sure to && and || before & and | by sorting reverse
#re_list.sort(reverse=True)
#self.license_operators_re = '(' + '|'.join(re_list) + ')'

def __identify_license(self, name):
if name in self.license_db[LICENSES_TAG]:
Expand Down Expand Up @@ -134,9 +128,9 @@ def __update_license_expression_helper(self, needles, needle_tag, license_expres

return {
"license_expression": re.sub(r'\s\s*', ' ', license_expression).strip(),
"identifications": replacements
"identifications": replacements
}

def expression_license(self, license_expression):
"""Returns an object with information about the normalized license for the license given.
Expand All @@ -158,11 +152,6 @@ def expression_license(self, license_expression):
ret['license_expression'])
replacements += ret['identifications']

# scancode identifiers -> SPDX
#ret = self.__update_license_expression_helper(self.license_db[SCANCODE_KEYS_TAG],
# "scancode_key",
# ret['license_expression'])
#replacements += ret['identifications']
license_parsed = str(self.license_expression.parse(ret['license_expression']))

return {
Expand All @@ -173,7 +162,7 @@ def expression_license(self, license_expression):

def licenses(self):
"""
Returns all licenses
Returns all licenses supported by flame
"""
return list(self.license_db[LICENSES_TAG].keys())

Expand Down Expand Up @@ -226,7 +215,7 @@ def compatibility_as_list(self):
licenses = self.license_db[LICENSES_TAG]
return [{COMPATIBILITY_AS_TAG: licenses[x][COMPATIBILITY_AS_TAG], 'spdxid': licenses[x]['spdxid']} for x in licenses if COMPATIBILITY_AS_TAG in licenses[x]]

def aliases_list(self, alias_license: str = None) -> [str]:
def aliases_list(self, alias_license: str = None) -> [str]:
"""Returns a list of all the aliases. Supplying will alias_license
will return a list of aliases beginning with alias_license
Expand All @@ -238,7 +227,7 @@ def aliases_list(self, alias_license: str = None) -> [str]:
return self.license_db[ALIASES_TAG]

def aliases(self, license_name):
"""Returns a list of all the aliases for a license
"""Returns a list of all the aliases for a license
:param str license_name: Exact name (SPDXID) of the license
"""
Expand Down Expand Up @@ -289,7 +278,7 @@ def expression_compatibility_as(self, license_expression, validate_spdx=False, v
self.__validate_license_spdx(compat_license_expression)
elif validate_relaxed:
self.__validate_license_relaxed(compat_license_expression)

return {
'compatibilities': compats,
'queried_license': license_expression,
Expand All @@ -310,9 +299,9 @@ def __validate_license_spdx(self, expr):
def __validate_license_relaxed(self, expr):
"""
"""
SPDX_OPERATORS=['AND', 'OR', 'WITH']
SPDX_OPERATORS = ['AND', 'OR', 'WITH']
license_list = re.split(f'{"|".join(SPDX_OPERATORS)}', expr)
for _lic in license_list:
lic=_lic.strip()
lic = _lic.strip()
if " " in lic.strip():
raise FlameException(f'Found license with multiple words "{lic}"')
117 changes: 78 additions & 39 deletions tests/python/test_ldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,81 +9,120 @@
import sys
import pytest

from flame.license_db import LicenseDatabase
from flame.exception import LicenseDatabaseError
from flame.license_db import FossLicenses
from flame.exception import FlameException
import logging

ldb = LicenseDatabase(check=True, license_dir="tests/licenses", logging_level=logging.INFO)
fl = FossLicenses(check=True, license_dir="tests/licenses", logging_level=logging.INFO)

def test_alias_list():
# list of all aliases
aliases = ldb.aliases_list()
aliases = fl.aliases_list()
assert len(aliases) == 5

def test_alias_list_alias():
def test_alias_list_gpl():
# list of all aliases with license with GPL
aliases = ldb.aliases_list("GPL")
aliases = fl.aliases_list("GPL")
assert len(aliases) == 3

def test_aliases():
# list of all aliases for GPL-2.0-or-later
aliases = ldb.aliases("GPL-2.0-or-later")
aliases = fl.aliases("GPL-2.0-or-later")
aliases.sort()
assert aliases == [ "GPL (v2 or later)", "GPL2+", "GPLv2+" ]

def test_aliases_bad_input():
# make sure bad input raises exception
with pytest.raises(LicenseDatabaseError) as _error:
ldb.aliases(None)
with pytest.raises(FlameException) as _error:
fl.aliases(None)

with pytest.raises(LicenseDatabaseError) as _error:
ldb.aliases("Dummy")
with pytest.raises(FlameException) as _error:
fl.aliases("Dummy")

def test_identify():
lic = ldb.license("GPL2+")
assert lic['license']['spdxid'] == "GPL-2.0-or-later"
def test_expression_license():
lic = fl.expression_license("GPL2+")
print("lic: " + str(lic), file=sys.stderr)
assert lic['identified_license'] == "GPL-2.0-or-later"

def test_identify_with_blank():
lic = ldb.license("GPL (v2 or later)")
assert lic['license']['spdxid'] == "GPL-2.0-or-later"
def test_expression_license_with_blank():
lic = fl.expression_license("GPL (v2 or later)")
assert lic['identified_license'] == "GPL-2.0-or-later"

def test_identify_expression():
lic = ldb.expression_license("BSD-3-Clause and BSD3")
def test_expression_license():
lic = fl.expression_license("BSD-3-Clause and BSD3")
assert lic['identified_license'] == "BSD-3-Clause AND BSD-3-Clause"

def test_fail_identify():
def test_expression_license_types():
lic = fl.expression_license("BSD-3-Clause and BSD3")
assert 'queried_license' in lic
assert isinstance(lic['queried_license'], str)

with pytest.raises(LicenseDatabaseError) as _error:
ldb.license("Dummy")
assert 'identified_license' in lic
assert isinstance(lic['identified_license'], str)

with pytest.raises(LicenseDatabaseError) as _error:
ldb.license(None)
assert 'identifications' in lic
assert isinstance(lic['identifications'], list)

def test_fail_expression_license():
with pytest.raises(FlameException) as _error:
fl.expression_license(None)

with pytest.raises(FlameException) as _error:
fl.expression_license([])

with pytest.raises(FlameException) as _error:
fl.expression_license(Exception())

def test_compat_as_list():
c = ldb.compatibility_as_list()
c = fl.compatibility_as_list()
assert len(c) == 1

def test_compat_as():
c = ldb.compatibility_as("GPL-2.0-or-later")
assert c['compatibility']['compat_as'] == "GPL-2.0-or-later"
c = fl.expression_compatibility_as("GPL-2.0-or-later")
assert c['compat_license'] == "GPL-2.0-or-later"

def test_compat_as_aliased():
c = fl.expression_compatibility_as("GPLv2+")
assert c['compat_license'] == "GPL-2.0-or-later"

def test_compat_as_aliased():
c = ldb.compatibility_as("GPLv2+")
assert c['compatibility']['compat_as'] == "GPL-2.0-or-later"
c = fl.expression_compatibility_as("GPLv2+ | BSD3")
assert c['compat_license'] == "GPL-2.0-or-later OR BSD-3-Clause"

c = fl.expression_compatibility_as("GPLv2+ || BSD3")
assert c['compat_license'] == "GPL-2.0-or-later OR BSD-3-Clause"

def test_licenses():
licenses = ldb.licenses()
assert len(licenses) == 3
c = fl.expression_compatibility_as("GPLv2+ & BSD3")
assert c['compat_license'] == "GPL-2.0-or-later AND BSD-3-Clause"

c = fl.expression_compatibility_as("GPLv2+ && BSD3")
assert c['compat_license'] == "GPL-2.0-or-later AND BSD-3-Clause"

# add misc blanks to the license expression
for i in range(1,10):
for j in range(1,10):
for k in range(1,10):
c = fl.expression_compatibility_as(f'{" "*i}GPLv2+{" "*j}&& BSD3{" "*k}')
assert c['compat_license'] == "GPL-2.0-or-later AND BSD-3-Clause"

def test_operator_op():
for op in [ "|", "||", "or", "OR" ]:
c = fl.expression_compatibility_as(f'GPLv2+ {op} BSD3')
assert c['compat_license'] == "GPL-2.0-or-later OR BSD-3-Clause"

def test_operator_and():
for op in [ "&", "&&", "and", "AND" ]:
c = fl.expression_compatibility_as(f'GPLv2+ {op} BSD3')
assert c['compat_license'] == "GPL-2.0-or-later AND BSD-3-Clause"

def test_operators():
operators = ldb.operators()
operators = fl.operators()
assert len(operators) > 3

def test_compat_as_bad_input():

with pytest.raises(LicenseDatabaseError) as _error:
ldb.compatibility_as(None)
def _do_nada_test_compat_as_bad_input():

with pytest.raises(LicenseDatabaseError) as _error:
ldb.compatibility_as("Not existing")
with pytest.raises(FlameException) as _error:
fl.expression_compatibility_as(None)

with pytest.raises(FlameException) as _error:
fl.expression_compatibility_as("Not existing")
1 change: 0 additions & 1 deletion var/licenses/BSD-2-Clause.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"scancode://bsd-simplified",


"BSD",
"2-clause BSD license",
"BSD 2-Clause",
"BSD Software License, 2-clause version",
Expand Down

0 comments on commit 8d063da

Please sign in to comment.