'''
Utilities
'''

import os,sys

def get_free_space_statvfs(path):
    s = os.statvfs(path)
    return s[statvfs.F_BSIZE]*s[statvfs.F_BAVAIL]

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
