Skip to content

Commit 41e2d71

Browse files
committed
Rollup merge of rust-lang#21539 - iKevinY:pythonic, r=alexcrichton
Also makes `errorck.py` and `tidy.py` compatible with Python 3.
2 parents dc32dfa + 76c279a commit 41e2d71

File tree

3 files changed

+30
-56
lines changed

3 files changed

+30
-56
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: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,18 @@
88
# option. This file may not be copied, modified, or distributed
99
# except according to those terms.
1010

11-
license1 = """// Copyright """
12-
license2 = """ The Rust Project Developers. See the COPYRIGHT
13-
// file at the top-level directory of this distribution and at
14-
// http://rust-lang.org/COPYRIGHT.
15-
//
16-
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
17-
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
18-
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
19-
// option. This file may not be copied, modified, or distributed
20-
// except according to those terms.
21-
"""
11+
import re
2212

23-
license3 = """# Copyright """
24-
license4 = """ The Rust Project Developers. See the COPYRIGHT
25-
# file at the top-level directory of this distribution and at
26-
# http://rust-lang.org/COPYRIGHT.
27-
#
28-
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
29-
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
30-
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
31-
# option. This file may not be copied, modified, or distributed
32-
# except according to those terms.
33-
"""
13+
license_re = re.compile(
14+
u"""(#|//) Copyright .* The Rust Project Developers. See the COPYRIGHT
15+
\\1 file at the top-level directory of this distribution and at
16+
\\1 http://rust-lang.org/COPYRIGHT.
17+
\\1
18+
\\1 Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
19+
\\1 http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
20+
\\1 <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
21+
\\1 option. This file may not be copied, modified, or distributed
22+
\\1 except according to those terms.""")
3423

3524
exceptions = [
3625
"rt/rust_android_dummy.cpp", # BSD, chromium
@@ -57,18 +46,14 @@
5746

5847
def check_license(name, contents):
5948
# Whitelist check
60-
for exception in exceptions:
61-
if name.endswith(exception):
62-
return True
49+
if any(name.endswith(e) for e in exceptions):
50+
return True
6351

6452
# Xfail check
6553
firstlineish = contents[:100]
66-
if firstlineish.find("ignore-license") != -1:
54+
if "ignore-license" in firstlineish:
6755
return True
6856

6957
# License check
7058
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):
73-
return False
74-
return True
59+
return bool(license_re.search(boilerplate))

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)