LATfileTools.py

Go to the documentation of this file.
00001 #!/usr/local/bin/python
00002 #
00003 # package LATfileTools.py
00004 #
00005 # This package creates an interface to adding specific LAT types of file to
00006 #    the system.  
00007 #
00008 # LAT types of file include:
00009 #   LATC blobs  (creation, etc)
00010 #   LCI  blobs
00011 #
00012 
00013 
00014 __facility__ = "Online"
00015 __abstract__ = "LAT level File tools for ScriptEngine classes"
00016 __author__   = "J. Panetta <panetta@SLAC.Stanford.edu> SLAC - GLAST LAT I&T/Online"
00017 __date__     = "2005/11/1 00:08:27"
00018 __updated__  = "$Date: 2006/01/03 19:42:08 $"
00019 __version__  = "$Revision: 1.2 $"
00020 __release__  = "$Name: HEAD $"
00021 __credits__  = "SLAC"
00022 
00023 import LICOS.copyright_SLAC
00024 
00025 import LICOS.tests.lib.fileTools      as fileTools
00026 import LICOS.tools.LATc.latte2latcXML as latte2latcXML
00027 
00028 import logging as log
00029 import time
00030 import os
00031 import struct
00032 from tempfile import NamedTemporaryFile
00033 
00034 LATC_PARSE_COMMAND = 'LATC_parser'    # for now, assume it is in path?
00035 LATC_DEVICE        = fileTools.FSW_DEVICE_RAM
00036 LATC_DIR           = fileTools.INT_DIR_LATC
00037 
00038 
00039 def convertSnapshotToLatcBlob(snapshot, purge=False):
00040   """!\brief Convert a disk-resident LAT snapshot to a set of LATC blobs
00041 
00042   \return the set [master, [blobs]] as FswFile objects suitable for upload
00043   """
00044 
00045   xmlList = convertSnapshotToLatcXML(snapshot, purge)
00046 
00047   blobSet = convertLatcXMLtoBlob(xmlList, purge)
00048 
00049   master  = makeLatcMasterFromBlobs(blobSet)
00050 
00051 
00052   return [master, blobSet]
00053 
00054 def convertSnapshotToLatcXML(snapshot):
00055   """!\brief Convert a disk-resident LAT snapshot to a set of LATC XML files
00056 
00057   \param   snapshot    A LATTE4 snapshot
00058   \return  List of xml file objects (already opened)
00059   """
00060   
00061   converter = latte2latcXML.latte2latcXML(latte2latcXML.LATTE2LATC_CONFIG,
00062                                           latte2latcXML.LATCREGATTR_CONFIG)
00063 
00064   log.debug( "reading LATTE xml")
00065   glatDOC    = converter.readLatteXML(snapshot)     # ~20 seconds
00066 
00067   log.debug( "converting to LATC")
00068   # straight 1:1 conversion btwn snapshot and LATC xml
00069   latcRawDOC = converter.makeLatcDOC(glatDOC)
00070 
00071   log.debug( "Building broadcast nodes")
00072   # Compute node statistics for building broadcast
00073   converter.computeNodeModes(converter.getLATC(latcRawDOC))
00074 
00075   # Create broadcast xml as a temp file
00076   bcast = converter.buildLatcBcast()
00077   bcastFile  = NamedTemporaryFile(mode='w+b')   
00078   converter.writeLatcXML(id='bcast', file=bcastFile.name)
00079 
00080   purged = converter.purgeBcast(latcRawDOC, bcast)
00081 
00082   # don't bother with the split, just return the non bcast nodes as a monolith
00083   singlesFile = NamedTemporaryFile(mode='w+b')
00084   converter.writeLatcXML(id='base', file=singlesFile.name)
00085 
00086   return [bcastFile, singlesFile]
00087 
00088 
00089 def convertLatcXMLtoBlob(xmlList):
00090   """!\brief Convert a disk-resident LAT snapshot to a set of LATC blobs
00091 
00092   \param   xmlList  List of xml file objects  
00093   \return  List of created FSW file objects
00094   """
00095 
00096   log.debug('Converting to blobs')
00097   # create comma separated list of filenames
00098   fileNames = [ f.name for f in xmlList ]
00099   fileListStr = ','.join(fileNames)
00100 
00101   outputStub = os.path.join(fileTools.FILETOOL_TMP_PATH, 'tmpLatcXML')
00102   __clearBlobs(outputStub)
00103 
00104   # dispatch latc parse command
00105   cmd  = LATC_PARSE_COMMAND
00106   cmd += ' -i=%s' % fileListStr
00107   cmd += ' -o=%s' % outputStub
00108   
00109 
00110   rc = os.system(cmd)
00111   if rc != 0:
00112     raise RuntimeError("Failure of system command %s" % cmd)
00113 
00114   log.debug('adding headers')
00115   # grab latc xml files from disk
00116   outputDir, outputHead  = os.path.split(outputStub)
00117   fmxList = []
00118 
00119   for f in os.listdir(outputDir):
00120     if f[:len(outputHead)] == outputHead:
00121       tmp = fileTools.FswFileFromDisk( fileTools.FswFileID( LATC_DEVICE, LATC_DIR, fileTools.arbitraryFileID()), 
00122                                        os.path.join(outputDir, f) )
00123       tmp2 = fileTools.addFmxHeader(tmp, name=f, key=fileTools.arbitraryFmxID())
00124       fmxList.append( tmp2 )
00125       
00126   
00127   return fmxList
00128   
00129 def makeLatcMasterFromBlobs(blobSet):
00130   """!\brief Create the LATC master file from a set of blobs
00131   """
00132   masterBin = ''
00133   for fmxFile in blobSet:
00134     print 'adding fileID %x to master' % fmxFile.fileID().id()
00135     masterBin += struct.pack('!L', fmxFile.fileID().id())
00136 
00137   tmp = fileTools.FswFileFromData( fileTools.FswFileID( LATC_DEVICE, LATC_DIR, fileTools.arbitraryFileID()),
00138                                    masterBin )
00139   masterFile = fileTools.addFmxHeader( tmp, name='LCMASTER', key=fileTools.arbitraryFmxID()) 
00140 
00141   return masterFile
00142 
00143 
00144 def __clearBlobs(outputStub):
00145   """!\brief clear out all the latc blobs from the output stub area
00146   
00147   """
00148 
00149   dir,stub = os.path.split(outputStub)
00150   if stub == '': return
00151   for f in os.listdir(dir):
00152     if f[:len(stub)] == stub:
00153       os.remove(os.path.join(dir,f))
00154 

Generated on Thu Apr 27 20:52:42 2006 for LICOS L02-01-00 by doxygen 1.4.6-NO