Skip to content

Commit

Permalink
Merge pull request #140 from hesa/hesa-update-py-meta
Browse files Browse the repository at this point in the history
Update pyhton setup.py, add tests for external matrix
  • Loading branch information
hesa committed Feb 26, 2024
2 parents 926b176 + 1570fc0 commit 6c0990a
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .reuse/dep5
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Files: var/licenses/*.json
Copyright: 2023 Henrik Sandklef <hesa@sandklef.com>
License: CC-BY-4.0

Files: tests/licenses/*.json
Files: tests/licenses/*.json tests/*.json
Copyright: 2023 Henrik Sandklef <hesa@sandklef.com>
License: CC-BY-4.0

Expand Down
7 changes: 7 additions & 0 deletions python/flame/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ def get_parser():
help='Add licenses as found in the supplied directory',
default=None)

parser.add_argument('-lmf', '--license-matrix-file',
type=str,
dest='license_matrix_file',
help='Supply license matrix to override OSADL\'s license compatibility matrix',
default=None)

parser.add_argument('-ndu', '--no-dual-update',
action='store_true',
help='do not update dual licenses (e.g. GPL-2.0-or-later -> GPL-2.0-only OR GPL-3.0-only)',
Expand Down Expand Up @@ -199,6 +205,7 @@ def main():
config['additional-license-dir'] = args.additional_license_dir
config['license-dir'] = args.license_dir
config['flame-config'] = args.flame_config
config['license-matrix-file'] = args.license_matrix_file

try:
fl = FossLicenses(config=config)
Expand Down
7 changes: 4 additions & 3 deletions python/flame/license_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def __init__(self, config=None):

self.config = config
self.license_dir = config.get('license-dir', LICENSE_DIR)
self.license_matrix_file = config.get('license-matrix-file', None)
self.additional_license_dir = config.get('additional-license-dir', [])
self.__init_license_db(check)
self.supported_licenses = None
Expand Down Expand Up @@ -578,10 +579,10 @@ def __validate_license(self, validations, license_expression):
if Validation.RELAXED in validations:
self.__validate_license_relaxed(license_expression)
if Validation.OSADL in validations:
compat_licenses = [x.strip() for x in re.split('\(|OR|AND|\)', license_expression)]
compat_license_expression = self.expression_compatibility_as(license_expression)['compat_license']
compat_licenses = [x.strip() for x in re.split('\(|OR|AND|\)', compat_license_expression)]
compat_licenses = [x for x in compat_licenses if x]
compat_support = self.__validate_compatibilities_support(compat_licenses)

self.__validate_licenses_osadl(compat_support)

def __validate_compatibilities_support(self, licenses):
Expand All @@ -601,7 +602,7 @@ def __validate_compatibilities_support(self, licenses):

def __validate_compatibility_support(self, lic):
if not self.supported_licenses:
self.support_licenses = osadl_matrix.supported_licenses()
self.support_licenses = osadl_matrix.supported_licenses(self.license_matrix_file)
return lic in self.support_licenses

def __validate_license_spdx(self, expr):
Expand Down
2 changes: 1 addition & 1 deletion python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
long_description=_long_description,
long_description_content_type="text/markdown",
license_files=('LICENSES/CC-BY-4.0.txt', 'LICENSES/GPL-3.0-or-later.txt'),
url="https://github.com/hesa/license-db",
url="https://github.com/hesa/foss-licenses",
packages=['flame'],
entry_points={
"console_scripts": [
Expand Down
17 changes: 17 additions & 0 deletions tests/mini-matrix.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"BSD-3-Clause": {
"BSD-3-Clause": "Same",
"Dummy": "Yes",
"GPL-2.0-or-later": "No"
},
"Dummy": {
"BSD-3-Clause": "No",
"Dummy": "Same",
"GPL-2.0-or-later": "No"
},
"GPL-2.0-or-later": {
"BSD-3-Clause": "Yes",
"Dummy": "Yes",
"GPL-2.0-or-later": "Same"
}
}
4 changes: 4 additions & 0 deletions tests/python/test_ldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
'license-dir': 'tests/licenses',
'level': 'logging.INFO'})

def test_supported():
licenses = fl.licenses()
assert len(licenses) == 6

def test_alias_list():
# list of all aliases
aliases = fl.alias_list()
Expand Down
44 changes: 44 additions & 0 deletions tests/python/test_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# SPDX-FileCopyrightText: 2024 Henrik Sandklef
#
# SPDX-License-Identifier: GPL-3.0-or-later

import pytest

from flame.exception import FlameException
from flame.license_db import FossLicenses, Validation
import logging

fl_default = FossLicenses(config={
'duals_file': 'tests/licenses-additional/duals.json',
'compunds_file': 'tests/licenses-additional/compounds.json',
'check': True,
'license-dir': 'tests/licenses',
'level': 'logging.INFO'})
fl_matrix = FossLicenses(config={
'duals_file': 'tests/licenses-additional/duals.json',
'compunds_file': 'tests/licenses-additional/compounds.json',
'check': True,
'license-dir': 'tests/licenses',
'level': 'logging.INFO',
'license-matrix-file': 'tests/mini-matrix.json'})

def test_licenses():
licenses = fl_default.licenses()
assert len(licenses) == 6

def test_compat_default():
compat = fl_default.expression_compatibility_as("MIT")
assert compat['compat_license'] == 'MIT'

def test_compat_matrix_mit():
compat = fl_default.expression_compatibility_as("MIT", validations=[Validation.OSADL])
assert compat['compat_license'] == 'MIT'

def test_compat_matrix_unknown():
compat = fl_default.expression_compatibility_as("unknown")
assert compat['compat_license'] == 'unknown'

def test_compat_matrix_unknown_validate():
with pytest.raises(FlameException):
compat = fl_default.expression_compatibility_as("unknown", validations=[Validation.OSADL])

0 comments on commit 6c0990a

Please sign in to comment.