Skip to content

Commit 7ade9bc

Browse files
committed
Fixed #116
It's now able to copy all the *_file field files such as license_text_file, notice_file etc...
1 parent a8234e3 commit 7ade9bc

File tree

4 files changed

+87
-70
lines changed

4 files changed

+87
-70
lines changed

README.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ information will be saved to the CSV file named "thirdparty_about.csv".
128128
129129
--all_in_one Generate all the ABOUT files in the [output_path] without
130130
any project structure
131-
132-
--copy_license=COPY_LICENSE
133-
Copy the 'license_text_file' from the project to the generated location
131+
132+
--copy_files=COPY_FILES
133+
Copy the '*_file' from the project to the generated location
134134
Project path - Project path
135-
135+
136136
--license_text_location=LICENSE_TEXT_LOCATION
137137
Copy the 'license_text_file' from the directory to the generated location
138138
License path - License text files path

USAGE

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ Syntax
7070
--all_in_one Generate all the ABOUT files in the [output_path] without
7171
any project structure
7272

73-
--copy_license=COPY_LICENSE
74-
Copy the 'license_text_file' from the project to the generated location
73+
--copy_files=COPY_FILES
74+
Copy the '*_file' from the project to the generated location
7575
Project path - Project path
7676

7777
--license_text_location=LICENSE_TEXT_LOCATION
@@ -114,19 +114,20 @@ Options
114114

115115
$ python genabout.py --all_in_one <input path> <output path>
116116

117-
--copy_license
117+
--copy_files
118118

119-
Copy the license files to the generated location based on the
120-
'license_text_file' value in the input from the project
119+
Copy the files to the generated location based on the
120+
*_file value in the input from the project
121121

122122
Purpose of this option is for users who want to generate ABOUT files
123123
in a different location other than the project side by side with the code.
124124

125125
For instance, the project is located at /home/project/, and users want to
126-
generate ABOUT files to /home/about/ and also want to copy the licenses from
126+
generate ABOUT files to /home/about/ and also want to copy the
127+
'license_text_file' and 'notice_text_file' from
127128
/home/project/ to /home/about/
128129

129-
$ python genabout.py --copy_license=/home/project/ <input path> /home/about/
130+
$ python genabout.py --copy_files=/home/project/ <input path> /home/about/
130131

131132
--license_text_location
132133

about_code_tool/genabout.py

Lines changed: 61 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -196,34 +196,50 @@ def get_non_supported_fields(input_list):
196196
first_line = input_list[0]
197197
return [field for field in first_line.keys() if not field in SUPPORTED_FIELDS]
198198

199-
def verify_license_files(self, input_list, project_dir, license_in_project):
199+
def verify_files_existance(self, input_list, project_dir, file_in_project):
200200
"""
201201
Verify the existence of the 'license text file'
202202
"""
203-
license_files_list = []
203+
files_list = []
204+
# Get all the dictionary keys
205+
column_keys = input_list[0].keys()
206+
207+
# Get all the keys that ending with _file except for the 'about_file
208+
file_keys = []
209+
for key in column_keys:
210+
if '_file' in key and key != 'about_file':
211+
file_keys.append(key)
212+
204213
for line in input_list:
205-
try:
206-
if line['license_text_file']:
207-
license_file = line['license_text_file']
214+
for file_key in file_keys:
215+
if line[file_key]:
216+
file_path_list = []
217+
file_value = []
208218
file_location = line['about_file']
209219
if file_location.startswith('/'):
210220
file_location = file_location.partition('/')[2]
211-
#if file_location.endswith('/'):
212-
# file_location = file_location.rpartition('/')[0]
213221
about_parent_dir = dirname(file_location)
214-
if license_in_project:
215-
license_file_path = join(project_dir, about_parent_dir, license_file)
216-
else:
217-
license_file_path = join(project_dir, license_file)
218-
if _exists(license_file_path):
219-
license_files_list.append((license_file_path, about_parent_dir))
222+
if file_in_project:
223+
if '\n' in line[file_key]:
224+
file_value = line[file_key].split('\n')
225+
else:
226+
file_value.append(line[file_key])
227+
for value in file_value:
228+
file_path_list.append(join(project_dir, about_parent_dir, value))
220229
else:
221-
self.warnings.append(Warn('license_text_file', license_file_path, "License does not exist."))
222-
except Exception as e:
223-
print(repr(e))
224-
print("The input does not have the 'license_text_file' key which is required.")
225-
raise Exception(repr(e))
226-
return license_files_list
230+
if '\n' in line[file_key]:
231+
file_value = line[file_key].split('\n')
232+
else:
233+
file_value.append(line[file_key])
234+
for value in file_value:
235+
file_path_list.append(join(project_dir, value))
236+
237+
for path in file_path_list:
238+
if _exists(path):
239+
files_list.append((path, about_parent_dir))
240+
else:
241+
self.warnings.append(Warn(file_key, path, "File does not exist."))
242+
return files_list
227243

228244
def request_license_data(self, url, username, api_key, license_key):
229245
"""
@@ -278,15 +294,15 @@ def request_license_data(self, url, username, api_key, license_key):
278294
return data
279295

280296
@staticmethod
281-
def copy_license_files(gen_location, license_list):
297+
def copy_files(gen_location, files_list):
282298
"""
283-
Copy the 'license_text_file' into the gen_location
299+
Copy the files into the gen_location
284300
"""
285-
for license_path, component_path in license_list:
286-
output_license_path = join(gen_location, component_path)
287-
if not _exists(output_license_path):
288-
makedirs(output_license_path)
289-
shutil.copy2(license_path, output_license_path)
301+
for file_path, component_path in files_list:
302+
output_file_path = join(gen_location, component_path)
303+
if not _exists(output_file_path):
304+
makedirs(output_file_path)
305+
shutil.copy2(file_path, output_file_path)
290306

291307
def write_licenses(self, license_context_list):
292308
for gen_license_path, license_context in license_context_list:
@@ -513,8 +529,8 @@ def _exists(file_path):
513529
any project structure
514530
"""
515531

516-
COPY_LICENSE_HELP = """\
517-
Copy the 'license_text_file' from the project to the generated location
532+
COPY_FILES_HELP = """\
533+
Copy the '*_file' from the project to the generated location
518534
Project path - Project path
519535
"""
520536

@@ -547,7 +563,7 @@ def main(parser, options, args):
547563
verbosity = options.verbosity
548564
action = options.action
549565
all_in_one = options.all_in_one
550-
copy_license_path = options.copy_license
566+
copy_files_path = options.copy_files
551567
license_text_path = options.license_text_location
552568
mapping_config = options.mapping
553569
extract_license = options.extract_license
@@ -575,8 +591,8 @@ def main(parser, options, args):
575591
else:
576592
action_num = action
577593

578-
if copy_license_path:
579-
if not _exists(copy_license_path):
594+
if copy_files_path:
595+
if not _exists(copy_files_path):
580596
print("The project path does not exist.")
581597
sys.exit(errno.EINVAL)
582598

@@ -656,20 +672,20 @@ def main(parser, options, args):
656672
if ignored_fields_list:
657673
input_list = gen.get_only_supported_fields(input_list, ignored_fields_list)
658674

659-
if copy_license_path:
660-
if not isdir(copy_license_path):
661-
print("The '--copy_license' <project_path> must be a directory.")
662-
print("'--copy_license' is skipped.")
675+
if copy_files_path:
676+
if not isdir(copy_files_path):
677+
print("The '--copy_files' <project_path> must be a directory.")
678+
print("'--copy_files' is skipped.")
663679
else:
664-
#if not copy_license_path.endswith('/'):
665-
# copy_license_path += '/'
666-
project_parent_dir = dirname(copy_license_path)
680+
#if not copy_files_path.endswith('/'):
681+
# copy_files_path += '/'
682+
project_parent_dir = dirname(copy_files_path)
667683
licenses_in_project = True
668-
license_list = gen.verify_license_files(input_list, project_parent_dir, licenses_in_project)
684+
license_list = gen.verify_files_existance(input_list, project_parent_dir, licenses_in_project)
669685
if not license_list:
670-
print("None of the 'license_text_file' is found. '--copy_license' is ignored.")
686+
print("None of the file is found. '--copy_files' is ignored.")
671687
else:
672-
gen.copy_license_files(output_path, license_list)
688+
gen.copy_files(output_path, license_list)
673689

674690
if license_text_path:
675691
if not isdir(license_text_path):
@@ -680,11 +696,11 @@ def main(parser, options, args):
680696
# license_text_path += '/'
681697
license_dir = dirname(license_text_path)
682698
licenses_in_project = False
683-
license_list = gen.verify_license_files(input_list, license_dir, licenses_in_project)
699+
license_list = gen.verify_files_existance(input_list, license_dir, licenses_in_project)
684700
if not license_list:
685-
print("None of the 'license_text_file' is found. '--copy_license' is ignored.")
701+
print("None of the file is found. '--copy_files' is ignored.")
686702
else:
687-
gen.copy_license_files(output_path, license_list)
703+
gen.copy_files(output_path, license_list)
688704

689705
if extract_license:
690706
if not api_url or not api_username or not api_key:
@@ -768,7 +784,7 @@ def format_option(self, option):
768784
parser.add_option('--verbosity', type=int, help=VERBOSITY_HELP)
769785
parser.add_option('--action', type=int, help=ACTION_HELP)
770786
parser.add_option('--all_in_one', action='store_true', help=ALL_IN_ONE_HELP)
771-
parser.add_option('--copy_license', type='string', help=COPY_LICENSE_HELP)
787+
parser.add_option('--copy_files', type='string', help=COPY_FILES_HELP)
772788
parser.add_option('--license_text_location', type='string', help=LICENSE_TEXT_LOCATION_HELP)
773789
parser.add_option('--mapping', action='store_true', help=MAPPING_HELP)
774790
parser.add_option(

about_code_tool/tests/test_genabout.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -401,60 +401,60 @@ def test_format_output_with_continuation(self):
401401
self.assertFalse(gen.warnings, "No warnings should be returned.")
402402
self.assertFalse(gen.errors, "No errors should be returned.")
403403

404-
def test_verify_license_files_exist(self):
404+
def test_verify_files_existance_exist(self):
405405
gen = genabout.GenAbout()
406406
input_list = [{'version': '0.8.1', 'about_file': '/TESTCASE/',
407407
'license_text_file': 'apache2.LICENSE',
408408
'name': 'ABOUT tool', 'about_resource': '.'}]
409409
path = '.'
410410
expected_list = [(join('.', 'apache2.LICENSE'), 'TESTCASE')]
411-
output = gen.verify_license_files(input_list, path, False)
411+
output = gen.verify_files_existance(input_list, path, False)
412412
self.assertEqual(expected_list, output)
413413
self.assertFalse(gen.warnings, "No warnings should be returned.")
414414
self.assertFalse(gen.errors, "No errors should be returned.")
415415

416-
def test_verify_license_files_exist_license_in_project(self):
416+
def test_verify_files_existance_exist_license_in_project(self):
417417
gen = genabout.GenAbout()
418418
input_list = [{'version': '0.8.1', 'about_file': '.',
419419
'license_text_file': 'apache2.LICENSE',
420420
'name': 'ABOUT tool', 'about_resource': '.'}]
421421
path = '.'
422422
expected_list = [(join('.', 'apache2.LICENSE'), '')]
423-
output = gen.verify_license_files(input_list, path, True)
423+
output = gen.verify_files_existance(input_list, path, True)
424424
self.assertEqual(expected_list, output)
425425
self.assertFalse(gen.warnings, "No warnings should be returned.")
426426
self.assertFalse(gen.errors, "No errors should be returned.")
427427

428-
def test_verify_license_files_not_exist(self):
428+
def test_verify_files_existance_not_exist(self):
429429
gen = genabout.GenAbout()
430430
input_list = [{'version': '0.8.1', 'about_file': '/about.py.ABOUT',
431431
'license_text_file': 'not_exist.LICENSE.txt',
432432
'name': 'ABOUT tool', 'about_resource': '.'}]
433433
path = '.'
434434
expected_list = []
435-
output = gen.verify_license_files(input_list, path, False)
435+
output = gen.verify_files_existance(input_list, path, False)
436436
self.assertTrue(expected_list == output)
437437
self.assertTrue(len(gen.warnings) == 1, "Should return 1 warning.")
438438
self.assertFalse(gen.errors, "No errors should be returned.")
439439

440-
def test_verify_license_files_not_exist_license_in_project(self):
440+
def test_verify_files_existance_not_exist_license_in_project(self):
441441
gen = genabout.GenAbout()
442442
input_list = [{'version': '0.8.1', 'about_file': '/TESTCASE/',
443443
'license_text_file': 'not_exist.LICENSE.txt',
444444
'name': 'ABOUT tool', 'about_resource': '.'}]
445445
path = '.'
446446
expected_list = []
447-
output = gen.verify_license_files(input_list, path, False)
447+
output = gen.verify_files_existance(input_list, path, False)
448448
self.assertTrue(expected_list == output)
449449
self.assertTrue(len(gen.warnings) == 1, "Should return 1 warning.")
450450
self.assertFalse(gen.errors, "No errors should be returned.")
451451

452-
def test_verify_license_files_no_key(self):
452+
def test_verify_files_existance_no_key(self):
453453
gen = genabout.GenAbout()
454454
input_list = [{'version': '0.8.1', 'about_file': '/about.py.ABOUT',
455455
'name': 'ABOUT tool', 'about_resource': '.'}]
456456
path = '.'
457-
self.assertRaises(Exception, gen.verify_license_files, input_list, path)
457+
self.assertRaises(Exception, gen.verify_files_existance, input_list, path)
458458

459459
def test_gen_license_list_license_text_file_no_value(self):
460460
gen = genabout.GenAbout()
@@ -476,25 +476,25 @@ def test_gen_license_list_no_license_text_file_key(self):
476476
output = gen.gen_license_list(input_list)
477477
self.assertTrue(expected_list == output)
478478

479-
def test_copy_license_files_test_path_not_endswith_slash(self):
479+
def test_copy_files_test_path_not_endswith_slash(self):
480480
gen = genabout.GenAbout()
481481
input_list = [('apache2.LICENSE', '.')]
482482
expected_list = ['apache2.LICENSE']
483483
project_path = os.path.abspath('.')
484484
tmp_path = tempfile.mkdtemp()
485-
gen.copy_license_files(tmp_path, input_list)
485+
gen.copy_files(tmp_path, input_list)
486486
self.assertTrue(expected_list == os.listdir(tmp_path))
487487
# According to the doc, the user of mkdtemp() is responsible for
488488
# deleting the temporary directory and its contents when done with it.
489489
shutil.rmtree(tmp_path)
490490

491-
def test_copy_license_files_test_path_endswith_slash(self):
491+
def test_copy_files_test_path_endswith_slash(self):
492492
gen = genabout.GenAbout()
493493
input_list = [('apache2.LICENSE', '.')]
494494
expected_list = ['apache2.LICENSE']
495495
project_path = os.path.abspath('.')
496496
tmp_path = tempfile.mkdtemp() + '/'
497-
gen.copy_license_files(tmp_path, input_list)
497+
gen.copy_files(tmp_path, input_list)
498498
self.assertTrue(expected_list == os.listdir(tmp_path))
499499
# According to the doc, the user of mkdtemp() is responsible for
500500
# deleting the temporary directory and its contents when done with it.

0 commit comments

Comments
 (0)