|
| 1 | +from __future__ import absolute_import |
| 2 | +from __future__ import print_function |
| 3 | +from __future__ import unicode_literals |
| 4 | + |
| 5 | +import argparse |
| 6 | +import io, re |
| 7 | +import sys, os |
| 8 | +import subprocess |
| 9 | +import platform |
| 10 | + |
| 11 | +COPYRIGHT = ''' |
| 12 | +Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. |
| 13 | + |
| 14 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 15 | +you may not use this file except in compliance with the License. |
| 16 | +You may obtain a copy of the License at |
| 17 | + |
| 18 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 19 | + |
| 20 | +Unless required by applicable law or agreed to in writing, software |
| 21 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 22 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 23 | +See the License for the specific language governing permissions and |
| 24 | +limitations under the License. |
| 25 | +''' |
| 26 | + |
| 27 | +LANG_COMMENT_MARK = None |
| 28 | + |
| 29 | +NEW_LINE_MARK = None |
| 30 | + |
| 31 | +COPYRIGHT_HEADER = None |
| 32 | + |
| 33 | +if platform.system() == "Windows": |
| 34 | + NEW_LINE_MARK = "\r\n" |
| 35 | +else: |
| 36 | + NEW_LINE_MARK = '\n' |
| 37 | + COPYRIGHT_HEADER = COPYRIGHT.split(NEW_LINE_MARK)[1] |
| 38 | + p = re.search('(\d{4})', COPYRIGHT_HEADER).group(0) |
| 39 | + process = subprocess.Popen(["date", "+%Y"], stdout=subprocess.PIPE) |
| 40 | + date, err = process.communicate() |
| 41 | + date = date.decode("utf-8").rstrip("\n") |
| 42 | + COPYRIGHT_HEADER = COPYRIGHT_HEADER.replace(p, date) |
| 43 | + |
| 44 | + |
| 45 | +def generate_copyright(template, lang='C'): |
| 46 | + if lang == 'Python': |
| 47 | + LANG_COMMENT_MARK = '#' |
| 48 | + else: |
| 49 | + LANG_COMMENT_MARK = "//" |
| 50 | + |
| 51 | + lines = template.split(NEW_LINE_MARK) |
| 52 | + BLANK = " " |
| 53 | + ans = LANG_COMMENT_MARK + BLANK + COPYRIGHT_HEADER + NEW_LINE_MARK |
| 54 | + for lino, line in enumerate(lines): |
| 55 | + if lino == 0 or lino == 1 or lino == len(lines) - 1: continue |
| 56 | + if len(line) == 0: |
| 57 | + BLANK = "" |
| 58 | + else: |
| 59 | + BLANK = " " |
| 60 | + ans += LANG_COMMENT_MARK + BLANK + line + NEW_LINE_MARK |
| 61 | + |
| 62 | + return ans + "\n" |
| 63 | + |
| 64 | + |
| 65 | +def lang_type(filename): |
| 66 | + if filename.endswith(".py"): |
| 67 | + return "Python" |
| 68 | + elif filename.endswith(".h"): |
| 69 | + return "C" |
| 70 | + elif filename.endswith(".c"): |
| 71 | + return "C" |
| 72 | + elif filename.endswith(".hpp"): |
| 73 | + return "C" |
| 74 | + elif filename.endswith(".cc"): |
| 75 | + return "C" |
| 76 | + elif filename.endswith(".cpp"): |
| 77 | + return "C" |
| 78 | + elif filename.endswith(".cu"): |
| 79 | + return "C" |
| 80 | + elif filename.endswith(".cuh"): |
| 81 | + return "C" |
| 82 | + elif filename.endswith(".go"): |
| 83 | + return "C" |
| 84 | + elif filename.endswith(".proto"): |
| 85 | + return "C" |
| 86 | + else: |
| 87 | + print("Unsupported filetype %s", filename) |
| 88 | + exit(0) |
| 89 | + |
| 90 | + |
| 91 | +PYTHON_ENCODE = re.compile("^[ \t\v]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)") |
| 92 | + |
| 93 | + |
| 94 | +def main(argv=None): |
| 95 | + parser = argparse.ArgumentParser( |
| 96 | + description='Checker for copyright declaration.') |
| 97 | + parser.add_argument('filenames', nargs='*', help='Filenames to check') |
| 98 | + args = parser.parse_args(argv) |
| 99 | + |
| 100 | + retv = 0 |
| 101 | + for filename in args.filenames: |
| 102 | + fd = io.open(filename, encoding="utf-8") |
| 103 | + first_line = fd.readline() |
| 104 | + second_line = fd.readline() |
| 105 | + if "COPYRIGHT (C)" in first_line.upper(): continue |
| 106 | + if first_line.startswith("#!") or PYTHON_ENCODE.match( |
| 107 | + second_line) != None or PYTHON_ENCODE.match(first_line) != None: |
| 108 | + continue |
| 109 | + original_contents = io.open(filename, encoding="utf-8").read() |
| 110 | + new_contents = generate_copyright( |
| 111 | + COPYRIGHT, lang_type(filename)) + original_contents |
| 112 | + print('Auto Insert Copyright Header {}'.format(filename)) |
| 113 | + retv = 1 |
| 114 | + with io.open(filename, 'w') as output_file: |
| 115 | + output_file.write(new_contents) |
| 116 | + |
| 117 | + return retv |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == '__main__': |
| 121 | + exit(main()) |
0 commit comments