Skip to content

Commit 6f6e82a

Browse files
committed
refine pre-commit
1 parent 5747a4a commit 6f6e82a

File tree

616 files changed

+42663
-50097
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

616 files changed

+42663
-50097
lines changed

.copyright.hook

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Copyright (c) 2023 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) 2023 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())

.flake8

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[flake8]
2+
ignore = E203, E402, E501, E731, E741, W503, W605, E722
3+
max-line-length = 119
4+
5+
# E402: module level import not at top of file
6+
per-file-ignores =
7+
__init__.py:F401,F403,E402

.pre-commit-config.yaml

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
1-
- repo: https://github.com/PaddlePaddle/mirrors-yapf.git
2-
sha: 0d79c0c469bab64f7229c9aca2b1186ef47f0e37
1+
repos:
2+
# For Python files
3+
- repo: https://github.com/psf/black.git
4+
rev: 22.8.0
35
hooks:
4-
- id: yapf
5-
files: \.py$
6+
- id: black
7+
files: \.(py|pyi)$
8+
additional_dependencies: [toml]
9+
- repo: https://github.com/PyCQA/isort
10+
rev: 5.11.5
11+
hooks:
12+
- id: isort
13+
- repo: https://github.com/PyCQA/flake8
14+
rev: 4.0.1
15+
hooks:
16+
- id: flake8
617
- repo: https://github.com/pre-commit/pre-commit-hooks
7-
sha: a11d9314b22d8f8c7556443875b731ef05965464
18+
rev: v4.1.0
819
hooks:
920
- id: check-merge-conflict
1021
- id: check-symlinks
1122
- id: detect-private-key
1223
files: (?!.*paddle)^.*$
1324
- id: end-of-file-fixer
14-
files: \.(md|yml)$
25+
files: \.md$
1526
- id: trailing-whitespace
16-
files: \.(md|yml)$
27+
files: \.md$
1728
- repo: https://github.com/Lucas-C/pre-commit-hooks
18-
sha: v1.0.1
29+
rev: v1.1.14
1930
hooks:
2031
- id: forbid-crlf
21-
files: \.(md|yml)$
32+
files: \.md$
2233
- id: remove-crlf
23-
files: \.(md|yml)$
34+
files: \.md$
2435
- id: forbid-tabs
25-
files: \.(md|yml)$
36+
files: \.md$
2637
- id: remove-tabs
27-
files: \.(md|yml)$
28-
- repo: local
29-
hooks:
30-
- id: clang-format-with-version-check
31-
name: clang-format
32-
description: Format files with ClangFormat.
33-
entry: bash ./.travis/codestyle/clang_format.hook -i
34-
language: system
35-
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx|proto)$
36-
38+
files: \.md$
3739
- repo: local
3840
hooks:
39-
- id: cpplint-cpp-source
40-
name: cpplint
41-
description: Check C++ code style using cpplint.py.
42-
entry: bash ./.travis/codestyle/cpplint_pre_commit.hook
41+
- id: copyright_checker
42+
name: copyright_checker
43+
entry: python .copyright.hook
4344
language: system
44-
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx)$
45+
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx|proto|xpu|kps|py|sh)$

0 commit comments

Comments
 (0)