from os.path import normpath, join
from datetime import datetime

from util import *
from ..configuration import db_info, config
        
def compare(cmd):
    if cmd.args.segments in ['summary', 'sum']:
        _segments = ['summary']
    else:
        _segments = [segment_run_map[seg] for seg in cmd.args.segments.split('|')] 
    
    for segment in _segments:
        #set region
        if cmd.args.region:
            _region = cmd.args.region  
        elif cmd.args.segments in ['summary', 'sum']:
            _region = ''
        else: 
            _region = 'all'
            
        _log = normpath(join(config.log_dir, 'set_%i' % config.current_set, 'compare', 
                             segment, ))
        
        cmd.task_queue.put({
            'segment': segment,
            'region': _region,
            'set': cmd.args.set,
            'db_info': db_info['stand'],
            'verbose': cmd.args.verbose,
            'log_dir': create_log_dir(_log, replace=True),
            'round': cmd.args.round
        })
        
        
    cmd.start() #start the compare worker threads
    cmd.task_queue.join() #wait for workers to finish
    cmd.flush() #flush the logs
    
    print "Finished compare at %s." % (datetime.now().isoformat(' '))