Skip to content

Commit 4b8a829

Browse files
committed
#450 - Code enhancement and added tests
1 parent 7f1c923 commit 4b8a829

File tree

9 files changed

+143
-16
lines changed

9 files changed

+143
-16
lines changed

src/attributecode/attrib.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def generate(abouts, template=None, variables=None):
9696
if about.license_expression.value or about.license_key.value:
9797
if about.license_expression.value:
9898
special_char, lic_list = parse_license_expression(about.license_expression.value)
99+
about.license_key.value = lic_list
99100
else:
100101
lic_list = about.license_key.value
101102
special_char = []
@@ -109,9 +110,8 @@ def generate(abouts, template=None, variables=None):
109110
'license_expression or license_key: %s' % special_char)
110111
return error, ''
111112
else:
112-
# No license_key or license_expression present. We will use
113-
# the license_file_name as the license_key as needed for the
114-
# linking feature in the jinja2 template
113+
# No license_key or license_expression present. We will put
114+
# None as the value of license key
115115
about.license_key.value = about.license_file.value.keys()
116116
lic_list = about.license_file.value.keys()
117117

src/attributecode/model.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,13 +1085,13 @@ def dumps(self, licenses_dict=None):
10851085
license_file.append(lic)
10861086
else:
10871087
if isinstance(field.original_value, list):
1088-
license_file = field.value.keys()
1088+
license_file = list(field.value.keys())
10891089
else:
10901090
# Restore the original license_file value
10911091
# See #444
10921092
license_file = [field.original_value]
10931093
else:
1094-
license_file = field.value.keys()
1094+
license_file = list(field.value.keys())
10951095
elif field.name == 'license_url' and field.value:
10961096
license_url = field.value
10971097
elif field.name in file_fields and field.value:
@@ -1105,10 +1105,10 @@ def dumps(self, licenses_dict=None):
11051105
# Group the same license information in a list
11061106
# This `licenses_dict` is a dictionary with license key as the key and the
11071107
# value is the list of [license_name, license_context, license_url]
1108-
lic_key_dup = license_key[:]
1108+
lic_key_copy = license_key[:]
1109+
lic_dict_list = []
11091110
for lic_key in license_key:
11101111
lic_dict = OrderedDict()
1111-
11121112
if licenses_dict and lic_key in licenses_dict:
11131113
lic_dict['key'] = lic_key
11141114
lic_name = licenses_dict[lic_key][0]
@@ -1120,30 +1120,43 @@ def dumps(self, licenses_dict=None):
11201120
lic_dict['url'] = lic_url
11211121

11221122
# Remove the license information if it has been handled
1123-
#license_key.remove(lic_key)
1124-
#print(license_key)
1125-
lic_key_dup.remove(lic_key)
1123+
lic_key_copy.remove(lic_key)
11261124
if lic_name in license_name:
11271125
license_name.remove(lic_name)
11281126
if lic_url in license_url:
11291127
license_url.remove(lic_url)
11301128
if lic_file in license_file:
11311129
license_file.remove(lic_file)
1132-
data.setdefault('licenses', []).append(lic_dict)
1130+
lic_dict_list.append(lic_dict)
11331131

11341132
# Handle license information that have not been handled.
1135-
license_group = list(zip_longest(lic_key_dup, license_name, license_file, license_url))
1133+
license_group = list(zip_longest(lic_key_copy, license_name, license_file, license_url))
11361134
for lic_group in license_group:
11371135
lic_dict = OrderedDict()
11381136
if lic_group[0]:
11391137
lic_dict['key'] = lic_group[0]
11401138
if lic_group[1]:
11411139
lic_dict['name'] = lic_group[1]
1140+
else:
1141+
# If no name is given, treat the key as the name
1142+
if lic_group[0]:
1143+
lic_dict['name'] = lic_group[0]
11421144
if lic_group[2]:
11431145
lic_dict['file'] = lic_group[2]
11441146
if lic_group[3]:
11451147
lic_dict['url'] = lic_group[3]
1146-
data.setdefault('licenses', []).append(lic_dict)
1148+
lic_dict_list.append(lic_dict)
1149+
1150+
# Format the license information in the same order of the license expression
1151+
if license_key:
1152+
for key in license_key:
1153+
for lic_dict in lic_dict_list:
1154+
if key == lic_dict['key']:
1155+
data.setdefault('licenses', []).append(lic_dict)
1156+
break
1157+
else:
1158+
for lic_dict in lic_dict_list:
1159+
data.setdefault('licenses', []).append(lic_dict)
11471160

11481161
return saneyaml.dump(data)
11491162

@@ -1234,13 +1247,16 @@ def dump_lic(self, location, license_dict):
12341247

12351248
if self.license_expression.present:
12361249
special_char_in_expression, lic_list = parse_license_expression(self.license_expression.value)
1250+
self.license_key.value = lic_list
12371251
self.license_key.present = True
12381252
if not special_char_in_expression:
12391253
for lic_key in lic_list:
12401254
try:
12411255
if license_dict[lic_key]:
1256+
"""
12421257
if not lic_key in self.license_key.value:
12431258
self.license_key.value.append(lic_key)
1259+
"""
12441260
license_path = posixpath.join(parent, lic_key)
12451261
license_path += u'.LICENSE'
12461262
license_path = add_unc(license_path)

templates/default_html.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ See "get_license_file_key" in `attrib.py` for more information
9595

9696
<h3>Common Licenses Used in This Product</h3>
9797

98-
{% for key in license_key_and_context %}
98+
{% for key in license_file_key_and_context %}
9999
{% if key in common_licenses %}
100100
<h3 id="component-license-{{ key }}">{{ key }}</h3>
101-
<pre>{{ license_key_and_context[key]|e }}</pre>
101+
<pre>{{ license_file_key_and_context[key]|e }}</pre>
102102
{% endif %}
103103
{% endfor %}
104104

tests/test_gen.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,13 @@ def test_generate_multi_lic_issue_443(self):
217217
version: '1.5'
218218
licenses:
219219
- key: License1
220+
name: License1
220221
file: LIC1.LICENSE
221222
- key: License2
223+
name: License2
222224
file: LIC2.LICENSE
223225
- key: License3
226+
name: License3
224227
file: LIC3.LICENSE
225228
'''
226229
)
@@ -238,11 +241,113 @@ def test_generate_multi_lic_issue_444(self):
238241
name: test.c
239242
licenses:
240243
- key: License1
244+
name: License1
241245
file: LIC1.LICENSE, LIC2.LICENSE
242246
'''
243247
)
244248
assert expected == result
245249

250+
def test_generate_license_key_with_custom_file_450_no_fetch(self):
251+
location = get_test_loc('test_gen/lic_issue_450/custom_and_valid_lic_key_with_file.csv')
252+
base_dir = get_temp_dir()
253+
254+
errors, abouts = gen.generate(location, base_dir)
255+
256+
result = [a.dumps() for a in abouts][0]
257+
expected = (
258+
'''about_resource: test.c
259+
name: test.c
260+
license_expression: public-domain AND custom
261+
licenses:
262+
- file: custom.txt
263+
'''
264+
)
265+
assert expected == result
266+
267+
def test_generate_license_key_with_custom_file_450_with_fetch(self):
268+
location = get_test_loc('test_gen/lic_issue_450/custom_and_valid_lic_key_with_file.csv')
269+
base_dir = get_temp_dir()
270+
271+
errors, abouts = gen.generate(location, base_dir)
272+
273+
lic_dict = {u'public-domain': [u'Public Domain',
274+
u'This component is released to the public domain by the author.',
275+
u'https://enterprise.dejacode.com/urn/?urn=urn:dje:license:public-domain'
276+
]}
277+
a = abouts[0]
278+
a.license_key.value.append('public-domain')
279+
a.license_key.value.append('custom')
280+
result = a.dumps(lic_dict)
281+
expected = (
282+
'''about_resource: test.c
283+
name: test.c
284+
license_expression: public-domain AND custom
285+
licenses:
286+
- key: public-domain
287+
name: Public Domain
288+
file: public-domain.LICENSE
289+
url: https://enterprise.dejacode.com/urn/?urn=urn:dje:license:public-domain
290+
- key: custom
291+
name: custom
292+
file: custom.txt
293+
'''
294+
)
295+
assert expected == result
296+
297+
298+
def test_generate_license_key_with_custom_file_450_with_fetch_with_order(self):
299+
location = get_test_loc('test_gen/lic_issue_450/custom_and_valid_lic_key_with_file.csv')
300+
base_dir = get_temp_dir()
301+
302+
errors, abouts = gen.generate(location, base_dir)
303+
304+
lic_dict = {u'public-domain': [u'Public Domain',
305+
u'This component is released to the public domain by the author.',
306+
u'https://enterprise.dejacode.com/urn/?urn=urn:dje:license:public-domain'
307+
]}
308+
# The first row from the test file
309+
a = abouts[0]
310+
a.license_key.value.append('public-domain')
311+
a.license_key.value.append('custom')
312+
result1 = a.dumps(lic_dict)
313+
# The second row from the test file
314+
b = abouts[1]
315+
b.license_key.value.append('custom')
316+
b.license_key.value.append('public-domain')
317+
result2 = b.dumps(lic_dict)
318+
319+
expected1 = (
320+
'''about_resource: test.c
321+
name: test.c
322+
license_expression: public-domain AND custom
323+
licenses:
324+
- key: public-domain
325+
name: Public Domain
326+
file: public-domain.LICENSE
327+
url: https://enterprise.dejacode.com/urn/?urn=urn:dje:license:public-domain
328+
- key: custom
329+
name: custom
330+
file: custom.txt
331+
'''
332+
)
333+
334+
expected2 = (
335+
'''about_resource: test.h
336+
name: test.h
337+
license_expression: custom AND public-domain
338+
licenses:
339+
- key: custom
340+
name: custom
341+
file: custom.txt
342+
- key: public-domain
343+
name: Public Domain
344+
file: public-domain.LICENSE
345+
url: https://enterprise.dejacode.com/urn/?urn=urn:dje:license:public-domain
346+
'''
347+
)
348+
assert expected1 == result1
349+
assert expected2 == result2
350+
246351
@skip('FIXME: this test is making a failed, live API call')
247352
def test_generate_not_overwrite_original_license_file(self):
248353
location = get_test_loc('test_gen/inv5.csv')

tests/test_model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ def test_About_dumps(self):
749749
vcs_repository: https://github.com/dejacode/about-code-tool.git
750750
licenses:
751751
- key: apache-2.0
752+
name: Apache 2.0
752753
file: apache-2.0.LICENSE
753754
'''
754755
result = a.dumps()

tests/testdata/test_attrib/gen_license_key_name_check/test.ABOUT

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
about_resource: .
22
name: test
3-
license_expression: LGPL-3.0-or-later OR Apache-2.0
3+
license_expression: Apache-2.0 OR LGPL-3.0-or-later
44
licenses:
55
- key: Apache-2.0
66
name: Apache License 2.0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a custom license.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
about_resource,name,license_expression,license_file
2+
test.c,test.c,public-domain AND custom,custom.txt
3+
test.h,test.h,custom AND public-domain,custom.txt

tests/testdata/test_model/dumps/about.ABOUT

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ description: |
1818

1919
license_expression: apache-2.0
2020
license_key: apache-2.0
21+
license_name: Apache 2.0
2122
license_file: apache-2.0.LICENSE
2223
copyright: Copyright (c) 2013-2014 nexB Inc.
2324

0 commit comments

Comments
 (0)