Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-12-01 23:40:05

0001 #!/usr/bin/env python3
0002 
0003 """
0004 Example script to test reading from local sqlite db.
0005 """
0006 import os
0007 import sys
0008 import ast
0009 import optparse
0010 import multiprocessing
0011 import CondCore.Utilities.conddblib as conddb
0012 
0013 officialdbs = {
0014     # frontier connections
0015     'frontier://PromptProd/CMS_CONDITIONS'   :'pro',
0016     'frontier://FrontierProd/CMS_CONDITIONS' :'pro',
0017     'frontier://FrontierArc/CMS_CONDITIONS'  :'arc',
0018     'frontier://FrontierInt/CMS_CONDITIONS'  :'int',
0019     'frontier://FrontierPrep/CMS_CONDITIONS' :'dev'
0020 }
0021 
0022 ##############################################
0023 def getCommandOutput(command):
0024 ##############################################
0025     """This function executes `command` and returns it output.
0026     Arguments:
0027     - `command`: Shell command to be invoked by this function.
0028     """
0029     print(command)
0030     child = os.popen(command)
0031     data = child.read()
0032     err = child.close()
0033     if err:
0034         raise Exception('%s failed w/ exit code %d' % (command, err))
0035     return data
0036 
0037 ##############################################
0038 def get_iovs(db, tag):
0039 ##############################################
0040     """Retrieve the list of IOVs from `db` for `tag`.
0041 
0042     Arguments:
0043     - `db`: database connection string
0044     - `tag`: tag of database record
0045     """
0046 
0047     ### unfortunately some gymnastics is needed here
0048     ### to make use of the conddb library
0049     if db in officialdbs.keys():
0050         db = officialdbs[db]
0051 
0052     ## allow to use input sqlite files as well
0053     db = db.replace("sqlite_file:", "").replace("sqlite:", "")
0054 
0055     con = conddb.connect(url = conddb.make_url(db))
0056     session = con.session()
0057     IOV = session.get_dbtype(conddb.IOV)
0058 
0059     iovs = set(session.query(IOV.since).filter(IOV.tag_name == tag).all())
0060     if len(iovs) == 0:
0061         print("No IOVs found for tag '"+tag+"' in database '"+db+"'.")
0062         sys.exit(1)
0063 
0064     session.close()
0065 
0066     return sorted([int(item[0]) for item in iovs])
0067 
0068 ##############################################
0069 def updateBits(blob):
0070 ##############################################
0071     if(blob[0]<blob[1]):
0072         command = 'conddb --yes --db %s copy %s --destdb %s --from %s --to %s' % (blob[4],
0073                                                                                   blob[5],
0074                                                                                   blob[5]+"_IOV_"+str(blob[2])+".db",
0075                                                                                   str(blob[2]) ,
0076                                                                                   str(blob[3]))
0077         getCommandOutput(command)
0078     else:
0079         # last IOV needs special command
0080         command = 'conddb --yes --db %s copy %s --destdb %s --from %s' % (blob[4],
0081                                                                           blob[5],
0082                                                                           blob[5]+"_IOV_"+str(blob[2])+".db",
0083                                                                           str(blob[2]))
0084         getCommandOutput(command)
0085 
0086     # update the trigger bits
0087     cmsRunCommand='cmsRun $CMSSW_BASE/src/CondTools/HLT/test/AlCaRecoTriggerBitsRcdUpdate_TEMPL_cfg.py \
0088     inputDB=%s inputTag=%s outputDB=%s outputTag=%s firstRun=%s' % ("sqlite_file:"+blob[5]+"_IOV_"+str(blob[2])+".db",
0089                                                                     blob[5],
0090                                                                     "sqlite_file:"+blob[5]+"_IOV_"+str(blob[2])+"_updated.db",
0091                                                                     blob[6],
0092                                                                     str(blob[2]))
0093     getCommandOutput(cmsRunCommand)
0094 
0095 ##############################################
0096 def main():
0097 ##############################################
0098      
0099     defaultDB     = 'sqlite_file:mySqlite.db'
0100     defaultInTag  = 'myInTag'
0101     defaultOutTag = 'myOutTag'
0102     defaultProc   = 20
0103 
0104     parser = optparse.OptionParser(usage = 'Usage: %prog [options] <file> [<file> ...]\n')
0105     parser.add_option('-f', '--inputDB',
0106                       dest = 'inputDB',
0107                       default = defaultDB,
0108                       help = 'file to inspect')
0109     parser.add_option('-i', '--inputTag',
0110                       dest = 'InputTag',
0111                       default = defaultInTag,
0112                       help = 'tag to be inspected')
0113     parser.add_option('-d', '--destTag',
0114                       dest = 'destTag',
0115                       default = defaultOutTag,
0116                       help = 'tag to be written')
0117     parser.add_option('-p', '--processes',
0118                       dest = 'nproc',
0119                       default = defaultProc,
0120                       help = 'multiprocesses to run')
0121     parser.add_option("-C", '--clean',
0122                       dest="doTheCleaning",
0123                       action="store_true",
0124                       default = True,
0125                       help = 'if true remove the transient files')
0126 
0127     (options, arguments) = parser.parse_args()
0128 
0129     db_url = options.inputDB
0130     if db_url in officialdbs.keys():
0131         db_url = officialdbs[db_url]
0132 
0133     ## allow to use input sqlite files as well
0134     db_url = db_url.replace("sqlite_file:", "").replace("sqlite:", "")
0135 
0136     sinces = get_iovs(options.inputDB,options.InputTag)
0137 
0138     print("List of sinces: %s" % sinces)
0139 
0140     myInputTuple=[]
0141 
0142     for i,since in enumerate(sinces):
0143         if(i<len(sinces)-1):
0144             #                   0             1        2               3      4                5               6
0145             myInputTuple.append((i,len(sinces)-1,sinces[i],sinces[i+1]-1,db_url,options.InputTag,options.destTag))
0146         else:
0147             myInputTuple.append((i,len(sinces)-1,sinces[i],-1,db_url,options.InputTag,options.destTag))
0148 
0149     pool = multiprocessing.Pool(processes=options.nproc)  # start nproc worker processes
0150     count = pool.map(updateBits,myInputTuple)
0151 
0152     # merge the output
0153     for i,since in enumerate(sinces):
0154         mergeCommand='conddb --yes --db %s copy %s --destdb %s --from %s' % (options.InputTag+"_IOV_"+str(sinces[i])+"_updated.db",
0155                                                                              options.destTag,
0156                                                                              options.destTag+".db",
0157                                                                              str(sinces[i]))
0158         getCommandOutput(mergeCommand)
0159 
0160     # clean the house (after all is done)
0161     if(options.doTheCleaning):
0162         cleanCommand = 'rm -fr *updated*.db *IOV_*.db'
0163         getCommandOutput(cleanCommand)
0164     else:
0165         print("======> keeping the transient files")
0166           
0167 if __name__ == "__main__":        
0168      main()