|
| 1 | +#!/usr/bin/env python |
| 2 | +"""A wrapper script around clang-format, suitable for linting multiple files |
| 3 | +and to use for continuous integration. |
| 4 | +This is an alternative API for the clang-format command line. |
| 5 | +It runs over multiple files and directories in parallel. |
| 6 | +A diff output is produced and a sensible exit code is returned. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import print_function, unicode_literals |
| 10 | + |
| 11 | +import argparse |
| 12 | +import codecs |
| 13 | +import difflib |
| 14 | +import fnmatch |
| 15 | +import io |
| 16 | +import errno |
| 17 | +import multiprocessing |
| 18 | +import os |
| 19 | +import signal |
| 20 | +import subprocess |
| 21 | +import sys |
| 22 | +import traceback |
| 23 | + |
| 24 | +from functools import partial |
| 25 | + |
| 26 | +try: |
| 27 | + from subprocess import DEVNULL # py3k |
| 28 | +except ImportError: |
| 29 | + DEVNULL = open(os.devnull, "wb") |
| 30 | + |
| 31 | + |
| 32 | +DEFAULT_EXTENSIONS = 'c,h,C,H,cpp,hpp,cc,hh,c++,h++,cxx,hxx' |
| 33 | +DEFAULT_CLANG_FORMAT_IGNORE = '.clang-format-ignore' |
| 34 | + |
| 35 | + |
| 36 | +class ExitStatus: |
| 37 | + SUCCESS = 0 |
| 38 | + DIFF = 1 |
| 39 | + TROUBLE = 2 |
| 40 | + |
| 41 | +def excludes_from_file(ignore_file): |
| 42 | + excludes = [] |
| 43 | + try: |
| 44 | + with io.open(ignore_file, 'r', encoding='utf-8') as f: |
| 45 | + for line in f: |
| 46 | + if line.startswith('#'): |
| 47 | + # ignore comments |
| 48 | + continue |
| 49 | + pattern = line.rstrip() |
| 50 | + if not pattern: |
| 51 | + # allow empty lines |
| 52 | + continue |
| 53 | + excludes.append(pattern) |
| 54 | + except EnvironmentError as e: |
| 55 | + if e.errno != errno.ENOENT: |
| 56 | + raise |
| 57 | + return excludes; |
| 58 | + |
| 59 | +def list_files(files, recursive=False, extensions=None, exclude=None): |
| 60 | + if extensions is None: |
| 61 | + extensions = [] |
| 62 | + if exclude is None: |
| 63 | + exclude = [] |
| 64 | + |
| 65 | + out = [] |
| 66 | + for file in files: |
| 67 | + if recursive and os.path.isdir(file): |
| 68 | + for dirpath, dnames, fnames in os.walk(file): |
| 69 | + fpaths = [os.path.join(dirpath, fname) for fname in fnames] |
| 70 | + for pattern in exclude: |
| 71 | + # os.walk() supports trimming down the dnames list |
| 72 | + # by modifying it in-place, |
| 73 | + # to avoid unnecessary directory listings. |
| 74 | + dnames[:] = [ |
| 75 | + x for x in dnames |
| 76 | + if |
| 77 | + not fnmatch.fnmatch(os.path.join(dirpath, x), pattern) |
| 78 | + ] |
| 79 | + fpaths = [ |
| 80 | + x for x in fpaths if not fnmatch.fnmatch(x, pattern) |
| 81 | + ] |
| 82 | + for f in fpaths: |
| 83 | + ext = os.path.splitext(f)[1][1:] |
| 84 | + if ext in extensions: |
| 85 | + out.append(f) |
| 86 | + else: |
| 87 | + out.append(file) |
| 88 | + return out |
| 89 | + |
| 90 | + |
| 91 | +def make_diff(file, original, reformatted): |
| 92 | + return list( |
| 93 | + difflib.unified_diff( |
| 94 | + original, |
| 95 | + reformatted, |
| 96 | + fromfile='{}\t(original)'.format(file), |
| 97 | + tofile='{}\t(reformatted)'.format(file), |
| 98 | + n=3)) |
| 99 | + |
| 100 | + |
| 101 | +class DiffError(Exception): |
| 102 | + def __init__(self, message, errs=None): |
| 103 | + super(DiffError, self).__init__(message) |
| 104 | + self.errs = errs or [] |
| 105 | + |
| 106 | + |
| 107 | +class UnexpectedError(Exception): |
| 108 | + def __init__(self, message, exc=None): |
| 109 | + super(UnexpectedError, self).__init__(message) |
| 110 | + self.formatted_traceback = traceback.format_exc() |
| 111 | + self.exc = exc |
| 112 | + |
| 113 | + |
| 114 | +def run_clang_format_diff_wrapper(args, file): |
| 115 | + try: |
| 116 | + ret = run_clang_format_diff(args, file) |
| 117 | + return ret |
| 118 | + except DiffError: |
| 119 | + raise |
| 120 | + except Exception as e: |
| 121 | + raise UnexpectedError('{}: {}: {}'.format(file, e.__class__.__name__, |
| 122 | + e), e) |
| 123 | + |
| 124 | + |
| 125 | +def run_clang_format_diff(args, file): |
| 126 | + try: |
| 127 | + with io.open(file, 'r', encoding='utf-8') as f: |
| 128 | + original = f.readlines() |
| 129 | + except IOError as exc: |
| 130 | + raise DiffError(str(exc)) |
| 131 | + invocation = [args.clang_format_executable, file] |
| 132 | + |
| 133 | + # Use of utf-8 to decode the process output. |
| 134 | + # |
| 135 | + # Hopefully, this is the correct thing to do. |
| 136 | + # |
| 137 | + # It's done due to the following assumptions (which may be incorrect): |
| 138 | + # - clang-format will returns the bytes read from the files as-is, |
| 139 | + # without conversion, and it is already assumed that the files use utf-8. |
| 140 | + # - if the diagnostics were internationalized, they would use utf-8: |
| 141 | + # > Adding Translations to Clang |
| 142 | + # > |
| 143 | + # > Not possible yet! |
| 144 | + # > Diagnostic strings should be written in UTF-8, |
| 145 | + # > the client can translate to the relevant code page if needed. |
| 146 | + # > Each translation completely replaces the format string |
| 147 | + # > for the diagnostic. |
| 148 | + # > -- http://clang.llvm.org/docs/InternalsManual.html#internals-diag-translation |
| 149 | + # |
| 150 | + # It's not pretty, due to Python 2 & 3 compatibility. |
| 151 | + encoding_py3 = {} |
| 152 | + if sys.version_info[0] >= 3: |
| 153 | + encoding_py3['encoding'] = 'utf-8' |
| 154 | + |
| 155 | + try: |
| 156 | + proc = subprocess.Popen( |
| 157 | + invocation, |
| 158 | + stdout=subprocess.PIPE, |
| 159 | + stderr=subprocess.PIPE, |
| 160 | + universal_newlines=True, |
| 161 | + **encoding_py3) |
| 162 | + except OSError as exc: |
| 163 | + raise DiffError( |
| 164 | + "Command '{}' failed to start: {}".format( |
| 165 | + subprocess.list2cmdline(invocation), exc |
| 166 | + ) |
| 167 | + ) |
| 168 | + proc_stdout = proc.stdout |
| 169 | + proc_stderr = proc.stderr |
| 170 | + if sys.version_info[0] < 3: |
| 171 | + # make the pipes compatible with Python 3, |
| 172 | + # reading lines should output unicode |
| 173 | + encoding = 'utf-8' |
| 174 | + proc_stdout = codecs.getreader(encoding)(proc_stdout) |
| 175 | + proc_stderr = codecs.getreader(encoding)(proc_stderr) |
| 176 | + # hopefully the stderr pipe won't get full and block the process |
| 177 | + outs = list(proc_stdout.readlines()) |
| 178 | + errs = list(proc_stderr.readlines()) |
| 179 | + proc.wait() |
| 180 | + if proc.returncode: |
| 181 | + raise DiffError( |
| 182 | + "Command '{}' returned non-zero exit status {}".format( |
| 183 | + subprocess.list2cmdline(invocation), proc.returncode |
| 184 | + ), |
| 185 | + errs, |
| 186 | + ) |
| 187 | + return make_diff(file, original, outs), errs |
| 188 | + |
| 189 | + |
| 190 | +def bold_red(s): |
| 191 | + return '\x1b[1m\x1b[31m' + s + '\x1b[0m' |
| 192 | + |
| 193 | + |
| 194 | +def colorize(diff_lines): |
| 195 | + def bold(s): |
| 196 | + return '\x1b[1m' + s + '\x1b[0m' |
| 197 | + |
| 198 | + def cyan(s): |
| 199 | + return '\x1b[36m' + s + '\x1b[0m' |
| 200 | + |
| 201 | + def green(s): |
| 202 | + return '\x1b[32m' + s + '\x1b[0m' |
| 203 | + |
| 204 | + def red(s): |
| 205 | + return '\x1b[31m' + s + '\x1b[0m' |
| 206 | + |
| 207 | + for line in diff_lines: |
| 208 | + if line[:4] in ['--- ', '+++ ']: |
| 209 | + yield bold(line) |
| 210 | + elif line.startswith('@@ '): |
| 211 | + yield cyan(line) |
| 212 | + elif line.startswith('+'): |
| 213 | + yield green(line) |
| 214 | + elif line.startswith('-'): |
| 215 | + yield red(line) |
| 216 | + else: |
| 217 | + yield line |
| 218 | + |
| 219 | + |
| 220 | +def print_diff(diff_lines, use_color): |
| 221 | + if use_color: |
| 222 | + diff_lines = colorize(diff_lines) |
| 223 | + if sys.version_info[0] < 3: |
| 224 | + sys.stdout.writelines((l.encode('utf-8') for l in diff_lines)) |
| 225 | + else: |
| 226 | + sys.stdout.writelines(diff_lines) |
| 227 | + |
| 228 | + |
| 229 | +def print_trouble(prog, message, use_colors): |
| 230 | + error_text = 'error:' |
| 231 | + if use_colors: |
| 232 | + error_text = bold_red(error_text) |
| 233 | + print("{}: {} {}".format(prog, error_text, message), file=sys.stderr) |
| 234 | + |
| 235 | + |
| 236 | +def main(): |
| 237 | + parser = argparse.ArgumentParser(description=__doc__) |
| 238 | + parser.add_argument( |
| 239 | + '--clang-format-executable', |
| 240 | + metavar='EXECUTABLE', |
| 241 | + help='path to the clang-format executable', |
| 242 | + default='clang-format') |
| 243 | + parser.add_argument( |
| 244 | + '--extensions', |
| 245 | + help='comma separated list of file extensions (default: {})'.format( |
| 246 | + DEFAULT_EXTENSIONS), |
| 247 | + default=DEFAULT_EXTENSIONS) |
| 248 | + parser.add_argument( |
| 249 | + '-r', |
| 250 | + '--recursive', |
| 251 | + action='store_true', |
| 252 | + help='run recursively over directories') |
| 253 | + parser.add_argument('files', metavar='file', nargs='+') |
| 254 | + parser.add_argument( |
| 255 | + '-q', |
| 256 | + '--quiet', |
| 257 | + action='store_true', |
| 258 | + help="disable output, useful for the exit code") |
| 259 | + parser.add_argument( |
| 260 | + '-j', |
| 261 | + metavar='N', |
| 262 | + type=int, |
| 263 | + default=0, |
| 264 | + help='run N clang-format jobs in parallel' |
| 265 | + ' (default number of cpus + 1)') |
| 266 | + parser.add_argument( |
| 267 | + '--color', |
| 268 | + default='auto', |
| 269 | + choices=['auto', 'always', 'never'], |
| 270 | + help='show colored diff (default: auto)') |
| 271 | + parser.add_argument( |
| 272 | + '-e', |
| 273 | + '--exclude', |
| 274 | + metavar='PATTERN', |
| 275 | + action='append', |
| 276 | + default=[], |
| 277 | + help='exclude paths matching the given glob-like pattern(s)' |
| 278 | + ' from recursive search') |
| 279 | + |
| 280 | + args = parser.parse_args() |
| 281 | + |
| 282 | + # use default signal handling, like diff return SIGINT value on ^C |
| 283 | + # https://bugs.python.org/issue14229#msg156446 |
| 284 | + signal.signal(signal.SIGINT, signal.SIG_DFL) |
| 285 | + try: |
| 286 | + signal.SIGPIPE |
| 287 | + except AttributeError: |
| 288 | + # compatibility, SIGPIPE does not exist on Windows |
| 289 | + pass |
| 290 | + else: |
| 291 | + signal.signal(signal.SIGPIPE, signal.SIG_DFL) |
| 292 | + |
| 293 | + colored_stdout = False |
| 294 | + colored_stderr = False |
| 295 | + if args.color == 'always': |
| 296 | + colored_stdout = True |
| 297 | + colored_stderr = True |
| 298 | + elif args.color == 'auto': |
| 299 | + colored_stdout = sys.stdout.isatty() |
| 300 | + colored_stderr = sys.stderr.isatty() |
| 301 | + |
| 302 | + version_invocation = [args.clang_format_executable, str("--version")] |
| 303 | + try: |
| 304 | + subprocess.check_call(version_invocation, stdout=DEVNULL) |
| 305 | + except subprocess.CalledProcessError as e: |
| 306 | + print_trouble(parser.prog, str(e), use_colors=colored_stderr) |
| 307 | + return ExitStatus.TROUBLE |
| 308 | + except OSError as e: |
| 309 | + print_trouble( |
| 310 | + parser.prog, |
| 311 | + "Command '{}' failed to start: {}".format( |
| 312 | + subprocess.list2cmdline(version_invocation), e |
| 313 | + ), |
| 314 | + use_colors=colored_stderr, |
| 315 | + ) |
| 316 | + return ExitStatus.TROUBLE |
| 317 | + |
| 318 | + retcode = ExitStatus.SUCCESS |
| 319 | + |
| 320 | + excludes = excludes_from_file(DEFAULT_CLANG_FORMAT_IGNORE) |
| 321 | + excludes.extend(args.exclude) |
| 322 | + |
| 323 | + files = list_files( |
| 324 | + args.files, |
| 325 | + recursive=args.recursive, |
| 326 | + exclude=excludes, |
| 327 | + extensions=args.extensions.split(',')) |
| 328 | + |
| 329 | + if not files: |
| 330 | + return |
| 331 | + |
| 332 | + njobs = args.j |
| 333 | + if njobs == 0: |
| 334 | + njobs = multiprocessing.cpu_count() + 1 |
| 335 | + njobs = min(len(files), njobs) |
| 336 | + |
| 337 | + if njobs == 1: |
| 338 | + # execute directly instead of in a pool, |
| 339 | + # less overhead, simpler stacktraces |
| 340 | + it = (run_clang_format_diff_wrapper(args, file) for file in files) |
| 341 | + pool = None |
| 342 | + else: |
| 343 | + pool = multiprocessing.Pool(njobs) |
| 344 | + it = pool.imap_unordered( |
| 345 | + partial(run_clang_format_diff_wrapper, args), files) |
| 346 | + while True: |
| 347 | + try: |
| 348 | + outs, errs = next(it) |
| 349 | + except StopIteration: |
| 350 | + break |
| 351 | + except DiffError as e: |
| 352 | + print_trouble(parser.prog, str(e), use_colors=colored_stderr) |
| 353 | + retcode = ExitStatus.TROUBLE |
| 354 | + sys.stderr.writelines(e.errs) |
| 355 | + except UnexpectedError as e: |
| 356 | + print_trouble(parser.prog, str(e), use_colors=colored_stderr) |
| 357 | + sys.stderr.write(e.formatted_traceback) |
| 358 | + retcode = ExitStatus.TROUBLE |
| 359 | + # stop at the first unexpected error, |
| 360 | + # something could be very wrong, |
| 361 | + # don't process all files unnecessarily |
| 362 | + if pool: |
| 363 | + pool.terminate() |
| 364 | + break |
| 365 | + else: |
| 366 | + sys.stderr.writelines(errs) |
| 367 | + if outs == []: |
| 368 | + continue |
| 369 | + if not args.quiet: |
| 370 | + print_diff(outs, use_color=colored_stdout) |
| 371 | + if retcode == ExitStatus.SUCCESS: |
| 372 | + retcode = ExitStatus.DIFF |
| 373 | + return retcode |
| 374 | + |
| 375 | + |
| 376 | +if __name__ == '__main__': |
| 377 | + sys.exit(main()) |
0 commit comments