Skip to content

Commit a10297d

Browse files
committed
Use license expression for multiple license support #275
* Change the `license` from ListField() to StringField() * Created a new function `parse_license_expression` to parse the license expression * Added/updated some test code Signed-off-by: Chin Yeung Li <[email protected]>
1 parent 6a9e329 commit a10297d

File tree

4 files changed

+34
-16
lines changed

4 files changed

+34
-16
lines changed

src/attributecode/model.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf8 -*-
33
# ============================================================================
4-
# Copyright (c) 2013-2016 nexB Inc. http://www.nexb.com/ - All rights reserved.
4+
# Copyright (c) 2013-2017 nexB Inc. http://www.nexb.com/ - All rights reserved.
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
77
# You may obtain a copy of the License at
@@ -47,6 +47,7 @@
4747
from attributecode import saneyaml
4848
from attributecode import util
4949
from attributecode.util import add_unc, UNC_PREFIX, UNC_PREFIX_POSIX, on_windows, copy_license_files
50+
from license_expression import Licensing
5051

5152

5253
class Field(object):
@@ -628,7 +629,7 @@ def create_fields(self):
628629
('home_url', UrlField()),
629630
('notes', StringField()),
630631

631-
('license', ListField()),
632+
('license', StringField()),
632633
('license_name', StringField()),
633634
('license_file', FileTextField()),
634635
('license_url', UrlField()),
@@ -1009,7 +1010,7 @@ def dump_lic(self, location, license_dict):
10091010
os.makedirs(add_unc(parent))
10101011

10111012
if self.license.present and not self.license_file.present:
1012-
for lic_key in self.license.value:
1013+
for lic_key in parse_license_expression(self.license.value):
10131014
try:
10141015
if license_dict[lic_key]:
10151016
license_path = posixpath.join(parent, lic_key)
@@ -1247,7 +1248,7 @@ def by_license(abouts):
12471248
no_license = grouped['']
12481249
for about in abouts:
12491250
if about.license.value:
1250-
for lic in about.license.value:
1251+
for lic in parse_license_expression(about.license.value):
12511252
if lic in grouped:
12521253
grouped[lic].append(about)
12531254
else:
@@ -1299,7 +1300,7 @@ def by_license_content(abouts):
12991300
no_license = grouped['']
13001301
for about in abouts:
13011302
if about.license.value:
1302-
for lic in about.license.value:
1303+
for lic in parse_license_expression(about.license.value):
13031304
if lic in grouped:
13041305
grouped[lic].append(about)
13051306
else:
@@ -1341,7 +1342,7 @@ def pre_process_and_fetch_license_dict(abouts, api_url, api_key):
13411342
if auth_error in errors:
13421343
break
13431344
if about.license.present:
1344-
for lic_key in about.license.value:
1345+
for lic_key in parse_license_expression(about.license.value):
13451346
if not lic_key in captured_license:
13461347
detail_list = []
13471348
license_name, license_key, license_text, errs = api.get_license_details_from_api(api_url, api_key, lic_key)
@@ -1357,6 +1358,11 @@ def pre_process_and_fetch_license_dict(abouts, api_url, api_key):
13571358
key_text_dict[license_key] = detail_list
13581359
return key_text_dict, errors
13591360

1361+
def parse_license_expression(lic_expression):
1362+
licensing = Licensing()
1363+
# Parse the license expression and save it into a list
1364+
lic_list = licensing.license_keys(lic_expression)
1365+
return lic_list
13601366

13611367
def valid_api_url(api_url):
13621368
try:

tests/test_model.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,13 +1131,26 @@ def test_collect_inventory_populate_about_file_path(self):
11311131
assert expected == result
11321132

11331133
def test_collect_inventory_with_multi_line(self):
1134-
test_loc = get_test_loc('parse/multi_line.ABOUT')
1134+
test_loc = get_test_loc('parse/multi_line_license_expresion.ABOUT')
11351135
errors, abouts = model.collect_inventory(test_loc)
11361136
assert [] == errors
1137-
expected_lic = [u'x11', u'mit', u'public-domain', u'bsd-simplified']
1137+
expected_lic_url = [u'https://enterprise.dejacode.com/urn/?urn=urn:dje:license:mit', u'https://enterprise.dejacode.com/urn/?urn=urn:dje:license:apache-2.0']
1138+
returned_lic_url = abouts[0].license_url.value
1139+
assert expected_lic_url == returned_lic_url
1140+
1141+
def test_collect_inventory_with_license_expression(self):
1142+
test_loc = get_test_loc('parse/multi_line_license_expresion.ABOUT')
1143+
errors, abouts = model.collect_inventory(test_loc)
1144+
assert [] == errors
1145+
expected_lic = u'mit or apache-2.0'
11381146
returned_lic = abouts[0].license.value
11391147
assert expected_lic == returned_lic
11401148

1149+
def test_parse_license_expression(self):
1150+
returned_lic = model.parse_license_expression(u'mit or apache-2.0')
1151+
expected_lic = [u'mit', u'apache-2.0']
1152+
assert expected_lic == returned_lic
1153+
11411154
def test_collect_inventory_works_with_relative_paths(self):
11421155
# FIXME: This test need to be run under src/attributecode/
11431156
# or otherwise it will fail as the test depends on the launching
@@ -1224,7 +1237,7 @@ def test_unique(self):
12241237
def test_by_license(self):
12251238
base_dir = 'some_dir'
12261239
a = model.About()
1227-
a.load_dict({'license': u'apache-2.0\n cddl-1.0', }, base_dir)
1240+
a.load_dict({'license': u'apache-2.0 and cddl-1.0', }, base_dir)
12281241
b = model.About()
12291242
b.load_dict({'license': u'apache-2.0', }, base_dir)
12301243
c = model.About()

tests/testdata/parse/multi_line.ABOUT

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
about_resource: .
2+
name: test
3+
license: mit or apache-2.0
4+
license_url: |
5+
https://enterprise.dejacode.com/urn/?urn=urn:dje:license:mit
6+
https://enterprise.dejacode.com/urn/?urn=urn:dje:license:apache-2.0

0 commit comments

Comments
 (0)