Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a brief summary or exactly the number of errors and warnings using pylint in python

How to get a brief summary of the pylint error messages or get the number of pylint errors, warnings, refactors using python code?

pylint -rn

This allows us to get pylint output with just messages and excludes the reports. what i have tried till now is :

lint_arg = "pylint -rn %s"%file_name
pr = subprocess.Popen(
                arg, cwd=os.path.dirname(lint_path),
                shell=True, stdout=subprocess.PIPE,
                stderr=subprocess.PIPE)
            (out, error) = pr.communicate()
            pylint_result =  out + "\n" + error
file=open("pylint_output",w)
file.write(pylint_result)
file.close()

I would like to get just the number of lines in which errors, warnings occurred and move this information into a string. My idea is below:

step 1 : run a pylint in python script using subprocess.Popen()
step 2 : store the output in a file or variable .
step 3: parse the file and generate the expected output as below:

Expected output : The following file < filename> has :
5 warnings in line number : 11, 34 ,56 (lines in which warnings occurred)
2 errors in line number :2 (lines in which errors occurred).

How can i do this with help of pylint command s or with the use of python script? Any help appreciated. advance thanks.

like image 589
Zumbaa Avatar asked Nov 07 '14 12:11

Zumbaa


People also ask

How do you check pylint?

Upgrading Pylint First, to check the version of Pylint you're running, run the following command: pylint --version.

What does pylint return?

Pylint returns bit-encoded exit codes. For example, an exit code of 20 means there was at least one warning message (4) and at least one convention message (16) and nothing else.


1 Answers

First, don't use Popen, to quote Pylint's docs:

To silently run Pylint on a module_name.py module, and get its standart output and error:

from pylint import epylint as lint
(pylint_stdout, pylint_stderr) = lint.py_run('module_name.py', True)

Second, see the output format, man pylint says:

Using the default text output, the message format is:

MESSAGE_TYPE: LINE_NUM:[OBJECT:] MESSAGE

Assuming line is a line of Pylint output, this should gather all you need:

pylint_res = {'C': [], 'R': [], 'W': [], 'E': [], 'F': []}
msg_type, lineno = line.split(':', 2)[:2]
pylint_res[msg_type].append(lineno)

All you need to do is iterate over Pylint's output, adding as above to pylint_res, and then printing out the results.

like image 87
ThinkChaos Avatar answered Sep 21 '22 23:09

ThinkChaos