| 1 |
#!/usr/bin/env python3 |
| 2 |
# Copyright 2017 gRPC authors. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
# you may not use this file except in compliance with the License. |
| 6 |
# You may obtain a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
# See the License for the specific language governing permissions and |
| 14 |
# limitations under the License. |
| 15 |
|
| 16 |
import argparse |
| 17 |
import multiprocessing |
| 18 |
import os |
| 19 |
import subprocess |
| 20 |
import sys |
| 21 |
|
| 22 |
sys.path.append( |
| 23 |
os.path.join(os.path.dirname(sys.argv[0]), '..', 'run_tests', |
| 24 |
'python_utils')) |
| 25 |
import jobset |
| 26 |
|
| 27 |
clang_tidy = os.environ.get('CLANG_TIDY', 'clang-tidy') |
| 28 |
|
| 29 |
argp = argparse.ArgumentParser(description='Run clang-tidy against core') |
| 30 |
argp.add_argument('files', nargs='+', help='Files to tidy') |
| 31 |
argp.add_argument('--fix', dest='fix', action='store_true') |
| 32 |
argp.add_argument('-j', |
| 33 |
'--jobs', |
| 34 |
type=int, |
| 35 |
default=multiprocessing.cpu_count(), |
| 36 |
help='Number of CPUs to use') |
| 37 |
argp.add_argument('--only-changed', dest='only_changed', action='store_true') |
| 38 |
argp.set_defaults(fix=False, only_changed=False) |
| 39 |
args = argp.parse_args() |
| 40 |
|
| 41 |
# Explicitly passing the .clang-tidy config by reading it. |
| 42 |
# This is required because source files in the compilation database are |
| 43 |
# in a different source tree so clang-tidy cannot find the right config file |
| 44 |
# by seeking their parent directories. |
| 45 |
with open(".clang-tidy") as f: |
| 46 |
config = f.read() |
| 47 |
cmdline = [ |
| 48 |
clang_tidy, |
| 49 |
'--config=' + config, |
| 50 |
] |
| 51 |
|
| 52 |
if args.fix: |
| 53 |
cmdline.append('--fix-errors') |
| 54 |
|
| 55 |
if args.only_changed: |
| 56 |
orig_files = set(args.files) |
| 57 |
actual_files = [] |
| 58 |
output = subprocess.check_output( |
| 59 |
['git', 'diff', 'upstream/master', 'HEAD', '--name-only']) |
| 60 |
for line in output.decode('ascii').splitlines(False): |
| 61 |
if line in orig_files: |
| 62 |
print(("check: %s" % line)) |
| 63 |
actual_files.append(line) |
| 64 |
else: |
| 65 |
print(("skip: %s - not in the build" % line)) |
| 66 |
args.files = actual_files |
| 67 |
|
| 68 |
jobs = [] |
| 69 |
for filename in args.files: |
| 70 |
jobs.append( |
| 71 |
jobset.JobSpec( |
| 72 |
cmdline + [filename], |
| 73 |
shortname=filename, |
| 74 |
timeout_seconds=15 * 60, |
| 75 |
)) |
| 76 |
|
| 77 |
num_fails, res_set = jobset.run(jobs, maxjobs=args.jobs, quiet_success=True) |
| 78 |
sys.exit(num_fails) |