Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-15 08:47:27

0001 #!/usr/bin/env python3
0002 '''Script that runs SiStrip DCS O2O.
0003 @author: Huilin Qu
0004 '''
0005 
0006 import os
0007 import atexit
0008 import logging
0009 import socket
0010 import argparse
0011 import subprocess
0012 from functools import partial
0013 import CondTools.SiStrip.o2o_helper as helper
0014 
0015 jobDirVar = 'JOBDIR'
0016 cfg_file = 'CondTools/SiStrip/python/SiStripDCS_popcon.py'
0017 
0018 def runjob(args):
0019     if args.debug:
0020         logging.debug(str(args))
0021 
0022     # find cfg file
0023     for basedir in os.environ['CMSSW_SEARCH_PATH'].split(':'):
0024         cfg = os.path.join(basedir, cfg_file)
0025         if os.path.exists(cfg):
0026             logging.info('Use config file %s' % cfg)
0027             break
0028 
0029     output_db = 'SiStripDetVOff_{delay}.db'.format(delay=args.delay)
0030     if os.path.exists(output_db):
0031         logging.info('Output sqlite file %s already exists! Deleting...' % output_db)
0032         os.remove(output_db)
0033 
0034     # run cmssw job: raise error if failed
0035     command = 'cmsRun {cfg} delay={delay} destinationConnection={destFile} sourceConnection={sourceDb} conddbConnection={conddb} tag={tag}'.format(
0036         cfg=cfg, delay=args.delay, destFile='sqlite:///%s' % output_db, sourceDb=args.sourceDb, conddb=args.condDbRead, tag=args.inputTag)
0037     pipe = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
0038     atexit.register(partial(helper.kill_subproc_noexcept, pipe))
0039     out = pipe.communicate()[0]
0040     logging.info('\n%s\n' % out)
0041     logging.info('@@@CMSSW job return code = %d@@@' % pipe.returncode)
0042     if pipe.returncode != 0:
0043         raise RuntimeError('O2O job FAILED!')
0044 
0045     # upload: raise error if failed
0046     if args.no_upload:
0047         logging.info('Will not run uploading as requested!')
0048         return
0049 
0050     if not helper.exists_iov(output_db, args.inputTag):
0051         logging.info('No IOV exists in the SQLite file. Will skip upload!')
0052         return
0053 
0054     if args.use_uploader:
0055         f = helper.upload_payload
0056     else:
0057         f = helper.copy_payload
0058     f(dbFile=output_db, inputTag=args.inputTag, destTags=args.destTags, destDb=args.destDb, since=None,
0059       userText='SiStripDCS {delay} hour delay'.format(delay=args.delay))
0060 
0061     # clean up
0062     try:
0063         os.remove(output_db)
0064         os.remove(output_db.replace('.db', '.txt'))
0065     except OSError:
0066         pass
0067 
0068 def main():
0069     parser = argparse.ArgumentParser(description='Run a single O2O job for SiStrip DCS and upload the payloads to condition database.')
0070     parser.add_argument('--delay', required=True, help='Time delay (in hours) for the O2O. The O2O then queries the PVSS DB from last IOV until (current hour - delay), ignoring minutes and seconds.')
0071     parser.add_argument('--destTags', required=True, help='Destination tag name(s) for upload. Use comma to separate multiple values.')
0072     parser.add_argument('--sourceDb', required=True, help='Connection string for the source database.')
0073     parser.add_argument('--destDb', required=True, help='Destination DB to upload.')
0074     parser.add_argument('--inputTag', required=True, help='Tag name to be used in the sqlite file.')
0075     parser.add_argument('--condDbRead', default='oracle://cms_orcon_adg/CMS_CONDITIONS', help='Connection string for the DB from which the fast O2O retrives payloads.')
0076 
0077     parser.add_argument('--no-upload', action="store_true", default=False, help='Do not upload payload. Default: %(default)s.')
0078     parser.add_argument('--use-uploader', action="store_true", default=False, help='Use conditionUploader instead of conddb copy. Default: %(default)s.')
0079     parser.add_argument('--debug', action="store_true", default=False, help='Switch on debug mode. Default: %(default)s.')
0080     args = parser.parse_args()
0081 
0082     loglevel = logging.INFO
0083     if args.debug:
0084         loglevel = logging.DEBUG
0085     logging.basicConfig(level=loglevel, format='[%(asctime)s] %(levelname)s: %(message)s')
0086 
0087     args.destTags = args.destTags.strip().split(',')
0088 
0089     logging.info('Running DCS O2O with %s hour(s) delay on machine [%s]' % (str(args.delay), socket.gethostname()))
0090 
0091     try:
0092         jobdirbase = os.environ[jobDirVar]
0093     except KeyError:
0094         jobdirbase = '/tmp'
0095         logging.warning('%s not set in env, will use %s' % (jobDirVar, jobdirbase))
0096 
0097 
0098     # change to O2O working directory
0099     jobdir = os.path.join(jobdirbase, '{delay}hourDelay'.format(delay=args.delay))
0100     if not os.path.exists(jobdir):
0101         os.makedirs(jobdir)
0102     os.chdir(jobdir)
0103     logging.info('Running O2O in %s' % jobdir)
0104 
0105     # run job and upload
0106     runjob(args)
0107 
0108     logging.info('Done!')
0109 
0110 
0111 
0112 if __name__ == '__main__':
0113     main()