def main():
    
    config = ConfigParser.ConfigParser()
    ltoUtil.load_config(config)
    ltoUtil.config_checks(config)
    ltoUtil.path_check(ltoUtil.get_tape_build_dir(config))
    ltoUtil.path_check(ltoUtil.get_tape_pending_dir(config))
    ltoBuildUtil.check_tape_build_dir_contents(ltoUtil.get_tape_build_dir(config))
    ltoBuildUtil.check_tape_build_size(config)

    tape_xml_doc = ltoBuildUtil.create_tape_xml_doc(ltoUtil.get_curr_datetime(), config) 
    ltoBuildUtil.create_tape_index(tape_xml_doc, config)
    tape_id = ltoBuildUtil.db_import_tape_xml(config, tape_xml_doc)
        
    ltoBuildUtil.update_tape_xml(tape_xml_doc, tape_id)
    ltoBuildUtil.write_tape_xml_file(config, tape_xml_doc, tape_id)
    ltoBuildUtil.move_build_virtual_tape_files(config, tape_id)
    ltoUtil.delete_dir_content(ltoUtil.get_tape_build_dir(config))
def create_tape_index(tape_xml_doc, config):
    tars = []
    for t in os.listdir(ltoUtil.get_tape_build_dir(config)):
        if t.endswith('.tar'):
            tars.append(t)
    tars.sort()
        
    block_size_bytes = int(config.get('Tape', 'block_size_bytes'))    
    index_size_mb = int(config.get('Tape', 'index_size_mb'))
    index_size_bytes = index_size_mb * 1024*1024
    current_block = index_size_bytes/block_size_bytes +1
        
    for index, tar in enumerate(tars):
        tar_name = tar[0:-4]
        position = index+2
        size = ltoUtil.get_filesize(os.path.join(ltoUtil.get_tape_build_dir(config), tar))
        blocks = int(size/block_size_bytes) 
        tar_xml_path = os.path.join(ltoUtil.get_tape_build_dir(config), tar_name+'.xml')
        append_tar_element_to_tape_xml_doc(tape_xml_doc, tar_xml_path, position, current_block)
        current_block += blocks + 1
def check_tape_build_size(config):
    dir = ltoUtil.get_tape_build_dir(config)
    min_gbs = config.getint('Tape', 'min_gb')
    max_gbs = config.getint('Tape', 'max_gb')
    min_size = int(min_gbs)*1024*1024*1024
    max_size = int(max_gbs)*1024*1024*1024
    index_size_mb = config.getint('Tape', 'index_size_mb')
    index_size = index_size_mb*1024*1024
    size = ltoUtil.get_dir_total_size(dir) + index_size
    size_gb = float(size)/(1024*1024*1024)
    
    if size > max_size:
        print 'The total size of the files in the tape build directory: '+dir+' is over the maximum limit of '+str(max_gbs)+'GB.'
        print ltoUtil.get_script_name()+' script terminated.'
        sys.exit(2)    
    elif size < min_size:
        print 'The total size of the files in the tape build directory: '+dir+' is under the minimum limit of '+str(min_gbs)+'GB.'
        print ltoUtil.get_script_name()+' script terminated.'
        sys.exit(2)
    else:
        print 'Virtual tape size: '+'%3.2f'%size_gb+'GB'
def write_tape_xml_file(config, tape_xml_doc, tape_id):
    tape_xml_filepath = ltoUtil.get_tape_build_dir(config)+'/'+tape_id+'.xml'
    print 'Creating tape index file '+tape_xml_filepath
    ltoUtil.write_xml(tape_xml_doc, tape_xml_filepath)
def write_tape_xml_file(config, tape_xml_doc, tape_id):
    tape_xml_filepath = ltoUtil.get_tape_build_dir(config)+'/'+tape_id+'.xml'
    print 'Creating tape index file '+tape_xml_filepath
    ltoUtil.write_xml(tape_xml_doc, tape_xml_filepath)
   
def move_build_virtual_tape_files(config, tape_id):
    dest = config.get('Dirs', 'virtual_tape_dir')+'/pending/'+tape_id
    try:
        if not os.path.exists(dest):
            os.mkdir(dest)
        else:
            ltoUtil.delete_dir_content(dest)
    except OSError, e:
        print 'Unable to create virtual tape directory: '+dest
        print 'OSError '+str(e.errno)+': '+e.strerror
        print ltoUtil.get_script_name()+' script terminated.'
        sys.exit(2)
    print 'Created virtual tape directory: '+dest
    
    tb = ltoUtil.get_tape_build_dir(config)
    for f in os.listdir(tb):
        if f.endswith('.tar') or f == tape_id+'.xml':
            try: 
                print 'Moving '+tb+'/'+f+' to '+dest 
                ltoUtil.move(tb+'/'+f, dest)
            except Exception, e:
                print '\nUnable to move file '+f+' to '+dest
                print ltoUtil.get_script_name()+' script terminated.'
                sys.exit(2)