From fc5bbdf70f8a9b0bfd7e377cc7df732b119bdac7 Mon Sep 17 00:00:00 2001 From: Kevin Yap Date: Thu, 22 Jan 2015 17:54:49 -0800 Subject: [PATCH 1/2] Make `make tidy` Python scripts more idiomatic Also makes errorck.py and tidy.py compatible with Python 3. --- src/etc/errorck.py | 39 ++++++++++++++------------------------- src/etc/licenseck.py | 12 ++++++------ src/etc/tidy.py | 2 +- 3 files changed, 21 insertions(+), 32 deletions(-) diff --git a/src/etc/errorck.py b/src/etc/errorck.py index 17659309d3b..952e299265d 100644 --- a/src/etc/errorck.py +++ b/src/etc/errorck.py @@ -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 @@ -28,15 +27,12 @@ for (dirpath, dirnames, filenames) in os.walk(src_dir): 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) - + 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: @@ -45,26 +41,19 @@ for (dirpath, dirnames, filenames) in os.walk(src_dir): 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) diff --git a/src/etc/licenseck.py b/src/etc/licenseck.py index 9ac0acc38a7..44a50efcd09 100644 --- a/src/etc/licenseck.py +++ b/src/etc/licenseck.py @@ -57,18 +57,18 @@ exceptions = [ 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): + if (license1 not in boilerplate or license2 not in boilerplate) and \ + (license3 not in boilerplate or license4 not in boilerplate): return False + return True diff --git a/src/etc/tidy.py b/src/etc/tidy.py index 536ab7f30b9..c65b762e517 100644 --- a/src/etc/tidy.py +++ b/src/etc/tidy.py @@ -113,7 +113,7 @@ try: 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)) From 76c279a4cfc9e54327bcc668a895ec19185139e9 Mon Sep 17 00:00:00 2001 From: Kevin Yap Date: Thu, 22 Jan 2015 20:40:15 -0800 Subject: [PATCH 2/2] Use a regex to perform license check --- src/etc/licenseck.py | 39 ++++++++++++--------------------------- 1 file changed, 12 insertions(+), 27 deletions(-) diff --git a/src/etc/licenseck.py b/src/etc/licenseck.py index 44a50efcd09..f38583ee1fb 100644 --- a/src/etc/licenseck.py +++ b/src/etc/licenseck.py @@ -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 or the MIT license -// , 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 or the MIT license -# , 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 +\\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 or the MIT license +\\1 , 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 @@ -67,8 +56,4 @@ def check_license(name, contents): # License check boilerplate = contents[:500] - if (license1 not in boilerplate or license2 not in boilerplate) and \ - (license3 not in boilerplate or license4 not in boilerplate): - return False - - return True + return bool(license_re.search(boilerplate))