From fccab36e1a3fbee42faad525ab2efb429ffd45ff Mon Sep 17 00:00:00 2001 From: dzhwinter Date: Fri, 19 Jan 2018 13:15:50 +0800 Subject: [PATCH] "fix python encode error" (#7613) --- .copyright.hook | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/.copyright.hook b/.copyright.hook index 2446e272481..6bfe58cebc3 100644 --- a/.copyright.hook +++ b/.copyright.hook @@ -77,10 +77,13 @@ def lang_type(filename): elif filename.endswith(".proto"): return "C" else: - print("Unsupported filetype") + print("Unsupported filetype %s", filename) exit(0) +PYTHON_ENCODE = re.compile("^[ \t\v]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)") + + def main(argv=None): parser = argparse.ArgumentParser( description='Checker for copyright declaration.') @@ -89,8 +92,15 @@ def main(argv=None): retv = 0 for filename in args.filenames: - first_line = io.open(filename).readline() - if "COPYRIGHT" in first_line.upper() : continue + fd = io.open(filename) + first_line = fd.readline() + if "COPYRIGHT" in first_line.upper(): continue + if filename.endswith(".py"): + second_line = fd.readline() + if first_line.startswith("#!") or PYTHON_ENCODE.match( + second_line) != None or PYTHON_ENCODE.match( + first_line) != None: + continue original_contents = io.open(filename).read() new_contents = generate_copyright( COPYRIGHT, lang_type(filename)) + original_contents -- GitLab