Count lines of code

import os, re, sys
from os.path import join

rootdir = '/'
filter = '.*\.js$|.*\.py$|.*\.html$|.*\.css$|.*\.sql$|.*\.cs$|.*\.cpp$|.*\.c$|.*\.h|.*\.java$'


def usage():
    print 'USAGE: python loc.py /<rootdir>'

def loc(fname):
    try:
        f = open(fname)
        return len(f.readlines())
    except:
        return 0


def scan(dir):
    total = 0
    for root, dirs, files in os.walk(dir):
        for file in files:
            if re.match(filter, file):
                fullpath = os.path.join(root, file)
                ln = loc(fullpath)
                print fullpath, ln
                total += ln

    print '\n', '_'*34, '\n\n', 'Total # Of lines:', total, '\n\n'


if __name__ == '__main__':

    fname = ''
    args = sys.argv

    if len(args) >= 2:
        fname = args[1]
    else:
        fname = raw_input("Enter root directory: ")

    scan(fname)
    raw_input()
Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s