Exemplo n.º 1
0
def sound_exists(prefix):
    """see if prefix.gsm or prefix.sln exists."""
    if dsh_utils.is_valid_file(prefix + '.sln', 'dsh_utils.sound_exists:',
                               silent=True) or \
       dsh_utils.is_valid_file(prefix + '.gsm', 'dsh_utils.sound_exists:',
                               silent=True):
        return True
    dsh_utils.give_bad_news('dsh_simple1.sound_exists: no out file: ' +
                            prefix, logging.error)
    return False
Exemplo n.º 2
0
def verify_ffmpeg_existence(RepositoryRoot):
    commandPath = os.path.join(RepositoryRoot, FFMPEG_PATH_SUFFIX)
    if dsh_utils.is_valid_file(commandPath, 'verify_ffmpeg_existence'):
        return commandPath
    dsh_utils.give_news('Cannot find ffmpeg.  Advise you to install it.',
                  logging.info)
    return None
Exemplo n.º 3
0
def add_to_tar(fileToAdd, tarFilePath):
    """adds fileToAdd to tarFilePath."""

    tarBin = dsh_django_config.lookup('TAR_PATH')
    mediaDir = dsh_config.lookup('MEDIA_DIR')
    
    try:
        os.chdir(mediaDir)

        if not dsh_utils.is_valid_file(fileToAdd, silent=True):
            message = 'dsh_dump.add_to_tar: invalid file: ' + fileToAdd
            tarMsg = dsh_utils.red_error_break_msg(message)
            dsh_django_utils.error_event(message, errorLevel='ERR')
            return (False, tarMsg)
        
        command = tarBin + ' rf ' + tarFilePath + ' ' + fileToAdd
        dsh_django_utils.debug_event(
            'dsh_dump.add_to_tar: command: ' + command, 8)
        result = dsh_utils.try_execute(command)
        if result == None:
            message = 'dsh_dump.add_to_tar: tar problem: ' + command
            dsh_django_utils.error_event(message, errorLevel='CRT')
            return (False, dsh_utils.red_error_break_msg(message))
        return (True, '')
    
    except:
        message = 'dsh_dump.add_to_tar: unknown tar problem: ' + command
        dsh_django_utils.error_event(message, errorLevel='CRT')
        return (False, dsh_utils.red_error_break_msg(message))
Exemplo n.º 4
0
def verify_ffmpeg_existence(RepositoryRoot):
    commandPath = r'/usr/bin/ffmpeg'
    if dsh_utils.is_valid_file(commandPath, 'verify_ffmpeg_existence'):
        return commandPath
    dsh_utils.give_news('Cannot find ffmpeg.  Advise you to install it.',
                  logging.info)
    return None
def auto_schedule_delete_all(force=False, sessionID=None):
    """
    moved from dsh_django_utils.py
    called by views.schedule_del_all().
    force=True when initiated by views.schedule_delete_all().
    """

    disableWipe = dsh_db_config.get('reschedule_wipe_disable')
    if not force and disableWipe:
        message = 'dsh_common_agi.auto_schedule_delete_all: ' +\
                  'note: wiping of existing schedule is disabled.'
        dsh_agi.report_event(message, sessionID=sessionID)
        return dsh_utils.black_break_msg(message)

    spoolDir = dsh_common_config.lookup('ASTERISK_DOT_CALL_DIR')
    if not dsh_utils.is_valid_dir(spoolDir, silence=True):
        message = 'dsh_common_agi.auto_schedule_delete_all: ' + \
                  'spool directory invalid: ' + spoolDir
        dsh_agi.report_event(message, reportLevel = 'CRT', sessionID=sessionID)
        return dsh_utils.red_error_break_msg(message)
    
    message = 'dsh_common_agi.auto_schedule_delete_all: ' +\
              'listdir() failed: ' + spoolDir
    try:
        listing = os.listdir(spoolDir)
        listing = dsh_common_db.filter_listdir_with_dbname(listing)
    except:
        dsh_agi.report_event(message, reportLevel = 'CRT', sessionID=sessionID)
        return dsh_utils.red_error_break_msg(message)

    message = dsh_utils.black_break_msg('deleting...')
    
    for one in listing:
        full = os.path.join(spoolDir, one)
        if not one:
            continue
        if os.path.isdir(full):
            continue
        if not dsh_utils.is_valid_file(full):
            continue
        if not one.endswith('.call'):
            continue

        dsh_utils.cleanup_path(
            full, 'dsh_common_agi.auto_schedule_delete_all: ')
        message += dsh_utils.black_break_msg(full)

    message += dsh_utils.black_break_msg('done.')
    return message
def field_to_sln_path(field, sessionID=None):
    """
    moved from dsh_django2.py
     fix up the file name: from the database, it's an mp3 URL.
     django should have taken the trouble of converting it to sln alredy.
     we call some function to get the path name of the sln file.
    """
    
    #mp3FilePath = item.file.url
    mp3FilePath = field.url
    pathStuff = dsh_agi.figure_out_sln_names(mp3FilePath)
    if not pathStuff:
        message = 'dsh_django1.get_sln_path_for_asterisk: ' + \
                  'failed to find path stuff for: ' + repr(mp3FilePath)
        dsh_agi.report_event(message, reportLevel='CRT',
                             sessionID=sessionID)
        dsh_utils.give_bad_news(message, logging.critical)
        return None
    slnDir,slnBaseName,wavBaseName,fullSlnName,fullWavName = pathStuff
    dsh_utils.db_print('dsh_django1.get_sln_path_for_asterisk: ' +
                       fullSlnName, 97)

    #
    # do a sanity check of the .sln file.
    #
    if not dsh_utils.is_valid_file(fullSlnName, msg='', silent=True):
        message = 'dsh_django.get_sln_path_for_asterisk: ' + \
                  'not a valid file: ' + fullSlnName
        dsh_agi.report_event(message, reportLevel='ERR',
                             sessionID=sessionID)
        dsh_utils.give_bad_news(message, logging.error)
        return None
    
    #
    # Asterisk doesn't like to be told the .sln extension so get rid of it.
    #
    chopped = fullSlnName.replace('.sln', '')
    dsh_utils.db_print('dsh_django1.get_sln_path_for_asterisk: success: ' +
                       chopped, 108)
    return chopped
Exemplo n.º 7
0
def hangup_signal_handler(signum, frame):
    """attempt to do file format conversion upon a hangup.
    from .wav to .mp3."""
    
    dsh_utils.db_print2('dsh_simple1.signal_handler: entered...', 94)


    #
    # get the name of the file that was just recorded.
    #
    if not globals4signal.has_key('in_file'):
        dsh_utils.give_news('dsh_simple1.hangup_signal_handler: ' +
                            'hangup before recording.', logging.info)
        sys_exit(0)

    inputFile = globals4signal['in_file']


    #
    # the recorded format is wav? if not, we don't convert.
    #
    fileFormat = dsh_config.lookup('record_file_format')
    if fileFormat != 'wav':
        dsh_utils.give_bad_news('dsh_simple1.hangup_signal_handler: ' +
                                "can't convert non-wav file: " + inputFile,
                                logging.error)

    #
    # does the .wav file exist and is it non-zero sized?
    #
    inputWav = inputFile + '.wav'
    success,bytes = dsh_utils.get_file_size(inputWav)
    if not success:
        dsh_utils.give_news('dsh_simple1.hangup_signal_handler: ' +
                            'no input file to convert: ' +
                            inputWav, logging.info)
        sys_exit(1)
    if bytes == 0:
        dsh_utils.give_news('dsh_simple1.hangup_signal_handler: ' +
                            'inputfile size 0: ' + inputWav, logging.info)
        sys_exit(1)

    dsh_utils.db_print2('dsh_simple1.signal_handler: ready to convert: ' +
                        inputWav, 94)

    #
    # where's the lame binary?
    #
    lamePath = dsh_config.lookup('lame_location')
    if not dsh_utils.is_valid_file(lamePath,
                                   msg='dsh_simple1.hangup_signal_handler:'):
        dsh_utils.give_bad_news('dsh_simple1.hangup_signal_handler: ' +
                                'need to install lame.', logging.error)
        sys_exit(1)


    #
    # stdout and stderr redirected to the log directory.
    #
    stdLogs = dsh_bizarro.stdio_logs_open(globals4signal['log_dir'])
    if not stdLogs:
        sys_exit(1)
    stdout,stderr = stdLogs


    #
    # the conversion command should be:
    # lame --resample 22.05 -b 24 test.wav test4.mp3
    #
    mp3Quality = dsh_config.lookup('lame_mp3_quality')
    outputMp3 = inputFile + '.mp3'
    #command = ffmpegPath + ' -i ' + inputWav + mp3Quality + outputMp3
    command = lamePath + mp3Quality + inputWav + ' ' + outputMp3

    ret = subprocess.call(command, shell=True, stdout=stdout, stderr=stderr)
    if ret != 0:
        dsh_utils.give_bad_news('dsh_simple1.signal_handler: ' +
                                'error in format conversion: ' +
                                command, logging.error)
        sys_exit(1)

    dsh_utils.give_news('dsh_simple1.signal_handler: conversion success: ' +
                        command, logging.info)
    if not dsh_utils.cleanup_path(inputWav, 'dsh_simple1.signal_handler'):
        dsh_utils.give_bad_news('dsh_simple1.signal_handler: ' +
                                'failed to remove original wav: ' +
                                inputWav, logging.warning)

    #
    # no point continuing execution after hangup.
    #
    sys_exit(0)
Exemplo n.º 8
0
def sudo_chmod(path, smode='777', imode=0777, repositoryRoot = None):
    """from ryw.chmod_tree(). smode is a string, like '666' or '777'
    imode is a numbr, like 0666 or 0777.
    smode is used on Linux.
    imode is used on XP.
    caller should supply both.
    """

    if repositoryRoot == None:
        repositoryRoot = RepositoryRoot
        
    #chmodPath = os.path.join(repositoryRoot, 'etc', 'chmod')
    chmodPath = os.path.join('/usr/local/bin', 'cmod2')

    if not dsh_utils.is_valid_file(chmodPath):
        dsh_utils.give_bad_news(
            'Cannot find suid chmod. Advise you to fix this.', logging.error)
        return

    command = chmodPath + ' ' + smode + ' ' + path
    ret = subprocess.call(command, shell=True)

    if ret != 0:
        dsh_utils.give_bad_news('sudo_chmod failed on ' + path, logging.error)



#
# moved from ryw_ffmpeg.py
#
Exemplo n.º 9
0
def hangup_signal_handler(signum, frame):
    """attempt to do file format conversion upon a hangup.
    from .wav to .mp3.
    after conversion, we put it in the django database."""
    
    dsh_utils.db_print2('dsh_django1.signal_handler: entered...', 101)


    #
    # get the name of the file that was just recorded.
    #
    if not globals4signal.has_key('in_file'):
        dsh_utils.give_news('dsh_django1.hangup_signal_handler: ' +
                            'hangup before recording.', logging.info)
        sys_exit(0)

    inputFile = globals4signal['in_file']


    #
    # the recorded format is wav? if not, we don't convert.
    #
    fileFormat = dsh_config.lookup('record_file_format')
    if fileFormat != 'wav':
        message = 'dsh_django1.hangup_signal_handler: ' + \
                  "can't convert non-wav file: " + inputFile
        dsh_utils.give_bad_news(message, logging.error)
        dsh_agi.report_event(message, reportLevel='ERR')
        sys_exit(1)


    #
    # does the .wav file exist and is it non-zero sized?
    #
    inputWav = inputFile + '.wav'
    success,bytes = dsh_utils.get_file_size(inputWav)
    if not success:
        message = 'dsh_django1.hangup_signal_handler: ' + \
                  'no input file to convert: ' + \
                  inputWav
        dsh_utils.give_news(message, logging.info)
        #
        # looks like we could get this far even if there's
        # a hangup before record.  that's because the
        # signal doesn't seem to be delivered fast enough.
        # so globals4signal['in_file'] gets set anyhow.
        #
        #dsh_agi.report_event(message, reportLevel='ERR')
        sys_exit(1)
    if bytes == 0:
        message = 'dsh_django1.hangup_signal_handler: ' + \
                  'inputfile size 0: ' + inputWav
        dsh_utils.give_news(message, logging.info)
        dsh_agi.report_event(message, reportLevel='WRN')
        sys_exit(1)

    dsh_utils.db_print2('dsh_django1.signal_handler: ready to convert: ' +
                        inputWav, 101)


    #
    # where's the lame binary?
    #
    lamePath = dsh_config.lookup('lame_location')
    if not dsh_utils.is_valid_file(lamePath,
                                   msg='dsh_django1.hangup_signal_handler:'):
        message = 'dsh_django1.hangup_signal_handler: ' + \
                  'need to install lame.'
        dsh_utils.give_bad_news(message, logging.error)
        dsh_agi.report_event(message, reportLevel='CRT')
        sys_exit(1)


    #
    # stdout and stderr redirected to the log directory.
    #
    stdLogs = dsh_bizarro.stdio_logs_open(globals4signal['log_dir'])
    if not stdLogs:
        sys_exit(1)
    stdout,stderr = stdLogs


    #
    # the conversion command should be:
    # lame --resample 22.05 -b 24 test.wav test4.mp3
    #
    mp3Quality = dsh_config.lookup('lame_mp3_quality')
    outputMp3 = inputFile + '.mp3'
    #command = ffmpegPath + ' -i ' + inputWav + mp3Quality + outputMp3
    command = lamePath + mp3Quality + inputWav + ' ' + outputMp3

    try:
        ret = subprocess.call(command, shell=True,
                              stdout=stdout, stderr=stderr)
    except:
        ret = -1
        
    if ret != 0:
        message = 'dsh_django1.signal_handler: ' + \
                  'error in format conversion: ' + \
                  command
        dsh_utils.give_bad_news(message, logging.error)
        dsh_agi.report_event(message, reportLevel='CRT')
        sys_exit(1)

    dsh_utils.give_news('dsh_django1.signal_handler: conversion success: ' +
                        command, logging.info)

    dsh_utils.chmod_tree2(outputMp3, recurse=False)
    
    if not dsh_utils.cleanup_path(inputWav, 'dsh_django1.signal_handler'):
        message = 'dsh_django1.signal_handler: ' + \
                  'failed to remove original wav: ' + \
                  inputWav
        dsh_utils.give_bad_news(message, logging.warning)
        dsh_agi.report_event(message, reportLevel='WRN')


    #
    # calculate some more fields for saving in the database.
    #
    callDur,recDur = calculate_durations()
    dsh_utils.db_print2('dsh_django1.signal_handler: durations are: ' +
                        str(callDur) + ', ' + str(recDur), 101)


    #
    # dirPrefix is: /u/rywang/phone_data/django/
    # chop that off.
    # so what's left is:  /media/voice/09/07/090729_215817_85983050_08935237794_unknown-org_no-name.mp3
    # no, need to chop of '/media/' as well!
    # otherwise deletes don't work!
    #
    dirPrefix = dsh_config.lookup('PHONE_DATA_DJANGO_PATH')
    choppedFileName = outputMp3.replace(dirPrefix, '/')
    #chopAbsURLPrefix = dsh_config.lookup('ABS_URL_PREFIX_CHOP')
    #choppedFileName = choppedFileName.replace(chopAbsURLPrefix, '')
    choppedFileName = dsh_agi.abs_url_to_relative(choppedFileName)
    dsh_utils.db_print2('dsh_django1.signal_handler: choppedFileName is: '+\
                        choppedFileName, 105)


    #
    # FINALLY save the object in the django database!
    #
    item = globals4signal['db_in_obj']
    item.file = choppedFileName
    item.call_duration = callDur
    item.rec_duration = recDur
    try:
        item.save()
        dsh_utils.db_print2('dsh_django1.signal_handler: item saved: ' +
                            choppedFileName, 101)
    except:
        message = 'dsh_django1.signal_handler: saving in django failed: ' + \
                  choppedFileName, 101
        dsh_utils.give_bad_news(message, logging.critical)
        dsh_agi.report_event(message, reportLevel='CRT')
        sys_exit(1)

    #
    # no point continuing execution after hangup.
    #
    sys_exit(0)