From 2aa138907a73c5f1709b322d20e09298b755bf4e Mon Sep 17 00:00:00 2001 From: Justin Santa Barbara Date: Wed, 4 Jan 2017 21:54:47 -0500 Subject: [PATCH] Consider 2017 a year And generally update to the latest upstream boilerplate script --- hack/boilerplate/boilerplate.py | 44 +++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/hack/boilerplate/boilerplate.py b/hack/boilerplate/boilerplate.py index b9e1edf433..a5205f4700 100755 --- a/hack/boilerplate/boilerplate.py +++ b/hack/boilerplate/boilerplate.py @@ -17,6 +17,7 @@ from __future__ import print_function import argparse +import difflib import glob import json import mmap @@ -25,16 +26,28 @@ import re import sys parser = argparse.ArgumentParser() -parser.add_argument("filenames", help="list of files to check, all files if unspecified", nargs='*') +parser.add_argument( + "filenames", + help="list of files to check, all files if unspecified", + nargs='*') rootdir = os.path.dirname(__file__) + "/../../" rootdir = os.path.abspath(rootdir) -parser.add_argument("--rootdir", default=rootdir, help="root directory to examine") +parser.add_argument( + "--rootdir", default=rootdir, help="root directory to examine") default_boilerplate_dir = os.path.join(rootdir, "hack/boilerplate") -parser.add_argument("--boilerplate-dir", default=default_boilerplate_dir) +parser.add_argument( + "--boilerplate-dir", default=default_boilerplate_dir) + +parser.add_argument( + "-v", "--verbose", + help="give verbose output regarding why a file does not pass", + action="store_true") + args = parser.parse_args() +verbose_out = sys.stderr if args.verbose else open("/dev/null", "w") def get_refs(): refs = {} @@ -52,7 +65,8 @@ def get_refs(): def file_passes(filename, refs, regexs): try: f = open(filename, 'r') - except: + except Exception as exc: + print("Unable to open %s: %s" % (filename, exc), file=verbose_out) return False data = f.read() @@ -79,6 +93,9 @@ def file_passes(filename, refs, regexs): # if our test file is smaller than the reference it surely fails! if len(ref) > len(data): + print('File %s smaller than reference (%d < %d)' % + (filename, len(data), len(ref)), + file=verbose_out) return False # trim our file to the same number of lines as the reference file @@ -87,9 +104,10 @@ def file_passes(filename, refs, regexs): p = regexs["year"] for d in data: if p.search(d): + print('File %s is missing the year' % filename, file=verbose_out) return False - # Replace all occurrences of the regex "2016|2015|2014" with "YEAR" + # Replace all occurrences of the regex "2017|2016|2015|2014" with "YEAR" p = regexs["date"] for i, d in enumerate(data): (data[i], found) = p.subn('YEAR', d) @@ -98,6 +116,12 @@ def file_passes(filename, refs, regexs): # if we don't match the reference at this point, fail if ref != data: + print("Header in %s does not match reference, diff:" % filename, file=verbose_out) + if args.verbose: + print(file=verbose_out) + for line in difflib.unified_diff(ref, data, 'reference', filename, lineterm=''): + print(line, file=verbose_out) + print(file=verbose_out) return False return True @@ -105,7 +129,9 @@ def file_passes(filename, refs, regexs): def file_extension(filename): return os.path.splitext(filename)[1].split(".")[-1].lower() -skipped_dirs = ['Godeps', 'third_party', '_gopath', '_output', '.git', 'cluster/env.sh', "vendor", "test/e2e/generated/bindata.go"] +skipped_dirs = ['Godeps', 'third_party', '_gopath', '_output', '.git', 'cluster/env.sh', + "vendor", "test/e2e/generated/bindata.go", "hack/boilerplate/test", + "pkg/generated/bindata.go"] def normalize_files(files): newfiles = [] @@ -149,8 +175,8 @@ def get_regexs(): regexs = {} # Search for "YEAR" which exists in the boilerplate, but shouldn't in the real thing regexs["year"] = re.compile( 'YEAR' ) - # dates can be 2014, 2015 or 2016, company holder names can be anything - regexs["date"] = re.compile( '(2014|2015|2016)' ) + # dates can be 2014, 2015, 2016, or 2017; company holder names can be anything + regexs["date"] = re.compile( '(2014|2015|2016|2017)' ) # strip // +build \n\n build constraints regexs["go_build_constraints"] = re.compile(r"^(// \+build.*\n)+\n", re.MULTILINE) # strip #!.* from shell scripts @@ -166,5 +192,7 @@ def main(): if not file_passes(filename, refs, regexs): print(filename, file=sys.stdout) + return 0 + if __name__ == "__main__": sys.exit(main())