00001 #!/usr/local/bin/python 00002 # 00003 # Copyright 2002 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__ = ("$Date: 2004/11/05 00:41:27 $").split(' ')[1] 00014 __version__ = "$Revision: 2.2 $" 00015 __credits__ = "SLAC" 00016 00017 import LATTE.copyright_SLAC 00018 00019 from ConfigParser import * 00020 import os 00021 00022 class rcRunIdHelper(object): 00023 SECTION_M = 'MachineData' 00024 SECTION_R = 'RunIdData' 00025 OPTION_MID = 'machineid' 00026 OPTION_RID = 'runid' 00027 00028 def __init__(self, prefs): 00029 self.__parser = ConfigParser() 00030 00031 if 'runiddir' in prefs: 00032 self.configFile = os.path.join(prefs['runiddir'], 'runId.cfg') 00033 else: 00034 raise RuntimeError, "Preference file doesn't specify runId location" 00035 00036 if os.path.exists(self.configFile): 00037 self.__parser.read(self.configFile) 00038 else: 00039 errorMsg = self.configFile + " not found. " 00040 errorMsg += "Did you forget to copy it from a previous installation or from runId.cfg.sample?" 00041 raise RuntimeError, errorMsg 00042 00043 def currentRunId(self): 00044 rid = -1 00045 mid = -1 00046 try: 00047 mid = int(self.__parser.get(self.SECTION_M, self.OPTION_MID)) 00048 rid = int(self.__parser.get(self.SECTION_R, self.OPTION_RID)) 00049 except: 00050 pass 00051 00052 if mid == -1 or rid == -1: 00053 return '-1' 00054 else: 00055 return ('%03d%06d' % (mid, rid)) 00056 00057 def nextRunId(self): 00058 rid = -1 00059 mid = -1 00060 try: 00061 mid = int(self.__parser.get(self.SECTION_M, self.OPTION_MID)) 00062 rid = int(self.__parser.get(self.SECTION_R, self.OPTION_RID)) 00063 except: 00064 pass 00065 00066 if mid == -1 or rid == -1: 00067 return '-1' 00068 else: 00069 if mid == 0: 00070 errorMsg = self.configFile + " has an invalid machine id. " 00071 errorMsg += "Please choose a valid and unique id for your subsystem and this teststand " 00072 errorMsg += "and enter it in this file (See runId.cfg.sample file in the same directory for " 00073 errorMsg += "subsystem allocation of machine ids)." 00074 raise RuntimeError, errorMsg 00075 else: 00076 rid +=1 00077 self.__parser.set(self.SECTION_R, self.OPTION_RID, str(rid)) 00078 self.__parser.write(file(self.configFile, "w+")) 00079 return ('%03d%06d' % (mid, rid))