Skip to content

Commit 0ecf774

Browse files
committed
Fixed #436
* Fixed the copy file issue caused by multi-line in file fields
1 parent 7dc5ebe commit 0ecf774

File tree

6 files changed

+76
-25
lines changed

6 files changed

+76
-25
lines changed

docs/CHANGELOG.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
* Update transform code (See #427 and #428)
66
* Fixed #431 - Error handling for empty "_file" fields
77
* Fixed #432 - Handled UTF-8 variant invented by Microsoft
8-
* Fixed #433 - problem was caused by the different multi-lic file format between json and CSV (CSV with '\n' line break)
8+
* Fixed #433 - problem was caused by the different multi-lic file format between json and CSV (CSV with '\n' line break)
9+
* Fixed #436 - issue about copy with the `--reference` option
910

1011
2020-05-05
1112
Release 4.0.2

src/attributecode/util.py

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -437,33 +437,55 @@ def copy_license_notice_files(fields, base_dir, reference_dir, afp):
437437
for key, value in fields:
438438
if key == 'license_file' or key == 'notice_file':
439439
if value:
440-
copy_file_name = value
440+
# This is to handle multiple license_file value in CSV format
441+
# The following code will construct a list to contain the
442+
# license file(s) that need to be copied.
443+
# Note that *ONLY* license_file field allows \n. Others file
444+
# fields that have \n will prompts error at validation stage
445+
file_list = []
446+
if '\n' in value:
447+
f_list = value.split('\n')
448+
else:
449+
if not isinstance(value, list):
450+
f_list = [value]
451+
else:
452+
f_list = value
453+
# The following code is to adopt the approach from #404
454+
# to use comma for multiple files which refer the same license
455+
for item in f_list:
456+
if ',' in item:
457+
item_list = item.split(',')
458+
for i in item_list:
459+
file_list.append(i.strip())
460+
else:
461+
file_list.append(item)
441462
else:
442463
continue
443464

444-
from_lic_path = posixpath.join(to_posix(reference_dir), copy_file_name)
445-
about_file_dir = os.path.dirname(to_posix(afp)).lstrip('/')
446-
to_lic_path = posixpath.join(to_posix(base_dir), about_file_dir)
447-
448-
if on_windows:
449-
from_lic_path = add_unc(from_lic_path)
450-
to_lic_path = add_unc(to_lic_path)
451-
452-
# Strip the white spaces
453-
from_lic_path = from_lic_path.strip()
454-
to_lic_path = to_lic_path.strip()
455-
456-
# Errors will be captured when doing the validation
457-
if not posixpath.exists(from_lic_path):
458-
continue
459-
460-
if not posixpath.exists(to_lic_path):
461-
os.makedirs(to_lic_path)
462-
try:
463-
shutil.copy2(from_lic_path, to_lic_path)
464-
except Exception as e:
465-
print(repr(e))
466-
print('Cannot copy file at %(from_lic_path)r.' % locals())
465+
for copy_file_name in file_list:
466+
from_lic_path = posixpath.join(to_posix(reference_dir), copy_file_name)
467+
about_file_dir = os.path.dirname(to_posix(afp)).lstrip('/')
468+
to_lic_path = posixpath.join(to_posix(base_dir), about_file_dir)
469+
470+
if on_windows:
471+
from_lic_path = add_unc(from_lic_path)
472+
to_lic_path = add_unc(to_lic_path)
473+
474+
# Strip the white spaces
475+
from_lic_path = from_lic_path.strip()
476+
to_lic_path = to_lic_path.strip()
477+
478+
# Errors will be captured when doing the validation
479+
if not posixpath.exists(from_lic_path):
480+
continue
481+
482+
if not posixpath.exists(to_lic_path):
483+
os.makedirs(to_lic_path)
484+
try:
485+
shutil.copy2(from_lic_path, to_lic_path)
486+
except Exception as e:
487+
print(repr(e))
488+
print('Cannot copy file at %(from_lic_path)r.' % locals())
467489

468490

469491
# FIXME: we should use a license object instead

tests/test_util.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
from testing_utils import extract_test_loc
2828
from testing_utils import get_test_loc
29+
from testing_utils import get_temp_dir
2930
from testing_utils import on_posix
3031
from testing_utils import on_windows
3132

@@ -611,3 +612,19 @@ def test_unique_can_handle_About_object(self):
611612
abouts = [a, b]
612613
results = util.unique(abouts)
613614
assert [a] == results
615+
616+
def test_copy_license_notice_files(self):
617+
base_dir = get_temp_dir()
618+
reference_dir = get_test_loc('test_util/licenses')
619+
fields = [(u'license_expression', u'mit or public-domain'),
620+
(u'about_resource', u'.'),
621+
(u'name', u'test'),
622+
(u'license_key', [u'mit', u'public-domain']),
623+
(u'license_file', [u'mit.LICENSE, mit2.LICENSE', u'public-domain.LICENSE'])]
624+
util.copy_license_notice_files(fields, base_dir, reference_dir, '')
625+
licenses = ['mit.LICENSE', 'mit2.LICENSE', 'public-domain.LICENSE']
626+
from os import listdir
627+
copied_files = listdir(base_dir)
628+
assert len(licenses) == len(copied_files)
629+
for license in licenses:
630+
assert license in copied_files
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
2+
3+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
4+
5+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.This component is released to the public domain by the author.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
2+
3+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
4+
5+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.This component is released to the public domain by the author.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This component is released to the public domain by the author.

0 commit comments

Comments
 (0)