Skip to content

Instantly share code, notes, and snippets.

@PatrickSchiffmann
Forked from gregorynicholas/pylint-recursive.py
Last active July 13, 2018 10:10
Show Gist options
  • Save PatrickSchiffmann/94b31329fbcef92bead8 to your computer and use it in GitHub Desktop.
Save PatrickSchiffmann/94b31329fbcef92bead8 to your computer and use it in GitHub Desktop.
Module that runs pylint on all python scripts found in a directory tree..
#! /usr/bin/env python
'''
Module that runs pylint on all python scripts found in a directory tree..
'''
import re
import sys
import subprocess
TOTAL = 0.0
COUNT = 0
def check(module):
'''
apply pylint to the file specified if it is a *.py file
'''
global TOTAL, COUNT
if module[-3:] == ".py":
print "CHECKING ", module
pout = subprocess.Popen("pylint %s" % module, stdout=subprocess.PIPE).stdout.read()
for line in pout.split("\n"):
if re.match("E....:.", line):
print line
if "Your code has been rated at" in line:
print line
score = re.findall(r"\d.+\d\d", line)[0]
TOTAL += float(score)
COUNT += 1
if __name__ == "__main__":
try:
print sys.argv
BASE_DIRECTORY = sys.argv[1]
except IndexError:
print "no directory specified, defaulting to current working directory"
BASE_DIRECTORY = os.getcwd()
print "looking for *.py scripts in subdirectories of ", BASE_DIRECTORY
for root, dirs, files in os.walk(BASE_DIRECTORY):
for name in files:
filepath = os.path.join(root, name)
check(filepath)
print "==" * 50
print "%d modules found" % COUNT
print "AVERAGE SCORE = %.02f" % (TOTAL / COUNT)
@icnocop
Copy link

icnocop commented Oct 30, 2015

Hi.

Thank you for this script.

One thing that's missing is the import os statement:

Traceback (most recent call last):
  File "...pylint-recursive.py", line 40, in <module>
    BASE_DIRECTORY = os.getcwd()
NameError: name 'os' is not defined

Also, I'm using pylint 1.4.3 and Python 2.7.9 and when I run this script, I get the following error:

C:\working\env\Scripts\python.exe C:/working/pylint-recursive.py
['C:/working/pylint-recursive.py']
no directory specified, defaulting to current working directory
looking for *.py scripts in subdirectories of  C:\working
CHECKING  C:\working\config.py
No config file found, using default configuration
Your code has been rated at 7.14/10 (previous run: 7.14/10, +0.00)
Traceback (most recent call last):
  File "C:/working/pylint-recursive.py", line 47, in <module>
    check(filepath)
  File "C:/working/pylint-recursive.py", line 31, in check
    TOTAL += float(score)
ValueError: invalid literal for float(): 7.14/10 (previous run: 7.14/10, +0.00

Process finished with exit code 1

Any ideas?

Thank you.

@icnocop
Copy link

icnocop commented Oct 30, 2015

I think it should be:
score = re.findall(r"(-?\d+\.\d+)", line)[0]

@MirYusuf123
Copy link

BUT how to generate the report The current script is just giving the ratre..

@p00j4
Copy link

p00j4 commented Jul 13, 2018

for me subprocess.Popen("pylint %s" % module, stdout=subprocess.PIPE).stdout.read() fails with OSError No such file or directory

Stacktrace
python linter.py /me/my_path

['linter.py', '/me/my_path']
looking for *.py scripts in subdirectories of  /me/my_path
CHECKING  /me/my_path/s.py
Traceback (most recent call last):
  File "linter.py", line 47, in <module>
    check(filepath)
  File "linter.py", line 24, in check
    pout = subprocess.Popen("pylint %s" % module, stdout=subprocess.PIPE).stdout.read()
  File "/usr/local/Cellar/python@2/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 390, in __init__
    errread, errwrite)
  File "/usr/local/Cellar/python@2/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1025, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

system uder use

MAC OSX
Using config file /me/.pylintrc
pylint 1.8.4,
astroid 1.6.3
Python 2.7.14 (default, Mar 22 2018, 14:43:05)

Any help pointer if i'm missing something important

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment