From cdd3206f8d20a2cc59138e257786f2179d1dc8cd Mon Sep 17 00:00:00 2001 From: Jared Roesch Date: Sun, 13 Sep 2020 16:04:38 -0700 Subject: [PATCH] [RFC][Formatting] Add scripts for applying Black to the Python code. (#6437) --- Jenkinsfile | 2 +- pyproject.toml | 5 +++- tests/lint/git-black.sh | 61 +++++++++++++++++++++++++++++++++++++++++++++ tests/lint/python_format.sh | 21 ++++++++++++++++ tests/scripts/task_lint.sh | 17 ++++++++----- 5 files changed, 98 insertions(+), 8 deletions(-) create mode 100755 tests/lint/git-black.sh create mode 100644 tests/lint/python_format.sh diff --git a/Jenkinsfile b/Jenkinsfile index 3ce5b60..f6faf25 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -44,7 +44,7 @@ // // NOTE: these lines are scanned by docker/dev_common.sh. Please update the regex as needed. --> -ci_lint = "tlcpack/ci-lint:v0.61" +ci_lint = "tlcpack/ci-lint:v0.62" ci_gpu = "tlcpack/ci-gpu:v0.64" ci_cpu = "tlcpack/ci-cpu:v0.65" ci_wasm = "tlcpack/ci-wasm:v0.60" diff --git a/pyproject.toml b/pyproject.toml index 6c8cfdc..2ea6321 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,6 +43,9 @@ exclude = ''' | src\/ | vta\/ | web\/ - )/|tests/lint/add_asf_header.py|tests/lint/check_file_type.py|python/tvm/topi/testing/pool3d_python.py|python/topi/python/test_topi_pooling.py + )/|python/topi/python/test_topi_pooling.py + |python/tvm/topi/testing/pool3d_python.py + |tests/lint/add_asf_header.py + |tests/lint/check_file_type.py ) ''' diff --git a/tests/lint/git-black.sh b/tests/lint/git-black.sh new file mode 100755 index 0000000..21bec69 --- /dev/null +++ b/tests/lint/git-black.sh @@ -0,0 +1,61 @@ +#!/bin/bash +# 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. +set -e +set -u +set -o pipefail + +if [[ "$1" == "-i" ]]; then + INPLACE_FORMAT=1 + shift 1 +else + INPLACE_FORMAT=0 +fi + +if [[ "$#" -lt 1 ]]; then + echo "Usage: tests/lint/git-black.sh [-i] " + echo "" + echo "Run black on Python files that changed since " + echo "Examples:" + echo "- Compare last one commit: tests/lint/git-black.sh HEAD~1" + echo "- Compare against upstream/master: tests/lint/git-black.sh upstream/master" + echo "The -i will use black to format files in-place instead of checking them." + exit 1 +fi + +if [ ! -x "$(command -v black)" ]; then + echo "Cannot find black" + exit 1 +fi + +# Print out specific version +echo "Version Information: $(black --version)" + +# Compute Python files which changed to compare. +IFS=$'\n' read -a FILES -d'\n' < <(git diff --name-only HEAD $1 -- "*.py" "*.pyi") || true +echo "read returned $?" +echo "Files: $FILES" + +if [[ ${INPLACE_FORMAT} -eq 1 ]]; then + echo "Running black on Python files against revision" $1): + CMD=( "black" "${FILES[@]}" ) + echo "${CMD[@]}" + "${CMD[@]}" +else + echo "Running black in checking mode" + black --diff --check +fi diff --git a/tests/lint/python_format.sh b/tests/lint/python_format.sh new file mode 100644 index 0000000..752abfd --- /dev/null +++ b/tests/lint/python_format.sh @@ -0,0 +1,21 @@ +#!/bin/bash -e +# 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. + + +./tests/lint/git-black.sh HEAD~1 +./tests/lint/git-black.sh origin/master diff --git a/tests/scripts/task_lint.sh b/tests/scripts/task_lint.sh index 7ac0611..f7f2427 100755 --- a/tests/scripts/task_lint.sh +++ b/tests/scripts/task_lint.sh @@ -27,22 +27,27 @@ cleanup() trap cleanup 0 -echo "Check file types..." +echo "Checking file types..." python3 tests/lint/check_file_type.py -echo "Check ASF license header..." +echo "Checking ASF license headers..." tests/lint/check_asf_header.sh -echo "Check codestyle of c++ code..." +echo "Linting the C++ code..." tests/lint/cpplint.sh echo "clang-format check..." tests/lint/clang_format.sh -echo "Check codestyle of python code..." +# TODO(@jroesch): enable black check +# echo "black check..." +# tests/lint/python_format.sh + +echo "Linting the Python code..." tests/lint/pylint.sh -echo "Check codestyle of jni code..." + +echo "Lintinf the JNI code..." tests/lint/jnilint.sh -echo "Check documentations of c++ code..." +echo "Checking C++ documentation..." tests/lint/cppdocs.sh -- 2.7.4