def free_MB(path):
    """returns the amount of available space in MB. used to be in ryw.py"""

    #tested.
    dsh_utils.db_print('ryw_xp:free_MB() entered...', 49)
    
    path = os.path.normpath(path)
    if not dsh_utils.try_mkdir_ifdoesnt_exist(path, 'free_MB'):
        dsh_utils.give_bad_news(
            'free_MB: try_mkdir_ifdoesnt_exist failed: ' + path,
            logging.critical)
        return 0
        
    try:
        sectorsPerCluster,bytesPerSector,numFreeClusters,totalNumClusters = \
            win32file.GetDiskFreeSpace(path)
    except:
        dsh_utils.give_bad_news(
            'fatal_error: failed to determine free disk space: '+path,
            logging.critical)
        return 0
    
    sectorsPerCluster = long(sectorsPerCluster)
    bytesPerSector = long(bytesPerSector)
    numFreeClusters = long(numFreeClusters)
    totalNumClusters = long(totalNumClusters)
    freeMB = (numFreeClusters * sectorsPerCluster * bytesPerSector) / \
             (1024 * 1024)
    return freeMB
def init_dump():
    """initializes the dump file."""

    #
    # makes the directory.
    #
    dumpDir = os.path.join(
        dsh_django_config.lookup('PHONE_DATA_DJANGO_PATH'),
        dsh_django_config.lookup('DB_DUMP_DIR'))
    if not dsh_utils.try_mkdir_ifdoesnt_exist(
        dumpDir, 'dsh_dump.init: ' + dumpDir):
        dsh_django_utils.error_event(
            'dsh_dump.init: failed to create dump dir: ' + dumpDir,
        errorLevel='CRT')
        return None

    #
    # determine the file name and open it.
    #
    randomName = dsh_utils.date_time_rand()
    fileName = randomName + '.py'
    tarName = randomName + '.tar'
    fullName = os.path.join(dumpDir, fileName)
    fullTarName = os.path.join(dumpDir, tarName)
    try:
        dumpFile = open(fullName, 'w')
        dumpFile.write('# -*- coding: latin-1 -*-\n')
        dumpFile.write('import datetime\n\n')
    except:
        dsh_django_utils.error_event(
            'dsh_dump.init: failed to open file: ' + fullName,
        errorLevel='CRT')
        return None
    return (dumpFile, fullName, fullTarName)    
def determine_recorded_file_name(caller, outgoingItem):
    #
    # I need to make a new item because that's where the new dsh_uid is.
    #
    item = Item(
        owner=caller,
        itype='I',
        followup_to=outgoingItem)

    #
    # the name has to be computed for placing in the django database.
    # so it's not done lightly.
    #
    incomingFullName = dsh_agi.make_full_unique_filename(
        item.dsh_uid,
        '',
        phoneNumber=caller.phone_number,
        orgAlias=caller.organization.alias,
        name=caller.__unicode__(),
        startWithRoot=True,
        uploadType='asterisk')

    dirName = os.path.dirname(incomingFullName)
    if not dsh_utils.try_mkdir_ifdoesnt_exist(
        dirName, 'dsh_django1.determine_recorded_file_name: '):
        message = 'dsh_django1.dsh_utils.try_mkdir_ifdoesnt_exist failed: '+\
                  dirName
        dsh_utils.give_bad_news(message, logging.critical)
        dsh_agi.report_event(message, reportLevel='CRT')
        return (item, None)

    dsh_utils.chmod_tree2(dirName, recurse=False)

    slnDir = os.path.join(dirName, 'sln')
    if not dsh_utils.try_mkdir_ifdoesnt_exist(
        slnDir, 'dsh_django1.determine_recorded_file_name: '):
        message = 'dsh_django1.dsh_utils.try_mkdir_ifdoesnt_exist failed: '+\
                  'on slnDir: ' + slnDir
        dsh_utils.give_bad_news(message, logging.critical)
        dsh_agi.report_event(message, reportLevel='CRT')
        return (item, None)
    dsh_utils.chmod_tree2(slnDir, recurse=False)
    
    return (item,incomingFullName)
def setup_dirs():
    """make voice directories if they don't exist already."""

    outDir = dsh_config.lookup('voice_data_dir_out')

    if not dsh_utils.try_mkdir_ifdoesnt_exist(outDir,
                                              'dsh_simple1.setup_dirs: ' +
                                              'outgoing dir '):
        return (False,None,None)

    miscDict = dsh_utils.get_misc_dict()
    yearMonth = miscDict['year_month']

    inDir = dsh_config.lookup('voice_data_dir_in')
    inDir = os.path.join(inDir, yearMonth)
    
    if not dsh_utils.try_mkdir_ifdoesnt_exist(inDir,
                                              'dsh_simple1.setup_dirs: ' +
                                              'incoming dir '):
        return (False,None,None)

    return (True,outDir,inDir)