Skip to content

Commit fc5bbdf

Browse files
committed
Make make tidy Python scripts more idiomatic
Also makes errorck.py and tidy.py compatible with Python 3.
1 parent d8d5e4d commit fc5bbdf

File tree

3 files changed

+21
-32
lines changed

3 files changed

+21
-32
lines changed

src/etc/errorck.py

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
import sys, os, re
1515

1616
src_dir = sys.argv[1]
17-
18-
errcode_map = { }
17+
errcode_map = {}
18+
error_re = re.compile("(E\d\d\d\d)")
1919

2020
for (dirpath, dirnames, filenames) in os.walk(src_dir):
21-
2221
if "src/test" in dirpath or "src/llvm" in dirpath:
2322
# Short circuit for fast
2423
continue
@@ -28,15 +27,12 @@
2827
continue
2928

3029
path = os.path.join(dirpath, filename)
31-
line_num = 1
32-
with open(path, 'r') as f:
33-
for line in f:
34-
35-
p = re.compile("(E\d\d\d\d)")
36-
m = p.search(line)
37-
if not m is None:
38-
errcode = m.group(1)
3930

31+
with open(path, 'r') as f:
32+
for line_num, line in enumerate(f, start=1):
33+
match = error_re.search(line)
34+
if match:
35+
errcode = match.group(1)
4036
new_record = [(errcode, path, line_num, line)]
4137
existing = errcode_map.get(errcode)
4238
if existing is not None:
@@ -45,26 +41,19 @@
4541
else:
4642
errcode_map[errcode] = new_record
4743

48-
line_num += 1
49-
5044
errors = False
5145
all_errors = []
52-
for errcode in errcode_map:
53-
entries = errcode_map[errcode]
54-
all_errors += [entries[0][0]]
46+
47+
for errcode, entries in errcode_map.items():
48+
all_errors.append(entries[0][0])
5549
if len(entries) > 1:
56-
print "error: duplicate error code " + errcode
50+
print("error: duplicate error code " + errcode)
5751
for entry in entries:
58-
print entry[1] + ": " + str(entry[2])
59-
print entry[3]
52+
print("{1}: {2}\n{3}".format(*entry))
6053
errors = True
6154

62-
print str(len(errcode_map)) + " error codes"
63-
64-
all_errors.sort()
65-
all_errors.reverse()
66-
67-
print "highest error code: " + all_errors[0]
55+
print("{0} error codes".format(len(errcode_map)))
56+
print("highest error code: " + max(all_errors))
6857

6958
if errors:
7059
sys.exit(1)

src/etc/licenseck.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,18 @@
5757

5858
def check_license(name, contents):
5959
# Whitelist check
60-
for exception in exceptions:
61-
if name.endswith(exception):
62-
return True
60+
if any(name.endswith(e) for e in exceptions):
61+
return True
6362

6463
# Xfail check
6564
firstlineish = contents[:100]
66-
if firstlineish.find("ignore-license") != -1:
65+
if "ignore-license" in firstlineish:
6766
return True
6867

6968
# License check
7069
boilerplate = contents[:500]
71-
if (boilerplate.find(license1) == -1 or boilerplate.find(license2) == -1) and \
72-
(boilerplate.find(license3) == -1 or boilerplate.find(license4) == -1):
70+
if (license1 not in boilerplate or license2 not in boilerplate) and \
71+
(license3 not in boilerplate or license4 not in boilerplate):
7372
return False
73+
7474
return True

src/etc/tidy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def do_license_check(name, contents):
113113
if current_name != "":
114114
do_license_check(current_name, current_contents)
115115

116-
except UnicodeDecodeError, e:
116+
except UnicodeDecodeError as e:
117117
report_err("UTF-8 decoding error " + str(e))
118118

119119

0 commit comments

Comments
 (0)