Skip to content

Commit 6e5a1a9

Browse files
committed
#492 Code enhancement
Signed-off-by: Chin Yeung Li <[email protected]>
1 parent 8a25f3b commit 6e5a1a9

File tree

2 files changed

+62
-42
lines changed

2 files changed

+62
-42
lines changed

src/attributecode/gen.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ def check_duplicated_columns(location):
7070
dup_msg = u', '.join(dup_msg)
7171
msg = ('Duplicated column name(s): %(dup_msg)s\n' % locals() +
7272
'Please correct the input and re-run.')
73-
errors.append(Error(ERROR, msg))
74-
return unique(errors)
73+
err = Error(ERROR, msg)
74+
if not err in errors:
75+
errors.append(err)
76+
return errors
7577

7678

7779
def check_duplicated_about_resource(arp, arp_list):
@@ -126,6 +128,7 @@ def load_inventory(location, from_attrib=False, base_dir=None, scancode=False, r
126128
"""
127129
errors = []
128130
abouts = []
131+
129132
if base_dir:
130133
base_dir = util.to_posix(base_dir)
131134
if scancode:
@@ -152,16 +155,17 @@ def load_inventory(location, from_attrib=False, base_dir=None, scancode=False, r
152155
arp = component['about_resource']
153156
dup_err = check_duplicated_about_resource(arp, arp_list)
154157
if dup_err:
155-
errors.append(dup_err)
158+
if not dup_err in errors:
159+
errors.append(dup_err)
156160
else:
157161
arp_list.append(arp)
158162

159163
newline_in_file_err = check_newline_in_file_field(component)
160-
for err in newline_in_file_err:
161-
errors.append(err)
164+
if newline_in_file_err:
165+
errors.extend(newline_in_file_err)
162166

163167
invalid_about_filename = check_about_resource_filename(arp)
164-
if invalid_about_filename:
168+
if invalid_about_filename and not invalid_about_filename in errors:
165169
errors.append(invalid_about_filename)
166170
if errors:
167171
return errors, abouts
@@ -172,7 +176,7 @@ def load_inventory(location, from_attrib=False, base_dir=None, scancode=False, r
172176
errors.append(Error(CRITICAL, msg))
173177
return errors, abouts
174178

175-
for i, fields in enumerate(inventory):
179+
for fields in inventory:
176180
# check does the input contains the required fields
177181
required_fields = model.About.required_fields
178182

@@ -223,9 +227,7 @@ def load_inventory(location, from_attrib=False, base_dir=None, scancode=False, r
223227
if e.message == 'Field about_resource is required':
224228
ld_errors.remove(e)
225229
"""
226-
for e in ld_errors:
227-
if not e in errors:
228-
errors.extend(ld_errors)
230+
errors.extend(ld_errors)
229231
abouts.append(about)
230232
# Covert the license_score value from string to list of int
231233
# The licesne_score is not in the spec but is specify in the scancode license scan.
@@ -239,7 +241,7 @@ def load_inventory(location, from_attrib=False, base_dir=None, scancode=False, r
239241
except:
240242
pass
241243

242-
return unique(errors), abouts
244+
return errors, abouts
243245

244246

245247
def update_about_resource(self):
@@ -285,6 +287,8 @@ def generate(location, base_dir, android=None, reference_dir=None, fetch_license
285287
errors.append(e)
286288

287289
for about in abouts:
290+
# Strip trailing spaces
291+
about.about_file_path = about.about_file_path.strip()
288292
if about.about_file_path.startswith('/'):
289293
about.about_file_path = about.about_file_path.lstrip('/')
290294
dump_loc = join(bdir, about.about_file_path.lstrip('/'))
@@ -328,14 +332,12 @@ def generate(location, base_dir, android=None, reference_dir=None, fetch_license
328332
msg = (u'Field about_resource: '
329333
u'%(path)s '
330334
u'does not exist' % locals())
331-
not_exist_errors.append(msg)
335+
errors.append(Error(INFO, msg))
332336

333337
licenses_dict = {}
334338
if gen_license:
335339
# Write generated LICENSE file
336-
license_key_name_context_url_list, err = about.dump_lic(dump_loc, license_dict)
337-
if err:
338-
errors.append(err)
340+
license_key_name_context_url_list = about.dump_lic(dump_loc, license_dict)
339341
if license_key_name_context_url_list:
340342
for lic_key, lic_name, lic_filename, lic_context, lic_url, spdx_lic_key in license_key_name_context_url_list:
341343
licenses_dict[lic_key] = [lic_name, lic_filename, lic_context, lic_url, spdx_lic_key]
@@ -372,9 +374,6 @@ def generate(location, base_dir, android=None, reference_dir=None, fetch_license
372374
else:
373375
notice_dict[notice_path] = notice_context
374376

375-
for e in not_exist_errors:
376-
errors.append(Error(INFO, e))
377-
378377
except Exception as e:
379378
# only keep the first 100 char of the exception
380379
# TODO: truncated errors are likely making diagnotics harder
@@ -393,4 +392,4 @@ def generate(location, base_dir, android=None, reference_dir=None, fetch_license
393392
else:
394393
about.dump_android_notice(path, notice_dict[path])
395394

396-
return unique(errors), abouts
395+
return errors, abouts

src/attributecode/model.py

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,7 @@ def hydrate(self, fields):
879879
"""
880880
errors = []
881881
seen_fields = {}
882+
illegal_name_list = []
882883

883884
for name, value in fields:
884885
orig_name = name
@@ -916,10 +917,16 @@ def hydrate(self, fields):
916917

917918
# A custom field
918919
# is the name valid?
920+
if not is_valid_name(name):
921+
if not name in illegal_name_list:
922+
illegal_name_list.append(name)
923+
continue
924+
"""
919925
illegal_name_error = validate_field_name(name)
920926
if illegal_name_error:
921-
errors.append(illegal_name_error)
927+
errors.append(illegal_name_list)
922928
continue
929+
"""
923930

924931
msg = 'Field %(orig_name)s is a custom field.'
925932
errors.append(Error(INFO, msg % locals()))
@@ -945,6 +952,10 @@ def hydrate(self, fields):
945952
msg = 'Internal error with custom field: %(name)r: %(value)r.'
946953
errors.append(Error(CRITICAL, msg % locals()))
947954

955+
if illegal_name_list:
956+
msg = ('Field name: %(illegal_name_list)r contains illegal name characters: '
957+
'0 to 9, a to z, A to Z and _. (or empty spaces) and is ignored.')
958+
errors.append(Error(CRITICAL, msg % locals()))
948959
return errors
949960

950961
def process(self, fields, about_file_path, running_inventory=False,
@@ -1034,7 +1045,7 @@ def load_dict(self, fields_dict, base_dir, scancode=False, from_attrib=False, ru
10341045

10351046
for key, value in fields:
10361047
if not value:
1037-
# never return empty or absent fieds
1048+
# never return empty or absent fields
10381049
continue
10391050
if key == u'licenses':
10401051
# FIXME: use a license object instead
@@ -1147,11 +1158,14 @@ def dumps(self, licenses_dict=None):
11471158
if licenses_dict and lic_key in licenses_dict:
11481159
lic_dict['key'] = lic_key
11491160
lic_name, lic_filename, lic_context, lic_url, spdx_lic_key = licenses_dict[lic_key]
1150-
1151-
lic_dict['name'] = lic_name
1152-
lic_dict['file'] = lic_filename
1153-
lic_dict['url'] = lic_url
1154-
lic_dict['spdx_license_key'] = spdx_lic_key
1161+
if lic_name:
1162+
lic_dict['name'] = lic_name
1163+
if lic_filename:
1164+
lic_dict['file'] = lic_filename
1165+
if lic_url:
1166+
lic_dict['url'] = lic_url
1167+
if spdx_lic_key:
1168+
lic_dict['spdx_license_key'] = spdx_lic_key
11551169

11561170
# Remove the license information if it has been handled
11571171
lic_key_copy.remove(lic_key)
@@ -1161,6 +1175,8 @@ def dumps(self, licenses_dict=None):
11611175
license_url.remove(lic_url)
11621176
if lic_filename in license_file:
11631177
license_file.remove(lic_filename)
1178+
if spdx_lic_key in spdx_license_key:
1179+
spdx_license_key.remove(spdx_lic_key)
11641180
lic_dict_list.append(lic_dict)
11651181

11661182
# Handle license information that have not been handled.
@@ -1277,7 +1293,6 @@ def dump_lic(self, location, license_dict):
12771293
loc = util.to_posix(location)
12781294
parent = posixpath.dirname(loc)
12791295
license_key_name_context_url = []
1280-
err = ''
12811296

12821297
if not posixpath.exists(parent):
12831298
os.makedirs(add_unc(parent))
@@ -1288,20 +1303,26 @@ def dump_lic(self, location, license_dict):
12881303
self.license_key.present = True
12891304
if not special_char_in_expression:
12901305
for lic_key in lic_list:
1291-
try:
1292-
if license_dict[lic_key]:
1293-
license_path = posixpath.join(parent, lic_key)
1294-
license_path += u'.LICENSE'
1295-
license_path = add_unc(license_path)
1296-
license_name, license_filename, license_context, license_url, spdx_license_key = license_dict[lic_key]
1297-
license_info = (lic_key, license_name, license_filename, license_context, license_url, spdx_license_key)
1298-
license_key_name_context_url.append(license_info)
1299-
with io.open(license_path, mode='w', encoding='utf-8', newline='\n', errors='replace') as lic:
1300-
lic.write(license_context)
1301-
except Exception as e:
1302-
err = Error(ERROR, 'Invalid license: ' + str(e))
1303-
1304-
return license_key_name_context_url, err
1306+
license_name = ''
1307+
license_filename = ''
1308+
license_context = ''
1309+
license_url = ''
1310+
spdx_license_key = ''
1311+
if lic_key in license_dict:
1312+
license_path = posixpath.join(parent, lic_key)
1313+
license_path += u'.LICENSE'
1314+
license_path = add_unc(license_path)
1315+
license_name, license_filename, license_context, license_url, spdx_license_key = license_dict[lic_key]
1316+
license_info = (lic_key, license_name, license_filename, license_context, license_url, spdx_license_key)
1317+
license_key_name_context_url.append(license_info)
1318+
with io.open(license_path, mode='w', encoding='utf-8', newline='\n', errors='replace') as lic:
1319+
lic.write(license_context)
1320+
else:
1321+
# Invalid license issue is already handled
1322+
license_info = (lic_key, license_name, license_filename, license_context, license_url, spdx_license_key)
1323+
license_key_name_context_url.append(license_info)
1324+
1325+
return license_key_name_context_url
13051326

13061327

13071328
def collect_inventory(location):
@@ -1324,7 +1345,7 @@ def collect_inventory(location):
13241345
msg = (about_file_path + ": " + message)
13251346
errors.append(Error(severity, msg))
13261347
abouts.append(about)
1327-
return unique(errors), abouts
1348+
return errors, abouts
13281349

13291350

13301351
def collect_abouts_license_expression(location):

0 commit comments

Comments
 (0)