|
| 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) 2022 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 | +def _generate_copyright(comment_mark): |
| 41 | + copyright=COPYRIGHT.split(os.linesep) |
| 42 | + header = copyright[0].rstrip() |
| 43 | + |
| 44 | + p = re.search('(\d{4})', header).group(0) |
| 45 | + now = datetime.datetime.now() |
| 46 | + |
| 47 | + header = header.replace(p,str(now.year)) |
| 48 | + |
| 49 | + ans=[comment_mark + " " + header + os.linesep] |
| 50 | + for idx, line in enumerate(copyright[1:]): |
| 51 | + ans.append(comment_mark + " " + line.rstrip() + os.linesep) |
| 52 | + |
| 53 | + return ans |
| 54 | + |
| 55 | +def _get_comment_mark(path): |
| 56 | + lang_type=re.compile(r"\.(py|sh)$") |
| 57 | + if lang_type.search(path) is not None: |
| 58 | + return "#" |
| 59 | + |
| 60 | + lang_type=re.compile(r"\.(h|c|hpp|cc|cpp|cu|go|cuh|proto)$") |
| 61 | + if lang_type.search(path) is not None: |
| 62 | + return "//" |
| 63 | + |
| 64 | + return None |
| 65 | + |
| 66 | + |
| 67 | +RE_ENCODE = re.compile(r"^[ \t\v]*#.*?coding[:=]", re.IGNORECASE) |
| 68 | +RE_COPYRIGHT = re.compile(r".*Copyright( \(c\))* \d{4}", re.IGNORECASE) |
| 69 | +RE_SHEBANG = re.compile(r"^[ \t\v]*#[ \t]?\!") |
| 70 | + |
| 71 | +def _check_copyright(path): |
| 72 | + head=[] |
| 73 | + try: |
| 74 | + with open(path) as f: |
| 75 | + head = [next(f) for x in range(4)] |
| 76 | + except StopIteration: |
| 77 | + pass |
| 78 | + |
| 79 | + for idx, line in enumerate(head): |
| 80 | + if RE_COPYRIGHT.search(line) is not None: |
| 81 | + return True |
| 82 | + |
| 83 | + return False |
| 84 | + |
| 85 | +def generate_copyright(path, comment_mark): |
| 86 | + original_contents = io.open(path, encoding="utf-8").readlines() |
| 87 | + head = original_contents[0:4] |
| 88 | + |
| 89 | + insert_line_no=0 |
| 90 | + for i, line in enumerate(head): |
| 91 | + if RE_ENCODE.search(line) or RE_SHEBANG.search(line): |
| 92 | + insert_line_no=i+1 |
| 93 | + |
| 94 | + copyright = _generate_copyright(comment_mark) |
| 95 | + if insert_line_no == 0: |
| 96 | + new_contents = copyright |
| 97 | + if len(original_contents) > 0 and len(original_contents[0].strip()) != 0: |
| 98 | + new_contents.append(os.linesep) |
| 99 | + new_contents.extend(original_contents) |
| 100 | + else: |
| 101 | + new_contents=original_contents[0:insert_line_no] |
| 102 | + new_contents.append(os.linesep) |
| 103 | + new_contents.extend(copyright) |
| 104 | + if len(original_contents) > insert_line_no and len(original_contents[insert_line_no].strip()) != 0: |
| 105 | + new_contents.append(os.linesep) |
| 106 | + new_contents.extend(original_contents[insert_line_no:]) |
| 107 | + new_contents="".join(new_contents) |
| 108 | + |
| 109 | + with io.open(path, 'w') as output_file: |
| 110 | + output_file.write(new_contents) |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | +def main(argv=None): |
| 115 | + parser = argparse.ArgumentParser( |
| 116 | + description='Checker for copyright declaration.') |
| 117 | + parser.add_argument('filenames', nargs='*', help='Filenames to check') |
| 118 | + args = parser.parse_args(argv) |
| 119 | + |
| 120 | + retv = 0 |
| 121 | + for path in args.filenames: |
| 122 | + comment_mark = _get_comment_mark(path) |
| 123 | + if comment_mark is None: |
| 124 | + print("warning:Unsupported file", path, file=sys.stderr) |
| 125 | + continue |
| 126 | + |
| 127 | + if _check_copyright(path): |
| 128 | + continue |
| 129 | + |
| 130 | + generate_copyright(path, comment_mark) |
| 131 | + |
| 132 | + |
| 133 | +if __name__ == '__main__': |
| 134 | + exit(main()) |
0 commit comments