Skip to content

Commit

Permalink
[CodeStyle] Use right ext for pre-commit hooks and refine copyright h…
Browse files Browse the repository at this point in the history
  • Loading branch information
SigureMo authored and co63oc committed Apr 10, 2024
1 parent d4e30bb commit 0039a2e
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 61 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ repos:
hooks:
- id: copyright_checker
name: copyright_checker
entry: python ./tools/codestyle/copyright.hook
entry: python ./tools/codestyle/copyright.py
language: system
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx|proto|xpu|kps|py|sh)$
exclude: |
Expand All @@ -67,15 +67,15 @@ repos:
- id: clang-format
name: clang-format
description: Format files with ClangFormat.
entry: bash ./tools/codestyle/clang_format.hook -i
entry: bash ./tools/codestyle/clang_format.sh -i
language: system
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx|xpu|kps)$
- repo: local
hooks:
- id: cpplint-cpp-source
name: cpplint
description: Check C++ code style using cpplint.py.
entry: bash ./tools/codestyle/cpplint_pre_commit.hook
entry: bash ./tools/codestyle/cpplint_pre_commit.sh
language: system
files: \.(cc|cxx|cpp|cu|h|hpp|hxx)$
args:
Expand Down
20 changes: 0 additions & 20 deletions tools/codestyle/clang_format.hook

This file was deleted.

35 changes: 35 additions & 0 deletions tools/codestyle/clang_format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

readonly VERSION="13.0.0"

version=$(clang-format -version)

if ! [[ $(python -V 2>&1 | awk '{print $2}' | awk -F '.' '{print $1$2}') -ge 36 ]]; then
echo "clang-format installation by pip need python version great equal 3.6,
please change the default python to higher version."
exit 1
fi

if ! [[ $version == *"$VERSION"* ]]; then
# low version of pip may not have the source of clang-format whl
pip install --upgrade pip
pip install clang-format==13.0.0
fi

clang-format $@
62 changes: 35 additions & 27 deletions tools/codestyle/copyright.hook → tools/codestyle/copyright.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -13,12 +13,12 @@
# limitations under the License.

import argparse
import datetime
import os
import re
import sys
import os
import datetime

COPYRIGHT = '''Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
COPYRIGHT = '''Copyright (c) {year} PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -32,27 +32,27 @@
See the License for the specific language governing permissions and
limitations under the License.'''

def _generate_copyright(comment_mark):
copyright=COPYRIGHT.split(os.linesep)
header = copyright[0].rstrip()

p = re.search(r'(\d{4})', header).group(0)
now = datetime.datetime.now()

header = header.replace(p,str(now.year))
def _generate_copyright(comment_mark):
year = datetime.datetime.now().year
copyright = COPYRIGHT.format(year=year)

ans=[comment_mark + " " + header + os.linesep]
for idx, line in enumerate(copyright[1:]):
ans.append(comment_mark + " " + line.rstrip() + os.linesep)
return [
(
f"{comment_mark} {line}{os.linesep}"
if line
else f"{comment_mark}{os.linesep}"
)
for line in copyright.splitlines()
]

return ans

def _get_comment_mark(path):
lang_type=re.compile(r"\.(py|sh)$")
lang_type = re.compile(r"\.(py|sh)$")
if lang_type.search(path) is not None:
return "#"

lang_type=re.compile(r"\.(h|c|hpp|cc|cpp|cu|go|cuh|proto)$")
lang_type = re.compile(r"\.(h|c|hpp|cc|cpp|cu|go|cuh|proto)$")
if lang_type.search(path) is not None:
return "//"

Expand All @@ -63,8 +63,9 @@ def _get_comment_mark(path):
RE_COPYRIGHT = re.compile(r".*Copyright \(c\) \d{4}", re.IGNORECASE)
RE_SHEBANG = re.compile(r"^[ \t\v]*#[ \t]?\!")


def _check_copyright(path):
head=[]
head = []
try:
with open(path, 'r', encoding='utf-8') as f:
head = [next(f) for x in range(4)]
Expand All @@ -77,38 +78,45 @@ def _check_copyright(path):

return False


def generate_copyright(path, comment_mark):
original_contents = open(path, 'r', encoding="utf-8").readlines()
head = original_contents[0:4]

insert_line_no=0
insert_line_no = 0
for i, line in enumerate(head):
if RE_ENCODE.search(line) or RE_SHEBANG.search(line):
insert_line_no=i+1
insert_line_no = i + 1

copyright = _generate_copyright(comment_mark)
if insert_line_no == 0:
new_contents = copyright
if len(original_contents) > 0 and len(original_contents[0].strip()) != 0:
if (
len(original_contents) > 0
and len(original_contents[0].strip()) != 0
):
new_contents.append(os.linesep)
new_contents.extend(original_contents)
else:
new_contents=original_contents[0:insert_line_no]
new_contents = original_contents[0:insert_line_no]
new_contents.append(os.linesep)
new_contents.extend(copyright)
if len(original_contents) > insert_line_no and len(original_contents[insert_line_no].strip()) != 0:
if (
len(original_contents) > insert_line_no
and len(original_contents[insert_line_no].strip()) != 0
):
new_contents.append(os.linesep)
new_contents.extend(original_contents[insert_line_no:])
new_contents="".join(new_contents)
new_contents = "".join(new_contents)

with open(path, 'w', encoding='utf-8') as output_file:
output_file.write(new_contents)



def main(argv=None):
parser = argparse.ArgumentParser(
description='Checker for copyright declaration.')
description='Checker for copyright declaration.'
)
parser.add_argument('filenames', nargs='*', help='Filenames to check')
args = parser.parse_args(argv)

Expand All @@ -126,4 +134,4 @@ def main(argv=None):


if __name__ == '__main__':
exit(main())
sys.exit(main())
11 changes: 0 additions & 11 deletions tools/codestyle/cpplint_pre_commit.hook

This file was deleted.

25 changes: 25 additions & 0 deletions tools/codestyle/cpplint_pre_commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

readonly VERSION="1.6.0"

version=$(cpplint --version)

if ! [[ $version == *"$VERSION"* ]]; then
pip install cpplint==1.6.0
fi

cpplint $@

0 comments on commit 0039a2e

Please sign in to comment.