blob: dd04ca65ee9b2e3f630f96aef48f8bf506d06b2e [file] [log] [blame]
mbligh99d2ded2008-06-23 16:17:36 +00001#!/usr/bin/python -u
beeps48fc6f52013-01-07 14:36:40 -08002"""
3Wrapper to patch pylint library functions to suit autotest.
4
5This script is invoked as part of the presubmit checks for autotest python
6files. It runs pylint on a list of files that it obtains either through
7the command line or from an environment variable set in pre-upload.py.
8
9Example:
10run_pylint.py filename.py
11"""
mbligh99d2ded2008-06-23 16:17:36 +000012
Prashanth Balasubramanianbaabef22014-11-04 12:38:44 -080013import fnmatch
14import logging
15import os
16import re
17import sys
mbligh99d2ded2008-06-23 16:17:36 +000018
beeps98365d82013-02-20 20:08:07 -080019import common
20from autotest_lib.client.common_lib import autotemp, revision_control
beeps48fc6f52013-01-07 14:36:40 -080021
22# Do a basic check to see if pylint is even installed.
mbligh65e06b12008-08-22 18:12:49 +000023try:
24 import pylint
Eric Li861b2d52011-02-04 14:50:35 -080025 from pylint.__pkginfo__ import version as pylint_version
mbligh65e06b12008-08-22 18:12:49 +000026except ImportError:
beeps48fc6f52013-01-07 14:36:40 -080027 print ("Unable to import pylint, it may need to be installed."
28 " Run 'sudo aptitude install pylint' if you haven't already.")
mbligh65e06b12008-08-22 18:12:49 +000029 sys.exit(1)
30
Eric Li861b2d52011-02-04 14:50:35 -080031major, minor, release = pylint_version.split('.')
32pylint_version = float("%s.%s" % (major, minor))
mbligh99d2ded2008-06-23 16:17:36 +000033
beeps48fc6f52013-01-07 14:36:40 -080034# some files make pylint blow up, so make sure we ignore them
Prathmesh Prabhu5dab08c2017-01-12 11:16:56 -080035BLACKLIST = ['/site-packages/*', '/contrib/*', '/frontend/afe/management.py']
jadmanski94a64932008-07-22 14:03:10 +000036
mbligh99d2ded2008-06-23 16:17:36 +000037import pylint.lint
beeps2c669642013-01-14 18:30:57 -080038from pylint.checkers import base, imports, variables
mbligh99d2ded2008-06-23 16:17:36 +000039
40# need to put autotest root dir on sys.path so pylint will be happy
41autotest_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
42sys.path.insert(0, autotest_root)
43
44# patch up pylint import checker to handle our importing magic
beeps48fc6f52013-01-07 14:36:40 -080045ROOT_MODULE = 'autotest_lib.'
beepse19d3032013-05-30 09:22:07 -070046
47# A list of modules for pylint to ignore, specifically, these modules
48# are imported for their side-effects and are not meant to be used.
Prashanth B2d8047e2014-04-27 18:54:47 -070049_IGNORE_MODULES=['common', 'frontend_test_utils',
50 'setup_django_environment',
Keyar Hoodf9a36512013-06-13 18:39:56 -070051 'setup_django_lite_environment',
Prashanth B2d8047e2014-04-27 18:54:47 -070052 'setup_django_readonly_environment', 'setup_test_environment',]
beeps48fc6f52013-01-07 14:36:40 -080053
54
beeps98365d82013-02-20 20:08:07 -080055class pylint_error(Exception):
56 """
57 Error raised when pylint complains about a file.
58 """
59
60
61class run_pylint_error(pylint_error):
62 """
63 Error raised when an assumption made in this file is violated.
64 """
65
66
beeps48fc6f52013-01-07 14:36:40 -080067def patch_modname(modname):
68 """
69 Patches modname so we can make sense of autotest_lib modules.
70
71 @param modname: name of a module, contains '.'
72 @return modified modname string.
73 """
74 if modname.startswith(ROOT_MODULE) or modname.startswith(ROOT_MODULE[:-1]):
75 modname = modname[len(ROOT_MODULE):]
76 return modname
77
78
79def patch_consumed_list(to_consume=None, consumed=None):
80 """
beepse19d3032013-05-30 09:22:07 -070081 Patches the consumed modules list to ignore modules with side effects.
beeps48fc6f52013-01-07 14:36:40 -080082
beepse19d3032013-05-30 09:22:07 -070083 Autotest relies on importing certain modules solely for their side
84 effects. Pylint doesn't understand this and flags them as unused, since
85 they're not referenced anywhere in the code. To overcome this we need
86 to transplant said modules into the dictionary of modules pylint has
87 already seen, before pylint checks it.
beeps48fc6f52013-01-07 14:36:40 -080088
beeps48fc6f52013-01-07 14:36:40 -080089 @param to_consume: a dictionary of names pylint needs to see referenced.
90 @param consumed: a dictionary of names that pylint has seen referenced.
beeps48fc6f52013-01-07 14:36:40 -080091 """
beepse19d3032013-05-30 09:22:07 -070092 ignore_modules = []
93 if (to_consume is not None and consumed is not None):
94 ignore_modules = [module_name for module_name in _IGNORE_MODULES
95 if module_name in to_consume]
96
97 for module_name in ignore_modules:
98 consumed[module_name] = to_consume[module_name]
99 del to_consume[module_name]
beeps48fc6f52013-01-07 14:36:40 -0800100
mbligh99d2ded2008-06-23 16:17:36 +0000101
102class CustomImportsChecker(imports.ImportsChecker):
beeps48fc6f52013-01-07 14:36:40 -0800103 """Modifies stock imports checker to suit autotest."""
Ryo Hashimotod35d5632018-08-01 15:53:42 +0900104 def visit_importfrom(self, node):
Allen Li4c275d22017-07-19 11:56:24 -0700105 """Patches modnames so pylints understands autotest_lib."""
beeps48fc6f52013-01-07 14:36:40 -0800106 node.modname = patch_modname(node.modname)
Ryo Hashimotod35d5632018-08-01 15:53:42 +0900107 return super(CustomImportsChecker, self).visit_importfrom(node)
beeps48fc6f52013-01-07 14:36:40 -0800108
109
110class CustomVariablesChecker(variables.VariablesChecker):
111 """Modifies stock variables checker to suit autotest."""
112
113 def visit_module(self, node):
114 """
115 Unflag 'import common'.
116
117 _to_consume eg: [({to reference}, {referenced}, 'scope type')]
118 Enteries are appended to this list as we drill deeper in scope.
beepse19d3032013-05-30 09:22:07 -0700119 If we ever come across a module to ignore, we immediately move it
beeps48fc6f52013-01-07 14:36:40 -0800120 to the consumed list.
121
122 @param node: node of the ast we're currently checking.
123 """
124 super(CustomVariablesChecker, self).visit_module(node)
125 scoped_names = self._to_consume.pop()
126 patch_consumed_list(scoped_names[0],scoped_names[1])
127 self._to_consume.append(scoped_names)
128
Ryo Hashimotod35d5632018-08-01 15:53:42 +0900129 def visit_importfrom(self, node):
beeps48fc6f52013-01-07 14:36:40 -0800130 """Patches modnames so pylints understands autotest_lib."""
131 node.modname = patch_modname(node.modname)
Ryo Hashimotod35d5632018-08-01 15:53:42 +0900132 return super(CustomVariablesChecker, self).visit_importfrom(node)
mbligh99d2ded2008-06-23 16:17:36 +0000133
beeps2c669642013-01-14 18:30:57 -0800134
135class CustomDocStringChecker(base.DocStringChecker):
136 """Modifies stock docstring checker to suit Autotest doxygen style."""
137
beeps1b6433b2013-01-31 17:46:50 -0800138 def visit_module(self, node):
139 """
140 Don't visit imported modules when checking for docstrings.
141
142 @param node: the node we're visiting.
143 """
144 pass
145
146
Ryo Hashimotod35d5632018-08-01 15:53:42 +0900147 def visit_functiondef(self, node):
beeps98365d82013-02-20 20:08:07 -0800148 """
149 Don't request docstrings for commonly overridden autotest functions.
150
151 @param node: node of the ast we're currently checking.
152 """
beepsc38decc2013-04-29 19:42:06 -0700153
154 # Even plain functions will have a parent, which is the
155 # module they're in, and a frame, which is the context
156 # of said module; They need not however, always have
157 # ancestors.
beeps98365d82013-02-20 20:08:07 -0800158 if (node.name in ('run_once', 'initialize', 'cleanup') and
beepsc38decc2013-04-29 19:42:06 -0700159 hasattr(node.parent.frame(), 'ancestors') and
beeps98365d82013-02-20 20:08:07 -0800160 any(ancestor.name == 'base_test' for ancestor in
161 node.parent.frame().ancestors())):
162 return
163
Prathmesh Prabhua9980672017-07-13 15:52:02 -0700164 if _is_test_case_method(node):
165 return
166
Ryo Hashimotod35d5632018-08-01 15:53:42 +0900167 super(CustomDocStringChecker, self).visit_functiondef(node)
beeps98365d82013-02-20 20:08:07 -0800168
169
Chris Sosaae6acd92013-02-06 15:08:04 -0800170 @staticmethod
171 def _should_skip_arg(arg):
beeps98365d82013-02-20 20:08:07 -0800172 """
173 @return: True if the argument given by arg is whitelisted, and does
174 not require a "@param" docstring.
175 """
Chris Sosaae6acd92013-02-06 15:08:04 -0800176 return arg in ('self', 'cls', 'args', 'kwargs', 'dargs')
177
beeps2c669642013-01-14 18:30:57 -0800178base.DocStringChecker = CustomDocStringChecker
mbligh99d2ded2008-06-23 16:17:36 +0000179imports.ImportsChecker = CustomImportsChecker
beeps48fc6f52013-01-07 14:36:40 -0800180variables.VariablesChecker = CustomVariablesChecker
mbligh99d2ded2008-06-23 16:17:36 +0000181
182
beeps98365d82013-02-20 20:08:07 -0800183def batch_check_files(file_paths, base_opts):
184 """
185 Run pylint on a list of files so we get consolidated errors.
186
187 @param file_paths: a list of file paths.
188 @param base_opts: a list of pylint config options.
189
Raul E Rangel67d9cfc2019-01-30 16:28:16 -0700190 @returns pylint return code
191
beeps98365d82013-02-20 20:08:07 -0800192 @raises: pylint_error if pylint finds problems with a file
193 in this commit.
194 """
beeps12a3c882013-04-22 13:42:04 -0700195 if not file_paths:
Raul E Rangel67d9cfc2019-01-30 16:28:16 -0700196 return 0
beeps12a3c882013-04-22 13:42:04 -0700197
beeps98365d82013-02-20 20:08:07 -0800198 pylint_runner = pylint.lint.Run(list(base_opts) + list(file_paths),
199 exit=False)
Raul E Rangel67d9cfc2019-01-30 16:28:16 -0700200 return pylint_runner.linter.msg_status
beeps98365d82013-02-20 20:08:07 -0800201
202
203def should_check_file(file_path):
204 """
205 Don't check blacklisted or non .py files.
206
207 @param file_path: abs path of file to check.
208 @return: True if this file is a non-blacklisted python file.
209 """
210 file_path = os.path.abspath(file_path)
211 if file_path.endswith('.py'):
212 return all(not fnmatch.fnmatch(file_path, '*' + pattern)
213 for pattern in BLACKLIST)
214 return False
215
216
beeps48fc6f52013-01-07 14:36:40 -0800217def check_file(file_path, base_opts):
218 """
219 Invokes pylint on files after confirming that they're not black listed.
220
beeps2c669642013-01-14 18:30:57 -0800221 @param base_opts: pylint base options.
beeps48fc6f52013-01-07 14:36:40 -0800222 @param file_path: path to the file we need to run pylint on.
Raul E Rangel67d9cfc2019-01-30 16:28:16 -0700223
224 @returns pylint return code
beeps48fc6f52013-01-07 14:36:40 -0800225 """
beeps98365d82013-02-20 20:08:07 -0800226 if not isinstance(file_path, basestring):
227 raise TypeError('expected a string as filepath, got %s'%
228 type(file_path))
229
230 if should_check_file(file_path):
231 pylint_runner = pylint.lint.Run(base_opts + [file_path], exit=False)
Raul E Rangel67d9cfc2019-01-30 16:28:16 -0700232
233 return pylint_runner.linter.msg_status
234
235 return 0
mbligh99d2ded2008-06-23 16:17:36 +0000236
237
238def visit(arg, dirname, filenames):
beeps48fc6f52013-01-07 14:36:40 -0800239 """
240 Visit function invoked in check_dir.
241
242 @param arg: arg from os.walk.path
243 @param dirname: dir from os.walk.path
244 @param filenames: files in dir from os.walk.path
245 """
mbligh99d2ded2008-06-23 16:17:36 +0000246 for filename in filenames:
Raul E Rangel67d9cfc2019-01-30 16:28:16 -0700247 arg.append(os.path.join(dirname, filename))
mbligh99d2ded2008-06-23 16:17:36 +0000248
249
beeps48fc6f52013-01-07 14:36:40 -0800250def check_dir(dir_path, base_opts):
251 """
252 Calls visit on files in dir_path.
253
beeps2c669642013-01-14 18:30:57 -0800254 @param base_opts: pylint base options.
beeps48fc6f52013-01-07 14:36:40 -0800255 @param dir_path: path to directory.
Raul E Rangel67d9cfc2019-01-30 16:28:16 -0700256
257 @returns pylint return code
beeps48fc6f52013-01-07 14:36:40 -0800258 """
Raul E Rangel67d9cfc2019-01-30 16:28:16 -0700259 files = []
260
261 os.path.walk(dir_path, visit, files)
262
263 return batch_check_files(files, base_opts)
mbligh99d2ded2008-06-23 16:17:36 +0000264
265
beeps48fc6f52013-01-07 14:36:40 -0800266def extend_baseopts(base_opts, new_opt):
267 """
268 Replaces an argument in base_opts with a cmd line argument.
269
270 @param base_opts: original pylint_base_opts.
271 @param new_opt: new cmd line option.
272 """
273 for args in base_opts:
274 if new_opt in args:
275 base_opts.remove(args)
276 base_opts.append(new_opt)
277
278
279def get_cmdline_options(args_list, pylint_base_opts, rcfile):
280 """
281 Parses args_list and extends pylint_base_opts.
282
283 Command line arguments might include options mixed with files.
284 Go through this list and filter out the options, if the options are
285 specified in the pylintrc file we cannot replace them and the file
286 needs to be edited. If the options are already a part of
287 pylint_base_opts we replace them, and if not we append to
288 pylint_base_opts.
289
290 @param args_list: list of files/pylint args passed in through argv.
291 @param pylint_base_opts: default pylint options.
292 @param rcfile: text from pylint_rc.
293 """
294 for args in args_list:
295 if args.startswith('--'):
296 opt_name = args[2:].split('=')[0]
Raul E Rangeld214c092019-01-30 16:48:43 -0700297 extend_baseopts(pylint_base_opts, args)
298 args_list.remove(args)
beeps48fc6f52013-01-07 14:36:40 -0800299
beeps98365d82013-02-20 20:08:07 -0800300
301def git_show_to_temp_file(commit, original_file, new_temp_file):
302 """
303 'Git shows' the file in original_file to a tmp file with
304 the name new_temp_file. We need to preserve the filename
305 as it gets reflected in pylints error report.
306
307 @param commit: commit hash of the commit we're running repo upload on.
308 @param original_file: the path to the original file we'd like to run
309 'git show' on.
310 @param new_temp_file: new_temp_file is the path to a temp file we write the
311 output of 'git show' into.
312 """
313 git_repo = revision_control.GitRepo(common.autotest_dir, None, None,
314 common.autotest_dir)
315
316 with open(new_temp_file, 'w') as f:
317 output = git_repo.gitcmd('show --no-ext-diff %s:%s'
318 % (commit, original_file),
319 ignore_status=False).stdout
320 f.write(output)
321
322
323def check_committed_files(work_tree_files, commit, pylint_base_opts):
324 """
325 Get a list of files corresponding to the commit hash.
326
327 The contents of a file in the git work tree can differ from the contents
328 of a file in the commit we mean to upload. To work around this we run
329 pylint on a temp file into which we've 'git show'n the committed version
330 of each file.
331
332 @param work_tree_files: list of files in this commit specified by their
333 absolute path.
334 @param commit: hash of the commit this upload applies to.
335 @param pylint_base_opts: a list of pylint config options.
Raul E Rangel67d9cfc2019-01-30 16:28:16 -0700336
337 @returns pylint return code
beeps98365d82013-02-20 20:08:07 -0800338 """
339 files_to_check = filter(should_check_file, work_tree_files)
340
341 # Map the absolute path of each file so it's relative to the autotest repo.
342 # All files that are a part of this commit should have an abs path within
343 # the autotest repo, so this regex should never fail.
344 work_tree_files = [re.search(r'%s/(.*)' % common.autotest_dir, f).group(1)
345 for f in files_to_check]
346
347 tempdir = None
348 try:
349 tempdir = autotemp.tempdir()
350 temp_files = [os.path.join(tempdir.name, file_path.split('/')[-1:][0])
351 for file_path in work_tree_files]
352
353 for file_tuple in zip(work_tree_files, temp_files):
354 git_show_to_temp_file(commit, *file_tuple)
355 # Only check if we successfully git showed all files in the commit.
Raul E Rangel67d9cfc2019-01-30 16:28:16 -0700356 return batch_check_files(temp_files, pylint_base_opts)
beeps98365d82013-02-20 20:08:07 -0800357 finally:
358 if tempdir:
359 tempdir.clean()
360
361
Prathmesh Prabhua9980672017-07-13 15:52:02 -0700362def _is_test_case_method(node):
363 """Determine if the given function node is a method of a TestCase.
364
365 We simply check for 'TestCase' being one of the parent classes in the mro of
366 the containing class.
367
368 @params node: A function node.
369 """
370 if not hasattr(node.parent.frame(), 'ancestors'):
371 return False
372
373 parent_class_names = {x.name for x in node.parent.frame().ancestors()}
374 return 'TestCase' in parent_class_names
375
376
beeps48fc6f52013-01-07 14:36:40 -0800377def main():
378 """Main function checks each file in a commit for pylint violations."""
379
beeps1b6433b2013-01-31 17:46:50 -0800380 # For now all error/warning/refactor/convention exceptions except those in
381 # the enable string are disabled.
382 # W0611: All imported modules (except common) need to be used.
383 # W1201: Logging methods should take the form
384 # logging.<loggingmethod>(format_string, format_args...); and not
385 # logging.<loggingmethod>(format_string % (format_args...))
386 # C0111: Docstring needed. Also checks @param for each arg.
387 # C0112: Non-empty Docstring needed.
beeps48fc6f52013-01-07 14:36:40 -0800388 # Ideally we would like to enable as much as we can, but if we did so at
389 # this stage anyone who makes a tiny change to a file will be tasked with
390 # cleaning all the lint in it. See chromium-os:37364.
391
beepse19d3032013-05-30 09:22:07 -0700392 # Note:
393 # 1. There are three major sources of E1101/E1103/E1120 false positives:
394 # * common_lib.enum.Enum objects
395 # * DB model objects (scheduler models are the worst, but Django models
396 # also generate some errors)
397 # 2. Docstrings are optional on private methods, and any methods that begin
398 # with either 'set_' or 'get_'.
beeps48fc6f52013-01-07 14:36:40 -0800399 pylint_rc = os.path.join(os.path.dirname(os.path.abspath(__file__)),
400 'pylintrc')
beepse19d3032013-05-30 09:22:07 -0700401
402 no_docstring_rgx = r'((_.*)|(set_.*)|(get_.*))'
beeps48fc6f52013-01-07 14:36:40 -0800403 if pylint_version >= 0.21:
404 pylint_base_opts = ['--rcfile=%s' % pylint_rc,
405 '--reports=no',
beeps55532562013-01-16 12:13:46 -0800406 '--disable=W,R,E,C,F',
beeps5e2bb4a2013-10-28 11:26:45 -0700407 '--enable=W0611,W1201,C0111,C0112,E0602,W0601',
beepse19d3032013-05-30 09:22:07 -0700408 '--no-docstring-rgx=%s' % no_docstring_rgx,]
beeps48fc6f52013-01-07 14:36:40 -0800409 else:
410 all_failures = 'error,warning,refactor,convention'
411 pylint_base_opts = ['--disable-msg-cat=%s' % all_failures,
412 '--reports=no',
413 '--include-ids=y',
beeps1b6433b2013-01-31 17:46:50 -0800414 '--ignore-docstrings=n',
beepse19d3032013-05-30 09:22:07 -0700415 '--no-docstring-rgx=%s' % no_docstring_rgx,]
beeps48fc6f52013-01-07 14:36:40 -0800416
417 # run_pylint can be invoked directly with command line arguments,
418 # or through a presubmit hook which uses the arguments in pylintrc. In the
419 # latter case no command line arguments are passed. If it is invoked
420 # directly without any arguments, it should check all files in the cwd.
421 args_list = sys.argv[1:]
422 if args_list:
423 get_cmdline_options(args_list,
424 pylint_base_opts,
425 open(pylint_rc).read())
Raul E Rangel67d9cfc2019-01-30 16:28:16 -0700426 return batch_check_files(args_list, pylint_base_opts)
beeps98365d82013-02-20 20:08:07 -0800427 elif os.environ.get('PRESUBMIT_FILES') is not None:
Raul E Rangel67d9cfc2019-01-30 16:28:16 -0700428 return check_committed_files(
beeps98365d82013-02-20 20:08:07 -0800429 os.environ.get('PRESUBMIT_FILES').split('\n'),
430 os.environ.get('PRESUBMIT_COMMIT'),
431 pylint_base_opts)
beeps48fc6f52013-01-07 14:36:40 -0800432 else:
Raul E Rangel67d9cfc2019-01-30 16:28:16 -0700433 return check_dir('.', pylint_base_opts)
beeps48fc6f52013-01-07 14:36:40 -0800434
435
436if __name__ == '__main__':
Prashanth Balasubramanianbaabef22014-11-04 12:38:44 -0800437 try:
Raul E Rangel67d9cfc2019-01-30 16:28:16 -0700438 ret = main()
439
440 sys.exit(ret)
Prathmesh Prabhu7e107082017-01-11 17:16:41 -0800441 except pylint_error as e:
Prashanth Balasubramanianbaabef22014-11-04 12:38:44 -0800442 logging.error(e)
443 sys.exit(1)