Skip to content

Commit 4237fef

Browse files
authored
Add shellcheck tools and modify copyright hook (#27722)
1 parent af57537 commit 4237fef

File tree

5 files changed

+121
-85
lines changed

5 files changed

+121
-85
lines changed

.pre-commit-config.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,12 @@ repos:
4848
name: copyright_checker
4949
entry: python ./tools/codestyle/copyright.hook
5050
language: system
51-
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx|proto|py)$
51+
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx|proto|py|sh)$
5252
exclude: (?!.*third_party)^.*$ | (?!.*book)^.*$
53+
- repo: local
54+
hooks:
55+
- id: shellcheck
56+
name: shellcheck
57+
entry: shellcheck
58+
language: system
59+
files: .sh$

paddle/fluid/train/demo/clean.sh

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
1+
#!/bin/bash
2+
3+
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
117
set -x
2-
cd `dirname $0`
18+
cd "$(dirname "$0")"
319
rm -rf build/
420
set +x

tools/codestyle/copyright.hook

Lines changed: 94 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
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+
115
from __future__ import absolute_import
216
from __future__ import print_function
317
from __future__ import unicode_literals
418

519
import argparse
6-
import io, re
7-
import sys, os
8-
import subprocess
9-
import platform
20+
import io
21+
import re
22+
import sys
23+
import os
24+
import datetime
1025

11-
COPYRIGHT = '''
12-
Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
26+
COPYRIGHT = '''Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
1327

1428
Licensed under the Apache License, Version 2.0 (the "License");
1529
you may not use this file except in compliance with the License.
@@ -21,74 +35,80 @@ Unless required by applicable law or agreed to in writing, software
2135
distributed under the License is distributed on an "AS IS" BASIS,
2236
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2337
See the License for the specific language governing permissions and
24-
limitations under the License.
25-
'''
38+
limitations under the License.'''
2639

27-
LANG_COMMENT_MARK = None
40+
def _generate_copyright(comment_mark):
41+
copyright=COPYRIGHT.split(os.linesep)
42+
header = copyright[0].rstrip()
2843

29-
NEW_LINE_MARK = None
44+
p = re.search('(\d{4})', header).group(0)
45+
now = datetime.datetime.now()
3046

31-
COPYRIGHT_HEADER = None
47+
header = header.replace(p,str(now.year))
3248

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)
49+
ans=[comment_mark + " " + header + os.linesep]
50+
for idx, line in enumerate(copyright[1:]):
51+
ans.append(comment_mark + " " + line.rstrip() + os.linesep)
4352

53+
return ans
4454

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"
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)
86100
else:
87-
print("Unsupported filetype %s", filename)
88-
exit(0)
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)
89108

109+
with io.open(path, 'w') as output_file:
110+
output_file.write(new_contents)
90111

91-
PYTHON_ENCODE = re.compile("^[ \t\v]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)")
92112

93113

94114
def main(argv=None):
@@ -98,23 +118,16 @@ def main(argv=None):
98118
args = parser.parse_args(argv)
99119

100120
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:
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)
108125
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
126+
127+
if _check_copyright(path):
128+
continue
129+
130+
generate_copyright(path, comment_mark)
118131

119132

120133
if __name__ == '__main__':

tools/dockerfile/Dockerfile.ubuntu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ RUN apt-get update && \
2929
python-matplotlib \
3030
automake locales clang-format swig \
3131
liblapack-dev liblapacke-dev \
32-
net-tools libtool module-init-tools && \
32+
net-tools libtool module-init-tools shellcheck && \
3333
apt-get clean -y
3434

3535
# Downgrade gcc&&g++

tools/manylinux1/Dockerfile.cuda10_cudnn7_gcc8_ubuntu16

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ RUN rm -rf /temp_gcc82 && rm -rf /gcc-8.2.0.tar.xz && rm -rf /gcc-8.2.0
2626
RUN apt-get update && \
2727
apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
2828
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
29-
xz-utils tk-dev libffi-dev liblzma-dev openmpi-bin openmpi-doc libopenmpi-dev
29+
xz-utils tk-dev libffi-dev liblzma-dev openmpi-bin openmpi-doc libopenmpi-dev shellcheck
3030

3131
# gcc8.2
3232
RUN wget -q https://paddle-docker-tar.bj.bcebos.com/home/users/tianshuo/bce-python-sdk-0.8.27/gcc-8.2.0.tar.xz && \

0 commit comments

Comments
 (0)