Brian Norris | e4a085a | 2020-04-01 16:40:04 -0700 | [diff] [blame] | 1 | #!/usr/bin/env vpython |
| 2 | |
| 3 | # [VPYTHON:BEGIN] |
| 4 | # # Third party dependencies. These are only listed because pylint itself needs |
| 5 | # # them. Feel free to add/remove anything here. |
| 6 | # |
| 7 | # wheel: < |
| 8 | # name: "infra/python/wheels/configparser-py2_py3" |
| 9 | # version: "version:3.5.0" |
| 10 | # > |
| 11 | # wheel: < |
| 12 | # name: "infra/python/wheels/futures-py2_py3" |
| 13 | # version: "version:3.1.1" |
| 14 | # > |
| 15 | # wheel: < |
| 16 | # name: "infra/python/wheels/isort-py2_py3" |
| 17 | # version: "version:4.3.4" |
| 18 | # > |
| 19 | # wheel: < |
| 20 | # name: "infra/python/wheels/wrapt/${vpython_platform}" |
| 21 | # version: "version:1.10.11" |
| 22 | # > |
| 23 | # wheel: < |
| 24 | # name: "infra/python/wheels/backports_functools_lru_cache-py2_py3" |
| 25 | # version: "version:1.5" |
| 26 | # > |
| 27 | # wheel: < |
| 28 | # name: "infra/python/wheels/lazy-object-proxy/${vpython_platform}" |
| 29 | # version: "version:1.3.1" |
| 30 | # > |
| 31 | # wheel: < |
| 32 | # name: "infra/python/wheels/singledispatch-py2_py3" |
| 33 | # version: "version:3.4.0.3" |
| 34 | # > |
| 35 | # wheel: < |
| 36 | # name: "infra/python/wheels/enum34-py2" |
| 37 | # version: "version:1.1.6" |
| 38 | # > |
| 39 | # wheel: < |
| 40 | # name: "infra/python/wheels/mccabe-py2_py3" |
| 41 | # version: "version:0.6.1" |
| 42 | # > |
| 43 | # wheel: < |
| 44 | # name: "infra/python/wheels/six-py2_py3" |
| 45 | # version: "version:1.10.0" |
| 46 | # > |
| 47 | # |
| 48 | # # Pylint dependencies. |
| 49 | # |
| 50 | # wheel: < |
| 51 | # name: "infra/python/wheels/astroid-py2_py3" |
| 52 | # version: "version:1.6.6" |
| 53 | # > |
| 54 | # |
| 55 | # wheel: < |
| 56 | # name: "infra/python/wheels/pylint-py2_py3" |
| 57 | # version: "version:1.9.5-45a720817e4de1df2f173c7e4029e176" |
| 58 | # > |
| 59 | # [VPYTHON:END] |
| 60 | |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 61 | """ |
| 62 | Wrapper to patch pylint library functions to suit autotest. |
| 63 | |
| 64 | This script is invoked as part of the presubmit checks for autotest python |
| 65 | files. It runs pylint on a list of files that it obtains either through |
| 66 | the command line or from an environment variable set in pre-upload.py. |
| 67 | |
| 68 | Example: |
| 69 | run_pylint.py filename.py |
| 70 | """ |
mbligh | 99d2ded | 2008-06-23 16:17:36 +0000 | [diff] [blame] | 71 | |
Prashanth Balasubramanian | baabef2 | 2014-11-04 12:38:44 -0800 | [diff] [blame] | 72 | import fnmatch |
| 73 | import logging |
| 74 | import os |
| 75 | import re |
| 76 | import sys |
mbligh | 99d2ded | 2008-06-23 16:17:36 +0000 | [diff] [blame] | 77 | |
beeps | 98365d8 | 2013-02-20 20:08:07 -0800 | [diff] [blame] | 78 | import common |
| 79 | from autotest_lib.client.common_lib import autotemp, revision_control |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 80 | |
| 81 | # Do a basic check to see if pylint is even installed. |
mbligh | 65e06b1 | 2008-08-22 18:12:49 +0000 | [diff] [blame] | 82 | try: |
| 83 | import pylint |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 84 | from pylint.__pkginfo__ import version as pylint_version |
mbligh | 65e06b1 | 2008-08-22 18:12:49 +0000 | [diff] [blame] | 85 | except ImportError: |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 86 | print ("Unable to import pylint, it may need to be installed." |
| 87 | " Run 'sudo aptitude install pylint' if you haven't already.") |
mbligh | 65e06b1 | 2008-08-22 18:12:49 +0000 | [diff] [blame] | 88 | sys.exit(1) |
| 89 | |
Kazuhiro Inaba | 0e7bd16 | 2019-06-07 16:24:45 +0900 | [diff] [blame] | 90 | pylint_version_parsed = tuple(map(int, pylint_version.split('.'))) |
mbligh | 99d2ded | 2008-06-23 16:17:36 +0000 | [diff] [blame] | 91 | |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 92 | # some files make pylint blow up, so make sure we ignore them |
Greg Edelston | 4511c48 | 2020-11-02 10:31:43 -0700 | [diff] [blame] | 93 | SKIPLIST = ['/site-packages/*', '/contrib/*', '/frontend/afe/management.py'] |
jadmanski | 94a6493 | 2008-07-22 14:03:10 +0000 | [diff] [blame] | 94 | |
Mike Frysinger | cb57463 | 2019-09-20 10:43:45 -0400 | [diff] [blame] | 95 | import astroid |
mbligh | 99d2ded | 2008-06-23 16:17:36 +0000 | [diff] [blame] | 96 | import pylint.lint |
beeps | 2c66964 | 2013-01-14 18:30:57 -0800 | [diff] [blame] | 97 | from pylint.checkers import base, imports, variables |
mbligh | 99d2ded | 2008-06-23 16:17:36 +0000 | [diff] [blame] | 98 | |
| 99 | # need to put autotest root dir on sys.path so pylint will be happy |
| 100 | autotest_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) |
| 101 | sys.path.insert(0, autotest_root) |
| 102 | |
| 103 | # patch up pylint import checker to handle our importing magic |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 104 | ROOT_MODULE = 'autotest_lib.' |
beeps | e19d303 | 2013-05-30 09:22:07 -0700 | [diff] [blame] | 105 | |
| 106 | # A list of modules for pylint to ignore, specifically, these modules |
| 107 | # are imported for their side-effects and are not meant to be used. |
Prashanth B | 2d8047e | 2014-04-27 18:54:47 -0700 | [diff] [blame] | 108 | _IGNORE_MODULES=['common', 'frontend_test_utils', |
| 109 | 'setup_django_environment', |
Keyar Hood | f9a3651 | 2013-06-13 18:39:56 -0700 | [diff] [blame] | 110 | 'setup_django_lite_environment', |
Prashanth B | 2d8047e | 2014-04-27 18:54:47 -0700 | [diff] [blame] | 111 | 'setup_django_readonly_environment', 'setup_test_environment',] |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 112 | |
| 113 | |
beeps | 98365d8 | 2013-02-20 20:08:07 -0800 | [diff] [blame] | 114 | class pylint_error(Exception): |
| 115 | """ |
| 116 | Error raised when pylint complains about a file. |
| 117 | """ |
| 118 | |
| 119 | |
| 120 | class run_pylint_error(pylint_error): |
| 121 | """ |
| 122 | Error raised when an assumption made in this file is violated. |
| 123 | """ |
| 124 | |
| 125 | |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 126 | def patch_modname(modname): |
| 127 | """ |
| 128 | Patches modname so we can make sense of autotest_lib modules. |
| 129 | |
| 130 | @param modname: name of a module, contains '.' |
| 131 | @return modified modname string. |
| 132 | """ |
| 133 | if modname.startswith(ROOT_MODULE) or modname.startswith(ROOT_MODULE[:-1]): |
| 134 | modname = modname[len(ROOT_MODULE):] |
| 135 | return modname |
| 136 | |
| 137 | |
| 138 | def patch_consumed_list(to_consume=None, consumed=None): |
| 139 | """ |
beeps | e19d303 | 2013-05-30 09:22:07 -0700 | [diff] [blame] | 140 | Patches the consumed modules list to ignore modules with side effects. |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 141 | |
beeps | e19d303 | 2013-05-30 09:22:07 -0700 | [diff] [blame] | 142 | Autotest relies on importing certain modules solely for their side |
| 143 | effects. Pylint doesn't understand this and flags them as unused, since |
| 144 | they're not referenced anywhere in the code. To overcome this we need |
| 145 | to transplant said modules into the dictionary of modules pylint has |
| 146 | already seen, before pylint checks it. |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 147 | |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 148 | @param to_consume: a dictionary of names pylint needs to see referenced. |
| 149 | @param consumed: a dictionary of names that pylint has seen referenced. |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 150 | """ |
beeps | e19d303 | 2013-05-30 09:22:07 -0700 | [diff] [blame] | 151 | ignore_modules = [] |
| 152 | if (to_consume is not None and consumed is not None): |
| 153 | ignore_modules = [module_name for module_name in _IGNORE_MODULES |
| 154 | if module_name in to_consume] |
| 155 | |
| 156 | for module_name in ignore_modules: |
| 157 | consumed[module_name] = to_consume[module_name] |
| 158 | del to_consume[module_name] |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 159 | |
mbligh | 99d2ded | 2008-06-23 16:17:36 +0000 | [diff] [blame] | 160 | |
| 161 | class CustomImportsChecker(imports.ImportsChecker): |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 162 | """Modifies stock imports checker to suit autotest.""" |
Ryo Hashimoto | d35d563 | 2018-08-01 15:53:42 +0900 | [diff] [blame] | 163 | def visit_importfrom(self, node): |
Allen Li | 4c275d2 | 2017-07-19 11:56:24 -0700 | [diff] [blame] | 164 | """Patches modnames so pylints understands autotest_lib.""" |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 165 | node.modname = patch_modname(node.modname) |
Ryo Hashimoto | d35d563 | 2018-08-01 15:53:42 +0900 | [diff] [blame] | 166 | return super(CustomImportsChecker, self).visit_importfrom(node) |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 167 | |
| 168 | |
| 169 | class CustomVariablesChecker(variables.VariablesChecker): |
| 170 | """Modifies stock variables checker to suit autotest.""" |
| 171 | |
| 172 | def visit_module(self, node): |
| 173 | """ |
| 174 | Unflag 'import common'. |
| 175 | |
| 176 | _to_consume eg: [({to reference}, {referenced}, 'scope type')] |
| 177 | Enteries are appended to this list as we drill deeper in scope. |
beeps | e19d303 | 2013-05-30 09:22:07 -0700 | [diff] [blame] | 178 | If we ever come across a module to ignore, we immediately move it |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 179 | to the consumed list. |
| 180 | |
| 181 | @param node: node of the ast we're currently checking. |
| 182 | """ |
| 183 | super(CustomVariablesChecker, self).visit_module(node) |
| 184 | scoped_names = self._to_consume.pop() |
Kazuhiro Inaba | 0e7bd16 | 2019-06-07 16:24:45 +0900 | [diff] [blame] | 185 | # The type of the object has changed in pylint 1.8.2 |
| 186 | if pylint_version_parsed >= (1, 8, 2): |
| 187 | patch_consumed_list(scoped_names.to_consume,scoped_names.consumed) |
| 188 | else: |
| 189 | patch_consumed_list(scoped_names[0],scoped_names[1]) |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 190 | self._to_consume.append(scoped_names) |
| 191 | |
Ryo Hashimoto | d35d563 | 2018-08-01 15:53:42 +0900 | [diff] [blame] | 192 | def visit_importfrom(self, node): |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 193 | """Patches modnames so pylints understands autotest_lib.""" |
| 194 | node.modname = patch_modname(node.modname) |
Ryo Hashimoto | d35d563 | 2018-08-01 15:53:42 +0900 | [diff] [blame] | 195 | return super(CustomVariablesChecker, self).visit_importfrom(node) |
mbligh | 99d2ded | 2008-06-23 16:17:36 +0000 | [diff] [blame] | 196 | |
Mike Frysinger | cb57463 | 2019-09-20 10:43:45 -0400 | [diff] [blame] | 197 | def visit_expr(self, node): |
| 198 | """ |
| 199 | Flag exceptions instantiated but not used. |
| 200 | |
| 201 | https://crbug.com/1005893 |
| 202 | """ |
| 203 | if not isinstance(node.value, astroid.Call): |
| 204 | return |
| 205 | func = node.value.func |
| 206 | try: |
| 207 | cls = next(func.infer()) |
| 208 | except astroid.InferenceError: |
| 209 | return |
| 210 | if not isinstance(cls, astroid.ClassDef): |
| 211 | return |
| 212 | if any(x for x in cls.ancestors() if x.name == 'BaseException'): |
| 213 | self.add_message('W0104', node=node, line=node.fromlineno) |
| 214 | |
beeps | 2c66964 | 2013-01-14 18:30:57 -0800 | [diff] [blame] | 215 | |
| 216 | class CustomDocStringChecker(base.DocStringChecker): |
| 217 | """Modifies stock docstring checker to suit Autotest doxygen style.""" |
| 218 | |
beeps | 1b6433b | 2013-01-31 17:46:50 -0800 | [diff] [blame] | 219 | def visit_module(self, node): |
| 220 | """ |
| 221 | Don't visit imported modules when checking for docstrings. |
| 222 | |
| 223 | @param node: the node we're visiting. |
| 224 | """ |
| 225 | pass |
| 226 | |
| 227 | |
Ryo Hashimoto | d35d563 | 2018-08-01 15:53:42 +0900 | [diff] [blame] | 228 | def visit_functiondef(self, node): |
beeps | 98365d8 | 2013-02-20 20:08:07 -0800 | [diff] [blame] | 229 | """ |
| 230 | Don't request docstrings for commonly overridden autotest functions. |
| 231 | |
| 232 | @param node: node of the ast we're currently checking. |
| 233 | """ |
beeps | c38decc | 2013-04-29 19:42:06 -0700 | [diff] [blame] | 234 | |
| 235 | # Even plain functions will have a parent, which is the |
| 236 | # module they're in, and a frame, which is the context |
| 237 | # of said module; They need not however, always have |
| 238 | # ancestors. |
beeps | 98365d8 | 2013-02-20 20:08:07 -0800 | [diff] [blame] | 239 | if (node.name in ('run_once', 'initialize', 'cleanup') and |
beeps | c38decc | 2013-04-29 19:42:06 -0700 | [diff] [blame] | 240 | hasattr(node.parent.frame(), 'ancestors') and |
beeps | 98365d8 | 2013-02-20 20:08:07 -0800 | [diff] [blame] | 241 | any(ancestor.name == 'base_test' for ancestor in |
| 242 | node.parent.frame().ancestors())): |
| 243 | return |
| 244 | |
Prathmesh Prabhu | a998067 | 2017-07-13 15:52:02 -0700 | [diff] [blame] | 245 | if _is_test_case_method(node): |
| 246 | return |
| 247 | |
Ryo Hashimoto | d35d563 | 2018-08-01 15:53:42 +0900 | [diff] [blame] | 248 | super(CustomDocStringChecker, self).visit_functiondef(node) |
beeps | 98365d8 | 2013-02-20 20:08:07 -0800 | [diff] [blame] | 249 | |
| 250 | |
Chris Sosa | ae6acd9 | 2013-02-06 15:08:04 -0800 | [diff] [blame] | 251 | @staticmethod |
| 252 | def _should_skip_arg(arg): |
beeps | 98365d8 | 2013-02-20 20:08:07 -0800 | [diff] [blame] | 253 | """ |
Greg Edelston | 4511c48 | 2020-11-02 10:31:43 -0700 | [diff] [blame] | 254 | @return: True if the argument given by arg is allowlisted, and does |
beeps | 98365d8 | 2013-02-20 20:08:07 -0800 | [diff] [blame] | 255 | not require a "@param" docstring. |
| 256 | """ |
Chris Sosa | ae6acd9 | 2013-02-06 15:08:04 -0800 | [diff] [blame] | 257 | return arg in ('self', 'cls', 'args', 'kwargs', 'dargs') |
| 258 | |
beeps | 2c66964 | 2013-01-14 18:30:57 -0800 | [diff] [blame] | 259 | base.DocStringChecker = CustomDocStringChecker |
mbligh | 99d2ded | 2008-06-23 16:17:36 +0000 | [diff] [blame] | 260 | imports.ImportsChecker = CustomImportsChecker |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 261 | variables.VariablesChecker = CustomVariablesChecker |
mbligh | 99d2ded | 2008-06-23 16:17:36 +0000 | [diff] [blame] | 262 | |
| 263 | |
beeps | 98365d8 | 2013-02-20 20:08:07 -0800 | [diff] [blame] | 264 | def batch_check_files(file_paths, base_opts): |
| 265 | """ |
| 266 | Run pylint on a list of files so we get consolidated errors. |
| 267 | |
| 268 | @param file_paths: a list of file paths. |
| 269 | @param base_opts: a list of pylint config options. |
| 270 | |
Raul E Rangel | 67d9cfc | 2019-01-30 16:28:16 -0700 | [diff] [blame] | 271 | @returns pylint return code |
| 272 | |
beeps | 98365d8 | 2013-02-20 20:08:07 -0800 | [diff] [blame] | 273 | @raises: pylint_error if pylint finds problems with a file |
| 274 | in this commit. |
| 275 | """ |
beeps | 12a3c88 | 2013-04-22 13:42:04 -0700 | [diff] [blame] | 276 | if not file_paths: |
Raul E Rangel | 67d9cfc | 2019-01-30 16:28:16 -0700 | [diff] [blame] | 277 | return 0 |
beeps | 12a3c88 | 2013-04-22 13:42:04 -0700 | [diff] [blame] | 278 | |
beeps | 98365d8 | 2013-02-20 20:08:07 -0800 | [diff] [blame] | 279 | pylint_runner = pylint.lint.Run(list(base_opts) + list(file_paths), |
| 280 | exit=False) |
Raul E Rangel | 67d9cfc | 2019-01-30 16:28:16 -0700 | [diff] [blame] | 281 | return pylint_runner.linter.msg_status |
beeps | 98365d8 | 2013-02-20 20:08:07 -0800 | [diff] [blame] | 282 | |
| 283 | |
| 284 | def should_check_file(file_path): |
| 285 | """ |
Greg Edelston | 4511c48 | 2020-11-02 10:31:43 -0700 | [diff] [blame] | 286 | Don't check skiplisted or non .py files. |
beeps | 98365d8 | 2013-02-20 20:08:07 -0800 | [diff] [blame] | 287 | |
| 288 | @param file_path: abs path of file to check. |
Greg Edelston | 4511c48 | 2020-11-02 10:31:43 -0700 | [diff] [blame] | 289 | @return: True if this file is a non-skiplisted python file. |
beeps | 98365d8 | 2013-02-20 20:08:07 -0800 | [diff] [blame] | 290 | """ |
| 291 | file_path = os.path.abspath(file_path) |
| 292 | if file_path.endswith('.py'): |
| 293 | return all(not fnmatch.fnmatch(file_path, '*' + pattern) |
Greg Edelston | 4511c48 | 2020-11-02 10:31:43 -0700 | [diff] [blame] | 294 | for pattern in SKIPLIST) |
beeps | 98365d8 | 2013-02-20 20:08:07 -0800 | [diff] [blame] | 295 | return False |
| 296 | |
| 297 | |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 298 | def check_file(file_path, base_opts): |
| 299 | """ |
| 300 | Invokes pylint on files after confirming that they're not black listed. |
| 301 | |
beeps | 2c66964 | 2013-01-14 18:30:57 -0800 | [diff] [blame] | 302 | @param base_opts: pylint base options. |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 303 | @param file_path: path to the file we need to run pylint on. |
Raul E Rangel | 67d9cfc | 2019-01-30 16:28:16 -0700 | [diff] [blame] | 304 | |
| 305 | @returns pylint return code |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 306 | """ |
beeps | 98365d8 | 2013-02-20 20:08:07 -0800 | [diff] [blame] | 307 | if not isinstance(file_path, basestring): |
| 308 | raise TypeError('expected a string as filepath, got %s'% |
| 309 | type(file_path)) |
| 310 | |
| 311 | if should_check_file(file_path): |
| 312 | pylint_runner = pylint.lint.Run(base_opts + [file_path], exit=False) |
Raul E Rangel | 67d9cfc | 2019-01-30 16:28:16 -0700 | [diff] [blame] | 313 | |
| 314 | return pylint_runner.linter.msg_status |
| 315 | |
| 316 | return 0 |
mbligh | 99d2ded | 2008-06-23 16:17:36 +0000 | [diff] [blame] | 317 | |
| 318 | |
| 319 | def visit(arg, dirname, filenames): |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 320 | """ |
| 321 | Visit function invoked in check_dir. |
| 322 | |
| 323 | @param arg: arg from os.walk.path |
| 324 | @param dirname: dir from os.walk.path |
| 325 | @param filenames: files in dir from os.walk.path |
| 326 | """ |
mbligh | 99d2ded | 2008-06-23 16:17:36 +0000 | [diff] [blame] | 327 | for filename in filenames: |
Raul E Rangel | 67d9cfc | 2019-01-30 16:28:16 -0700 | [diff] [blame] | 328 | arg.append(os.path.join(dirname, filename)) |
mbligh | 99d2ded | 2008-06-23 16:17:36 +0000 | [diff] [blame] | 329 | |
| 330 | |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 331 | def check_dir(dir_path, base_opts): |
| 332 | """ |
| 333 | Calls visit on files in dir_path. |
| 334 | |
beeps | 2c66964 | 2013-01-14 18:30:57 -0800 | [diff] [blame] | 335 | @param base_opts: pylint base options. |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 336 | @param dir_path: path to directory. |
Raul E Rangel | 67d9cfc | 2019-01-30 16:28:16 -0700 | [diff] [blame] | 337 | |
| 338 | @returns pylint return code |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 339 | """ |
Raul E Rangel | 67d9cfc | 2019-01-30 16:28:16 -0700 | [diff] [blame] | 340 | files = [] |
| 341 | |
| 342 | os.path.walk(dir_path, visit, files) |
| 343 | |
| 344 | return batch_check_files(files, base_opts) |
mbligh | 99d2ded | 2008-06-23 16:17:36 +0000 | [diff] [blame] | 345 | |
| 346 | |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 347 | def extend_baseopts(base_opts, new_opt): |
| 348 | """ |
| 349 | Replaces an argument in base_opts with a cmd line argument. |
| 350 | |
| 351 | @param base_opts: original pylint_base_opts. |
| 352 | @param new_opt: new cmd line option. |
| 353 | """ |
| 354 | for args in base_opts: |
| 355 | if new_opt in args: |
| 356 | base_opts.remove(args) |
| 357 | base_opts.append(new_opt) |
| 358 | |
| 359 | |
| 360 | def get_cmdline_options(args_list, pylint_base_opts, rcfile): |
| 361 | """ |
| 362 | Parses args_list and extends pylint_base_opts. |
| 363 | |
| 364 | Command line arguments might include options mixed with files. |
| 365 | Go through this list and filter out the options, if the options are |
| 366 | specified in the pylintrc file we cannot replace them and the file |
| 367 | needs to be edited. If the options are already a part of |
| 368 | pylint_base_opts we replace them, and if not we append to |
| 369 | pylint_base_opts. |
| 370 | |
| 371 | @param args_list: list of files/pylint args passed in through argv. |
| 372 | @param pylint_base_opts: default pylint options. |
| 373 | @param rcfile: text from pylint_rc. |
| 374 | """ |
| 375 | for args in args_list: |
| 376 | if args.startswith('--'): |
| 377 | opt_name = args[2:].split('=')[0] |
Raul E Rangel | d214c09 | 2019-01-30 16:48:43 -0700 | [diff] [blame] | 378 | extend_baseopts(pylint_base_opts, args) |
| 379 | args_list.remove(args) |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 380 | |
beeps | 98365d8 | 2013-02-20 20:08:07 -0800 | [diff] [blame] | 381 | |
| 382 | def git_show_to_temp_file(commit, original_file, new_temp_file): |
| 383 | """ |
| 384 | 'Git shows' the file in original_file to a tmp file with |
| 385 | the name new_temp_file. We need to preserve the filename |
| 386 | as it gets reflected in pylints error report. |
| 387 | |
| 388 | @param commit: commit hash of the commit we're running repo upload on. |
| 389 | @param original_file: the path to the original file we'd like to run |
| 390 | 'git show' on. |
| 391 | @param new_temp_file: new_temp_file is the path to a temp file we write the |
| 392 | output of 'git show' into. |
| 393 | """ |
| 394 | git_repo = revision_control.GitRepo(common.autotest_dir, None, None, |
| 395 | common.autotest_dir) |
| 396 | |
| 397 | with open(new_temp_file, 'w') as f: |
| 398 | output = git_repo.gitcmd('show --no-ext-diff %s:%s' |
| 399 | % (commit, original_file), |
| 400 | ignore_status=False).stdout |
| 401 | f.write(output) |
| 402 | |
| 403 | |
| 404 | def check_committed_files(work_tree_files, commit, pylint_base_opts): |
| 405 | """ |
| 406 | Get a list of files corresponding to the commit hash. |
| 407 | |
| 408 | The contents of a file in the git work tree can differ from the contents |
| 409 | of a file in the commit we mean to upload. To work around this we run |
| 410 | pylint on a temp file into which we've 'git show'n the committed version |
| 411 | of each file. |
| 412 | |
| 413 | @param work_tree_files: list of files in this commit specified by their |
| 414 | absolute path. |
| 415 | @param commit: hash of the commit this upload applies to. |
| 416 | @param pylint_base_opts: a list of pylint config options. |
Raul E Rangel | 67d9cfc | 2019-01-30 16:28:16 -0700 | [diff] [blame] | 417 | |
| 418 | @returns pylint return code |
beeps | 98365d8 | 2013-02-20 20:08:07 -0800 | [diff] [blame] | 419 | """ |
| 420 | files_to_check = filter(should_check_file, work_tree_files) |
| 421 | |
| 422 | # Map the absolute path of each file so it's relative to the autotest repo. |
| 423 | # All files that are a part of this commit should have an abs path within |
| 424 | # the autotest repo, so this regex should never fail. |
| 425 | work_tree_files = [re.search(r'%s/(.*)' % common.autotest_dir, f).group(1) |
| 426 | for f in files_to_check] |
| 427 | |
| 428 | tempdir = None |
| 429 | try: |
| 430 | tempdir = autotemp.tempdir() |
| 431 | temp_files = [os.path.join(tempdir.name, file_path.split('/')[-1:][0]) |
| 432 | for file_path in work_tree_files] |
| 433 | |
| 434 | for file_tuple in zip(work_tree_files, temp_files): |
| 435 | git_show_to_temp_file(commit, *file_tuple) |
| 436 | # Only check if we successfully git showed all files in the commit. |
Raul E Rangel | 67d9cfc | 2019-01-30 16:28:16 -0700 | [diff] [blame] | 437 | return batch_check_files(temp_files, pylint_base_opts) |
beeps | 98365d8 | 2013-02-20 20:08:07 -0800 | [diff] [blame] | 438 | finally: |
| 439 | if tempdir: |
| 440 | tempdir.clean() |
| 441 | |
| 442 | |
Prathmesh Prabhu | a998067 | 2017-07-13 15:52:02 -0700 | [diff] [blame] | 443 | def _is_test_case_method(node): |
| 444 | """Determine if the given function node is a method of a TestCase. |
| 445 | |
| 446 | We simply check for 'TestCase' being one of the parent classes in the mro of |
| 447 | the containing class. |
| 448 | |
| 449 | @params node: A function node. |
| 450 | """ |
| 451 | if not hasattr(node.parent.frame(), 'ancestors'): |
| 452 | return False |
| 453 | |
| 454 | parent_class_names = {x.name for x in node.parent.frame().ancestors()} |
| 455 | return 'TestCase' in parent_class_names |
| 456 | |
| 457 | |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 458 | def main(): |
| 459 | """Main function checks each file in a commit for pylint violations.""" |
| 460 | |
beeps | 1b6433b | 2013-01-31 17:46:50 -0800 | [diff] [blame] | 461 | # For now all error/warning/refactor/convention exceptions except those in |
| 462 | # the enable string are disabled. |
| 463 | # W0611: All imported modules (except common) need to be used. |
| 464 | # W1201: Logging methods should take the form |
| 465 | # logging.<loggingmethod>(format_string, format_args...); and not |
| 466 | # logging.<loggingmethod>(format_string % (format_args...)) |
| 467 | # C0111: Docstring needed. Also checks @param for each arg. |
| 468 | # C0112: Non-empty Docstring needed. |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 469 | # Ideally we would like to enable as much as we can, but if we did so at |
| 470 | # this stage anyone who makes a tiny change to a file will be tasked with |
| 471 | # cleaning all the lint in it. See chromium-os:37364. |
| 472 | |
beeps | e19d303 | 2013-05-30 09:22:07 -0700 | [diff] [blame] | 473 | # Note: |
| 474 | # 1. There are three major sources of E1101/E1103/E1120 false positives: |
| 475 | # * common_lib.enum.Enum objects |
| 476 | # * DB model objects (scheduler models are the worst, but Django models |
| 477 | # also generate some errors) |
| 478 | # 2. Docstrings are optional on private methods, and any methods that begin |
| 479 | # with either 'set_' or 'get_'. |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 480 | pylint_rc = os.path.join(os.path.dirname(os.path.abspath(__file__)), |
| 481 | 'pylintrc') |
beeps | e19d303 | 2013-05-30 09:22:07 -0700 | [diff] [blame] | 482 | |
| 483 | no_docstring_rgx = r'((_.*)|(set_.*)|(get_.*))' |
Kazuhiro Inaba | 0e7bd16 | 2019-06-07 16:24:45 +0900 | [diff] [blame] | 484 | if pylint_version_parsed >= (0, 21): |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 485 | pylint_base_opts = ['--rcfile=%s' % pylint_rc, |
| 486 | '--reports=no', |
beeps | 5553256 | 2013-01-16 12:13:46 -0800 | [diff] [blame] | 487 | '--disable=W,R,E,C,F', |
Mike Frysinger | cb57463 | 2019-09-20 10:43:45 -0400 | [diff] [blame] | 488 | '--enable=W0104,W0611,W1201,C0111,C0112,E0602,' |
| 489 | 'W0601,E0633', |
beeps | e19d303 | 2013-05-30 09:22:07 -0700 | [diff] [blame] | 490 | '--no-docstring-rgx=%s' % no_docstring_rgx,] |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 491 | else: |
| 492 | all_failures = 'error,warning,refactor,convention' |
| 493 | pylint_base_opts = ['--disable-msg-cat=%s' % all_failures, |
| 494 | '--reports=no', |
| 495 | '--include-ids=y', |
beeps | 1b6433b | 2013-01-31 17:46:50 -0800 | [diff] [blame] | 496 | '--ignore-docstrings=n', |
beeps | e19d303 | 2013-05-30 09:22:07 -0700 | [diff] [blame] | 497 | '--no-docstring-rgx=%s' % no_docstring_rgx,] |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 498 | |
| 499 | # run_pylint can be invoked directly with command line arguments, |
| 500 | # or through a presubmit hook which uses the arguments in pylintrc. In the |
| 501 | # latter case no command line arguments are passed. If it is invoked |
| 502 | # directly without any arguments, it should check all files in the cwd. |
| 503 | args_list = sys.argv[1:] |
| 504 | if args_list: |
| 505 | get_cmdline_options(args_list, |
| 506 | pylint_base_opts, |
| 507 | open(pylint_rc).read()) |
Raul E Rangel | 67d9cfc | 2019-01-30 16:28:16 -0700 | [diff] [blame] | 508 | return batch_check_files(args_list, pylint_base_opts) |
beeps | 98365d8 | 2013-02-20 20:08:07 -0800 | [diff] [blame] | 509 | elif os.environ.get('PRESUBMIT_FILES') is not None: |
Raul E Rangel | 67d9cfc | 2019-01-30 16:28:16 -0700 | [diff] [blame] | 510 | return check_committed_files( |
beeps | 98365d8 | 2013-02-20 20:08:07 -0800 | [diff] [blame] | 511 | os.environ.get('PRESUBMIT_FILES').split('\n'), |
| 512 | os.environ.get('PRESUBMIT_COMMIT'), |
| 513 | pylint_base_opts) |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 514 | else: |
Raul E Rangel | 67d9cfc | 2019-01-30 16:28:16 -0700 | [diff] [blame] | 515 | return check_dir('.', pylint_base_opts) |
beeps | 48fc6f5 | 2013-01-07 14:36:40 -0800 | [diff] [blame] | 516 | |
| 517 | |
| 518 | if __name__ == '__main__': |
Prashanth Balasubramanian | baabef2 | 2014-11-04 12:38:44 -0800 | [diff] [blame] | 519 | try: |
Raul E Rangel | 67d9cfc | 2019-01-30 16:28:16 -0700 | [diff] [blame] | 520 | ret = main() |
| 521 | |
| 522 | sys.exit(ret) |
Prathmesh Prabhu | 7e10708 | 2017-01-11 17:16:41 -0800 | [diff] [blame] | 523 | except pylint_error as e: |
Prashanth Balasubramanian | baabef2 | 2014-11-04 12:38:44 -0800 | [diff] [blame] | 524 | logging.error(e) |
| 525 | sys.exit(1) |