Skip to content

Commit 04efaa8

Browse files
committed
Init hooks.
1 parent e716fa7 commit 04efaa8

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed

.hooks/copyright.hook

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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) 2019 PaddlePaddle Authors. All Rights Reserved.
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+
BLANK = " "
53+
ans = LANG_COMMENT_MARK + BLANK + COPYRIGHT_HEADER + NEW_LINE_MARK
54+
for lino, line in enumerate(lines):
55+
if lino == 0 or lino == 1 or lino == len(lines) - 1: continue
56+
if len(line) == 0:
57+
BLANK = ""
58+
else:
59+
BLANK = " "
60+
ans += LANG_COMMENT_MARK + BLANK + line + NEW_LINE_MARK
61+
62+
return ans + "\n"
63+
64+
65+
def lang_type(filename):
66+
if filename.endswith(".py"):
67+
return "Python"
68+
elif filename.endswith(".h"):
69+
return "C"
70+
elif filename.endswith(".c"):
71+
return "C"
72+
elif filename.endswith(".hpp"):
73+
return "C"
74+
elif filename.endswith(".cc"):
75+
return "C"
76+
elif filename.endswith(".cpp"):
77+
return "C"
78+
elif filename.endswith(".cu"):
79+
return "C"
80+
elif filename.endswith(".cuh"):
81+
return "C"
82+
elif filename.endswith(".go"):
83+
return "C"
84+
elif filename.endswith(".proto"):
85+
return "C"
86+
else:
87+
print("Unsupported filetype %s", filename)
88+
exit(0)
89+
90+
91+
PYTHON_ENCODE = re.compile("^[ \t\v]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)")
92+
93+
94+
def main(argv=None):
95+
parser = argparse.ArgumentParser(
96+
description='Checker for copyright declaration.')
97+
parser.add_argument('filenames', nargs='*', help='Filenames to check')
98+
args = parser.parse_args(argv)
99+
100+
retv = 0
101+
for filename in args.filenames:
102+
fd = io.open(filename, encoding="utf-8")
103+
first_line = fd.readline()
104+
second_line = fd.readline()
105+
if "COPYRIGHT (C)" in first_line.upper(): continue
106+
if first_line.startswith("#!") or PYTHON_ENCODE.match(
107+
second_line) != None or PYTHON_ENCODE.match(first_line) != None:
108+
continue
109+
original_contents = io.open(filename, encoding="utf-8").read()
110+
new_contents = generate_copyright(
111+
COPYRIGHT, lang_type(filename)) + original_contents
112+
print('Auto Insert Copyright Header {}'.format(filename))
113+
retv = 1
114+
with io.open(filename, 'w') as output_file:
115+
output_file.write(new_contents)
116+
117+
return retv
118+
119+
120+
if __name__ == '__main__':
121+
exit(main())

.hooks/pylint_pre_commit.hook

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
TOTAL_ERRORS=0
4+
5+
6+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
7+
export PYTHONPATH=$DIR:$PYTHONPATH
8+
9+
# The trick to remove deleted files: https://stackoverflow.com/a/2413151
10+
for file in $(git diff --name-status | awk 'Extra open brace or missing close brace2}'); do
11+
pylint --disable=all --load-plugins=docstring_checker \
12+
--enable=doc-string-one-line,doc-string-end-with,doc-string-with-all-args,doc-string-triple-quotes,doc-string-missing,doc-string-indent-error,doc-string-with-returns,doc-string-with-raises $file;
13+
TOTAL_ERRORS=$(expr $TOTAL_ERRORS + $?);
14+
done
15+
16+
exit $TOTAL_ERRORS
17+
#For now, just warning:
18+
#exit 0

.pre-commit-config.yaml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
repos:
2+
- repo: https://github.com/Lucas-C/pre-commit-hooks.git
3+
sha: v1.0.1
4+
hooks:
5+
- id: remove-crlf
6+
files: .∗
7+
- repo: https://github.com/PaddlePaddle/mirrors-yapf.git
8+
sha: 0d79c0c469bab64f7229c9aca2b1186ef47f0e37
9+
hooks:
10+
- id: yapf
11+
files: (.*\.(py|bzl)|BUILD|.*\.BUILD|WORKSPACE)$
12+
- repo: https://github.com/pre-commit/pre-commit-hooks
13+
sha: 5bf6c09bfa1297d3692cadd621ef95f1284e33c0
14+
hooks:
15+
- id: check-added-large-files
16+
- id: check-merge-conflict
17+
- id: check-symlinks
18+
- id: detect-private-key
19+
files: .∗
20+
- id: end-of-file-fixer
21+
- repo: local
22+
hooks:
23+
- id: pylint-doc-string
24+
name: pylint
25+
description: Check python docstring style using docstring_checker.
26+
entry: bash .hooks/pylint_pre_commit.hook
27+
language: system
28+
files: \.(py)$
29+
- repo: local
30+
hooks:
31+
- id: copyright_checker
32+
name: copyright_checker
33+
entry: python .hooks/copyright.hook
34+
language: system
35+
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx|proto|py)$
36+
exclude: (?!.*third_party)^.*|(?!.∗book).∗

0 commit comments

Comments
 (0)