def create_tar_xml_file(config, session_id, device_code, tar_xml_doc):
    
    xmlfile = open(ltoUtil.get_tar_build_dir(config)+'/'+session_id+'-'+device_code+'.xml', "w")
    pretty_doc = tar_xml_doc.toprettyxml()
    #Hack to put id attribute at the start
    pretty_doc = string.replace(pretty_doc, '_id="', 'id="')
    xmlfile.write(pretty_doc)
    xmlfile.close()
def generate_proxy_files(config, domain):
    dir = ltoUtil.get_tar_build_dir(config)
    filelist = []
    for file in os.listdir(dir):
        if media_in_domain(file, domain, config):
            filepath = os.path.join(dir, file)
            filelist.append(filepath)
    filelist.sort()
    for filepath in filelist:
        generate_proxy(config, domain, filepath)
def create_referenced_items_file(config, session_id, device_code):
    tarMeta = open(os.path.join(ltoUtil.get_tar_build_dir(config), session_id+'-'+device_code+'-referenced-items.xml'),'w')
    params = urllib.urlencode({'sessionIds':session_id, 'deviceCodes':device_code})
    conn = httplib.HTTPConnection(ltoUtil.get_host_port(config))
    try:
        conn.request('GET', ltoUtil.get_transcript_url(config)+'/xquery/get-referenced-items.xql?'+params, None, {})
        response = conn.getresponse()
        tarMeta.writelines(response.read())
    except httplib.HTTPException, e:
        print 'Unable to connect to database'
def create_tar_archive(config, session_id, device_code, tar_xml_doc):
    ref_file_name = session_id+'-'+device_code+'-referenced-items.xml'
    tar_name = session_id+'-'+device_code+'.tar'
    tar_path = ltoUtil.get_tar_build_dir(config)+'/'+tar_name
    block_size_bytes = int(config.get('Tape', 'block_size_bytes'))
    blocking_factor = block_size_bytes/512
    filelist = []
    for file in os.listdir(ltoUtil.get_tar_build_dir(config)):
        filelist.append(file)
    filelist.remove(ref_file_name)
    filelist.sort()   
    
    filelist_str = ref_file_name+' '+string.join(filelist, ' ')
    print '\nCreating tar archive: '+tar_path
    p = subprocess.Popen('tar -cvR -b '+str(blocking_factor)+' --format='+ltoUtil.get_tar_format(config)+' -C '+ltoUtil.get_tar_build_dir(config)+' -f '+tar_path+' '+filelist_str, shell=True, stdout=subprocess.PIPE)
    stdout_value = p.stdout.readlines()
    del stdout_value[0]
    for line in stdout_value:
        update_block_xml_attributes(tar_xml_doc, line, config)
       
    update_tar_xml_root_attributes(config, tar_xml_doc, tar_name)
def move_tar_files(config, session_id, device_code):
    tb = ltoUtil.get_tar_build_dir(config)
    archive_id = session_id+'-'+device_code
    tar = tb+'/'+archive_id+'.tar'
    xml = tb+'/'+archive_id+'.xml'
    dest = config.get('Dirs', 'tar_archive_dir')
    try: 
        print '\nMoving '+archive_id+'.tar to '+dest 
        ltoUtil.move(tar, dest)
        print '\nMoving '+archive_id+'.xml to '+dest 
        ltoUtil.move(xml, dest)
    except Exception, e:
        print '\nUnable to move archive files to: '+dest
        print ltoUtil.get_script_name()+' script terminated.'
        sys.exit(2)
def media_file_types_check(config, category, path):
    category_filetypes = config.get('CategoryFileTypes', category).split(',')
    media_file_count = 0
    total_file_size = 0
    non_category_files = []
    for dirpath, dirnames, filenames in os.walk(path):
        for file in filenames:
            if media_in_domain(file, 'video', config) or media_in_domain(file, 'audio', config) or media_in_domain(file, 'image', config):
                if not media_in_category(file, category, config): 
                    non_category_files.append(os.path.join(dirpath, file))
                else: 
                    media_file_count += 1
                    total_file_size += ltoUtil.get_filesize(os.path.join(dirpath, file))
                    
    free_bytes = ltoUtil.get_freespace(ltoUtil.get_tar_build_dir(config))                          
    if (len(non_category_files) > 0):
        print 'WARNING: The following unexpected media files were found in the folder: '+path+'\n'
        for f in non_category_files:
            print f    
        print '\nExpecting only '+string.upper(string.join(category_filetypes, ','))+' files for "'+category+'" category.'
        print '\nNOTE: This situation should only occur if when one device has generated multiple media formats. (e.g. a video camera taking stills/audio as well as video)'
        print 'Usually this indicates that the files have been misplaced, in which case the user should manually remove them before re-running the script.\n'
        proceed = raw_input('Do you still want to proceed, including these files in the archive? [y/n]: ')
        if proceed == 'y':
            return
        else:
            print ltoUtil.get_script_name()+' script terminated.'
            sys.exit(2)
            
    elif media_file_count == 0:
        print 'No recognised media files were found in: '+path
        print ltoUtil.get_script_name()+' script terminated.'
        sys.exit(2)
    elif total_file_size > free_bytes - (1024*1024):
        print 'The total file size of the media files to be archived ('+ltoUtil.format_bytes_to_gbs(total_file_size)+') exceeds the free space available in '+ltoUtil.get_tar_build_dir(config)+' ('+ltoUtil.format_bytes_to_gbs(free_bytes)+').'
        print ltoUtil.get_script_name()+' script terminated.'
        sys.exit(2) 
def update_tar_xml_root_attributes(config, doc, tar_name):               
    doc.documentElement.setAttribute('md5', ltoUtil.get_md5_hash(ltoUtil.get_tar_build_dir(config)+'/'+tar_name))
    doc.documentElement.setAttribute('size', str(ltoUtil.get_filesize(ltoUtil.get_tar_build_dir(config)+'/'+tar_name)))
def get_new_filepath(config, domain, media_id, filepath):
    tb = ltoUtil.get_tar_build_dir(config)
    extn = string.lower(ltoUtil.get_file_extn(filepath))
    new_fp = tb+'/'+domain+'-'+media_id+'.'+extn
    return new_fp