Fix more pylint warnings.

* add .pylintrc to use 2 space indentation
* rename single-letter local variables

Test: ./warn.py build.log > warnings.html
Change-Id: I2ca56a6cb130a9d6c73328c5592ad7cde8a974ab
This commit is contained in:
Chih-Hung Hsieh 2021-04-30 14:30:58 -07:00
parent 10b66f6975
commit a606822f35
3 changed files with 89 additions and 91 deletions

4
tools/warn/.pylintrc Normal file
View file

@ -0,0 +1,4 @@
[FORMAT]
# Two spaces for each indentation level.
indent-string=' '

View file

@ -144,12 +144,11 @@ def create_warnings(warn_patterns, project_names):
2D warnings array where warnings[p][s] is # of warnings in project name p of
severity level s
"""
# pylint:disable=invalid-name
warnings = {p: {s.value: 0 for s in Severity.levels} for p in project_names}
for i in warn_patterns:
s = i['severity'].value
for p in i['projects']:
warnings[p][s] += i['projects'][p]
for pattern in warn_patterns:
value = pattern['severity'].value
for project in pattern['projects']:
warnings[project][value] += pattern['projects'][project]
return warnings
@ -173,11 +172,11 @@ def emit_table_header(total_by_severity):
"""Returns list of HTML-formatted content for severity stats."""
stats_header = ['Project']
for s in Severity.levels:
if total_by_severity[s.value]:
for severity in Severity.levels:
if total_by_severity[severity.value]:
stats_header.append(
'<span style=\'background-color:{}\'>{}</span>'.format(
s.color, s.column_header))
severity.color, severity.column_header))
stats_header.append('TOTAL')
return stats_header
@ -200,15 +199,15 @@ def emit_row_counts_per_project(warnings, total_by_project, total_by_severity,
total_all_projects = 0
stats_rows = []
for p in project_names:
if total_by_project[p]:
one_row = [p]
for s in Severity.levels:
if total_by_severity[s.value]:
one_row.append(warnings[p][s.value])
one_row.append(total_by_project[p])
for p_name in project_names:
if total_by_project[p_name]:
one_row = [p_name]
for severity in Severity.levels:
if total_by_severity[severity.value]:
one_row.append(warnings[p_name][severity.value])
one_row.append(total_by_project[p_name])
stats_rows.append(one_row)
total_all_projects += total_by_project[p]
total_all_projects += total_by_project[p_name]
return total_all_projects, stats_rows
@ -226,10 +225,10 @@ def emit_row_counts_per_severity(total_by_severity, stats_header, stats_rows,
total_all_severities = 0
one_row = ['<b>TOTAL</b>']
for s in Severity.levels:
if total_by_severity[s.value]:
one_row.append(total_by_severity[s.value])
total_all_severities += total_by_severity[s.value]
for severity in Severity.levels:
if total_by_severity[severity.value]:
one_row.append(total_by_severity[severity.value])
total_all_severities += total_by_severity[severity.value]
one_row.append(total_all_projects)
stats_rows.append(one_row)
writer('<script>')
@ -328,8 +327,8 @@ def dump_fixed(writer, warn_patterns):
for text in fixed_patterns:
cur_row_class = 1 - cur_row_class
# remove last '\n'
t = text[:-1] if text[-1] == '\n' else text
writer('<tr><td class="c' + str(cur_row_class) + '">' + t + '</td></tr>')
out_text = text[:-1] if text[-1] == '\n' else text
writer('<tr><td class="c' + str(cur_row_class) + '">' + out_text + '</td></tr>')
writer('</table></div>')
writer('</blockquote>')
@ -339,10 +338,10 @@ def write_severity(csvwriter, sev, kind, warn_patterns):
total = 0
for pattern in warn_patterns:
if pattern['severity'] == sev and pattern['members']:
n = len(pattern['members'])
total += n
num_members = len(pattern['members'])
total += num_members
warning = kind + ': ' + (pattern['description'] or '?')
csvwriter.writerow([n, '', warning])
csvwriter.writerow([num_members, '', warning])
# print number of warnings for each project, ordered by project name
projects = sorted(pattern['projects'].keys())
for project in projects:
@ -355,8 +354,8 @@ def dump_csv(csvwriter, warn_patterns):
"""Dump number of warnings in CSV format to writer."""
sort_warnings(warn_patterns)
total = 0
for s in Severity.levels:
total += write_severity(csvwriter, s, s.column_header, warn_patterns)
for severity in Severity.levels:
total += write_severity(csvwriter, severity, severity.column_header, warn_patterns)
csvwriter.writerow([total, '', 'All warnings'])
@ -379,35 +378,35 @@ def dump_csv_with_description(csvwriter, warning_records, warning_messages,
csvwriter.writerow(output)
# Return s with escaped backslash and quotation characters.
def escape_string(s):
return s.replace('\\', '\\\\').replace('"', '\\"')
# Return line with escaped backslash and quotation characters.
def escape_string(line):
return line.replace('\\', '\\\\').replace('"', '\\"')
# Return s without trailing '\n' and escape the quotation characters.
def strip_escape_string(s):
if not s:
return s
s = s[:-1] if s[-1] == '\n' else s
return escape_string(s)
# Return line without trailing '\n' and escape the quotation characters.
def strip_escape_string(line):
if not line:
return line
line = line[:-1] if line[-1] == '\n' else line
return escape_string(line)
def emit_warning_array(name, writer, warn_patterns):
writer('var warning_{} = ['.format(name))
for w in warn_patterns:
for pattern in warn_patterns:
if name == 'severity':
writer('{},'.format(w[name].value))
writer('{},'.format(pattern[name].value))
else:
writer('{},'.format(w[name]))
writer('{},'.format(pattern[name]))
writer('];')
def emit_warning_arrays(writer, warn_patterns):
emit_warning_array('severity', writer, warn_patterns)
writer('var warning_description = [')
for w in warn_patterns:
if w['members']:
writer('"{}",'.format(escape_string(w['description'])))
for pattern in warn_patterns:
if pattern['members']:
writer('"{}",'.format(escape_string(pattern['description'])))
else:
writer('"",') # no such warning
writer('];')
@ -566,32 +565,32 @@ def emit_const_string(name, value, writer):
# Emit a JavaScript const integer array.
def emit_const_int_array(name, array, writer):
writer('const ' + name + ' = [')
for n in array:
writer(str(n) + ',')
for item in array:
writer(str(item) + ',')
writer('];')
# Emit a JavaScript const string array.
def emit_const_string_array(name, array, writer):
writer('const ' + name + ' = [')
for s in array:
writer('"' + strip_escape_string(s) + '",')
for item in array:
writer('"' + strip_escape_string(item) + '",')
writer('];')
# Emit a JavaScript const string array for HTML.
def emit_const_html_string_array(name, array, writer):
writer('const ' + name + ' = [')
for s in array:
writer('"' + html.escape(strip_escape_string(s)) + '",')
for item in array:
writer('"' + html.escape(strip_escape_string(item)) + '",')
writer('];')
# Emit a JavaScript const object array.
def emit_const_object_array(name, array, writer):
writer('const ' + name + ' = [')
for x in array:
writer(str(x) + ',')
for item in array:
writer(str(item) + ',')
writer('];')
@ -671,8 +670,8 @@ def write_html(flags, project_names, warn_patterns, html_path, warning_messages,
warning_links, warning_records, header_str):
"""Write warnings html file."""
if html_path:
with open(html_path, 'w') as f:
dump_html(flags, f, warning_messages, warning_links, warning_records,
with open(html_path, 'w') as outf:
dump_html(flags, outf, warning_messages, warning_links, warning_records,
header_str, warn_patterns, project_names)
@ -680,12 +679,12 @@ def write_out_csv(flags, warn_patterns, warning_messages, warning_links,
warning_records, header_str, project_names):
"""Write warnings csv file."""
if flags.csvpath:
with open(flags.csvpath, 'w') as f:
dump_csv(csv.writer(f, lineterminator='\n'), warn_patterns)
with open(flags.csvpath, 'w') as outf:
dump_csv(csv.writer(outf, lineterminator='\n'), warn_patterns)
if flags.csvwithdescription:
with open(flags.csvwithdescription, 'w') as f:
dump_csv_with_description(csv.writer(f, lineterminator='\n'),
with open(flags.csvwithdescription, 'w') as outf:
dump_csv_with_description(csv.writer(outf, lineterminator='\n'),
warning_records, warning_messages,
warn_patterns, project_names)

View file

@ -116,22 +116,20 @@ def get_project_names(project_list):
def find_project_index(line, project_patterns):
"""Return the index to the project pattern array."""
# pylint:disable=invalid-name
for i, p in enumerate(project_patterns):
if p.match(line):
return i
for idx, pattern in enumerate(project_patterns):
if pattern.match(line):
return idx
return -1
def classify_one_warning(warning, link, results, project_patterns,
warn_patterns):
"""Classify one warning line."""
# pylint:disable=invalid-name
for i, w in enumerate(warn_patterns):
for cpat in w['compiled_patterns']:
for idx, pattern in enumerate(warn_patterns):
for cpat in pattern['compiled_patterns']:
if cpat.match(warning):
p = find_project_index(warning, project_patterns)
results.append([warning, link, i, p])
project_idx = find_project_index(warning, project_patterns)
results.append([warning, link, idx, project_idx])
return
# If we end up here, there was a problem parsing the log
# probably caused by 'make -j' mixing the output from
@ -310,7 +308,6 @@ def parse_input_file_chrome(infile, flags):
# Remove the duplicated warnings save ~8% of time when parsing
# one typical build log than before
unique_warnings = dict()
# pylint:disable=invalid-name
for line in infile:
if warning_pattern.match(line):
normalized_line = normalize_warning_line(line, flags)
@ -318,17 +315,17 @@ def parse_input_file_chrome(infile, flags):
unique_warnings[normalized_line] = generate_cs_link(line, flags)
elif (platform_version == 'unknown' or board_name == 'unknown' or
architecture == 'unknown'):
m = re.match(r'.+Package:.+chromeos-base/chromeos-chrome-', line)
if m is not None:
result = re.match(r'.+Package:.+chromeos-base/chromeos-chrome-', line)
if result is not None:
platform_version = 'R' + line.split('chrome-')[1].split('_')[0]
continue
m = re.match(r'.+Source\sunpacked\sin\s(.+)', line)
if m is not None:
board_name = m.group(1).split('/')[2]
result = re.match(r'.+Source\sunpacked\sin\s(.+)', line)
if result is not None:
board_name = result.group(1).split('/')[2]
continue
m = re.match(r'.+USE:\s*([^\s]*).*', line)
if m is not None:
architecture = m.group(1)
result = re.match(r'.+USE:\s*([^\s]*).*', line)
if result is not None:
architecture = result.group(1)
continue
header_str = '%s - %s - %s' % (platform_version, board_name, architecture)
@ -396,22 +393,21 @@ def parse_input_file_android(infile, flags):
line, flags, android_root, unique_warnings)
continue
# pylint:disable=invalid-name
if line_counter < 100:
# save a little bit of time by only doing this for the first few lines
line_counter += 1
m = re.search('(?<=^PLATFORM_VERSION=).*', line)
if m is not None:
platform_version = m.group(0)
m = re.search('(?<=^TARGET_PRODUCT=).*', line)
if m is not None:
target_product = m.group(0)
m = re.search('(?<=^TARGET_BUILD_VARIANT=).*', line)
if m is not None:
target_variant = m.group(0)
m = re.search('(?<=^TOP=).*', line)
if m is not None:
android_root = m.group(1)
result = re.search('(?<=^PLATFORM_VERSION=).*', line)
if result is not None:
platform_version = result.group(0)
result = re.search('(?<=^TARGET_PRODUCT=).*', line)
if result is not None:
target_product = result.group(0)
result = re.search('(?<=^TARGET_BUILD_VARIANT=).*', line)
if result is not None:
target_variant = result.group(0)
result = re.search('(?<=^TOP=).*', line)
if result is not None:
android_root = result.group(1)
if android_root:
new_unique_warnings = dict()
@ -458,12 +454,11 @@ def get_warn_patterns(platform):
other_patterns.warn_patterns)
else:
raise Exception('platform name %s is not valid' % platform)
# pylint:disable=invalid-name
for w in warn_patterns:
w['members'] = []
for pattern in warn_patterns:
pattern['members'] = []
# Each warning pattern has a 'projects' dictionary, that
# maps a project name to number of warnings in that project.
w['projects'] = {}
pattern['projects'] = {}
return warn_patterns