|  | 
|  | 1 | +# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. | 
|  | 2 | +# | 
|  | 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 4 | +# you may not use this file except in compliance with the License. | 
|  | 5 | +# You may obtain a copy of the License at | 
|  | 6 | +# | 
|  | 7 | +# http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 8 | +# | 
|  | 9 | +# Unless required by applicable law or agreed to in writing, software | 
|  | 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 12 | +# See the License for the specific language governing permissions and | 
|  | 13 | +# limitations under the License. | 
|  | 14 | + | 
|  | 15 | +from __future__ import absolute_import | 
|  | 16 | +from __future__ import print_function | 
|  | 17 | +from __future__ import unicode_literals | 
|  | 18 | + | 
|  | 19 | +import argparse | 
|  | 20 | +import io | 
|  | 21 | +import re | 
|  | 22 | +import sys | 
|  | 23 | +import os | 
|  | 24 | +import datetime | 
|  | 25 | + | 
|  | 26 | +COPYRIGHT = '''Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. | 
|  | 27 | +
 | 
|  | 28 | +Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 29 | +you may not use this file except in compliance with the License. | 
|  | 30 | +You may obtain a copy of the License at | 
|  | 31 | +
 | 
|  | 32 | + http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 33 | +
 | 
|  | 34 | +Unless required by applicable law or agreed to in writing, software | 
|  | 35 | +distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 36 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 37 | +See the License for the specific language governing permissions and | 
|  | 38 | +limitations under the License. | 
|  | 39 | +''' | 
|  | 40 | + | 
|  | 41 | +def _generate_copyright(comment_mark): | 
|  | 42 | + copyright = COPYRIGHT.splitlines(keepends=True) | 
|  | 43 | + header = copyright[0].rstrip() | 
|  | 44 | + | 
|  | 45 | + p = re.search('(\d{4})', header).group(0) | 
|  | 46 | + now = datetime.datetime.now() | 
|  | 47 | + | 
|  | 48 | + header = header.replace(p,str(now.year)) | 
|  | 49 | + | 
|  | 50 | + ans=[comment_mark + " " + header + os.linesep] | 
|  | 51 | + for idx, line in enumerate(copyright[1:]): | 
|  | 52 | + ans.append(comment_mark + " " + line.rstrip() + os.linesep) | 
|  | 53 | + | 
|  | 54 | + return ans | 
|  | 55 | + | 
|  | 56 | +def _get_comment_mark(path): | 
|  | 57 | + lang_type=re.compile(r"\.(py|sh)$") | 
|  | 58 | + if lang_type.search(path) is not None: | 
|  | 59 | + return "#" | 
|  | 60 | + | 
|  | 61 | + lang_type=re.compile(r"\.(h|c|hpp|cc|cpp|cu|go|cuh|proto)$") | 
|  | 62 | + if lang_type.search(path) is not None: | 
|  | 63 | + return "//" | 
|  | 64 | + | 
|  | 65 | + return None | 
|  | 66 | + | 
|  | 67 | + | 
|  | 68 | +RE_ENCODE = re.compile(r"^[ \t\v]*#.*?coding[:=]", re.IGNORECASE) | 
|  | 69 | +RE_COPYRIGHT = re.compile(r".*Copyright \(c\) \d{4}", re.IGNORECASE) | 
|  | 70 | +RE_SHEBANG = re.compile(r"^[ \t\v]*#[ \t]?\!") | 
|  | 71 | + | 
|  | 72 | +def _check_copyright(path): | 
|  | 73 | + head=[] | 
|  | 74 | + try: | 
|  | 75 | + with open(path) as f: | 
|  | 76 | + head = [next(f) for x in range(4)] | 
|  | 77 | + except StopIteration: | 
|  | 78 | + pass | 
|  | 79 | + | 
|  | 80 | + for idx, line in enumerate(head): | 
|  | 81 | + if RE_COPYRIGHT.search(line) is not None: | 
|  | 82 | + return True | 
|  | 83 | + | 
|  | 84 | + return False | 
|  | 85 | + | 
|  | 86 | +def generate_copyright(path, comment_mark): | 
|  | 87 | + original_contents = io.open(path, encoding="utf-8").readlines() | 
|  | 88 | + head = original_contents[0:4] | 
|  | 89 | + | 
|  | 90 | + insert_line_no=0 | 
|  | 91 | + for i, line in enumerate(head): | 
|  | 92 | + if RE_ENCODE.search(line) or RE_SHEBANG.search(line): | 
|  | 93 | + insert_line_no=i+1 | 
|  | 94 | + | 
|  | 95 | + copyright = _generate_copyright(comment_mark) | 
|  | 96 | + if insert_line_no == 0: | 
|  | 97 | + new_contents = copyright | 
|  | 98 | + if len(original_contents) > 0 and len(original_contents[0].strip()) != 0: | 
|  | 99 | + new_contents.append(os.linesep) | 
|  | 100 | + new_contents.extend(original_contents) | 
|  | 101 | + else: | 
|  | 102 | + new_contents=original_contents[0:insert_line_no] | 
|  | 103 | + new_contents.append(os.linesep) | 
|  | 104 | + new_contents.extend(copyright) | 
|  | 105 | + if len(original_contents) > insert_line_no and len(original_contents[insert_line_no].strip()) != 0: | 
|  | 106 | + new_contents.append(os.linesep) | 
|  | 107 | + new_contents.extend(original_contents[insert_line_no:]) | 
|  | 108 | + new_contents="".join(new_contents) | 
|  | 109 | + | 
|  | 110 | + with io.open(path, 'w') as output_file: | 
|  | 111 | + output_file.write(new_contents) | 
|  | 112 | + | 
|  | 113 | + | 
|  | 114 | + | 
|  | 115 | +def main(argv=None): | 
|  | 116 | + parser = argparse.ArgumentParser( | 
|  | 117 | + description='Checker for copyright declaration.') | 
|  | 118 | + parser.add_argument('filenames', nargs='*', help='Filenames to check') | 
|  | 119 | + args = parser.parse_args(argv) | 
|  | 120 | + | 
|  | 121 | + retv = 0 | 
|  | 122 | + for path in args.filenames: | 
|  | 123 | + comment_mark = _get_comment_mark(path) | 
|  | 124 | + if comment_mark is None: | 
|  | 125 | + print("warning:Unsupported file", path, file=sys.stderr) | 
|  | 126 | + continue | 
|  | 127 | + | 
|  | 128 | + if _check_copyright(path): | 
|  | 129 | + continue | 
|  | 130 | + | 
|  | 131 | + generate_copyright(path, comment_mark) | 
|  | 132 | + | 
|  | 133 | + | 
|  | 134 | +if __name__ == '__main__': | 
|  | 135 | + exit(main()) | 
0 commit comments