Skip to content

Commit fb614c0

Browse files
committed
Merge branch '86_genattrib_text_update' into develop
Conflicts: about_code_tool/about.py about_code_tool/templates/default.html The common_license should use the DJE license name instead of the name that we used for the confirmed license. In addition, we should use the dje_license_name instead of the dje_license.
2 parents 48b7555 + 197741f commit fb614c0

File tree

5 files changed

+86
-45
lines changed

5 files changed

+86
-45
lines changed

about_code_tool/about.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ def repr_problem(obj):
156156
'dje_component',
157157
'dje_license',
158158
'dje_organization',
159+
'dje_license_name'
159160
)
160161

161162
OPTIONAL_FIELDS = BASIC_FIELDS + OWNERSHIP_FIELDS + LICENSE_FIELDS + \
@@ -1097,7 +1098,8 @@ def generate_attribution(self, template_path='templates/default.html',
10971098
license_key = []
10981099
license_text = []
10991100
license_dict = {}
1100-
common_license = ['GPL 2.0','OpenSSL/SSLeay License', 'Apache 2.0', 'BSD-Modified', 'CC-BY-SA-3.0']
1101+
common_license = ['GNU General Public License 2.0','OpenSSL/SSLeay License', 'Apache License 2.0', 'BSD-Modified']
1102+
11011103
for about_object in self:
11021104
about_relative_path = '/'+ about_object.location.partition(self.user_provided_path)[2]
11031105
if not limit_to or about_relative_path in limit_to:

about_code_tool/genabout.py

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757

5858
# The 'dje_license_key' will be removed and will use the 'dje_license' instead.
5959
SUPPORTED_FIELDS = about.OPTIONAL_FIELDS + about.MANDATORY_FIELDS + \
60-
('about_file', 'dje_license_key',)
60+
('about_file',)
6161

6262
Warn = namedtuple('Warn', 'field_name field_value message',)
6363
Error = namedtuple('Error', 'field_name field_value message',)
@@ -232,6 +232,8 @@ def request_license_data(self, url, username, api_key, license_key):
232232
full_url = '{0}{1}/?{2}'.format(
233233
url if url.endswith('/') else url + '/',
234234
license_key, urllib.urlencode(payload))
235+
# The following is to handle special characters in URL such as space etc.
236+
full_url = urllib.quote(full_url, safe="%/:=&?~#+!$,;'@()*[]")
235237

236238
try:
237239
request = urllib2.Request(full_url)
@@ -247,7 +249,7 @@ def request_license_data(self, url, username, api_key, license_key):
247249
self.extract_dje_license_error = True
248250
self.errors.append(Error('username/key', username + '/' + api_key, error_msg))
249251
else:
250-
self.errors.append(Error('dje_license_key', license_key, "Invalid 'dje_license_key'"))
252+
self.errors.append(Error('dje_license', license_key, "Invalid 'dje_license_key'"))
251253
return {}
252254
except urllib2.URLError:
253255
if about.check_network_connection():
@@ -281,19 +283,22 @@ def copy_license_files(gen_location, license_list):
281283
def write_licenses(self, license_context_list):
282284
for gen_license_path, license_context in license_context_list:
283285
try:
286+
if not _exists(dirname(gen_license_path)):
287+
makedirs(dirname(gen_license_path))
284288
with open(gen_license_path, 'wb') as output:
285289
output.write(license_context)
286290
except Exception as e:
287291
self.errors.append(Error('Unknown', gen_license_path, "Something is wrong."))
288292

289-
def get_license_text_from_api(self, url, username, api_key, license_key):
293+
def get_license_details_from_api(self, url, username, api_key, license_key):
290294
"""
291295
Returns the license_text of a given license_key using an API request.
292296
Returns an empty string if the text is not available.
293297
"""
294298
data = self.request_license_data(url, username, api_key, license_key)
299+
license_name = data.get('name', '')
295300
license_text = data.get('full_text', '')
296-
return license_text
301+
return [license_name, license_text]
297302

298303
def get_dje_license_list(self, gen_location, input_list, gen_license):
299304
license_output_list = []
@@ -311,22 +316,49 @@ def get_dje_license_list(self, gen_location, input_list, gen_license):
311316
self.errors.append(Error('license_text_file', license_file, "The 'license_text_file' does not exist."))
312317
else:
313318
if gen_license:
314-
if line['dje_license_key']:
319+
if line['dje_license']:
315320
license_output_list.append(self.gen_license_list(line))
316321
else:
317-
self.warnings.append(Warn('dje_license_key', '',
318-
"Missing 'dje_license_key' for " + line['about_file']))
322+
self.warnings.append(Warn('dje_license', '',
323+
"Missing 'dje_license' for " + line['about_file']))
319324
# This except condition will force the tool to create the
320325
# 'license_text_file' key column from the self.gen_license_list(line)
321326
except Exception as e:
322327
if gen_license:
323-
if line['dje_license_key']:
328+
if line['dje_license']:
324329
license_output_list.append(self.gen_license_list(line))
325330
else:
326-
self.warnings.append(Warn('dje_license_key', '',
327-
"Missing 'dje_license_key' for " + line['about_file']))
331+
self.warnings.append(Warn('dje_license', '',
332+
"Missing 'dje_license' for " + line['about_file']))
328333
return license_output_list
329334

335+
def pre_process_and_dje_license_dict(self, input_list, api_url, api_username, api_key):
336+
key_text_dict = {}
337+
for line in input_list:
338+
try:
339+
if line['dje_license']:
340+
detail = self.get_license_details_from_api(api_url, api_username, api_key, line['dje_license'])
341+
line['dje_license_name'], key_text_dict[line['dje_license']] = detail
342+
except Exception as e:
343+
self.warnings.append(Warn('dje_license', '',
344+
"Missing 'dje_license' for " + line['about_file']))
345+
return key_text_dict
346+
347+
def process_dje_licenses(self, dje_license_list, dje_license_dict, output_path):
348+
license_list_context = []
349+
for gen_path, license_key in dje_license_list:
350+
if gen_path.startswith('/'):
351+
gen_path = gen_path.partition('/')[2]
352+
gen_license_path = join(output_path, gen_path, license_key) + '.LICENSE'
353+
if not _exists(gen_license_path) and not self.extract_dje_license_error:
354+
context = dje_license_dict[license_key]
355+
if context:
356+
gen_path_context = []
357+
gen_path_context.append(gen_license_path)
358+
gen_path_context.append(context.encode('utf8'))
359+
license_list_context.append(gen_path_context)
360+
return license_list_context
361+
330362
def pre_generation(self, gen_location, input_list, action_num, all_in_one):
331363
output_list = []
332364
for line in input_list:
@@ -372,7 +404,7 @@ def pre_generation(self, gen_location, input_list, action_num, all_in_one):
372404

373405
@staticmethod
374406
def gen_license_list(line):
375-
dje_key = line['dje_license_key']
407+
dje_key = line['dje_license']
376408
file_location = line['about_file']
377409
if file_location.endswith('/'):
378410
file_location = file_location.rpartition('/')[0]
@@ -636,38 +668,34 @@ def main(parser, options, args):
636668
sys.exit(errno.EINVAL)
637669
for line in input_list:
638670
try:
639-
if line['dje_license_key']:
671+
if line['dje_license']:
640672
break
641673
except Exception as e:
642674
print(repr(e))
643-
print("The input does not have the 'dje_license_key' key which is required.")
675+
print("The input does not have the 'dje_license' key which is required.")
644676
sys.exit(errno.EINVAL)
645677

646678
dje_license_list = gen.get_dje_license_list(output_path, input_list, gen_license)
679+
680+
# The dje_license_list is an empty list if gen_license is 'False'
681+
if gen_license:
682+
dje_license_dict = gen.pre_process_and_dje_license_dict(input_list,
683+
api_url,
684+
api_username,
685+
api_key)
686+
license_list_context = gen.process_dje_licenses(dje_license_list,
687+
dje_license_dict,
688+
output_path)
689+
gen.write_licenses(license_list_context)
690+
647691
components_list = gen.pre_generation(output_path, input_list, action_num, all_in_one)
648692
formatted_output = gen.format_output(components_list)
649693
gen.write_output(formatted_output)
650694

651-
if dje_license_list:
652-
license_list_context = []
653-
for gen_path, license_key in dje_license_list:
654-
if gen_path.startswith('/'):
655-
gen_path = gen_path.partition('/')[2]
656-
gen_license_path = join(output_path, gen_path, license_key) + '.LICENSE'
657-
if not _exists(gen_license_path) and not gen.extract_dje_license_error:
658-
context = gen.get_license_text_from_api(api_url, api_username, api_key, license_key)
659-
if context:
660-
gen_path_context = []
661-
gen_path_context.append(gen_license_path)
662-
gen_path_context.append(context.encode('utf8'))
663-
license_list_context.append(gen_path_context)
664-
gen.write_licenses(license_list_context)
665-
666695
gen.warnings_errors_summary()
667696
print('Warnings: %s' % len(gen.warnings))
668697
print('Errors: %s' % len(gen.errors))
669698

670-
671699
def get_parser():
672700
class MyFormatter(optparse.IndentedHelpFormatter):
673701
def _format_text(self, text):

about_code_tool/templates/default.html

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ <h1>OPEN SOURCE SOFTWARE INFORMATION</h1>
5151
<h3 class="component-name">{{ about_object.name }}
5252
{% if about_object.version %}{{ about_object.version }}{% endif %}
5353
</h3>
54-
{% if about_object.dje_license in license_dicts.keys() %}
55-
<p>This component is licensed under {{about_object.dje_license }}.
54+
{% if about_object.dje_license_name in license_dicts.keys() %}
55+
<p>This component is licensed under {{about_object.dje_license_name }}.
5656
{% endif %}
5757
{% if about_object.copyright %}
5858
<pre>{{about_object.copyright}}</pre>
@@ -62,18 +62,21 @@ <h3 class="component-name">{{ about_object.name }}
6262
{% elif about_object.notice_file %}
6363
<pre class="component-notice">{{ notice_texts[loop.index0] }}</pre>
6464
{% endif %}
65-
{% if about_object.dje_license in license_dicts.keys() %}
66-
{% if about_object.dje_license in common_licenses %}
65+
{% if about_object.dje_license_name in license_dicts.keys() %}
66+
{% if about_object.dje_license_name in common_licenses %}
6767
<p>Full text of
68-
<a class="{{ about_object.dje_license }}" href="#component-license-{{ about_object.dje_license }}">
69-
{{ about_object.dje_license}}
70-
</a>
68+
<a class="{{ about_object.dje_license_name }}" href="#component-license-{{ about_object.dje_license_name }}">
69+
{{ about_object.dje_license_name }}
70+
</a>
7171
is available at the end of this document.</p>
7272
{% else %}
73-
<pre>{{ license_dicts[about_object.dje_license]|e }}</pre>
73+
<pre>{{ license_dicts[about_object.dje_license_name]|e }}</pre>
7474
{% endif %}
7575
{% elif about_object.license_text_file in license_dicts.keys() %}
76-
{% if about_object.license_text_file in common_licenses %}
76+
{% if about_object.dje_license_name in common_licenses %}
77+
<p>
78+
This component is licensed under {{ about_object.dje_license_name }}.
79+
</p>
7780
<p>Full text of
7881
<a class="{{ about_object.license_text_file }}" href="#component-license-{{ about_object.license_text_file }}">
7982
{{ about_object.license_text_file }}

about_code_tool/tests/test_about.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def test_header_row_in_csv_output(self):
9494
'license_spdx,redistribute,attribute,track_changes,vcs_tool,' \
9595
'vcs_repository,vcs_path,vcs_tag,vcs_branch,vcs_revision,' \
9696
'checksum_sha1,checksum_md5,checksum_sha256,dje_component,' \
97-
'dje_license,dje_organization,warnings,errors'
97+
'dje_license,dje_organization,dje_license_name,warnings,errors'
9898

9999
input = "about_code_tool/tests/testdata/basic"
100100
temp_file = tempfile.NamedTemporaryFile(suffix='.csv', delete=True)

about_code_tool/tests/test_genabout.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def test_get_dje_license_list_gen_license_with_dje_license_key_empty_license_tex
231231
gen_location = join(TESTDATA_PATH, "test_files_for_genabout/")
232232
input_list = [{'about_file': '/about.py.ABOUT', 'version': '0.8.1',
233233
'about_resource': '.', 'name': 'ABOUT tool',
234-
'license_text_file': '', 'dje_license_key': 'apache-2.0'}]
234+
'license_text_file': '', 'dje_license': 'apache-2.0'}]
235235
expected_output_list = [('/', 'apache-2.0')]
236236
gen_license = True
237237
lic_output_list = gen.get_dje_license_list(gen_location, input_list, gen_license)
@@ -244,7 +244,7 @@ def test_get_dje_license_list_gen_license_with_empty_dje_license_key_empty_licen
244244
gen_location = join(TESTDATA_PATH, "test_files_for_genabout/")
245245
input_list = [{'about_file': '/about.py.ABOUT', 'version': '0.8.1',
246246
'about_resource': '.', 'name': 'ABOUT tool',
247-
'license_text_file': '', 'dje_license_key': ''}]
247+
'license_text_file': '', 'dje_license': ''}]
248248
expected_output_list = []
249249
gen_license = True
250250
lic_output_list = gen.get_dje_license_list(gen_location, input_list, gen_license)
@@ -257,7 +257,7 @@ def test_get_dje_license_list_gen_license_with_dje_license_key_no_license_text_f
257257
gen_location = join(TESTDATA_PATH, "test_files_for_genabout/")
258258
input_list = [{'about_file': '/about.py.ABOUT', 'version': '0.8.1',
259259
'about_resource': '.', 'name': 'ABOUT tool',
260-
'dje_license_key': 'apache-2.0'}]
260+
'dje_license': 'apache-2.0'}]
261261
expected_output_list = [('/', 'apache-2.0')]
262262
gen_license = True
263263
lic_output_list = gen.get_dje_license_list(gen_location, input_list, gen_license)
@@ -433,7 +433,7 @@ def test_verify_license_files_no_key(self):
433433
def test_gen_license_list_license_text_file_no_value(self):
434434
gen = genabout.GenAbout()
435435
input_list = {'about_file': '/tmp/3pp/opensans/', 'name': 'OpenSans Fonts',
436-
'version': '1', 'dje_license_key': 'apache-2.0',
436+
'version': '1', 'dje_license': 'apache-2.0',
437437
'license_text_file': '', 'about_resource': 'opensans'}
438438
expected_list = ('/tmp/3pp', 'apache-2.0')
439439
output = gen.gen_license_list(input_list)
@@ -443,7 +443,7 @@ def test_gen_license_list_license_text_file_no_value(self):
443443
def test_gen_license_list_no_license_text_file_key(self):
444444
gen = genabout.GenAbout()
445445
input_list = {'about_file': '/tmp/3pp/opensans/', 'name': 'OpenSans Fonts',
446-
'version': '1', 'dje_license_key': 'apache-2.0',
446+
'version': '1', 'dje_license': 'apache-2.0',
447447
'about_resource': 'opensans'}
448448
expected_list = ('/tmp/3pp', 'apache-2.0')
449449
output = gen.gen_license_list(input_list)
@@ -487,3 +487,11 @@ def test_write_licenses(self):
487487
context = line
488488
self.assertTrue(context == tmp_license_context)
489489

490+
def test_process_dje_licenses(self):
491+
gen = genabout.GenAbout()
492+
test_license_list = [('/', 'test')]
493+
test_license_dict = {'test': u'This is a test license.'}
494+
test_path = '/test'
495+
expected_output = [['/test/test.LICENSE', 'This is a test license.']]
496+
output = gen.process_dje_licenses(test_license_list, test_license_dict, test_path)
497+
self.assertTrue(output == expected_output)

0 commit comments

Comments
 (0)