Skip to content

Make make tidy Python scripts more idiomatic #21539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 14 additions & 25 deletions src/etc/errorck.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
import sys, os, re

src_dir = sys.argv[1]

errcode_map = { }
errcode_map = {}
error_re = re.compile("(E\d\d\d\d)")

for (dirpath, dirnames, filenames) in os.walk(src_dir):

if "src/test" in dirpath or "src/llvm" in dirpath:
# Short circuit for fast
continue
Expand All @@ -28,15 +27,12 @@
continue

path = os.path.join(dirpath, filename)
line_num = 1
with open(path, 'r') as f:
for line in f:

p = re.compile("(E\d\d\d\d)")
m = p.search(line)
if not m is None:
errcode = m.group(1)

with open(path, 'r') as f:
for line_num, line in enumerate(f, start=1):
match = error_re.search(line)
if match:
errcode = match.group(1)
new_record = [(errcode, path, line_num, line)]
existing = errcode_map.get(errcode)
if existing is not None:
Expand All @@ -45,26 +41,19 @@
else:
errcode_map[errcode] = new_record

line_num += 1

errors = False
all_errors = []
for errcode in errcode_map:
entries = errcode_map[errcode]
all_errors += [entries[0][0]]

for errcode, entries in errcode_map.items():
all_errors.append(entries[0][0])
if len(entries) > 1:
print "error: duplicate error code " + errcode
print("error: duplicate error code " + errcode)
for entry in entries:
print entry[1] + ": " + str(entry[2])
print entry[3]
print("{1}: {2}\n{3}".format(*entry))
errors = True

print str(len(errcode_map)) + " error codes"

all_errors.sort()
all_errors.reverse()

print "highest error code: " + all_errors[0]
print("{0} error codes".format(len(errcode_map)))
print("highest error code: " + max(all_errors))

if errors:
sys.exit(1)
45 changes: 15 additions & 30 deletions src/etc/licenseck.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,18 @@
# option. This file may not be copied, modified, or distributed
# except according to those terms.

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

license3 = """# Copyright """
license4 = """ The Rust Project Developers. See the COPYRIGHT
# file at the top-level directory of this distribution and at
# http://rust-lang.org/COPYRIGHT.
#
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
# option. This file may not be copied, modified, or distributed
# except according to those terms.
"""
license_re = re.compile(
u"""(#|//) Copyright .* The Rust Project Developers. See the COPYRIGHT
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be a raw string literal (i.e., ur"""…"""), so all the doubled backslashes can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I implemented it this way initially, but Python 3 didn't like it.

\\1 file at the top-level directory of this distribution and at
\\1 http://rust-lang.org/COPYRIGHT.
\\1
\\1 Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
\\1 http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
\\1 <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
\\1 option. This file may not be copied, modified, or distributed
\\1 except according to those terms.""")

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

def check_license(name, contents):
# Whitelist check
for exception in exceptions:
if name.endswith(exception):
return True
if any(name.endswith(e) for e in exceptions):
return True

# Xfail check
firstlineish = contents[:100]
if firstlineish.find("ignore-license") != -1:
if "ignore-license" in firstlineish:
return True

# License check
boilerplate = contents[:500]
if (boilerplate.find(license1) == -1 or boilerplate.find(license2) == -1) and \
(boilerplate.find(license3) == -1 or boilerplate.find(license4) == -1):
return False
return True
return bool(license_re.search(boilerplate))
2 changes: 1 addition & 1 deletion src/etc/tidy.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def do_license_check(name, contents):
if current_name != "":
do_license_check(current_name, current_contents)

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


Expand Down