diff --git a/.clang-format b/.clang-format index 98c6497..bf96a59 100644 --- a/.clang-format +++ b/.clang-format @@ -2,6 +2,7 @@ BasedOnStyle: Google IndentWidth: 2 +ColumnLimit: 80 ContinuationIndentWidth: 4 UseTab: Never MaxEmptyLinesToKeep: 2 @@ -34,4 +35,4 @@ BinPackArguments: true BinPackParameters: true ConstructorInitializerAllOnOneLineOrOnePerLine: false -IndentCaseLabels: true \ No newline at end of file +IndentCaseLabels: true diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml deleted file mode 100644 index a724718..0000000 --- a/.github/workflows/codeql.yml +++ /dev/null @@ -1,90 +0,0 @@ -# Copyright 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions -# are met: -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above copyright -# notice, this list of conditions and the following disclaimer in the -# documentation and/or other materials provided with the distribution. -# * Neither the name of NVIDIA CORPORATION nor the names of its -# contributors may be used to endorse or promote products derived -# from this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY -# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR -# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY -# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -name: "CodeQL" - -on: - push: - branches: [ 'main' ] - pull_request: - # The branches below must be a subset of the branches above - branches: [ 'main' ] - schedule: - - cron: '0 1 * * 1-6' - -jobs: - analyze: - name: Analyze - runs-on: ubuntu-latest - permissions: - actions: read - contents: read - security-events: write - - strategy: - fail-fast: false - matrix: - language: [ 'python' ] - # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] - # Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support - - steps: - - name: Checkout repository - uses: actions/checkout@v3 - - # Initializes the CodeQL tools for scanning. - - name: Initialize CodeQL - uses: github/codeql-action/init@v2 - with: - languages: ${{ matrix.language }} - # If you wish to specify custom queries, you can do so here or in a config file. - # By default, queries listed here will override any specified in a config file. - # Prefix the list here with "+" to use these queries and those in the config file. - - # Details on CodeQL's query packs refer to: - # https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs - queries: +security-and-quality - - - # Autobuild attempts to build any compiled languages (C/C++, C#, Go, or Java). - # If this step fails, then you should remove it and run the build manually (see below) - - name: Autobuild - uses: github/codeql-action/autobuild@v2 - - # Command-line programs to run using the OS shell. - # See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun - - # If the Autobuild fails above, remove it and uncomment the following three lines. - # modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance. - - # - run: | - # echo "Run, Build Application using script" - # ./location_of_script_within_repo/buildscript.sh - - - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v2 - with: - category: "/language:${{matrix.language}}" diff --git a/tools/pre-commit b/.github/workflows/pre-commit.yml old mode 100755 new mode 100644 similarity index 57% rename from tools/pre-commit rename to .github/workflows/pre-commit.yml index 5e8ba37..ab4bd95 --- a/tools/pre-commit +++ b/.github/workflows/pre-commit.yml @@ -1,5 +1,4 @@ -#!/bin/bash -# Copyright (c) 2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# Copyright 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions @@ -25,32 +24,15 @@ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -############################################################################### -# -# Git pre-commit hook for Triton related projects -# -# To install this hook for a project, copy "pre-commit" and "format.py" into -# ".git/hooks/" directory of the project -# -############################################################################### - -############################################################################### -# -# Run formatter script -# -############################################################################### - -# Repo root -GIT_REPO_ROOT=$(git rev-parse --show-toplevel) - -PYTHON_CMD=python3 -FORMATTER_PY=${GIT_REPO_ROOT}/.git/hooks/format.py +name: pre-commit -CHANGED_FILES="$(git --no-pager diff --name-status --no-color --cached | awk '{ if (match($1, /R[0-9]+/)) { print $3 } else if ($1 != "D") { print $2 } }')" +on: + pull_request: -echo "Running Python auto-format..." -for CHANGED_FILE in $CHANGED_FILES; -do - ${PYTHON_CMD} ${FORMATTER_PY} ${GIT_REPO_ROOT}/${CHANGED_FILE} - git add ${GIT_REPO_ROOT}/${CHANGED_FILE} -done +jobs: + pre-commit: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v3 + - uses: pre-commit/action@v3.0.0 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..298baab --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,73 @@ +# Copyright 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of NVIDIA CORPORATION nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY +# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +repos: +- repo: https://github.com/timothycrosley/isort + rev: 5.12.0 + hooks: + - id: isort + additional_dependencies: [toml] +- repo: https://github.com/psf/black + rev: 23.1.0 + hooks: + - id: black + types_or: [python, cython] +- repo: https://github.com/PyCQA/flake8 + rev: 5.0.4 + hooks: + - id: flake8 + args: [--max-line-length=88, --select=C,E,F,W,B,B950, --extend-ignore = E203,E501] + types_or: [python, cython] +- repo: https://github.com/pre-commit/mirrors-clang-format + rev: v16.0.5 + hooks: + - id: clang-format + types_or: [c, c++, cuda, proto, textproto, java] + args: ["-fallback-style=none", "-style=file", "-i"] +- repo: https://github.com/codespell-project/codespell + rev: v2.2.4 + hooks: + - id: codespell + additional_dependencies: [tomli] + args: ["--toml", "pyproject.toml"] + exclude: (?x)^(.*stemmer.*|.*stop_words.*|^CHANGELOG.md$) +# More details about these pre-commit hooks here: +# https://pre-commit.com/hooks.html +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.4.0 + hooks: + - id: check-case-conflict + - id: check-executables-have-shebangs + - id: check-merge-conflict + - id: check-json + - id: check-toml + - id: check-yaml + - id: check-shebang-scripts-are-executable + - id: end-of-file-fixer + types_or: [c, c++, cuda, proto, textproto, java, python] + - id: mixed-line-ending + - id: requirements-txt-fixer + - id: trailing-whitespace diff --git a/cmake/TritonCommonConfig.cmake.in b/cmake/TritonCommonConfig.cmake.in index 56cb146..7f4d28a 100644 --- a/cmake/TritonCommonConfig.cmake.in +++ b/cmake/TritonCommonConfig.cmake.in @@ -43,7 +43,7 @@ check_required_components(triton-common-json triton-common-thread-pool ) -set(TRITONCOMMON_LIBRARIES +set(TRITONCOMMON_LIBRARIES TritonCommon::triton-common-json TritonCommon::triton-common-sync-queue TritonCommon::triton-common-async-work-queue diff --git a/include/triton/common/model_config.h b/include/triton/common/model_config.h index dee305f..c0fda25 100644 --- a/include/triton/common/model_config.h +++ b/include/triton/common/model_config.h @@ -71,28 +71,28 @@ enum Platform { /// \param dims The shape. /// \return The number of elements, or -1 if the number of elements /// cannot be determined because the shape contains one or more -/// wilcard dimensions. +/// wildcard dimensions. int64_t GetElementCount(const DimsList& dims); /// Get the number of elements in a shape. /// \param dims The shape. /// \return The number of elements, or -1 if the number of elements /// cannot be determined because the shape contains one or more -/// wilcard dimensions. +/// wildcard dimensions. int64_t GetElementCount(const std::vector& dims); /// Get the number of elements in the shape of a model input. /// \param mio The model input. /// \return The number of elements, or -1 if the number of elements /// cannot be determined because the shape contains one or more -/// wilcard dimensions. +/// wildcard dimensions. int64_t GetElementCount(const inference::ModelInput& mio); /// Get the number of elements in the shape of a model output. /// \param mio The model output. /// \return The number of elements, or -1 if the number of elements /// cannot be determined because the shape contains one or more -/// wilcard dimensions. +/// wildcard dimensions. int64_t GetElementCount(const inference::ModelOutput& mio); /// Are values of a datatype fixed-size, or variable-sized. diff --git a/include/triton/common/triton_json.h b/include/triton/common/triton_json.h index ec3bb7e..7a1b052 100644 --- a/include/triton/common/triton_json.h +++ b/include/triton/common/triton_json.h @@ -373,7 +373,7 @@ class TritonJson { return TRITONJSON_STATUSSUCCESS; } - // Add a reference to a expicit-length string as a new member to + // Add a reference to a explicit-length string as a new member to // this value. It is assumed that 'name' and 'value' can be used // by reference, it is the caller's responsibility to make sure // the lifetime of 'name' and 'value' extend at least as long as @@ -531,7 +531,7 @@ class TritonJson { return TRITONJSON_STATUSSUCCESS; } - // Append a reference to a expicit-length string to this value, + // Append a reference to a explicit-length string to this value, // which must be an array. It is assumed that 'value' can be used // by reference, it is the caller's responsibility to make sure // the lifetime of 'value' extends at least as long as the object. @@ -688,7 +688,7 @@ class TritonJson { return false; } - // Whether the object is null value. Note that false will also be retuned + // Whether the object is null value. Note that false will also be returned // if the object is not a JSON value. bool IsNull() const { return ((value_ != nullptr) && value_->IsNull()); } diff --git a/protobuf/grpc_service.proto b/protobuf/grpc_service.proto index cebd475..3e01c0c 100644 --- a/protobuf/grpc_service.proto +++ b/protobuf/grpc_service.proto @@ -994,7 +994,7 @@ message InferStatistics //@@ cache. On a cache miss, the request hash and response output tensor //@@ data is added to the cache. See response cache docs for more info: //@@ - //https://github.com/triton-inference-server/server/blob/main/docs/response_cache.md + // https://github.com/triton-inference-server/server/blob/main/docs/response_cache.md //@@ StatisticDuration cache_hit = 7; @@ -1002,7 +1002,7 @@ message InferStatistics //@@ //@@ The count of response cache misses and cumulative duration to lookup //@@ and insert output tensor data from the computed response to the - //cache. + // cache. //@@ For example, this duration should include the time to copy //@@ output tensor data from the response object to the Response Cache. //@@ Assuming the response cache is enabled for a given model, a cache @@ -1010,7 +1010,7 @@ message InferStatistics //@@ does NOT hash to an existing entry in the cache. See the response //@@ cache docs for more info: //@@ - //https://github.com/triton-inference-server/server/blob/main/docs/response_cache.md + // https://github.com/triton-inference-server/server/blob/main/docs/response_cache.md //@@ StatisticDuration cache_miss = 8; } diff --git a/protobuf/model_config.proto b/protobuf/model_config.proto index 7ff2f2d..0cc601b 100644 --- a/protobuf/model_config.proto +++ b/protobuf/model_config.proto @@ -118,7 +118,7 @@ message ModelRateLimiter //@@ //@@ The resources required to execute the request on a model instance. //@@ Resources are just names with a corresponding count. The execution - //@@ of the instance will be blocked until the specificied resources are + //@@ of the instance will be blocked until the specified resources are //@@ available. By default an instance uses no rate-limiter resources. //@@ repeated Resource resources = 1; @@ -252,7 +252,7 @@ message ModelInstanceGroup //@@ //@@ GPU(s) where instances should be available. For each GPU listed, //@@ 'count' instances of the model will be available. Setting 'gpus' - //@@ to empty (or not specifying at all) is eqivalent to listing all + //@@ to empty (or not specifying at all) is equivalent to listing all //@@ available GPUs. //@@ repeated int32 gpus = 3; @@ -439,7 +439,7 @@ message ModelOutput //@@ .. cpp:var:: ModelTensorReshape reshape //@@ //@@ The shape produced for this output by the backend. The output will - //@@ be reshaped from this to the shape specifed in 'dims' before being + //@@ be reshaped from this to the shape specified in 'dims' before being //@@ returned in the inference response. The reshape must have the same //@@ number of elements as the output shape specified by 'dims'. Optional. //@@ @@ -887,7 +887,7 @@ message ModelOptimizationPolicy //@@ .. cpp:var:: map parameters //@@ - //@@ Additional paremeters used to configure the accelerator. + //@@ Additional parameters used to configure the accelerator. //@@ map parameters = 2; } @@ -1011,7 +1011,7 @@ message ModelOptimizationPolicy //@@ device has direct access to the source buffer and the destination //@@ buffer. In such case, the gather kernel will be used only if the //@@ number of buffers to be gathered is greater or equal to - //@@ the specifed value. If 0, the gather kernel will be disabled. + //@@ the specified value. If 0, the gather kernel will be disabled. //@@ Default value is 0. //@@ Currently only recognized by TensorRT backend. //@@ @@ -1434,7 +1434,7 @@ message ModelSequenceBatching //@@ .. cpp:var:: int32 max_candidate_sequences //@@ //@@ Maximum number of candidate sequences that the batcher - //@@ maintains. Excess seqences are kept in an ordered backlog + //@@ maintains. Excess sequences are kept in an ordered backlog //@@ and become candidates when existing candidate sequences //@@ complete. //@@ @@ -1462,10 +1462,13 @@ message ModelSequenceBatching //@@ //@@ Should the dynamic batcher preserve the ordering of responses to //@@ match the order of requests received by the scheduler. Default is - //@@ false. If true, the responses will be returned in the same order as - //@@ the order of requests sent to the scheduler. If false, the responses + //@@ false. If true, the responses will be returned in the same order + // as + //@@ the order of requests sent to the scheduler. If false, the + // responses //@@ may be returned in arbitrary order. This option is specifically - //@@ needed when a sequence of related inference requests (i.e. inference + //@@ needed when a sequence of related inference requests (i.e. + // inference //@@ requests with the same correlation ID) are sent to the dynamic //@@ batcher to ensure that the sequence responses are in the correct //@@ order. @@ -1473,8 +1476,10 @@ message ModelSequenceBatching //@@ When using decoupled models, setting this to true may block the //@@ responses from independent sequences from being returned to the //@@ client until the previous request completes, hurting overall - //@@ performance. If using GRPC streaming protocol, the stream ordering - //@@ guarantee may be sufficient alone to ensure the responses for each + //@@ performance. If using GRPC streaming protocol, the stream + // ordering + //@@ guarantee may be sufficient alone to ensure the responses for + // each //@@ sequence are returned in sequence-order without blocking based on //@@ independent requests, depending on the use case. //@@ @@ -1781,7 +1786,7 @@ message ModelRepositoryAgents //@@ .. cpp:var:: Agent agents (repeated) //@@ //@@ The ordered list of agents for the model. These agents will be - //@@ invoked in order to respond to repository actions occuring for the + //@@ invoked in order to respond to repository actions occurring for the //@@ model. //@@ repeated Agent agents = 1; diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..5e8749f --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,48 @@ +# Copyright 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of NVIDIA CORPORATION nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY +# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +[tool.codespell] +# note: pre-commit passes explicit lists of files here, which this skip file list doesn't override - +# this is only to allow you to run codespell interactively +skip = "./.git,./.github" +# ignore short words, and typename parameters like OffsetT +ignore-regex = "\\b(.{1,4}|[A-Z]\\w*T)\\b" +# use the 'clear' dictionary for unambiguous spelling mistakes +builtin = "clear" +# disable warnings about binary files and wrong encoding +quiet-level = 3 + +[tool.isort] +profile = "black" +use_parentheses = true +multi_line_output = 3 +include_trailing_comma = true +force_grid_wrap = 0 +ensure_newline_before_comments = true +line_length = 88 +balanced_wrapping = true +indent = " " +skip = ["build"] diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 78b1146..85ebda6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -109,7 +109,7 @@ target_link_libraries(triton-common-async-work-queue ) # -# Thread Pool +# Thread Pool # add_library( triton-common-thread-pool diff --git a/src/table_printer.cc b/src/table_printer.cc index 779f499..9af6113 100644 --- a/src/table_printer.cc +++ b/src/table_printer.cc @@ -149,7 +149,7 @@ TablePrinter::FairShare() line.substr(start_index, end_index - start_index)); } - // We need to advance the index for the splitted lines. + // We need to advance the index for the split lines. line_index += num_rows - 1; } diff --git a/src/test/triton_json/CMakeLists.txt b/src/test/triton_json/CMakeLists.txt index 7f19330..3ef9311 100644 --- a/src/test/triton_json/CMakeLists.txt +++ b/src/test/triton_json/CMakeLists.txt @@ -27,7 +27,7 @@ cmake_minimum_required (VERSION 3.18) add_executable(triton-json-test triton_json_test.cc ../../error.cc) target_link_libraries( - triton-json-test + triton-json-test GTest::gtest GTest::gtest_main) diff --git a/tools/format.py b/tools/format.py deleted file mode 100755 index 7363490..0000000 --- a/tools/format.py +++ /dev/null @@ -1,116 +0,0 @@ -#!/usr/bin/python - -# Copyright (c) 2018-2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions -# are met: -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above copyright -# notice, this list of conditions and the following disclaimer in the -# documentation and/or other materials provided with the distribution. -# * Neither the name of NVIDIA CORPORATION nor the names of its -# contributors may be used to endorse or promote products derived -# from this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY -# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR -# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY -# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -import argparse -import os -import subprocess -import yapf - -FLAGS = None -FORMAT_EXTS = ('proto', 'cc', 'cu', 'h') -SKIP_PATHS = ('tools',) - - -def visit(path): - if FLAGS.verbose: - print("visiting " + path) - - valid_ext = False - python_file = False - for ext in FORMAT_EXTS: - if path.endswith('.' + ext): - valid_ext = True - break - if path.endswith('.py'): - valid_ext = True - python_file = True - if not valid_ext: - if FLAGS.verbose: - print("skipping due to extension: " + path) - return True - - for skip in SKIP_PATHS: - if path.startswith(skip): - if FLAGS.verbose: - print("skipping due to path prefix: " + path) - return True - if python_file: - yapf.yapflib.yapf_api.FormatFile(path, - in_place=True, - style_config='google') - return True - else: - args = ['clang-format-15', '--style=file', '-i'] - if FLAGS.verbose: - args.append('-verbose') - args.append(path) - - ret = subprocess.call(args) - if ret != 0: - print("format failed for " + path) - return False - - return True - - -if __name__ == '__main__': - parser = argparse.ArgumentParser() - parser.add_argument('-v', - '--verbose', - action="store_true", - required=False, - default=False, - help='Enable verbose output') - parser.add_argument('paths', - type=str, - nargs='*', - default=None, - help='Directories or files to format') - FLAGS = parser.parse_args() - - # Check the version of yapf. Needs a consistent version - # of yapf to prevent unneccessary changes in the code. - if (yapf.__version__ != '0.30.0'): - print("Needs yapf 0.30.0, but got yapf {}".format(yapf.__version__)) - - if (FLAGS.paths is None) or (len(FLAGS.paths) == 0): - parser.print_help() - exit(1) - - ret = True - for path in FLAGS.paths: - if not os.path.isdir(path): - if not visit(path): - ret = False - else: - for root, dirs, files in os.walk(path): - for name in files: - if not visit(os.path.join(root, name)): - ret = False - - exit(0 if ret else 1)