diff --git a/.flake8 b/.flake8 new file mode 100644 index 000000000000..a1a4693b2a4f --- /dev/null +++ b/.flake8 @@ -0,0 +1,43 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +[flake8] +select = B,C,E,F,P,T4,W,B9 +max-line-length = 120 +ignore = E203,E305 +optional-ascii-coding = True +exclude = + ./.git, + ./.github, + ./3rdparty, + ./benchmark, + ./cd, + ./ci, + ./cmake, + ./config, + ./contrib, + ./cpp-package, + ./docker, + ./docs, + ./example, + ./include, + ./licences, + ./plugin, + ./src, + ./tools, + ./build, + *.pyi diff --git a/ci/docker/runtime_functions.sh b/ci/docker/runtime_functions.sh index 84b56013c052..b83a14e504b8 100755 --- a/ci/docker/runtime_functions.sh +++ b/ci/docker/runtime_functions.sh @@ -704,6 +704,7 @@ sanity_check() { sanity_clang sanity_license sanity_python + sanity_python_flake8 sanity_cpp } @@ -712,6 +713,22 @@ sanity_license() { tools/license_header.py check } +sanity_python_flake8() { + set -ex + + # Install dependencies + pip3 install -r requirements-flake8.txt --user + + # Run flake8 + python3 -m flake8 | tee flake8-output.txt + + if [ -s flake8-output.txt ]; then + echo 'Please fix the above Flake8 warnings.' + exit 1 + fi + exit 0 +} + sanity_cpp() { set -ex 3rdparty/dmlc-core/scripts/lint.py mxnet cpp include src plugin cpp-package tests --exclude_path src/operator/contrib/ctc_include include/onednn diff --git a/requirements-flake8.txt b/requirements-flake8.txt new file mode 100644 index 000000000000..eee31f4d7eb3 --- /dev/null +++ b/requirements-flake8.txt @@ -0,0 +1,5 @@ +flake8==3.8.2 +flake8-pyi==20.5.0 +flake8-bugbear==20.1.4 +flake8-comprehensions==3.3.0 +flake8-executable==2.0.4 \ No newline at end of file diff --git a/tools/git-pre-commit b/tools/git-pre-commit index 2d697889af50..d877f6ece929 100644 --- a/tools/git-pre-commit +++ b/tools/git-pre-commit @@ -20,3 +20,6 @@ set -e echo "Running pre-commit clang-format" tools/lint/git-clang-format HEAD~ --force + +echo "Running pre-commit flake8 checker" +python tools/lint/flake8_hook.py \ No newline at end of file diff --git a/tools/lint/flake8_hook.py b/tools/lint/flake8_hook.py new file mode 100644 index 000000000000..49e2f65f9a41 --- /dev/null +++ b/tools/lint/flake8_hook.py @@ -0,0 +1,44 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +#!/usr/bin/env python3 + +import sys + +from flake8.main import git + +if __name__ == '__main__': + sys.exit( + git.hook( + # (optional): + # any value > 0 enables complexity checking with mccabe + complexity=0, + + # (optional): + # if True, this returns the total number of error which will cause + # the hook to fail + strict=True, + + # (optional): + # a comma-separated list of errors and warnings to ignore + # ignore= + + # (optional): + # allows for the instances where you don't add the the files to the + # index before running a commit, e.g git commit -a + lazy=git.config_for('lazy'), + ) + )