Skip to content

Commit 7f1c923

Browse files
committed
#450 Potential fixes
* Need to add tests
1 parent 6c504d9 commit 7f1c923

File tree

3 files changed

+56
-27
lines changed

3 files changed

+56
-27
lines changed

src/attributecode/gen.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -303,27 +303,28 @@ def generate(location, base_dir, android=None, reference_dir=None, fetch_license
303303
u'does not exist' % locals())
304304
not_exist_errors.append(msg)
305305

306+
licenses_dict = {}
306307
if gen_license:
307308
# Write generated LICENSE file
308309
license_key_name_context_url_list = about.dump_lic(dump_loc, license_dict)
309310
if license_key_name_context_url_list:
310-
# use value not "presence"
311-
if not about.license_file.present:
312-
about.license_file.value = OrderedDict()
313-
for lic_key, lic_name, lic_context, lic_url in license_key_name_context_url_list:
314-
gen_license_name = lic_key + u'.LICENSE'
315-
about.license_file.value[gen_license_name] = lic_context
311+
for lic_key, lic_name, lic_context, lic_url in license_key_name_context_url_list:
312+
licenses_dict[lic_key] = [lic_name, lic_context, lic_url]
313+
gen_license_name = lic_key + u'.LICENSE'
314+
if not lic_name in about.license_name.value:
315+
about.license_name.value.append(lic_name)
316+
about.license_file.value[gen_license_name] = license_dict[lic_key][1]
317+
if not lic_url in about.license_url.value:
318+
about.license_url.value.append(lic_url)
319+
320+
if about.license_name.value:
321+
about.license_name.present = True
322+
if about.license_file.value:
316323
about.license_file.present = True
317-
if not about.license_name.present:
318-
about.license_name.value.append(lic_name)
319-
if not about.license_url.present:
320-
about.license_url.value.append(lic_url)
321324
if about.license_url.value:
322325
about.license_url.present = True
323-
if about.license_name.value:
324-
about.license_name.present = True
325326

326-
about.dump(dump_loc)
327+
about.dump(dump_loc, licenses_dict)
327328

328329
if android:
329330
"""

src/attributecode/model.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,7 @@ def from_dict(cls, about_data, base_dir=''):
10501050
about.load_dict(about_data, base_dir=base_dir)
10511051
return about
10521052

1053-
def dumps(self):
1053+
def dumps(self, licenses_dict=None):
10541054
"""
10551055
Return self as a formatted ABOUT string.
10561056
"""
@@ -1064,7 +1064,6 @@ def dumps(self):
10641064
for field in self.all_fields():
10651065
if not field.value and not field.name in bool_fields:
10661066
continue
1067-
10681067
if field.name == 'license_key' and field.value:
10691068
license_key = field.value
10701069
elif field.name == 'license_name' and field.value:
@@ -1095,10 +1094,6 @@ def dumps(self):
10951094
license_file = field.value.keys()
10961095
elif field.name == 'license_url' and field.value:
10971096
license_url = field.value
1098-
1099-
# No multiple 'about_resource' reference supported.
1100-
# Take the first element (should only be one) in the list for the
1101-
# value of 'about_resource'
11021097
elif field.name in file_fields and field.value:
11031098
data[field.name] = field.original_value
11041099
elif field.name in bool_fields and not field.value == None:
@@ -1108,7 +1103,36 @@ def dumps(self):
11081103
data[field.name] = field.value
11091104

11101105
# Group the same license information in a list
1111-
license_group = list(zip_longest(license_key, license_name, license_file, license_url))
1106+
# This `licenses_dict` is a dictionary with license key as the key and the
1107+
# value is the list of [license_name, license_context, license_url]
1108+
lic_key_dup = license_key[:]
1109+
for lic_key in license_key:
1110+
lic_dict = OrderedDict()
1111+
1112+
if licenses_dict and lic_key in licenses_dict:
1113+
lic_dict['key'] = lic_key
1114+
lic_name = licenses_dict[lic_key][0]
1115+
lic_url = licenses_dict[lic_key][2]
1116+
lic_file = lic_key + '.LICENSE'
1117+
1118+
lic_dict['name'] = lic_name
1119+
lic_dict['file'] = lic_file
1120+
lic_dict['url'] = lic_url
1121+
1122+
# 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)
1126+
if lic_name in license_name:
1127+
license_name.remove(lic_name)
1128+
if lic_url in license_url:
1129+
license_url.remove(lic_url)
1130+
if lic_file in license_file:
1131+
license_file.remove(lic_file)
1132+
data.setdefault('licenses', []).append(lic_dict)
1133+
1134+
# Handle license information that have not been handled.
1135+
license_group = list(zip_longest(lic_key_dup, license_name, license_file, license_url))
11121136
for lic_group in license_group:
11131137
lic_dict = OrderedDict()
11141138
if lic_group[0]:
@@ -1123,7 +1147,7 @@ def dumps(self):
11231147

11241148
return saneyaml.dump(data)
11251149

1126-
def dump(self, location):
1150+
def dump(self, location, lic_dict=None):
11271151
"""
11281152
Write formatted ABOUT representation of self to location.
11291153
"""
@@ -1146,7 +1170,7 @@ def dump(self, location):
11461170

11471171
with io.open(about_file_path, mode='w', encoding='utf-8') as dumped:
11481172
dumped.write(genereated_tk_version)
1149-
dumped.write(self.dumps())
1173+
dumped.write(self.dumps(lic_dict))
11501174

11511175
def dump_android_notice(self, path, context):
11521176
"""
@@ -1208,14 +1232,15 @@ def dump_lic(self, location, license_dict):
12081232
if not posixpath.exists(parent):
12091233
os.makedirs(add_unc(parent))
12101234

1211-
if self.license_expression.present and not self.license_file.present:
1235+
if self.license_expression.present:
12121236
special_char_in_expression, lic_list = parse_license_expression(self.license_expression.value)
1213-
self.license_key.value = lic_list
12141237
self.license_key.present = True
12151238
if not special_char_in_expression:
12161239
for lic_key in lic_list:
12171240
try:
12181241
if license_dict[lic_key]:
1242+
if not lic_key in self.license_key.value:
1243+
self.license_key.value.append(lic_key)
12191244
license_path = posixpath.join(parent, lic_key)
12201245
license_path += u'.LICENSE'
12211246
license_path = add_unc(license_path)

tests/test_model.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -700,8 +700,8 @@ def test_load_dict_issue_433(self):
700700
'license_expression': 'license1 AND license2',
701701
'notice_file': 'package1.zip.NOTICE',
702702
'licenses': [
703-
{'key': 'license1', 'name': 'License1', 'file': 'license1.LICENSE'},
704-
{'key': 'license2', 'name': 'License2', 'file': 'license2.LICENSE'},
703+
{'key': 'license1', 'name': 'License1', 'file': 'license1.LICENSE', 'url': 'some_url'},
704+
{'key': 'license2', 'name': 'License2', 'file': 'license2.LICENSE','url': 'some_url'},
705705
],
706706
}
707707
about = model.About()
@@ -717,11 +717,14 @@ def test_load_dict_issue_433(self):
717717
- key: license1
718718
name: License1
719719
file: license1.LICENSE
720+
url: some_url
720721
- key: license2
721722
name: License2
722723
file: license2.LICENSE
724+
url: some_url
723725
'''
724-
assert about.dumps() == expected
726+
lic_dict = {u'license1': [u'License1', u'', u'some_url'], u'license2' : [u'License2', u'', u'some_url']}
727+
assert about.dumps(lic_dict) == expected
725728

726729
class SerializationTest(unittest.TestCase):
727730
def test_About_dumps(self):

0 commit comments

Comments
 (0)