Skip to content

Commit c181c88

Browse files
committed
Fixed #204
* The current error.log will log the path that have problem, and I believe this is sufficient * Display the error count in the error.log and stdout. Signed-off-by: Chin Yeung Li <[email protected]>
1 parent 39b3678 commit c181c88

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

docs/CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Multiple licenses support
66
* No support for referenceing multuiple resources
77
* Fix the incorrect boolean field behavior
8+
* Display number of errors/warnings in the error log
89

910
2018-09-19
1011

src/attributecode/cmd.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,16 @@ def inventory(location, output, mapping, mapping_file, mapping_output, filter, q
198198
'Please correct and re-run.'
199199
print(msg)
200200

201+
error_count = 0
201202
finalized_errors = update_severity_level_about_resource_path_not_exist_error(errors)
202203

203-
log_errors(finalized_errors, quiet, verbose, os.path.dirname(output))
204+
for e in finalized_errors:
205+
# Only count as warning/error if CRITICAL, ERROR and WARNING
206+
if e.severity > 20:
207+
error_count = error_count + 1
208+
209+
log_errors(finalized_errors, error_count, quiet, verbose, os.path.dirname(output))
210+
click.echo(' %(error_count)d errors or warnings detected.' % locals())
204211
sys.exit(0)
205212

206213

@@ -282,9 +289,8 @@ def gen(location, output, mapping, mapping_file, license_notice_text_location, f
282289
# Only count as warning/error if CRITICAL, ERROR and WARNING
283290
if e.severity > 20:
284291
error_count = error_count + 1
285-
click.echo(
286-
'Generated %(about_count)d .ABOUT files with %(error_count)d errors or warnings' % locals())
287-
log_errors(finalized_errors, quiet, verbose, output)
292+
log_errors(finalized_errors, error_count, quiet, verbose, output)
293+
click.echo('Generated %(about_count)d .ABOUT files with %(error_count)d errors or warnings' % locals())
288294
sys.exit(0)
289295

290296

@@ -365,9 +371,16 @@ def attrib(location, output, template, mapping, mapping_file, inventory, vartext
365371
for no_match_error in no_match_errors:
366372
inv_errors.append(no_match_error)
367373

374+
error_count = 0
368375
finalized_errors = update_severity_level_about_resource_path_not_exist_error(inv_errors)
369376

370-
log_errors(finalized_errors, quiet, verbose, os.path.dirname(output))
377+
for e in finalized_errors:
378+
# Only count as warning/error if CRITICAL, ERROR and WARNING
379+
if e.severity > 20:
380+
error_count = error_count + 1
381+
382+
log_errors(finalized_errors, error_count, quiet, verbose, os.path.dirname(output))
383+
click.echo(' %(error_count)d errors or warnings detected.' % locals())
371384
click.echo('Finished.')
372385
sys.exit(0)
373386

@@ -429,7 +442,7 @@ def check(location, verbose):
429442
sys.exit(0)
430443

431444

432-
def log_errors(errors, quiet, verbose, base_dir=False):
445+
def log_errors(errors, err_count, quiet, verbose, base_dir=False):
433446
"""
434447
Iterate of sequence of Error objects and print and log errors with
435448
a severity superior or equal to level.
@@ -450,7 +463,9 @@ def log_errors(errors, quiet, verbose, base_dir=False):
450463
log_path = join(bdir, LOG_FILENAME)
451464
if exists(log_path):
452465
os.remove(log_path)
453-
466+
f = open(log_path, "a")
467+
error_msg = str(err_count) + u" errors or warnings detected."
468+
f.write(error_msg)
454469
file_handler = logging.FileHandler(log_path)
455470
file_logger.addHandler(file_handler)
456471

src/attributecode/model.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,12 +555,14 @@ def _validate(self, *args, **kwargs):
555555
False. Return a list of errors.
556556
"""
557557
errors = super(BooleanField, self)._validate(*args, ** kwargs)
558+
self.about_file_path = kwargs.get('about_file_path')
558559
flag = self.get_flag(self.original_value)
559560
if flag is False:
560561
name = self.name
561562
val = self.original_value
563+
about_file_path = self.about_file_path
562564
flag_values = self.flag_values
563-
msg = (u'Field %(name)s: Invalid flag value: %(val)r is not '
565+
msg = (u'Path: %(about_file_path)s - Field %(name)s: Invalid flag value: %(val)r is not '
564566
u'one of: %(flag_values)s' % locals())
565567
errors.append(Error(ERROR, msg))
566568
self.value = None

tests/test_cmd.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ def test_log_errors(capsys):
3939
Error(DEBUG, 'msg4'),
4040
Error(NOTSET, 'msg4'),
4141
]
42-
cmd.log_errors(errors, quiet, show_all, base_dir='')
42+
error_count = len(errors)
43+
cmd.log_errors(errors, error_count, quiet, show_all, base_dir='')
4344
out, err = capsys.readouterr()
4445
expected_out = '''CRITICAL: msg1
4546
ERROR: msg2
@@ -62,7 +63,8 @@ def test_log_errors_without_show_all(capsys):
6263
Error(DEBUG, 'msg4'),
6364
Error(NOTSET, 'msg4'),
6465
]
65-
cmd.log_errors(errors, quiet, show_all, base_dir='')
66+
error_count = len(errors)
67+
cmd.log_errors(errors, error_count, quiet, show_all, base_dir='')
6668
out, err = capsys.readouterr()
6769
expected_out = '''CRITICAL: msg1
6870
ERROR: msg2
@@ -82,7 +84,8 @@ def test_log_errors_with_quiet(capsys):
8284
Error(DEBUG, 'msg4'),
8385
Error(NOTSET, 'msg4'),
8486
]
85-
cmd.log_errors(errors, quiet, show_all, base_dir='')
87+
error_count = len(errors)
88+
cmd.log_errors(errors, error_count, quiet, show_all, base_dir='')
8689
out, err = capsys.readouterr()
8790
assert '' == out
8891
assert '' == err

0 commit comments

Comments
 (0)