#!/usr/bin/env jython

from subprocess import call, PIPE, Popen
import tempfile
from xml.etree.ElementTree import ElementTree, Element, tostring
import re
import sys
import time
import traceback
import logging.config

from ipptopsps import IppToPsps
from batch import Batch
from datastore import Datastore
from dxlayer import DXLayer

'''
Cleanup class
'''
class Cleanup(IppToPsps):

    '''
    Constructor
    '''
    def __init__(self, argv):
        super(Cleanup, self).__init__(argv)

        self.datastore = Datastore(self.logger, self.skychunk, self.ippToPspsDb)
        self.dxlayer = DXLayer(self.logger)
        if len(argv) > 2: self.parsePollTimeArg(sys.argv[2]) 


    '''
    Runs...
    '''
    def run(self):

        if self.SECONDS: self.logger.infoPair("Performing clean-up every", "%f hours" % (self.SECONDS/60.0/60.0))
        while True:

            self.logger.infoSeparator()
            self.logger.infoPair("Skychunk", self.skychunk.name)
            self.skychunk.printDeletionPolicy()
            self.clean("IN")
            self.clean("P2")
            self.clean("ST")
            self.clean("OB")
            self.clean("DF")
            self.clean("DO")
            self.clean("FW")
            self.clean("FO")
            self.clean("FG")

            self.checkClientStatus()
            if not self.waitForPollTime(): break
   
    '''
    Reports results of cleanup for this set of IDs 
    '''
    def reportResults(self, name, ids, success):

        if len(ids) > 0:
            self.logger.infoSeparator()
            self.logger.infoPair("Removed from " + name, "%d of %d" % (success, len(ids)))
    

    '''
    Finds stuff to delete for this batch type, then:

    - removes from the datastore
    - deletes from local disk
    - deletes files from DXLayer

    '''
    def clean(self, batchType):

        self.logger.infoSeparator()
        self.logger.infoTitle("Cleaning up batch type %s" % batchType)

        mergedLocalIDs = self.ippToPspsDb.getMergedReadyToDeleteFromLocalDisk(batchType)
        purgedLocalIDs = self.ippToPspsDb.getPurgedReadyToDeleteFromLocalDisk(batchType)
        deleteFromLocalIDs = mergedLocalIDs + purgedLocalIDs

        loadedDatastoreIDs = self.ippToPspsDb.getLoadedToODMReadyToDeleteFromDatastore(batchType)
        purgedDatastoreIDs = self.ippToPspsDb.getPurgedReadyToDeleteFromDatastore(batchType)
        deleteFromDatastoreIDs = loadedDatastoreIDs + purgedDatastoreIDs

        deleteFromDxLayerIDs = self.ippToPspsDb.getLoadedToODMReadyToDeleteFromDxLayer(batchType)

        # report to log what we are going to do
        self.logger.infoPair("To delete from datastore", "%d" % len(deleteFromDatastoreIDs))
        self.logger.infoPair("To delete from DXLayer", "%d" % len(deleteFromDxLayerIDs))
        self.logger.infoPair("To delete from local disk", "%d" % len(deleteFromLocalIDs))
    #    self.logger.infoPair("Did I get here", "yes?")
        # delete stuff from local disk
        if self.skychunk.deleteLocal:
            count = 0
            for id in deleteFromLocalIDs:
        
                if Batch.deleteFromDisk(self.logger, self.skychunk.basePath, batchType, self.skychunk.dvoLabel, id):
                    self.ippToPspsDb.updateDeletedLocal(id, 1)
                    count = count + 1
    
            self.reportResults("local disk", deleteFromLocalIDs, count) 
    
        # remove stuff from datastore
        if self.skychunk.deleteDatastore:
            count = 0
            for id in deleteFromDatastoreIDs:
        
                batchName = Batch.getNameFromID(id)
    
                if self.datastore.remove(batchName):
                    count = count + 1
    
            self.reportResults("datastore", deleteFromDatastoreIDs, count) 
       # self.logger.infoPair("here","here")
        # remove stuff from DXLayer
        if self.skychunk.deleteDxLayer:
#            self.logger.infoPair ("skip remove from dxlayer", "HAF")
            count = 0
            for id in deleteFromDxLayerIDs:
                self.logger.infoPair("delete from dxlayer", id);
                if self.dxlayer.deleteBatch(id):
                    self.logger.infoPair("delete dxlayer",id);
                    self.ippToPspsDb.updateDeletedDxlayer(id, 1)
                    count = count + 1
    
            self.reportResults("DXLayer", deleteFromDxLayerIDs, count) 
       
    '''
    Overrides base-class version
    '''
    def printUsage(self):
        super(Cleanup, self).printUsage("[<poll time in hours>]")


'''
Start of program
'''
try:
    cleanup = Cleanup(sys.argv)
    cleanup.run()
    cleanup.exitProgram("completed")
except Exception, e:
    print str(e)
    traceback.print_exc()
