rcRunIdHelper.py

Go to the documentation of this file.
00001 #!/usr/local/bin/python
00002 #
00003 #                               Copyright 2005
00004 #                                     by
00005 #                        The Board of Trustees of the
00006 #                     Leland Stanford Junior University.
00007 #                            All rights reserved.
00008 #
00009 
00010 __facility__ = "Online"
00011 __abstract__ = "Utility class to set and retrieve run id"
00012 __author__   = "S. Tuvi <stuvi@SLAC.Stanford.edu> SLAC - GLAST LAT I&T/Online"
00013 __date__     = "2005/07/23 00:08:27"
00014 __updated__  = "$Date: 2006/01/28 05:39:49 $"
00015 __version__  = "$Revision: 1.6 $"
00016 __release__  = "$Name: HEAD $"
00017 __credits__  = "SLAC"
00018 
00019 import LICOS.copyright_SLAC
00020 
00021 from ConfigParser import *
00022 import os
00023 
00024 class rcRunIdHelper(object):
00025   """!\brief Utility class to set and retrieve run id.
00026 
00027   """
00028   SECTION_M = 'MachineData'
00029   SECTION_R = 'RunIdData'
00030   OPTION_MID = 'machineid'
00031   OPTION_RID = 'runid'
00032 
00033   def __init__(self, prefs):
00034     """!\brief rcRunIdHelper constructor.
00035 
00036     \param prefs Preferences instance.
00037     """
00038     self.__parser = ConfigParser()
00039 
00040     if 'runiddir' in prefs:
00041       self.configFile = os.path.join(prefs['runiddir'], 'runId.cfg')
00042     else:
00043       raise RuntimeError, "Preference file doesn't specify runId location"
00044 
00045     if os.path.exists(self.configFile):
00046       self.__parser.read(self.configFile)
00047     else:
00048       errorMsg = self.configFile + " not found. "
00049       errorMsg += "Did you forget to copy it from a previous installation or from runId.cfg.sample?"
00050       raise RuntimeError, errorMsg
00051 
00052   def currentRunId(self):
00053     """!\brief Retrieve the current run id.
00054 
00055     \return The current run id (includes the machine id).
00056     """
00057     rid = -1
00058     mid = -1
00059     try:
00060       mid = int(self.__parser.get(self.SECTION_M, self.OPTION_MID))
00061       rid = int(self.__parser.get(self.SECTION_R, self.OPTION_RID))
00062     except:
00063       pass
00064 
00065     if mid == -1 or rid == -1:
00066       return '-1'
00067     else:
00068       return ('%03d%06d' % (mid, rid))
00069 
00070   def nextRunId(self):
00071     """!\brief Increment the run id and update runId.cfg.
00072 
00073     \return The next run id.
00074     """
00075     self.__parser.read(self.configFile)
00076     
00077     rid = -1
00078     mid = -1
00079     try:
00080       mid = int(self.__parser.get(self.SECTION_M, self.OPTION_MID))
00081       rid = int(self.__parser.get(self.SECTION_R, self.OPTION_RID))
00082     except:
00083       pass
00084 
00085     if mid == -1 or rid == -1:
00086       return '-1'
00087     else:
00088       if mid == 0:
00089         errorMsg = self.configFile + " has an invalid machine id. "
00090         errorMsg += "Please choose a valid and unique id for your subsystem and this teststand "
00091         errorMsg += "and enter it in this file (See runId.cfg.sample file in the same directory for "
00092         errorMsg += "subsystem allocation of machine ids)."
00093         raise RuntimeError, errorMsg
00094       else:
00095         rid +=1
00096         self.__parser.set(self.SECTION_R, self.OPTION_RID, str(rid))
00097         self.__parser.write(file(self.configFile, "w+"))
00098         return ('%03d%06d' % (mid, rid))

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