|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2019 Google |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""Lints source files for conformance with the style guide that applies. |
| 18 | +
|
| 19 | +Currently supports linting Objective-C, Objective-C++, C++, and Python source. |
| 20 | +""" |
| 21 | + |
| 22 | +import argparse |
| 23 | +import logging |
| 24 | +import os |
| 25 | +import re |
| 26 | +import subprocess |
| 27 | +import sys |
| 28 | +import textwrap |
| 29 | + |
| 30 | +from lib import checker |
| 31 | +from lib import command_trace |
| 32 | +from lib import git |
| 33 | +from lib import source |
| 34 | + |
| 35 | +_logger = logging.getLogger('lint') |
| 36 | + |
| 37 | + |
| 38 | +_dry_run = False |
| 39 | + |
| 40 | + |
| 41 | +_CPPLINT_OBJC_FILTERS = [ |
| 42 | + # Objective-C uses #import and does not use header guards |
| 43 | + '-build/header_guard', |
| 44 | + |
| 45 | + # Inline definitions of Objective-C blocks confuse |
| 46 | + '-readability/braces', |
| 47 | + |
| 48 | + # C-style casts are acceptable in Objective-C++ |
| 49 | + '-readability/casting', |
| 50 | + |
| 51 | + # Objective-C needs use type 'long' for interop between types like NSInteger |
| 52 | + # and printf-style functions. |
| 53 | + '-runtime/int', |
| 54 | + |
| 55 | + # cpplint is generally confused by Objective-C mixing with C++. |
| 56 | + # * Objective-C method invocations in a for loop make it think its a |
| 57 | + # range-for |
| 58 | + # * Objective-C dictionary literals confuse brace spacing |
| 59 | + # * Empty category declarations ("@interface Foo ()") look like function |
| 60 | + # invocations |
| 61 | + '-whitespace', |
| 62 | +] |
| 63 | + |
| 64 | +_CPPLINT_OBJC_OPTIONS = [ |
| 65 | + # cpplint normally excludes Objective-C++ |
| 66 | + '--extensions=h,m,mm', |
| 67 | + |
| 68 | + # Objective-C style allows longer lines |
| 69 | + '--linelength=100', |
| 70 | + |
| 71 | + '--filter=' + ','.join(_CPPLINT_OBJC_FILTERS), |
| 72 | +] |
| 73 | + |
| 74 | + |
| 75 | +def main(): |
| 76 | + global _dry_run |
| 77 | + |
| 78 | + parser = argparse.ArgumentParser(description='Lint source files.') |
| 79 | + parser.add_argument('--dry-run', '-n', action='store_true', |
| 80 | + help='Show what the linter would do without doing it') |
| 81 | + parser.add_argument('--all', action='store_true', |
| 82 | + help='run the linter over all known sources') |
| 83 | + parser.add_argument('rev_or_files', nargs='*', |
| 84 | + help='A single revision that specifies a point in time ' |
| 85 | + 'from which to look for changes. Defaults to ' |
| 86 | + 'origin/master. Alternatively, a list of specific ' |
| 87 | + 'files or git pathspecs to lint.') |
| 88 | + args = command_trace.parse_args(parser) |
| 89 | + |
| 90 | + if args.dry_run: |
| 91 | + _dry_run = True |
| 92 | + command_trace.enable_tracing() |
| 93 | + |
| 94 | + pool = checker.Pool() |
| 95 | + |
| 96 | + sources = _unique(source.CC_DIRS + source.OBJC_DIRS + source.PYTHON_DIRS) |
| 97 | + patterns = git.make_patterns(sources) |
| 98 | + |
| 99 | + files = git.find_changed_or_files(args.all, args.rev_or_files, patterns) |
| 100 | + check(pool, files) |
| 101 | + |
| 102 | + pool.exit() |
| 103 | + |
| 104 | + |
| 105 | +def check(pool, files): |
| 106 | + group = source.categorize_files(files) |
| 107 | + |
| 108 | + for kind, files in group.kinds.items(): |
| 109 | + for chunk in checker.shard(files): |
| 110 | + if not chunk: |
| 111 | + continue |
| 112 | + |
| 113 | + linter = _linters[kind] |
| 114 | + pool.submit(linter, chunk) |
| 115 | + |
| 116 | + |
| 117 | +def lint_cc(files): |
| 118 | + return _run_cpplint([], files) |
| 119 | + |
| 120 | + |
| 121 | +def lint_objc(files): |
| 122 | + return _run_cpplint(_CPPLINT_OBJC_OPTIONS, files) |
| 123 | + |
| 124 | + |
| 125 | +def _run_cpplint(options, files): |
| 126 | + scripts_dir = os.path.dirname(os.path.abspath(__file__)) |
| 127 | + cpplint = os.path.join(scripts_dir, 'cpplint.py') |
| 128 | + |
| 129 | + command = [sys.executable, cpplint, '--quiet'] |
| 130 | + command.extend(options) |
| 131 | + command.extend(files) |
| 132 | + |
| 133 | + return _read_output(command) |
| 134 | + |
| 135 | + |
| 136 | +_flake8_warned = False |
| 137 | + |
| 138 | + |
| 139 | +def lint_py(files): |
| 140 | + flake8 = which('flake8') |
| 141 | + if flake8 is None: |
| 142 | + global _flake8_warned |
| 143 | + if not _flake8_warned: |
| 144 | + _flake8_warned = True |
| 145 | + _logger.warn(textwrap.dedent( |
| 146 | + """ |
| 147 | + Could not find flake8 on the path; skipping python lint. |
| 148 | + Install with: |
| 149 | +
|
| 150 | + pip install --user flake8 |
| 151 | + """)) |
| 152 | + return |
| 153 | + |
| 154 | + command = [flake8] |
| 155 | + command.extend(files) |
| 156 | + |
| 157 | + return _read_output(command) |
| 158 | + |
| 159 | + |
| 160 | +def _read_output(command): |
| 161 | + command_trace.log(command) |
| 162 | + |
| 163 | + if _dry_run: |
| 164 | + return checker.Result(0, '') |
| 165 | + |
| 166 | + proc = subprocess.Popen( |
| 167 | + command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 168 | + output = proc.communicate('')[0] |
| 169 | + sc = proc.wait() |
| 170 | + |
| 171 | + return checker.Result(sc, output) |
| 172 | + |
| 173 | + |
| 174 | +_linters = { |
| 175 | + 'cc': lint_cc, |
| 176 | + 'objc': lint_objc, |
| 177 | + 'py': lint_py, |
| 178 | +} |
| 179 | + |
| 180 | + |
| 181 | +def _unique(items): |
| 182 | + return list(set(items)) |
| 183 | + |
| 184 | + |
| 185 | +def make_path(): |
| 186 | + """Makes a list of paths to search for binaries. |
| 187 | +
|
| 188 | + Returns: |
| 189 | + A list of directories that can be sources of binaries to run. This includes |
| 190 | + both the PATH environment variable and any bin directories associated with |
| 191 | + python install locations. |
| 192 | + """ |
| 193 | + # Start with the system-supplied PATH. |
| 194 | + path = os.environ['PATH'].split(os.pathsep) |
| 195 | + |
| 196 | + # In addition, add any bin directories near the lib directories in the python |
| 197 | + # path. This makes it possible to find flake8 in ~/Library/Python/2.7/bin |
| 198 | + # after pip install --user flake8. Also handle installations on Windows which |
| 199 | + # go in %APPDATA%/Python/Scripts. |
| 200 | + lib_pattern = re.compile(r'(.*)/[^/]*/site-packages') |
| 201 | + for entry in sys.path: |
| 202 | + entry = entry.replace(os.sep, '/') |
| 203 | + m = lib_pattern.match(entry) |
| 204 | + if not m: |
| 205 | + continue |
| 206 | + |
| 207 | + python_root = m.group(1).replace('/', os.sep) |
| 208 | + |
| 209 | + for bin_basename in ('bin', 'Scripts'): |
| 210 | + bin_dir = os.path.join(python_root, bin_basename) |
| 211 | + if bin_dir not in path and os.path.exists(bin_dir): |
| 212 | + path.append(bin_dir) |
| 213 | + |
| 214 | + return path |
| 215 | + |
| 216 | + |
| 217 | +_PATH = make_path() |
| 218 | + |
| 219 | + |
| 220 | +def which(executable): |
| 221 | + """Finds the executable with the given name. |
| 222 | +
|
| 223 | + Returns: |
| 224 | + The fully qualified path to the executable or None if the executable isn't |
| 225 | + found. |
| 226 | + """ |
| 227 | + if executable.startswith('/'): |
| 228 | + return executable |
| 229 | + |
| 230 | + for executable_with_ext in _executable_names(executable): |
| 231 | + for entry in _PATH: |
| 232 | + joined = os.path.join(entry, executable_with_ext) |
| 233 | + if os.path.isfile(joined) and os.access(joined, os.X_OK): |
| 234 | + return joined |
| 235 | + |
| 236 | + return None |
| 237 | + |
| 238 | + |
| 239 | +def _executable_names(executable): |
| 240 | + """Yields a sequence of all possible executable names.""" |
| 241 | + |
| 242 | + if os.name == 'nt': |
| 243 | + pathext = os.environ.get('PATHEXT', '').split(os.pathsep) |
| 244 | + for ext in pathext: |
| 245 | + yield executable + ext |
| 246 | + |
| 247 | + else: |
| 248 | + yield executable |
| 249 | + |
| 250 | + |
| 251 | +if __name__ == '__main__': |
| 252 | + main() |
0 commit comments