Skip to content

Commit 5901f23

Browse files
committed
Add code to handle special character(s) in license_expression #275
* The following characters cannot be in the license expression: ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '+', '=', '{', '}', '|', '[', ']', '\\', ':', ';', '<', '>', '?', ',', '/'] Signed-off-by: Chin Yeung Li <[email protected]>
1 parent a10297d commit 5901f23

File tree

2 files changed

+75
-41
lines changed

2 files changed

+75
-41
lines changed

src/attributecode/model.py

Lines changed: 65 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,19 +1010,21 @@ def dump_lic(self, location, license_dict):
10101010
os.makedirs(add_unc(parent))
10111011

10121012
if self.license.present and not self.license_file.present:
1013-
for lic_key in parse_license_expression(self.license.value):
1014-
try:
1015-
if license_dict[lic_key]:
1016-
license_path = posixpath.join(parent, lic_key)
1017-
license_path += u'.LICENSE'
1018-
license_path = add_unc(license_path)
1019-
license_name, license_context, license_url = license_dict[lic_key]
1020-
license_info = (lic_key, license_name, license_context, license_url)
1021-
license_key_name_context_url.append(license_info)
1022-
with codecs.open(license_path, mode='wb', encoding='utf-8') as lic:
1023-
lic.write(license_context)
1024-
except:
1025-
pass
1013+
special_char_in_expression, lic_list = parse_license_expression(self.license.value)
1014+
if not special_char_in_expression:
1015+
for lic_key in lic_list:
1016+
try:
1017+
if license_dict[lic_key]:
1018+
license_path = posixpath.join(parent, lic_key)
1019+
license_path += u'.LICENSE'
1020+
license_path = add_unc(license_path)
1021+
license_name, license_context, license_url = license_dict[lic_key]
1022+
license_info = (lic_key, license_name, license_context, license_url)
1023+
license_key_name_context_url.append(license_info)
1024+
with codecs.open(license_path, mode='wb', encoding='utf-8') as lic:
1025+
lic.write(license_context)
1026+
except:
1027+
pass
10261028
return license_key_name_context_url
10271029

10281030
# valid field name
@@ -1248,11 +1250,13 @@ def by_license(abouts):
12481250
no_license = grouped['']
12491251
for about in abouts:
12501252
if about.license.value:
1251-
for lic in parse_license_expression(about.license.value):
1252-
if lic in grouped:
1253-
grouped[lic].append(about)
1254-
else:
1255-
grouped[lic] = [about]
1253+
special_char_in_expression, lic_list = parse_license_expression(about.license.value)
1254+
if not special_char_in_expression:
1255+
for lic in lic_list:
1256+
if lic in grouped:
1257+
grouped[lic].append(about)
1258+
else:
1259+
grouped[lic] = [about]
12561260
else:
12571261
no_license.append(about)
12581262
return OrderedDict(sorted(grouped.items()))
@@ -1300,11 +1304,13 @@ def by_license_content(abouts):
13001304
no_license = grouped['']
13011305
for about in abouts:
13021306
if about.license.value:
1303-
for lic in parse_license_expression(about.license.value):
1304-
if lic in grouped:
1305-
grouped[lic].append(about)
1306-
else:
1307-
grouped[lic] = [about]
1307+
special_char_in_expression, lic_list = parse_license_expression(about.license.value)
1308+
if not special_char_in_expression:
1309+
for lic in lic_list:
1310+
if lic in grouped:
1311+
grouped[lic].append(about)
1312+
else:
1313+
grouped[lic] = [about]
13081314
else:
13091315
no_license.append(about)
13101316
return OrderedDict(sorted(grouped.items()))
@@ -1342,27 +1348,46 @@ def pre_process_and_fetch_license_dict(abouts, api_url, api_key):
13421348
if auth_error in errors:
13431349
break
13441350
if about.license.present:
1345-
for lic_key in parse_license_expression(about.license.value):
1346-
if not lic_key in captured_license:
1347-
detail_list = []
1348-
license_name, license_key, license_text, errs = api.get_license_details_from_api(api_url, api_key, lic_key)
1349-
for e in errs:
1350-
if e not in errors:
1351-
errors.append(e)
1352-
if license_key:
1353-
captured_license.append(lic_key)
1354-
dje_lic_url = dje_lic_urn + license_key
1355-
detail_list.append(license_name)
1356-
detail_list.append(license_text)
1357-
detail_list.append(dje_lic_url)
1358-
key_text_dict[license_key] = detail_list
1351+
special_char_in_expression, lic_list = parse_license_expression(about.license.value)
1352+
if special_char_in_expression:
1353+
msg = (u"The following character(s) cannot be in the licesne_expression: " +
1354+
str(special_char_in_expression))
1355+
errors.append(Error(ERROR, msg))
1356+
else:
1357+
for lic_key in lic_list:
1358+
if not lic_key in captured_license:
1359+
detail_list = []
1360+
license_name, license_key, license_text, errs = api.get_license_details_from_api(api_url, api_key, lic_key)
1361+
for e in errs:
1362+
if e not in errors:
1363+
errors.append(e)
1364+
if license_key:
1365+
captured_license.append(lic_key)
1366+
dje_lic_url = dje_lic_urn + license_key
1367+
detail_list.append(license_name)
1368+
detail_list.append(license_text)
1369+
detail_list.append(dje_lic_url)
1370+
key_text_dict[license_key] = detail_list
13591371
return key_text_dict, errors
13601372

13611373
def parse_license_expression(lic_expression):
13621374
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
1375+
lic_list = []
1376+
special_char = special_char_in_license_expresion(lic_expression)
1377+
if not special_char:
1378+
# Parse the license expression and save it into a list
1379+
lic_list = licensing.license_keys(lic_expression)
1380+
return special_char, lic_list
1381+
1382+
def special_char_in_license_expresion(lic_expression):
1383+
not_support_char = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
1384+
'+', '=', '{', '}', '|', '[', ']', '\\',
1385+
':', ';', '<', '>', '?', ',', '/']
1386+
special_character = []
1387+
for char in not_support_char:
1388+
if char in lic_expression:
1389+
special_character.append(char)
1390+
return special_character
13661391

13671392
def valid_api_url(api_url):
13681393
try:

tests/test_model.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1147,9 +1147,18 @@ def test_collect_inventory_with_license_expression(self):
11471147
assert expected_lic == returned_lic
11481148

11491149
def test_parse_license_expression(self):
1150-
returned_lic = model.parse_license_expression(u'mit or apache-2.0')
1150+
spec_char, returned_lic = model.parse_license_expression(u'mit or apache-2.0')
11511151
expected_lic = [u'mit', u'apache-2.0']
1152+
expected_spec_char = []
11521153
assert expected_lic == returned_lic
1154+
assert expected_spec_char == spec_char
1155+
1156+
def test_parse_license_expression_with_special_chara(self):
1157+
spec_char, returned_lic = model.parse_license_expression(u'mit, apache-2.0')
1158+
expected_lic = []
1159+
expected_spec_char = [',']
1160+
assert expected_lic == returned_lic
1161+
assert expected_spec_char == spec_char
11531162

11541163
def test_collect_inventory_works_with_relative_paths(self):
11551164
# FIXME: This test need to be run under src/attributecode/

0 commit comments

Comments
 (0)