'''
Utilities
'''

numlines = 100

import os,sys

def get_disk_usage_os_walk(path):
    size = 0L
    from os.path import join,getsize
    for root,dirs,files in os.walk(path):
        size += sum([getsize(join(root,name)) for name in files])
    return size

def get_disk_usage_default(path):
    size = 0L
    dirs = [path]
    while len(dirs) > 0:
        dir = dirs.pop()
        for name in os.listdir(dir):
            fullpath = os.path.join(dir,name)
            if os.path.isfile(fullpath):
                size += os.path.getsize(fullpath)
            elif os.path.isdir(fullpath):
                dirs.append(fullpath)
    return size

try:
    os.walk
except AttributeError:
    get_disk_usage = get_disk_usage_default
else:
    get_disk_usage = get_disk_usage_os_walk

def get_free_space_statvfs(path):
    try:
        s = os.statvfs(path)
        free = long(s[statvfs.F_BSIZE]*s[statvfs.F_BAVAIL])
    except:
        free = -1
    return free

def get_free_space_win32(path):
    free = -1
    try:
        fi = os.popen('dir /-C "%s"' % path,'r')
        last_line = fi.readlines()[-1]
        free = long(last_line.split()[-3])
    except:
        free = -1
    return free

def get_free_space_unsupported(path):
    return -1

try:
    os.statvfs
except AttributeError:
    if sys.platform == 'win32':
        get_free_space = get_free_space_win32
    else:
        get_free_space = get_free_space_unsupported
else:
    import statvfs
    get_free_space = get_free_space_statvfs

def clear_screen_posix():
    os.system('clear')

def clear_screen_win32():
    os.system('CLS')

def clear_screen_default():
    print '\n' * numlines

if os.name == 'posix':
    clear_screen = clear_screen_posix
elif os.name in ('nt','dos','ce'):
    clear_screen = clear_screen_win32
else:
    clear_screen = clear_screen_default

if __name__ == '__main__':
    print get_disk_usage('.')
