archwhite
7/14/2017 - 1:46 PM

Count Size of Code, NOT Lines !

Count Size of Code, NOT Lines !

import os
import traceback

path = 'F:\workspace'

total_size_of_code = dict()

folders_in_path_to_count = set([
    "server",
    "web",
    "android",
    "ios"
])


def sizeof_fmt(num, suffix='B'):
    for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
        if abs(num) < 1024.0:
            return "%3.1f%s%s" % (num, unit, suffix)
        num /= 1024.0
    return "%.1f%s%s" % (num, 'Yi', suffix)


def show_sizes(ext_size_dict):
    sum = 0
    for key, value in ext_size_dict.items():
        print(key, end=': ')
        print(sizeof_fmt(value))

        sum += value

    print('TOTAL = ', sizeof_fmt(sum))


for folder in folders_in_path_to_count:
    folderpath = path + '\\' + folder
    print('COUNTING FOR = ', folderpath)
    if not os.path.exists(folderpath):
        print("NOT FOUND")

    size_of_code = dict()

    for dirname, dirnames, filenames in os.walk(folderpath):
        # print(f'dirname = {dirname}, dirnames={dirnames}, filenames={filenames}')
        try:
            for filename in filenames:
                ext_index = filename.rfind('.')

                if ext_index == -1:
                    ext = filename
                else:
                    ext = filename[ext_index:]

                filepath = os.path.join(dirname, filename)
                size = os.stat(filepath).st_size

                # print('file = ', filepath, 'size = ', sizeof_fmt(size))

                if ext not in size_of_code:
                    size_of_code[ext] = 0
                size_of_code[ext] += size
        except Exception as e:
            traceback.print_exc()
            print(f'filename = {filename}, \n ext = {ext}, \n size = {size}')

        continue

        # print path to all subdirectories first.
        print("print path to all subdirectories first.")
        for subdirname in dirnames:
            print(os.path.join(dirname, subdirname))
        # print path to all filenames.
        print('print path to all filenames.')
        for filename in filenames:
            print(os.path.join(dirname, filename))
        # Advanced usage:
        # editing the 'dirnames' list will stop os.walk() from recursing into there.
        # if '.git' in dirnames:
            # don't go into any .git directories.
        #    dirnames.remove('.git')
        # exclude_ext = ['.svn-base', '.exe', '.db', '.dll',
        # ".eot",
        # ".exp",
        # ".filters",
        # ".gif",
        # ".ico",
        # ".jpg",
        # ".lg7",
        # ".lg8",
        # ".LICENSE"
        # ]

    show_sizes(size_of_code)
    print()

    for key in size_of_code.keys():
        if key not in total_size_of_code:
            total_size_of_code[key] = 0
        total_size_of_code[key] += size_of_code[key]

print('-------------------')
print('TOTAL SIZE:')
show_sizes(total_size_of_code)