def compression_rec_file(logger, ts, target_info_dict):
    """
    Compression record file
    :param          logger:                     Logger
    :param          ts:                         System time
    :param          target_info_dict:           Target information dictionary
    """
    global COMPRESSION_CNT
    # Compression record file
    logger.info("1. Compression record file")
    target_dir_path = target_info_dict.get('directory_path')
    if target_dir_path[-1] == "/":
        target_dir_path = target_dir_path[:-1]
    compression_file_date = int(target_info_dict.get('compression_file_date'))
    tmp_date = (datetime.fromtimestamp(ts) - timedelta(days=compression_file_date)).strftime('%Y%m%d')
    target_dir_path += "/{0}".format(tmp_date)
    logger.info("Target directory path : {0}".format(target_dir_path))
    if not os.path.exists(target_dir_path):
        logger.info("Target directory not existed")
        return
    enc = target_info_dict.get('enc')
    w_ob = os.walk(target_dir_path)
    for dir_path, sub_dirs, files in w_ob:
        for file_name in files:
            try:
                file_path = os.path.join(dir_path, file_name)
                if not os.path.exists(file_path):
                    logger.debug("Not existed -> {0}".format(file_path))
                    continue
                ts = time.time()
                dt = datetime.fromtimestamp(ts).strftime('%Y%m%d%H%M%S')
                if not file_name.startswith('comp_'):
                    logger.info('Compression file [{0}]'.format(file_path))
                    if file_name.endswith('.enc'):
                        decrypt_file([file_path])
                        tmp_file_name = file_name[:-4].replace('.', '_')
                    else:
                        tmp_file_name = file_name.replace('.', '_')
                    tmp_file_path = file_path if not file_path.endswith('enc') else file_path[:-4]
                    s16_file_path = "{0}.s16".format(os.path.join(dir_path, tmp_file_name))
                    wav_file_path = "{0}/comp_{1}.wav".format(dir_path, tmp_file_name)
                    os.rename(tmp_file_path, s16_file_path)
                    sox_cmd = 'sox -r 8000 -c 1 {0} -r 8000 -c 1 -e gsm {1}'.format(s16_file_path, wav_file_path)
                    sub_process(logger, sox_cmd)
                    COMPRESSION_CNT += 1
                    del_garbage(logger, s16_file_path)
                    if enc:
                        rename_file_path = "{0}/encrypting_comp_{1}.wav".format(dir_path, tmp_file_name)
                        logger.debug("Rename [ {0} -> {1} ]".format(wav_file_path, rename_file_path))
                        os.rename(wav_file_path, rename_file_path)
                        logger.debug("Encrypt [ {0} ]".format(rename_file_path))
                        encrypt_file([rename_file_path])
                    logger.info('Compression file [{0}], The time required = {1}'.format(
                        file_path, elapsed_time(dt)))
            except Exception:
                exc_info = traceback.format_exc()
                logger.error(exc_info)
                continue
예제 #2
0
def rollback_rec_file(logger, input_dir_path):
    """
    Rollback compression record file
    :param          logger:                     Logger
    :param          input_dir_path:             Input directory path
    """
    global ERR_CNT
    global RECORD_CNT
    # Rollback compression record file
    logger.info("Rollback compression record file")
    if input_dir_path[-1] == "/":
        input_dir_path = input_dir_path[:-1]
    logger.info("Target directory path : {0}".format(input_dir_path))
    temp_input_dir_path = "{0}/temp_{1}".format(CONFIG['temp_rollback_dir_path'], os.path.basename(input_dir_path))
    logger.info("Copy record file directory -> {0}".format(temp_input_dir_path))
    if os.path.exists(temp_input_dir_path):
        del_garbage(logger, temp_input_dir_path)
    copy_tree(input_dir_path, temp_input_dir_path)
    w_ob = os.walk(temp_input_dir_path)
    for dir_path, sub_dirs, files in w_ob:
        for file_name in files:
            target_file = os.path.join(dir_path, file_name)
            logger.info("Target file -> {0}".format(target_file))
            try:
                # Decrypt file
                if target_file.endswith('.enc'):
                    logger.debug("Decrypt {0}".format(target_file))
                    decrypt_file([target_file])
                decrypted_rec_file = target_file[:-4] if target_file.endswith('.enc') else target_file
                temp_rec_file_name = os.path.basename(decrypted_rec_file)
                temp_rec_file_name = temp_rec_file_name[5:] if temp_rec_file_name.startswith('comp_') else temp_rec_file_name
                temp_rec_file_name = temp_rec_file_name[:-4] if temp_rec_file_name.endswith('.wav') else temp_rec_file_name
                modified_rec_file_name = temp_rec_file_name.replace("_", ".").replace("__", ".")
                output_rec_file = "{0}/{1}".format(os.path.dirname(target_file), modified_rec_file_name)
                sox_cmd = "sox -t wav {0} -r 8000 -b 16 -t raw {1}".format(decrypted_rec_file, output_rec_file)
                sub_process(logger, sox_cmd)
                logger.debug("Encrypt {0}".format(output_rec_file))
                encrypt_file([output_rec_file])
                RECORD_CNT += 1
                del_garbage(logger, decrypted_rec_file)
                if not os.path.exists(CONFIG['decompression_dir_path']):
                    os.makedirs(CONFIG['decompression_dir_path'])
                if os.path.exists("{0}/{1}.enc".format(CONFIG['decompression_dir_path'], modified_rec_file_name)):
                    del_garbage(logger, "{0}/{1}.enc".format(CONFIG['decompression_dir_path'], modified_rec_file_name))
                if os.path.exists("{0}.enc".format(output_rec_file)):
                    shutil.move("{0}.enc".format(output_rec_file), CONFIG['decompression_dir_path'])
            except Exception:
                ERR_CNT += 1
                exc_info = traceback.format_exc()
                logger.error("Can't rollback record file -> {0}".format(target_file))
                logger.error(exc_info)
                continue
    del_garbage(logger, temp_input_dir_path)
def compression_and_delete_rec_file(logger, target_info_dict):
    """
    Compression and delete file
    :param          logger:                     Logger
    :param          target_info_dict:           Target information dictionary
    """
    global DELETE_CNT
    global COMPRESSION_CNT
    # Compression and delete record file
    logger.info("1. Compression and delete record file")
    target_dir_path = target_info_dict.get('directory_path')
    logger.info("Target directory path : {0}".format(target_dir_path))
    if target_dir_path[-1] == "/":
        target_dir_path = target_dir_path[:-1]
    compression_file_date = int(target_info_dict.get('compression_file_date'))
    delete_file_date = int(target_info_dict.get('delete_file_date'))
    enc = target_info_dict.get('enc')
    w_ob = os.walk(target_dir_path)
    date_time_now = datetime.now()
    logger.info('Date time now : {0}'.format(date_time_now))
    for dir_path, sub_dirs, files in w_ob:
        for file_name in files:
            try:
                file_path = os.path.join(dir_path, file_name)
                if not os.path.exists(file_path):
                    logger.debug("Not existed -> {0}".format(file_path))
                    continue
                m_time = os.path.getmtime(file_path)
                date_m_time = datetime.fromtimestamp(m_time)
                logger.info("Modified file date : {0}".format(date_m_time))
                check_com_date = date_m_time + timedelta(days=compression_file_date)
                check_del_date = date_m_time + timedelta(days=delete_file_date - compression_file_date)
                ts = time.time()
                dt = datetime.fromtimestamp(ts).strftime('%Y%m%d%H%M%S')
                if date_time_now > check_com_date and not file_name.startswith('comp_'):
                    logger.info('Compression file [{0}]'.format(file_path))
                    logger.info('Compression target date : {0}'.format(check_com_date))
                    if file_name.endswith('.enc'):
                        decrypt_file([file_path])
                        tmp_file_name = file_name[:-4].replace('.', '_')
                    else:
                        tmp_file_name = file_name.replace('.', '_')
                    tmp_file_path = file_path if not file_path.endswith('enc') else file_path[:-4]
                    s16_file_path = "{0}.s16".format(os.path.join(dir_path, tmp_file_name))
                    wav_file_path = "{0}/comp_{1}.wav".format(dir_path, tmp_file_name)
                    os.rename(tmp_file_path, s16_file_path)
                    sox_cmd = 'sox -r 8000 -c 1 {0} -r 8000 -c 1 -e gsm {1}'.format(s16_file_path, wav_file_path)
                    sub_process(logger, sox_cmd)
                    COMPRESSION_CNT += 1
                    del_garbage(logger, s16_file_path)
                    if enc:
                        rename_file_path = "{0}/encrypting_comp_{1}.wav".format(dir_path, tmp_file_name)
                        logger.debug("Rename [ {0} -> {1} ]".format(wav_file_path, rename_file_path))
                        os.rename(wav_file_path, rename_file_path)
                        logger.debug("Encrypt [ {0} ]".format(rename_file_path))
                        encrypt_file([rename_file_path])
                    logger.info('Compression file [{0}], The time required = {1}'.format(
                        file_path, elapsed_time(dt)))
                if date_time_now > check_del_date and file_name.startswith('comp_'):
                    logger.info('Delete target date : {0}'.format(check_del_date))
                    logger.info('Delete file [{0}]'.format(file_path))
                    del_garbage(logger, file_path)
                    DELETE_CNT += 1
            except Exception:
                exc_info = traceback.format_exc()
                logger.error(exc_info)
                continue
    # Delete empty directory
    dir_name_list = list()
    w_ob = os.walk(target_dir_path)
    for dir_path, sub_dirs, files in w_ob:
        dir_name_list = sub_dirs
        break
    for dir_name in dir_name_list:
        if dir_name == 'incident_file':
            continue
        dir_path = "{0}/{1}".format(target_dir_path, dir_name)
        file_path_list = glob.glob("{0}/*".format(dir_path))
        if len(file_path_list) == 0:
            shutil.rmtree(dir_path)