#!/usr/bin/env python

"""
Generate the complete DAG for ONE exposure from chip to warp
"""
import sys
from ipp.helpers import mkdir
from ipp.ChipManager import ChipManager as ChipManager
from ipp.CamManager import CamManager as CamManager
from ipp.FakeManager import FakeManager as FakeManager
from ipp.WarpManager import WarpManager as WarpManager
from ipp.constants.Globals import Globals as GlobalConstants

class DagGenerator(ChipManager, CamManager, FakeManager, WarpManager):
    """
    Generation of all DAGs needed for the processing of an exposure
    """
    def __init__(self, 
                 label, 
                 exposure_name, 
                 recipient):
        """
        Create a connector to the gpc1 database and a local directory
        dedicated to the exposure
        """
        ChipManager.__init__(self, label, exposure_name, recipient)
        CamManager.__init__(self, label, exposure_name, recipient)
        FakeManager.__init__(self, label, exposure_name, recipient)
        WarpManager.__init__(self, label, exposure_name, recipient)
        self.exposure_directory = '%s/%s/%s' % (GlobalConstants.LOCAL_TMP_DIR, 
                                                label, exposure_name)
        mkdir(self.exposure_directory)
        return

    def build_dag(self, need_configuration_file = True):
        """Build the DAG to generating all stages from chip to warp
        """
        dag_name = '%s/%s.dag' % (self.exposure_directory, self.exposure_name)
        print "Generating Warp file [%s] for exposure %s (label %s)" % (dag_name,
                                                                        self.exposure_name,
                                                                        self.label)
        output = open(dag_name, 'w')
        output.write('# Generated DAG\n')
        output.write('\n')
        if need_configuration_file:
            output.write('CONFIG %s/%s/configuration' % (GlobalConstants.LOCAL_TMP_DIR, self.label))
            output.write('\n')
        output.write('# Chip stages\n')
        output.write('JOB\tA010\t%s/A010.jobs\n' % self.chip_directory)
        output.write('SCRIPT POST A010 %s A020 %s %s %s\n' % (ChipManager.SCRIPT_NAME, 
                                                              self.label, 
                                                              self.exposure_name, self.recipient))
        output.write('JOB\tA020\t%s/A020.jobs\n' % self.chip_directory)
        output.write('SCRIPT POST A020 %s A030 %s %s %s\n' % (ChipManager.SCRIPT_NAME, 
                                                              self.label, 
                                                              self.exposure_name, self.recipient))
        output.write('JOB\tA030\t%s/A030.jobs\n' % self.chip_directory)
        output.write('SCRIPT POST A030 %s A040 %s %s %s\n' % (ChipManager.SCRIPT_NAME, 
                                                              self.label, 
                                                              self.exposure_name, self.recipient))
        output.write('JOB\tA040\t%s/A040.jobs\n' % self.chip_directory)
        output.write('SCRIPT POST A040 %s A050 %s %s %s\n' % (ChipManager.SCRIPT_NAME, 
                                                              self.label, 
                                                              self.exposure_name, self.recipient))
        output.write('JOB\tA050\t%s/A050.jobs\n' % self.chip_directory)
        output.write('\n')
        output.write('# Chip-Cam link\n')
        output.write('SCRIPT POST A050 %s B010 %s %s %s\n' % (CamManager.SCRIPT_NAME, 
                                                              self.label, 
                                                              self.exposure_name, self.recipient))
        output.write('\n')
        output.write('# Cam stages\n')
        output.write('JOB\tB010\t%s/B010.jobs\n' % self.cam_directory)
        output.write('SCRIPT POST B010 %s B020 %s %s %s\n' % (CamManager.SCRIPT_NAME, 
                                                              self.label, 
                                                              self.exposure_name, self.recipient))
        output.write('JOB\tB020\t%s/B020.jobs\n' % self.cam_directory)
        output.write('SCRIPT POST B020 %s B030 %s %s %s\n' % (CamManager.SCRIPT_NAME, 
                                                              self.label, 
                                                              self.exposure_name, self.recipient))
        output.write('JOB\tB030\t%s/B030.jobs\n' % self.cam_directory)
        output.write('SCRIPT POST B030 %s B040 %s %s %s\n' % (CamManager.SCRIPT_NAME, 
                                                              self.label, 
                                                              self.exposure_name, self.recipient))
        output.write('JOB\tB040\t%s/B040.jobs\n' % self.cam_directory)
        output.write('SCRIPT POST B040 %s B050 %s %s %s\n' % (CamManager.SCRIPT_NAME,
                                                              self.label, 
                                                              self.exposure_name, self.recipient))
        output.write('JOB\tB050\t%s/B050.jobs\n' % self.cam_directory)
        output.write('\n')
        output.write('# Cam-Fake link\n')
        output.write('SCRIPT POST B050 %s C010 %s %s %s\n' % (FakeManager.SCRIPT_NAME, 
                                                              self.label, 
                                                              self.exposure_name, self.recipient))
        output.write('\n')
        output.write('# Fake stages\n')
        output.write('JOB\tC010\t%s/C010.jobs\n' % self.fake_directory)
        output.write('SCRIPT POST C010 %s C020 %s %s %s\n' % (FakeManager.SCRIPT_NAME, 
                                                              self.label, 
                                                              self.exposure_name, self.recipient))
        output.write('JOB\tC020\t%s/C020.jobs\n' % self.fake_directory)
        output.write('SCRIPT POST C020 %s C030 %s %s %s\n' % (FakeManager.SCRIPT_NAME, 
                                                              self.label, 
                                                              self.exposure_name, self.recipient))
        output.write('JOB\tC030\t%s/C030.jobs\n' % self.fake_directory)
        output.write('SCRIPT POST C030 %s C040 %s %s %s\n' % (FakeManager.SCRIPT_NAME, 
                                                              self.label, 
                                                              self.exposure_name, self.recipient))
        output.write('JOB\tC040\t%s/C040.jobs\n' % self.fake_directory)
        output.write('SCRIPT POST C040 %s C050 %s %s %s\n' % (FakeManager.SCRIPT_NAME,
                                                              self.label, 
                                                              self.exposure_name, self.recipient))
        output.write('JOB\tC050\t%s/C050.jobs\n' % self.fake_directory)
        output.write('\n')
        output.write('# Fake-Warp link\n')
        output.write('SCRIPT POST C050 %s D010 %s %s %s\n' % (WarpManager.SCRIPT_NAME,
                                                              self.label, 
                                                              self.exposure_name, self.recipient))
        output.write('\n')
        output.write('# Warp stages\n')
        output.write('JOB\tD010\t%s/D010.jobs\n' % self.warp_directory)
        output.write('SCRIPT POST D010 %s D020 %s %s %s\n' % (WarpManager.SCRIPT_NAME, 
                                                              self.label, 
                                                              self.exposure_name, self.recipient))
        output.write('JOB\tD020\t%s/D020.jobs\n' % self.warp_directory)
        output.write('SCRIPT POST D020 %s D030 %s %s %s\n' % (WarpManager.SCRIPT_NAME, 
                                                              self.label, 
                                                              self.exposure_name, self.recipient))
        output.write('JOB\tD030\t%s/D030.jobs\n' % self.warp_directory)
        output.write('SCRIPT POST D030 %s D040 %s %s %s\n' % (WarpManager.SCRIPT_NAME, 
                                                              self.label, 
                                                              self.exposure_name, self.recipient))
        output.write('JOB\tD040\t%s/D040.jobs\n' % self.warp_directory)
        output.write('SCRIPT POST D040 %s D110 %s %s %s\n' % (WarpManager.SCRIPT_NAME, 
                                                              self.label, 
                                                              self.exposure_name, self.recipient))
        output.write('\n')
        output.write('# Skycell\n')
        output.write('JOB\tD110\t%s/D110.jobs\n' % self.warp_directory)
        output.write('SCRIPT POST D110 %s D120 %s %s %s\n' % (WarpManager.SCRIPT_NAME, 
                                                              self.label, 
                                                              self.exposure_name, self.recipient))
        output.write('JOB\tD120\t%s/D120.jobs\n' % self.warp_directory)
        output.write('SCRIPT POST D120 %s D130 %s %s %s\n' % (WarpManager.SCRIPT_NAME, 
                                                              self.label, 
                                                              self.exposure_name, self.recipient))
        output.write('JOB\tD130\t%s/D130.jobs\n' % self.warp_directory)
        output.write('SCRIPT POST D130 %s D140 %s %s %s\n' % (WarpManager.SCRIPT_NAME, 
                                                              self.label, 
                                                              self.exposure_name, self.recipient))
        output.write('JOB\tD140\t%s/D140.jobs\n' % self.warp_directory)
        output.write('SCRIPT POST D140 %s D210 %s %s %s\n' % (WarpManager.SCRIPT_NAME, 
                                                              self.label, 
                                                              self.exposure_name, self.recipient))
        output.write('\n')
        output.write('# Summary\n')
        output.write('JOB\tD210\t%s/D210.jobs\n' % self.warp_directory)
        output.write('SCRIPT POST D210 %s D310 %s %s %s\n' % (WarpManager.SCRIPT_NAME, 
                                                              self.label, 
                                                              self.exposure_name, self.recipient))
        output.write('\n')
        output.write('# Advance\n')
        output.write('JOB\tD310\t%s/D310.jobs\n' % self.warp_directory)
        output.write('\n')

        output.write('\n')
        output.write('# Chip dependencies\n')
        output.write('PARENT A010 CHILD A020\n')
        output.write('PARENT A020 CHILD A030\n')
        output.write('PARENT A030 CHILD A040\n')
        output.write('PARENT A040 CHILD A050\n')
        output.write('# Chip-Cam dependencies\n')
        output.write('PARENT A050 CHILD B010\n')
        output.write('# Cam dependencies\n')
        output.write('PARENT B010 CHILD B020\n')
        output.write('PARENT B020 CHILD B030\n')
        output.write('PARENT B030 CHILD B040\n')
        output.write('PARENT B040 CHILD B050\n')
        output.write('# Cam-Fake dependencies\n')
        output.write('PARENT B050 CHILD C010\n')
        output.write('# Fake dependencies\n')
        output.write('PARENT C010 CHILD C020\n')
        output.write('PARENT C020 CHILD C030\n')
        output.write('PARENT C030 CHILD C040\n')
        output.write('PARENT C040 CHILD C050\n')
        output.write('# Fake-Warp dependencies\n')
        output.write('PARENT C050 CHILD D010\n')
        output.write('# Warp dependencies\n')
        output.write('PARENT D010 CHILD D020\n')
        output.write('PARENT D020 CHILD D030\n')
        output.write('PARENT D030 CHILD D040\n')
        output.write('PARENT D040 CHILD D110\n')
        output.write('PARENT D110 CHILD D120\n')
        output.write('PARENT D120 CHILD D130\n')
        output.write('PARENT D130 CHILD D140\n')
        output.write('PARENT D140 CHILD D210\n')
        output.write('PARENT D210 CHILD D310\n')
        output.write('\n')
        output.close()
        return dag_name

if __name__ == '__main__':
    if len(sys.argv) != 4:
        print 'generate_exposure_dag.py <label> <recipient> <exposure_name>'
        print '  e.g.: generate_exposure_dag.py MD08.jtrp schastel@ifa.hawaii.edu o5806g0078o'
        sys.exit(1)
    LABEL = sys.argv[1]
    RECIPIENT = sys.argv[2]
    EXPOSURE_NAME = sys.argv[3]
    GENERATOR = DagGenerator(LABEL, EXPOSURE_NAME, RECIPIENT)
    DAG_NAME = GENERATOR.build_dag()
    GENERATOR.build_new_chips_jobs()
    print 'Now run'
    print 'condor_submit_dag %s' % DAG_NAME
    print
    sys.exit(0)
