def diskspace_check(config, tape_id):
    verify_dir = ltoUtil.get_tape_verify_dir(config)
    free_bytes = ltoUtil.get_freespace(verify_dir)
    index_path = ltoUtil.get_tape_written_dir(config)+'/'+tape_id+'/'+tape_id+'.xml'
    tape_xml_doc = xml.dom.minidom.parse(index_path)
    tar_elems = tape_xml_doc.getElementsByTagName('tar')
    max_tar_size = 0
    for t in tar_elems:
        tar_size = int(t.getAttribute('size'))
        if tar_size > max_tar_size:
            max_tar_size = tar_size
    if max_tar_size > free_bytes - (1024*1024):
        free_gbs = ltoUtil.format_bytes_to_gbs(free_bytes)
        needed_gbs = ltoUtil.format_bytes_to_gbs(max_tar_size)
        print 'There is only '+free_gbs+' available space on '+verify_dir+'. At least '+needed_gbs+' is needed to perform the tape verification.'
        print ltoUtil.get_script_name()+' script terminated.'
        sys.exit(2)
def check_total_size(config, vectors):
    total_size = 0
    for v in vectors:
        total_size += v[2]
    free_bytes = ltoUtil.get_freespace(ltoUtil.get_restore_dir(config))
    if total_size > free_bytes - (1024*1024):
        print 'Insufficient free space on drive ('+ltoUtil.format_bytes_to_gbs(free_bytes)+') to restore all files ('+ltoUtil.format_bytes_to_gbs(total_size)+').'
        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)