Skip to content

Instantly share code, notes, and snippets.

@gregorynicholas
Created July 20, 2012 17:53
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save gregorynicholas/3152237 to your computer and use it in GitHub Desktop.
Save gregorynicholas/3152237 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 os
import re
import sys
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 = os.popen('pylint %s'% module, 'r')
for line in pout:
if re.match("E....:.", line):
print line
if "Your code has been rated at" in line:
print line
score = re.findall("\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)
@thatarchguy
Copy link

score = re.findall("\d.\d\d", line)[0]
This was causing 10.00 to not be added to the total.
I changed to:
score = re.findall("\d+.\d\d", line)[0]
and it works now

@PatrickSchiffmann
Copy link

Thanks for the script!
I changed it to use subprocess, because os.popen had problems with Windows paths with spaces and made it Pylint compliant to the standard config, except for the global statement.
If you want to merge: https://gist.github.com/PatrickSchiffmann/94b31329fbcef92bead8

@rtaft
Copy link

rtaft commented Feb 9, 2016

pylint can give you a negative score, need to updated score regex for that as well

@surekasri
Copy link

What about python files that don't have a .py extension?

@MaLiN2223
Copy link

@surekasri
if module[-3:] == ".py":

can be changed to something like
if module[-3:] in extensions
extension array would contain ".XX" where XX is an extension.

If you need a longer/shorter extensions I would create some regex ;)

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