項目管理之源碼目錄下的代碼行數統計

#!/usr/local/xxx/opt/bin/python
# vim: set ts=4 et sw=4 sts=4 fileencoding=utf-8 :

import os, sys

def count_file_lines(filename=None):
    lines = 0

    if filename == None:
        return lines

    if os.path.isfile(filename):
        file = open(filename)
        while True:
            line = file.readline()
            if not line:
                break
            lines += 1
        file.close()

    return lines

class ManageCode(object):
    def __init__(self, rootpath="./"):
        if not os.path.exists(rootpath):
            rootpath = "./"
        self.rootpath = rootpath
        self.all_files = 0
        self.all_lines = 0

    def count_lines(self, path=None):
        if path == None:
            path = self.rootpath

        if os.path.isdir(path):
            dir = os.listdir(path)
            print "++%s" % path

            for item in dir:
                if item == sys.argv[0][2:]:
                    continue;
                if os.path.isdir(item):
                    newpath = os.path.join(path, item)
                    self.count_lines(newpath)
                else:
                    self.all_files += 1
                    filename = os.path.join(path, item)
                    lines = count_file_lines(filename)
                    print "--%s: %d" % (filename, lines)
                    self.all_lines += lines

if __name__ == "__main__":

    code = ManageCode("./")

    code.count_lines()

    print "All Files: %d All Lines: %d" % (code.all_files, code.all_lines)


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章