1# Checks related to the python code done with pylint 2# 3# Copyright (C) 2016 Intel Corporation 4# 5# SPDX-License-Identifier: GPL-2.0-only 6 7import base 8from io import StringIO 9from patchtest_parser import PatchtestParser 10from pylint.reporters.text import TextReporter 11import pylint.lint as lint 12 13 14class PyLint(base.Base): 15 pythonpatches = [] 16 pylint_pretest = {} 17 pylint_test = {} 18 pylint_options = " -E --disable='E0611, E1101, F0401, E0602' --msg-template='L:{line} F:{module} I:{msg}'" 19 20 @classmethod 21 def setUpClassLocal(cls): 22 # get just those patches touching python files 23 cls.pythonpatches = [] 24 for patch in cls.patchset: 25 if patch.path.endswith('.py'): 26 if not patch.is_removed_file: 27 cls.pythonpatches.append(patch) 28 29 def setUp(self): 30 if self.unidiff_parse_error: 31 self.skip('Python-unidiff parse error') 32 if not PyLint.pythonpatches: 33 self.skip('No python related patches, skipping test') 34 35 def pretest_pylint(self): 36 for pythonpatch in self.pythonpatches: 37 if pythonpatch.is_modified_file: 38 pylint_output = StringIO() 39 reporter = TextReporter(pylint_output) 40 lint.Run([self.pylint_options, pythonpatch.path], reporter=reporter, exit=False) 41 for line in pylint_output.readlines(): 42 if not '*' in line: 43 if line.strip(): 44 self.pylint_pretest[line.strip().split(' ',1)[0]] = line.strip().split(' ',1)[1] 45 46 def test_pylint(self): 47 for pythonpatch in self.pythonpatches: 48 # a condition checking whether a file is renamed or not 49 # unidiff doesn't support this yet 50 if pythonpatch.target_file is not pythonpatch.path: 51 path = pythonpatch.target_file[2:] 52 else: 53 path = pythonpatch.path 54 pylint_output = StringIO() 55 reporter = TextReporter(pylint_output) 56 lint.Run([self.pylint_options, pythonpatch.path], reporter=reporter, exit=False) 57 for line in pylint_output.readlines(): 58 if not '*' in line: 59 if line.strip(): 60 self.pylint_test[line.strip().split(' ',1)[0]] = line.strip().split(' ',1)[1] 61 62 for issue in self.pylint_test: 63 if self.pylint_test[issue] not in self.pylint_pretest.values(): 64 self.fail('Errors in your Python code were encountered. Please check your code with a linter and resubmit', 65 data=[('Output', 'Please, fix the listed issues:'), ('', issue + ' ' + self.pylint_test[issue])]) 66