CMS 3D CMS Logo

SiStripDCSPopCon.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 '''Script that runs SiStrip DCS O2O.
3 @author: Huilin Qu
4 '''
5 
6 import os
7 import atexit
8 import logging
9 import socket
10 import argparse
11 import subprocess
12 from functools import partial
13 import CondTools.SiStrip.o2o_helper as helper
14 
15 jobDirVar = 'JOBDIR'
16 cfg_file = 'CondTools/SiStrip/python/SiStripDCS_popcon.py'
17 
18 def runjob(args):
19  if args.debug:
20  logging.debug(str(args))
21 
22  # find cfg file
23  for basedir in os.environ['CMSSW_SEARCH_PATH'].split(':'):
24  cfg = os.path.join(basedir, cfg_file)
25  if os.path.exists(cfg):
26  logging.info('Use config file %s' % cfg)
27  break
28 
29  output_db = 'SiStripDetVOff_{delay}.db'.format(delay=args.delay)
30  if os.path.exists(output_db):
31  logging.info('Output sqlite file %s already exists! Deleting...' % output_db)
32  os.remove(output_db)
33 
34  # run cmssw job: raise error if failed
35  command = 'cmsRun {cfg} delay={delay} destinationConnection={destFile} sourceConnection={sourceDb} conddbConnection={conddb} tag={tag}'.format(
36  cfg=cfg, delay=args.delay, destFile='sqlite:///%s' % output_db, sourceDb=args.sourceDb, conddb=args.condDbRead, tag=args.inputTag)
37  pipe = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
38  atexit.register(partial(helper.kill_subproc_noexcept, pipe))
39  out = pipe.communicate()[0]
40  logging.info('\n%s\n' % out)
41  logging.info('@@@CMSSW job return code = %d@@@' % pipe.returncode)
42  if pipe.returncode != 0:
43  raise RuntimeError('O2O job FAILED!')
44 
45  # upload: raise error if failed
46  if args.no_upload:
47  logging.info('Will not run uploading as requested!')
48  return
49 
50  if not helper.exists_iov(output_db, args.inputTag):
51  logging.info('No IOV exists in the SQLite file. Will skip upload!')
52  return
53 
54  if args.use_uploader:
55  f = helper.upload_payload
56  else:
57  f = helper.copy_payload
58  f(dbFile=output_db, inputTag=args.inputTag, destTags=args.destTags, destDb=args.destDb, since=None,
59  userText='SiStripDCS {delay} hour delay'.format(delay=args.delay))
60 
61  # clean up
62  try:
63  os.remove(output_db)
64  os.remove(output_db.replace('.db', '.txt'))
65  except OSError:
66  pass
67 
68 def main():
69  parser = argparse.ArgumentParser(description='Run a single O2O job for SiStrip DCS and upload the payloads to condition database.')
70  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.')
71  parser.add_argument('--destTags', required=True, help='Destination tag name(s) for upload. Use comma to separate multiple values.')
72  parser.add_argument('--sourceDb', required=True, help='Connection string for the source database.')
73  parser.add_argument('--destDb', required=True, help='Destination DB to upload.')
74  parser.add_argument('--inputTag', required=True, help='Tag name to be used in the sqlite file.')
75  parser.add_argument('--condDbRead', default='oracle://cms_orcon_adg/CMS_CONDITIONS', help='Connection string for the DB from which the fast O2O retrives payloads.')
76 
77  parser.add_argument('--no-upload', action="store_true", default=False, help='Do not upload payload. Default: %(default)s.')
78  parser.add_argument('--use-uploader', action="store_true", default=False, help='Use conditionUploader instead of conddb copy. Default: %(default)s.')
79  parser.add_argument('--debug', action="store_true", default=False, help='Switch on debug mode. Default: %(default)s.')
80  args = parser.parse_args()
81 
82  loglevel = logging.INFO
83  if args.debug:
84  loglevel = logging.DEBUG
85  logging.basicConfig(level=loglevel, format='[%(asctime)s] %(levelname)s: %(message)s')
86 
87  args.destTags = args.destTags.strip().split(',')
88 
89  logging.info('Running DCS O2O with %s hour(s) delay on machine [%s]' % (str(args.delay), socket.gethostname()))
90 
91  try:
92  jobdirbase = os.environ[jobDirVar]
93  except KeyError:
94  jobdirbase = '/tmp'
95  logging.warning('%s not set in env, will use %s' % (jobDirVar, jobdirbase))
96 
97 
98  # change to O2O working directory
99  jobdir = os.path.join(jobdirbase, '{delay}hourDelay'.format(delay=args.delay))
100  if not os.path.exists(jobdir):
101  os.makedirs(jobdir)
102  os.chdir(jobdir)
103  logging.info('Running O2O in %s' % jobdir)
104 
105  # run job and upload
106  runjob(args)
107 
108  logging.info('Done!')
109 
110 
111 
112 if __name__ == '__main__':
113  main()
double f[11][100]
Definition: main.py:1
#define str(s)