IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 31222


Ignore:
Timestamp:
Apr 6, 2011, 10:25:10 PM (15 years ago)
Author:
rhenders
Message:

Added logging; methods to create tarball and publish to datastore

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippToPsps/jython/batch.py

    r31201 r31222  
    77import os
    88import md5
    9 
     9import shutil
     10import logging
     11from subprocess import call, PIPE, Popen
     12
     13from datastore import Datastore
    1014from gpc1db import Gpc1Db
    1115from ipptopspsdb import IppToPspsDb
     
    1418from java.sql import *
    1519from xml.etree.ElementTree import ElementTree, Element, tostring
    16 from xml.dom import minidom
    1720
    1821'''
     
    3033    "../config/2/tables.vot"
    3134    '''
    32     def __init__(self, batchType, inputFitsPath="", survey=""):
     35    def __init__(self, logger, batchType, inputFitsPath="", survey=""):
     36
     37        # set up logging
     38        self.logger = logger
     39        self.logger.debug("Batch class constructor")
    3340
    3441        # set up class variables
     
    5158
    5259        # create Gpc1Db object
    53         self.gpc1Db = Gpc1Db()
     60        self.gpc1Db = Gpc1Db(self.logger)
    5461
    5562        # get survey ID from init table
     
    6067            self.surveyID = rs.getInt(1)
    6168        except:
    62             self.log("No survey ID found for this survey: '" + survey + "'")
     69            self.logger.exception("No survey ID found for this survey: '" + survey + "'")
    6370            self.surveyID = -1;
    6471   
    6572        # get dvo info from config
    66         dvoName = doc.find(survey+"dvo/name").text
    67         dvoLocation = doc.find(survey+"dvo/location").text
     73        if survey != "":
     74            dvoName = doc.find(survey+"dvo/name").text
     75            dvoLocation = doc.find(survey+"dvo/location").text
     76        else:
     77            dvoName = ""
     78            dvoLocation = ""
    6879         
    6980        # get datastore info from config
    70         datastoreProduct = doc.find("datastore/product").text
     81        self.datastore = Datastore(self.logger)
    7182
    7283        # create IppToPspsDb object and create a new batch
    73         self.ippToPspsDb = IppToPspsDb()
    74         self.batchID = self.ippToPspsDb.createNewBatch(66, survey, self.getPspsBatchType(), dvoName, datastoreProduct)
     84        self.ippToPspsDb = IppToPspsDb(logger)
     85        self.batchID = self.ippToPspsDb.createNewBatch(66,
     86                survey,
     87                self.getPspsBatchType(),
     88                dvoName,
     89                self.datastore.product)
    7590
    7691        # get local storage location from config
    7792        self.batchName = "B%08d" % self.batchID
    78         self.localOutPath = doc.find("localOutPath").text + "/" + self.getPspsBatchType() + "/" + dvoName + "/%s" % self.batchName
     93        self.subDir = doc.find("localOutPath").text + "/" + self.getPspsBatchType() + "/" + dvoName
     94        self.localOutPath = self.subDir + "/" + self.batchName
    7995        if not os.path.exists(self.localOutPath): os.makedirs(self.localOutPath)
    8096
     
    94110    def __del__(self):
    95111
    96         self.log("Batch destructor")
     112        self.logger.debug("Batch destructor")
    97113        self.localStmt.close()
    98114        self.localCon.close()
     
    105121        outPath = self.localOutPath + "/BatchManifest.xml"
    106122        tmpPath = "./tmp.xml"
    107         self.log("Creating batch manifest file here: " + outPath)
     123        self.logger.info("Creating batch manifest file here: " + outPath)
    108124        root = Element('manifest')
    109125
     
    117133
    118134        # get md5sum
    119         fits=os.popen("md5sum " + self.outputFitsPath)
    120         for f in fits.readlines(): md5sum = f[0:f.rfind(" ")]
     135        p = Popen("md5sum " + self.outputFitsPath, shell=True, stdout=PIPE)
     136        p.wait()
     137        out = p.stdout.read()
     138        md5sum = out[0:out.rfind(" ")]
    121139
    122140        # get file size
     
    136154
    137155        # clunky way to prettify XML
    138         os.popen("xmllint --format " + tmpPath + " > " + outPath)
    139 #        os.remove(tmpPath)
    140 
     156        p = Popen("xmllint --format " + tmpPath + " > " + outPath, shell=True, stdout=PIPE)
     157        p.wait()
     158        os.remove(tmpPath)
     159
     160
     161    '''
     162    tar and zips batch directory
     163    '''
     164    def createTarball(self):
     165     
     166        # set up filenams and paths
     167        tarFile = self.batchName + ".tar"
     168        tarPath = self.subDir + "/" + tarFile
     169
     170        self.tarballFile = tarFile + ".gz"
     171        tarballPath = self.subDir + "/" + self.tarballFile
     172
     173        # tar directory
     174        p = Popen("tar -cvf " + tarPath + "\
     175                -C " + self.subDir + " \
     176                " + self.batchName, shell=True, stdout=PIPE)
     177        p.wait()
     178
     179        # zip tar archive
     180        p = Popen("gzip -c " + tarPath + " > " + tarballPath, shell=True, stdout=PIPE)
     181        p.wait()
     182
     183        # delete tar file and original directory
     184        os.remove(tarPath)
     185        shutil.rmtree(self.localOutPath)
     186
     187    '''
     188    Publishes this batch to the datastore
     189    '''
     190    def publishToDatastore(self):
     191
     192        self.datastore.publish(self.batchName, self.subDir, self.tarballFile, "tgz")
     193        # TODO update ippToPsps Db here
    141194
    142195    '''
     
    147200        if self.survey == "ThreePi": return "3PI"
    148201        elif self.survey == "MD04": return "MD04"
    149         else: self.log("ERROR: Don't know this survey: " + self.survey)
     202        else: self.logger.error("Don't know this survey: " + self.survey)
    150203
    151204    '''
     
    157210        elif self.batchType == "detection": return "P2"
    158211        elif self.batchType == "stack": return "ST"
    159         else: self.log("ERROR: Don't know this batch type: " + self.survey)
     212        else: self.logger.error("Don't know this batch type: " + self.survey)
    160213
    161214
     
    244297         self.pspsTables = stilts.treads(self.pspsVoTableFilePath)
    245298         for table in self.pspsTables:
    246              self.log("Creating PSPS table: " + table.name)
     299             self.logger.info("Creating PSPS table: " + table.name)
    247300             table.write(self.localUrl + '#' + table.name)
    248301
     
    253306    '''   
    254307    def indexIppTables(self):
    255         self.log("indexIppTables not implemented")
     308        self.logger.warn("indexIppTables not implemented")
    256309
    257310
     
    261314    def createIndex(self, table, column):
    262315
    263         self.log("Creating index on column '"+column+"' for table '"+table+"'")
     316        self.logger.info("Creating index on column '"+column+"' for table '"+table+"'")
    264317
    265318        sql = "CREATE INDEX "+table+"_index ON "+table+" ("+column+")"
     
    267320            self.localStmt.execute(sql)
    268321        except:
    269             self.log("Index already in place on '" + column + "' for table '" + table + "'")
     322            self.logger.warn("Index already in place on '" + column + "' for table '" + table + "'")
    270323
    271324
     
    274327    '''   
    275328    def indexPspsTables(self):
    276         self.log("indexPspsTables not implemented")
     329        self.logger.warn("indexPspsTables not implemented")
    277330
    278331    '''
     
    290343          match = re.match(filter, table.name)
    291344          if not match: continue
    292           self.log("Creating IPP table " + table.name)
     345          self.logger.info("Creating IPP table " + table.name)
    293346          table = stilts.tpipe(table, cmd='explodeall')
    294347          try:
    295348              table.write(self.localUrl + '#' + table.name)
    296349          except:
    297               self.log("ERROR problem writing table '" + table.name + "' to the database")
     350              self.logger.exception("Problem writing table '" + table.name + "' to the database")
    298351
    299352          count = count + 1
    300353
    301       self.log("Imported %d tables from '%s' " % (count, self.inputFitsPath))
     354      self.logger.info("Imported %d tables from '%s' " % (count, self.inputFitsPath))
    302355
    303356      self.indexIppTables()
     
    308361    def exportPspsTablesToFits(self):
    309362
    310         self.log("Exporting all PSPS tables to FITS")
     363        self.logger.info("Exporting all PSPS tables to FITS")
    311364        _tables = []
    312365
    313         self.log("    Selecting database tables")
     366        self.logger.info("    Selecting database tables")
    314367        for table in self.pspsTables:
    315368           _table = stilts.tread(self.localUrl + '#SELECT * FROM ' + table.name)
     
    317370           _tables.append(_table)
    318371
    319         self.log("    Writing to FITS file '" + self.outputFitsPath + "'...")
     372        self.logger.info("    Writing to FITS file '" + self.outputFitsPath + "'...")
    320373        stilts.twrites(_tables, self.outputFitsPath, fmt='fits')
    321         self.log("    ...done")
     374        self.logger.info("    ...done")
    322375
    323376    '''
     
    394447    def replaceAllPspsNulls(self, sub):
    395448
    396         self.log("Replacing all NULL values in PSPS tables with '" + sub + "'...")
     449        self.logger.info("Replacing all NULL values in PSPS tables with '" + sub + "'...")
    397450        for table in self.pspsTables:
    398451            self.replaceNulls(table.name, sub)
    399         self.log("...done")
     452        self.logger.info("...done")
    400453
    401454    '''
     
    403456    '''
    404457    def populatePspsTables(self):
    405         self.log("Not implemented yet")
     458        self.logger.warn("Not implemented yet")
    406459
    407460    '''
     
    410463    def updateDvoIDs(self, table):
    411464
    412         self.log("Updating table '" + table + "' with DVO IDs...")
     465        self.logger.info("Updating table '" + table + "' with DVO IDs...")
    413466        sql = "UPDATE " + table + " AS a, dvo AS b SET \
    414467               a.ippObjID = b.ippObjID, \
     
    416469               WHERE a.ippDetectID = b.ippDetectID"
    417470        self.localStmt.execute(sql)
    418         self.log("...done")
     471        self.logger.info("...done")
    419472
    420473
     
    424477    def createDvoTable(self):
    425478
    426         self.log("Creating DVO table for ID matching")
     479        self.logger.info("Creating DVO table for ID matching")
    427480        sql = "DROP TABLE dvo"
    428481        self.localStmt.execute(sql)
Note: See TracChangeset for help on using the changeset viewer.