Skip to content

Commit 042f53b

Browse files
authored
Add static code analysis tools (#150)
1 parent da76d4b commit 042f53b

File tree

153 files changed

+2425
-3016
lines changed

Some content is hidden

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

153 files changed

+2425
-3016
lines changed

.pre-commit-config.yaml

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,47 @@
1-
- repo: https://github.com/dnephin/pre-commit-golang.git
2-
sha: v0.3.5
3-
hooks:
4-
- id: go-fmt
5-
- id: go-lint
6-
files: \.go$
7-
exclude: (.*\/client\/.*\.go|.*\generated\.deepcopy\.go)$
8-
- repo: https://github.com/Lucas-C/pre-commit-hooks.git
9-
sha: v1.0.1
1+
repos:
2+
- repo: https://github.com/psf/black
3+
rev: 19.10b0
104
hooks:
11-
- id: remove-crlf
12-
files: (?!.*third_party)^.*$ | (?!.*book)^.*$
13-
- repo: https://github.com/PaddlePaddle/mirrors-yapf.git
14-
sha: 0d79c0c469bab64f7229c9aca2b1186ef47f0e37
5+
- id: black
6+
exclude: ^(\.tools\/|example\/|k8s\/)
7+
- repo: https://github.com/pre-commit/pygrep-hooks
8+
rev: v1.5.1
159
hooks:
16-
- id: yapf
17-
files: (.*\.(py|bzl)|BUILD|.*\.BUILD|WORKSPACE)$
10+
- id: python-use-type-annotations
11+
exclude: ^(\.tools\/|example\/|k8s\/)
1812
- repo: https://github.com/pre-commit/pre-commit-hooks
19-
sha: 5bf6c09bfa1297d3692cadd621ef95f1284e33c0
13+
rev: v3.2.0
2014
hooks:
15+
- id: trailing-whitespace
16+
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx|proto|py|sh)$
17+
- id: check-docstring-first
18+
- id: check-json
2119
- id: check-added-large-files
20+
- id: debug-statements
21+
exclude: ^(\.tools\/|example\/|k8s\/)
22+
- id: requirements-txt-fixer
2223
- id: check-merge-conflict
2324
- id: check-symlinks
2425
- id: detect-private-key
25-
files: (?!.*third_party)^.*$ | (?!.*book)^.*$
2626
- id: end-of-file-fixer
2727
- repo: local
2828
hooks:
29-
- id: clang-format-with-version-check
30-
name: clang-format
31-
description: Format files with ClangFormat.
32-
entry: bash .tools/codestyle/clang_format.hook -i
29+
- id: copyright_checker
30+
name: copyright_checker
31+
entry: python .tools/codestyle/copyright.py
3332
language: system
34-
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx|proto)$
35-
- repo: local
33+
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx|proto|py|sh)$
34+
- repo: https://gitlab.com/pycqa/flake8
35+
rev: 3.8.3
3636
hooks:
37-
- id: cpplint-cpp-source
38-
name: cpplint
39-
description: Check C++ code style using cpplint.py.
40-
entry: bash .tools/codestyle/cpplint_pre_commit.hook
41-
language: system
42-
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx)$
37+
- id: flake8
38+
exclude: ^(\.tools\/|example\/|k8s\/)
39+
args: ['--max-line-length=100', '--extend-ignore=E203']
4340
- repo: local
4441
hooks:
45-
- id: copyright_checker
46-
name: copyright_checker
47-
entry: python .tools/codestyle/copyright.hook
42+
- id: shellcheck
43+
name: shellcheck
44+
entry: shellcheck
4845
language: system
49-
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx|proto|py)$
50-
exclude: (?!.*third_party)^.*$ | (?!.*book)^.*$
46+
files: .sh$
47+
exclude: ^(\.tools\/|example\/|k8s\/)

.tools/check_style.sh

Lines changed: 0 additions & 24 deletions
This file was deleted.

.tools/codestyle/copyright.hook

Lines changed: 0 additions & 121 deletions
This file was deleted.

.tools/codestyle/copyright.py

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

.tools/codestyle/cpplint_pre_commit.hook

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)