from util import segment_run_map
from ..util import check_file
from ..configuration import config, db_info
from ..db import create_db, create_lookups
from ..metadata import create_segment_metadata

def stage(cmd):
    args = cmd.args
    _segments = [segment_run_map[seg] for seg in args.segments.split('|')]
    
    #stage the databases
    try:
        print "Staging data..."
        print "...Stage databases..."
        if not args.no_db:
            for id in ('stand', 'recid'):
                create_db(db_info[id])
                create_lookups(db_info[id])
            print '...databases staged...'
            print '...create segment metadata...'
            create_segment_metadata(config.meta_dir)
            print '...metadata created...'
        print '...database staging complete.'
         
    except:    
        if args.verbose:
            raise
        else:
            print "Problem creating database."
            print "Aborting stage and terminating program."
            sys.exit(1)
    
    #set the run options
    db_info['stand']['user'] = args.username
    db_info['stand']['pass'] = args.password
         
    opt = {'in_dir': check_file(args.in_dir, as_dir=True),
           'meta_dir': check_file(config.meta_dir, as_dir=True),
           'in_format': 'del',
           'in_delimiter': '|',
           'original': True, 
           'verbose': args.verbose,
           'db_info': db_info['stand'],
          }
    
    #run the subject segment and wait for completion
    if 'subject' in _segments:
        _opt = opt.copy()
        _opt['segment'] = 'subject'
        cmd.task_queue.put(_opt)
    
    cmd.start() # start the subject worker
    cmd.task_queue.join() #wait until subject worker is complete
    
    for segment in _segments:
        if segment == 'subject': continue
        _opt = opt.copy()
        _opt['segment'] = segment
        cmd.task_queue.put(_opt)
    
    cmd.start() # start the subject worker
    cmd.task_queue.join() #wait until subject worker is complete
    cmd.flush() #flush the cmd logs
    
    print "...done."