# $CVSHeader: pal8e/src/PSSDWeekly.py,v 1.5 2005/02/20 00:32:25 indraneel Rel $

# will update pssd files weekly
# requires status of weekly PDB files to determine files to delete and
# regenerate. This is calculated for weekly folders using info file.
# There is a single run for the list of files generated.
# environment variable PAL8ECONF or ~/.pal8e.conf is required


import os
from PAL8E import PSSDRunner
import ConfigParser

class PDBBuildList:
    def __init__(self, config):
        self.config = config
        self.toRun = []
        self.toDel = []

    def update(self):
        statusdir = config.get('Input', 'PDBstatusdir')
        statusdirWeek = config.get('Input', 'PDBstatusdirWeek')

        def getName(lsoutline):
            return lsoutline.split()[-1]

        toRun = []
        fnames = []
        fnames.append(statusdir+statusdirWeek+'/added.pdb')
        fnames.append(statusdir+statusdirWeek+'/modified.pdb')
        for fname in fnames:
            fh = file(fname)
            lines = fh.readlines()
            fh.close()
            toRun += map(getName, lines)
        for fname in toRun:
            if fname not in self.toRun:
                self.toRun.append(fname)
        obsoletefname = statusdir+statusdirWeek+'/obsolete.pdb'
        fh = file(obsoletefname)
        lines = fh.readlines()
        fh.close()
        toDel = map(getName, lines)
        for fname in toDel:
            if fname in self.toRun:
                self.toRun.remove(fname)
            if fname not in self.toDel:
                self.toDel.append(fname)

    def getToRun(self):
        pdbdir = config.get('Input', 'pdbdir')
        buff = []
        for (i, fname) in enumerate(self.toRun):
            buff.append(pdbdir+fname)
        return buff

    def getToDel(self):
        pssddir = config.get('Output', 'secoutdir')
        buff = []
        for (i, fname) in enumerate(self.toDel):
            buff.append(pssddir+fname)
        return buff

class PSSDUpdateWeeks:
    def __init__(self, config):
        self.config = config
        
    def run(self):
        config = self.config
        infofile = config.get('Input', 'WeeklyInfoFile')
        statusdir = config.get('Input', 'PDBstatusdir')
        # locate updateDate from infofile
        updateDate = ''
        fh = file(infofile)
        lines = fh.readlines()
        fh.close()
        lines.reverse()
        for line in lines:
            if (line[:10] == 'updateDate'):
                updateDate = line.split()[1]
                break
        if not updateDate:
            print >> sys.stderr, 'exiting: last updated date not found'
            return
        updateDate = int(updateDate)
        # list and sort status sub directories
        buff = os.listdir(statusdir)
        subdirs = []
        def conv(s):
            if s.isdigit(): subdirs.append(int(s))
        map(conv, buff)
        subdirs.sort()
        if (updateDate == subdirs[-1]):
            return
        idx = subdirs.index(updateDate)
        PDBfilelist = PDBBuildList(config)
        for subdir in subdirs[idx+1:]:
            config.set('Input', 'PDBstatusdirWeek', str(subdir))
            PDBfilelist.update()
        statusdirWeek = str(subdir)

        filelist = PDBfilelist.getToDel()
        for fname in filelist:
            if os.path.isfile(fname):
                os.unlink(fname)
        
        filelist = PDBfilelist.getToRun()
        pssdrunner = PSSDRunner(config)
        pssdrunner.filelist = filelist
        pssdrunner.run()

        line = 'updateDate\t'+statusdirWeek+'\n'
        infofile = config.get('Input', 'WeeklyInfoFile')
        fh = file(infofile, 'a')
        fh.write(line)
        fh.close()


if __name__ == '__main__':
    if not os.environ.has_key('PAL8ECONF'):
        os.environ['PAL8ECONF'] = os.path.expanduser('~/.pal8e.conf')

    conf_fname = os.getenv('PAL8ECONF')
    config = ConfigParser.ConfigParser()
    config.read(conf_fname)

    pssdWeeks = PSSDUpdateWeeks(config)
    pssdWeeks.run()
    
