|
| 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) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 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 | + ans = LANG_COMMENT_MARK + " " + COPYRIGHT_HEADER + NEW_LINE_MARK |
| 53 | + for lino, line in enumerate(lines): |
| 54 | + if lino == 0 or lino == 1 or lino == len(lines) - 1: continue |
| 55 | + ans += LANG_COMMENT_MARK + " " + line + NEW_LINE_MARK |
| 56 | + |
| 57 | + return ans + "\n" |
| 58 | + |
| 59 | + |
| 60 | +def lang_type(filename): |
| 61 | + if filename.endswith(".py"): |
| 62 | + return "Python" |
| 63 | + elif filename.endswith(".h"): |
| 64 | + return "C" |
| 65 | + elif filename.endswith(".hpp"): |
| 66 | + return "C" |
| 67 | + elif filename.endswith(".cc"): |
| 68 | + return "C" |
| 69 | + elif filename.endswith(".cpp"): |
| 70 | + return "C" |
| 71 | + elif filename.endswith(".cu"): |
| 72 | + return "C" |
| 73 | + elif filename.endswith(".cuh"): |
| 74 | + return "C" |
| 75 | + elif filename.endswith(".go"): |
| 76 | + return "C" |
| 77 | + elif filename.endswith(".proto"): |
| 78 | + return "C" |
| 79 | + else: |
| 80 | + print("Unsupported filetype") |
| 81 | + exit(0) |
| 82 | + |
| 83 | + |
| 84 | +def main(argv=None): |
| 85 | + parser = argparse.ArgumentParser( |
| 86 | + description='Checker for copyright declaration.') |
| 87 | + parser.add_argument('filenames', nargs='*', help='Filenames to check') |
| 88 | + args = parser.parse_args(argv) |
| 89 | + |
| 90 | + retv = 0 |
| 91 | + for filename in args.filenames: |
| 92 | + first_line = io.open(filename).readline() |
| 93 | + if "COPYRIGHT" in first_line.upper() : continue |
| 94 | + original_contents = io.open(filename).read() |
| 95 | + new_contents = generate_copyright( |
| 96 | + COPYRIGHT, lang_type(filename)) + original_contents |
| 97 | + print('Auto Insert Copyright Header {}'.format(filename)) |
| 98 | + retv = 1 |
| 99 | + with io.open(filename, 'w') as output_file: |
| 100 | + output_file.write(new_contents) |
| 101 | + |
| 102 | + return retv |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == '__main__': |
| 106 | + exit(main()) |
0 commit comments