Skip to content

Commit 38058ea

Browse files
committed
Merge remote-tracking branch 'origin/v2x_maintenance'
2 parents da211cc + 2735d12 commit 38058ea

File tree

17 files changed

+371
-115
lines changed

17 files changed

+371
-115
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*.py[cod]
2+
.Python
23

34
/build
45
/bin

README.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,10 @@ as an input.
187187
with the generated .ABOUT file using data fetched from a DejaCode License Library.
188188
The following additional options are required:
189189
--api_url - URL to the DejaCode License Library API endpoint
190-
--api_username - DejaCode username
191-
--api_key - DejaCode API key for this username
192-
190+
--api_key - DejaCode API key
191+
193192
Example syntax:
194-
python about_code_tool/genabout.py --extract_license --api_url='api_url' --api_username='api_username' --api_key='api_key'
193+
python about_code_tool/genabout.py --extract_license --api_url='api_url' --api_key='api_key'
195194

196195

197196
Example::

USAGE.rst

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,11 @@ genabout.py
8383
Extract License text and create <license_key>.LICENSE side-by-side
8484
with the .ABOUT from DJE License Library.
8585
api_url - URL to the DJE License Library
86-
api_username - The regular DJE username
87-
api_key - Hash attached to your username which is used to authenticate
88-
yourself in the API. Contact us to get the hash key.
86+
api_key - Hash which is used to authenticate yourself in the API.
87+
Contact us to get the hash key.
8988

9089
Example syntax:
91-
genabout.py --extract_license --api_url='api_url' --api_username='api_username' --api_key='api_key'
90+
genabout.py --extract_license --api_url='api_url' --api_key='api_key'
9291

9392
Purpose
9493
-------
@@ -177,12 +176,11 @@ Options
177176

178177
This option requires 3 parameters:
179178
api_url - URL to the DJE License Library
180-
api_username - The regular DJE username
181-
api_key - Hash attached to your username which is used to authenticate
182-
yourself in the API.
179+
api_key - Hash which is used to authenticate yourself in the API.
180+
Contact us to get the hash key.
183181
(Please contact us to get the api_* value to use this feature)
184182

185-
$ python genabout.py --extract_license --api_url='api_url' --api_username='api_username' --api_key='api_key' <input path> <output path>
183+
$ python genabout.py --extract_license --api_url='api_url' --api_key='api_key' <input path> <output path>
186184

187185

188186
genattrib.py

about_code_tool/about.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- coding: utf8 -*-
33

44
# ============================================================================
5-
# Copyright (c) 2013-2015 nexB Inc. http://www.nexb.com/ - All rights reserved.
5+
# Copyright (c) 2013-2016 nexB Inc. http://www.nexb.com/ - All rights reserved.
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
88
# You may obtain a copy of the License at
@@ -428,9 +428,14 @@ def validate_field_values_are_not_empty(self):
428428
continue
429429

430430
if field_name in MANDATORY_FIELDS:
431-
err = Error(VALUE, field_name, None,
432-
'This mandatory field has no value.')
433-
self.errors.append(err)
431+
if field_name == 'version':
432+
err = Warn(VALUE, field_name, None,
433+
'This mandatory field has no value.')
434+
self.warnings.append(err)
435+
else:
436+
err = Error(VALUE, field_name, None,
437+
'This mandatory field has no value.')
438+
self.errors.append(err)
434439
elif field_name in OPTIONAL_FIELDS:
435440
err = Warn(VALUE, field_name, None,
436441
'This optional field has no value.')
@@ -517,8 +522,12 @@ def validate_mandatory_fields_are_present(self):
517522
"""
518523
for field_name in MANDATORY_FIELDS:
519524
if field_name not in self.validated_fields:
520-
self.errors.append(Error(VALUE, field_name, None,
525+
if field_name == 'version' :
526+
self.warnings.append(Error(VALUE, field_name, None,
521527
'Mandatory field missing'))
528+
else:
529+
self.errors.append(Error(VALUE, field_name, None,
530+
'Mandatory field missing'))
522531

523532
def validate_known_optional_fields(self, field_name):
524533
"""

about_code_tool/genabout.py

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- coding: utf8 -*-
33

44
# ============================================================================
5-
# Copyright (c) 2013-2015 nexB Inc. http://www.nexb.com/ - All rights reserved.
5+
# Copyright (c) 2013-2016 nexB Inc. http://www.nexb.com/ - All rights reserved.
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
88
# You may obtain a copy of the License at
@@ -279,50 +279,51 @@ def verify_files_existence(self, input_list, project_dir, file_in_project):
279279
self.warnings.append(Warn(FILE, file_key, path, 'File does not exist.'))
280280
return files_list
281281

282-
def request_license_data(self, url, username, api_key, license_key):
282+
def request_license_data(self, url, api_key, license_key):
283283
"""
284-
Return a dictionary of license data.
285-
284+
Return a dictionary of license data.
286285
Send a request to a given API URL to gather license data for
287-
license_key, authenticating through an api_key and username.
286+
license_key, authenticating through an api_key.
288287
"""
289288
payload = {
290-
'username': username,
291289
'api_key': api_key,
290+
'key': license_key,
292291
'format': 'json'
293292
}
294293

295294
url = url.rstrip('/')
296295
encoded_payload = urllib.urlencode(payload)
297-
full_url = '%(url)s/%(license_key)s/?%(encoded_payload)s' % locals()
296+
full_url = '%(url)s/?%(encoded_payload)s' % locals()
298297
# handle special characters in URL such as space etc.
299298
full_url = urllib.quote(full_url, safe="%/:=&?~#+!$,;'@()*[]")
299+
headers = {'Authorization': 'Token %s' % api_key}
300300
license_data = {}
301301
try:
302302
# The 'self.extract_license_api_error' is True if any of the
303-
# api_url/api_username/api_key/network status have problem.
303+
# api_url/api_key/network status have problem.
304304
if not self.extract_license_api_error:
305-
request = urllib2.Request(full_url)
305+
request = urllib2.Request(full_url, headers=headers)
306306
response = urllib2.urlopen(request)
307307
response_content = response.read()
308308
license_data = json.loads(response_content)
309309
except urllib2.HTTPError, http_e:
310310
# some auth problem
311311
if http_e.code == 401:
312-
error_msg = ("Authorization denied. Invalid '--api_username' or '--api_key'. License generation is skipped.")
312+
error_msg = ("Authorization denied. Invalid '--api_key'. License generation is skipped.")
313313
print('\n%(error_msg)s\n' % locals())
314314
self.extract_license_api_error = True
315-
self.errors.append(Error(VALUE, 'username/api_key', username + '/' + api_key, error_msg))
315+
self.errors.append(Error(VALUE, 'api_key', api_key, error_msg))
316316
else:
317-
# Since no api_url/api_username/api_key/network status have
317+
# Since no api_url/api_key/network status have
318318
# problem detected, it yields 'dje_license_key' is the cause of
319-
# this exception.
319+
# this exception.
320320
self.errors.append(Error(VALUE, 'dje_license_key', license_key, "Invalid 'dje_license_key'"))
321321
except ValueError as e:
322322
# FIXME: when does this happen?
323323
pass
324324
finally:
325-
return license_data
325+
license_data = license_data.get('results')[0] if license_data.get('count') == 1 else {}
326+
return license_data
326327

327328
@staticmethod
328329
def copy_files(gen_location, files_list):
@@ -348,12 +349,12 @@ def write_licenses(self, license_context_list):
348349
'Something is wrong.')
349350
self.errors.append(err)
350351

351-
def get_license_details_from_api(self, url, username, api_key, license_key):
352+
def get_license_details_from_api(self, url, api_key, license_key):
352353
"""
353354
Returns the license_text of a given license_key using an API request.
354355
Returns an empty string if the text is not available.
355356
"""
356-
license_data = self.request_license_data(url, username, api_key, license_key)
357+
license_data = self.request_license_data(url, api_key, license_key)
357358
license_name = license_data.get('name', '')
358359
license_text = license_data.get('full_text', '')
359360
license_key = license_data.get('key', '')
@@ -398,7 +399,7 @@ def get_dje_license_list(self, gen_location, input_list, gen_license, dje_licens
398399
"Missing 'dje_license_key' for " + line['about_file']))
399400
return license_output_list
400401

401-
def pre_process_and_dje_license_dict(self, input_list, api_url, api_username, api_key):
402+
def pre_process_and_dje_license_dict(self, input_list, api_url, api_key):
402403
"""
403404
Modify a list of About data dictionaries by adding license information
404405
fetched from the DejaCode API.
@@ -427,9 +428,9 @@ def pre_process_and_dje_license_dict(self, input_list, api_url, api_username, ap
427428
self.errors.append(Error(VALUE, 'dje_license_key', line['dje_license_key'], "No multiple licenses or newline character are accepted."))
428429
continue
429430
lic = line['dje_license_key']
430-
if not lic in license_dict:
431+
if lic not in license_dict:
431432
detail_list = []
432-
license_name, license_key, license_text = self.get_license_details_from_api(api_url, api_username, api_key, lic)
433+
license_name, license_key, license_text = self.get_license_details_from_api(api_url, api_key, lic)
433434
line['dje_license_key'] = license_key
434435
license_dict[license_key] = license_name
435436
line['dje_license_name'] = license_name
@@ -448,13 +449,13 @@ def pre_process_and_dje_license_dict(self, input_list, api_url, api_username, ap
448449
def valid_api_url(self, api_url):
449450
try:
450451
request = urllib2.Request(api_url)
451-
# This will always goes to exception as no username/key are provided.
452+
# This will always goes to exception as no key are provided.
452453
# The purpose of this code is to validate the provided api_url is correct
453454
response = urllib2.urlopen(request)
454455
except urllib2.HTTPError, http_e:
455-
# The 405 error code is refer to "Method not allowed".
456-
# This is correct as no username and key are provided.
457-
if http_e.code == 405:
456+
# The 403 error code is refer to "Authentication credentials were not provided.".
457+
# This is correct as no key are provided.
458+
if http_e.code == 403:
458459
return True
459460
except:
460461
# All other exceptions yield to invalid api_url
@@ -481,7 +482,7 @@ def process_dje_licenses(self, dje_license_list, dje_license_dict, output_path):
481482

482483
def pre_generation(self, gen_location, input_list, action_num):
483484
"""
484-
Perfom some pre-generation.
485+
Perform some pre-generation.
485486
TODO: document me
486487
"""
487488
output_list = []
@@ -500,6 +501,18 @@ def pre_generation(self, gen_location, input_list, action_num):
500501
file_location = dirname(file_location)
501502
file_location = join(file_location, basename(file_location))
502503
file_location += '.ABOUT'
504+
# The following code is to check if there is any directory ends with spaces
505+
split_path = file_location.split('/')
506+
dir_endswith_space = False
507+
for segment in split_path:
508+
if segment.endswith(' '):
509+
msg = 'File path contains directory name ends with spaces which is not allowed. Generation skipped.'
510+
self.errors.append(Error(VALUE, 'about_file_path',
511+
file_location, msg))
512+
dir_endswith_space = True
513+
break
514+
if dir_endswith_space:
515+
continue
503516

504517
about_file_location = join(gen_location, file_location)
505518
about_file_dir = dirname(about_file_location)
@@ -670,12 +683,11 @@ def filter_dicts_of_empty_values(abouts):
670683
Fetch and save license texts in <license_key>.LICENSE files side-by-side with the
671684
.ABOUT files using the DejaCode License Library API.
672685
api_url - URL to the DejaCode License Library.
673-
api_username - The DejaCode username.
674-
api_key - Key attached to your username and used to authenticate
675-
yourself in the API. Contact us to get an API key.
686+
api_key - Key used to authenticate yourself in the API.
687+
Contact us to get an API key.
676688
677689
Example syntax:
678-
genabout.py --extract_license --api_url='api_url' --api_username='api_username' --api_key='api_key'
690+
genabout.py --extract_license --api_url='api_url' --api_key='api_key'
679691
"""
680692

681693

@@ -692,7 +704,6 @@ def main(parser, options, args):
692704

693705
action_num = 0
694706
api_url = ''
695-
api_username = ''
696707
api_key = ''
697708
gen_license = False
698709
dje_license_dict = {}
@@ -734,12 +745,11 @@ def main(parser, options, args):
734745

735746
if extract_license:
736747
api_url = extract_license[0].partition('--api_url=')[2]
737-
api_username = extract_license[1].partition('--api_username=')[2]
738-
api_key = extract_license[2].partition('--api_key=')[2]
748+
api_key = extract_license[1].partition('--api_key=')[2]
739749
gen_license = True
740750

741751
if not len(args) == 2:
742-
print('ERROR: <input_path> and <utput_path> are required.')
752+
print('ERROR: <input_path> and <output_path> are required.')
743753
print()
744754
parser.print_help()
745755
sys.exit(errno.EEXIST)
@@ -800,13 +810,15 @@ def main(parser, options, args):
800810
if mapping_config:
801811
mappings = get_mappings(location=None)
802812
input_data = apply_mappings(input_data, mappings)
803-
user_keys = mappings.values()
813+
user_keys = mappings.keys()
804814

805815
gen.validate(input_data)
806816

807817
ignored_fields_list = get_unknown_fields(input_data, user_keys)
808818
if ignored_fields_list:
809819
input_list = gen.get_only_supported_fields(input_data, ignored_fields_list)
820+
else:
821+
input_list = input_data
810822

811823
if copy_files_path:
812824
if not isdir(copy_files_path):
@@ -837,8 +849,8 @@ def main(parser, options, args):
837849
gen.copy_files(output_path, license_list)
838850

839851
if extract_license:
840-
if not api_url or not api_username or not api_key:
841-
print("ERROR: Missing argument for --extract_license")
852+
if not api_url or not api_key:
853+
print("Missing argument for --extract_license")
842854
sys.exit(errno.EINVAL)
843855
for line in input_list:
844856
try:
@@ -850,11 +862,10 @@ def main(parser, options, args):
850862
sys.exit(errno.EINVAL)
851863

852864
if gen_license:
853-
# Strip the ' and " for api_url, api_username and api_key from input
865+
# Strip the ' and " for api_url, and api_key from input
854866
api_url = api_url.strip("'").strip("\"")
855-
api_username = api_username.strip("'").strip("\"")
856867
api_key = api_key.strip("'").strip("\"")
857-
dje_license_dict = gen.pre_process_and_dje_license_dict(input_list, api_url, api_username, api_key)
868+
dje_license_dict = gen.pre_process_and_dje_license_dict(input_list, api_url, api_key)
858869

859870
dje_license_list = gen.get_dje_license_list(output_path, input_list, gen_license, dje_license_dict)
860871
components_list = gen.pre_generation(output_path, input_list, action_num)
@@ -888,7 +899,7 @@ def get_parser():
888899
parser.add_option('--copy_files', type='string', help=COPY_FILES_HELP)
889900
parser.add_option('--license_text_location', type='string', help=LICENSE_TEXT_LOCATION_HELP)
890901
parser.add_option('--mapping', action='store_true', help=MAPPING_HELP)
891-
parser.add_option('--extract_license', type='string', nargs=3, help=EXTRACT_LICENSE_HELP)
902+
parser.add_option('--extract_license', type='string', nargs=2, help=EXTRACT_LICENSE_HELP)
892903
return parser
893904

894905

about_code_tool/genattrib.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- coding: utf8 -*-
33

44
# ============================================================================
5-
# Copyright (c) 2013-2015 nexB Inc. http://www.nexb.com/ - All rights reserved.
5+
# Copyright (c) 2013-2016 nexB Inc. http://www.nexb.com/ - All rights reserved.
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
88
# You may obtain a copy of the License at
@@ -200,15 +200,17 @@ def main(parser, options, args):
200200
input_path = extract_zip(input_path)
201201

202202
if isdir(output_path):
203-
print('ERROR: <output_path> must be an HTML file, not a directory')
203+
print('ERROR: <output_path> cannot be a directory')
204204
parser.print_help()
205205
sys.exit(errno.EISDIR)
206206

207+
"""
207208
# We only support HTML currently
208209
if not output_path.endswith('.html'):
209210
print('ERROR: <output_path> must be an HTML file.')
210211
parser.print_help()
211212
sys.exit(errno.EINVAL)
213+
"""
212214

213215
if exists(output_path) and not overwrite:
214216
print('ERROR: A file at <output_path> already exists. Select a different file name or use the --overwrite option.')

about_code_tool/help.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- coding: utf8 -*-
33

44
# ============================================================================
5-
# Copyright (c) 2013-2015 nexB Inc. http://www.nexb.com/ - All rights reserved.
5+
# Copyright (c) 2013-2016 nexB Inc. http://www.nexb.com/ - All rights reserved.
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
88
# You may obtain a copy of the License at
@@ -19,10 +19,10 @@
1919

2020
__about_spec_version__ = '1.0'
2121

22-
__version__ = '2.2.0'
22+
__version__ = '2.2.1'
2323

2424
__copyright__ = """
25-
Copyright (c) 2013-2015 nexB Inc. All rights reserved.
25+
Copyright (c) 2013-2016 nexB Inc. All rights reserved.
2626
2727
Licensed under the Apache License, Version 2.0 (the "License");
2828
you may not use this file except in compliance with the License.

about_code_tool/licenses.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- coding: utf8 -*-
33

44
# ============================================================================
5-
# Copyright (c) 2013-2015 nexB Inc. http://www.nexb.com/ - All rights reserved.
5+
# Copyright (c) 2013-2016 nexB Inc. http://www.nexb.com/ - All rights reserved.
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
88
# You may obtain a copy of the License at

0 commit comments

Comments
 (0)