7

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.

3
  • Pylint is a python script, I am pretty sure you can use its internal functionality instead of grabing and parsing the output of the main script. Take a look at the source code and you will find an API that offers you what you want. Commented Nov 7, 2014 at 12:28
  • @ikaros- Can you tell me any APIs which will suit my needs? Commented Nov 7, 2014 at 12:30
  • no I can't... I don't know too much about pylint. plg's answer is better than Popen, but still is not the approach I'm suggesting... somehow it should be possible to call the function that does the file analysis and get a datastructure with the results. Maybe you can ask in the mailing list. Commented Nov 7, 2014 at 13:16

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This is interesting, but you still have to parse the stdout and stderr... probably it is possible to call the API that returns raw python data structures with the results, before they are converted to the output.
I don't know enough about Pylint either, but after reading a bit of the docs I think it's not designed to be used that way, especially if they provide their own Popen wrapper...
4

Eg. to count the errors, you can use this snippet:

from pylint.lint import Run

paths_to_check = ['./']
pylint_params = ["--rcfile=.pylintrc", "--exit-zero", "--errors-only"]
results = Run(pylint_params + paths_to_check, do_exit=False)
errors = results.linter.stats['error']

message = ('Number of errors: {}'.format(errors))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.