CMS 3D CMS Logo

Functions
o2o_helper Namespace Reference

Functions

def configLogger (logfile, loglevel=logging.INFO)
 
def copy_payload (dbFile, inputTag, destTags, destDb, since, userText)
 
def create_metadata (metadataFilename, inputTag, destTags, destDb, since, userText)
 
def exists_iov (dbFile, tag)
 
def insert_to_file (template, target, replace_dict)
 
def kill_subproc_noexcept (p)
 
def send_mail (subject, message, send_to, send_from, text_attachments=[])
 
def upload_payload (dbFile, inputTag, destTags, destDb, since, userText)
 

Detailed Description

Helper Script for StripO2O
@author: Huilin Qu

Function Documentation

◆ configLogger()

def o2o_helper.configLogger (   logfile,
  loglevel = logging.INFO 
)
Setting up logging to both file and console.
@see: https://docs.python.org/2/howto/logging-cookbook.html

Definition at line 25 of file o2o_helper.py.

25 def configLogger(logfile,loglevel=logging.INFO):
26  '''Setting up logging to both file and console.
27  @see: https://docs.python.org/2/howto/logging-cookbook.html
28  '''
29  # set up logging to file
30  logging.basicConfig(level=loglevel,
31  format='[%(asctime)s] %(levelname)s: %(message)s',
32  filename=logfile,
33  filemode='w')
34  # define a Handler which writes INFO messages or higher to the sys.stderr
35  console = logging.StreamHandler()
36  console.setLevel(loglevel)
37  # set a format which is simpler for console use
38  formatter = logging.Formatter('[%(levelname)s] %(message)s')
39  # tell the handler to use this format
40  console.setFormatter(formatter)
41  # add the handler to the root logger
42  logging.getLogger('').addHandler(console)
43 

◆ copy_payload()

def o2o_helper.copy_payload (   dbFile,
  inputTag,
  destTags,
  destDb,
  since,
  userText 
)
Upload payload using conddb copy.

Definition at line 104 of file o2o_helper.py.

104 def copy_payload(dbFile, inputTag, destTags, destDb, since, userText):
105  '''Upload payload using conddb copy.'''
106  if isinstance(destTags, str):
107  destTags = [destTags]
108  if destDb.lower() == 'oracle://cms_orcon_prod/cms_conditions':
109  copyDestDb = 'onlineorapro'
110  elif destDb.lower() == 'oracle://cms_orcoff_prep/cms_conditions':
111  copyDestDb = 'oradev'
112  else:
113  copyDestDb = destDb
114  success = 0
115  def copy(dest):
116  command = 'conddb --force --yes --db {db} copy {inputTag} {destTag} --destdb {destDb} --synchronize --note "{note}"'.format(
117  db=dbFile, inputTag=inputTag, destTag=dest, destDb=copyDestDb, note=userText)
118  logging.info('Copy tag [%s] from %s to [%s] in %s:' % (inputTag, dbFile, dest, destDb))
119  logging.debug(command)
120  pipe = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
121  out = pipe.communicate()[0]
122  logging.info(out)
123  return pipe.returncode
124  for dest in destTags:
125  returncode = copy(dest)
126  if returncode == 0: success += 1
127  logging.info('@@@Upload return code = %d@@@' % (success - len(destTags)))
128  if success != len(destTags):
129  raise RuntimeError('Upload FAILED!')
130 
131 

References filterCSVwithJSON.copy.

◆ create_metadata()

def o2o_helper.create_metadata (   metadataFilename,
  inputTag,
  destTags,
  destDb,
  since,
  userText 
)
Create metadata file for the conditionsUpload service.
@see: uploadConditions.runWizard()
@see: https://twiki.cern.ch/twiki/bin/view/CMS/DropBox

Keyword arguments:
metadataFilename -- output metadata filename
inputTag -- input tag name
destTags -- a list of destination tags
destDb -- [destinationDatabase] in metadata
since -- [since] in metadata
userText -- [userText] in metadata

Definition at line 56 of file o2o_helper.py.

56 def create_metadata(metadataFilename, inputTag, destTags, destDb, since, userText):
57  '''Create metadata file for the conditionsUpload service.
58  @see: uploadConditions.runWizard()
59  @see: https://twiki.cern.ch/twiki/bin/view/CMS/DropBox
60 
61  Keyword arguments:
62  metadataFilename -- output metadata filename
63  inputTag -- input tag name
64  destTags -- a list of destination tags
65  destDb -- [destinationDatabase] in metadata
66  since -- [since] in metadata
67  userText -- [userText] in metadata
68  '''
69  if isinstance(destTags, str):
70  destTags = [destTags]
71  if since:
72  since = int(since) # convert to int if since is not None (input since can be a str)
73  destinationTags = {}
74  for destinationTag in destTags:
75  destinationTags[destinationTag] = {}
76  metadata = {
77  'destinationDatabase': destDb,
78  'destinationTags': destinationTags,
79  'inputTag': inputTag,
80  'since': since,
81  'userText': userText,
82  }
83  logging.info('Writing metadata in %s', metadataFilename)
84  logging.debug(metadata)
85  with open(metadataFilename, 'wb') as metadataFile:
86  metadataFile.write(json.dumps(metadata, sort_keys=True, indent=4))
87 

References createfilelist.int.

Referenced by upload_payload().

◆ exists_iov()

def o2o_helper.exists_iov (   dbFile,
  tag 
)
Check if there exists any IOV for a specific tag in the given sqlite file.

Definition at line 150 of file o2o_helper.py.

150 def exists_iov(dbFile, tag):
151  '''Check if there exists any IOV for a specific tag in the given sqlite file.'''
152  dataConnection = sqlite3.connect(dbFile)
153  dataCursor = dataConnection.cursor()
154  dataCursor.execute('select SINCE from IOV where TAG_NAME=:tag_name', {'tag_name' : tag})
155  return len(dataCursor.fetchall()) > 0

◆ insert_to_file()

def o2o_helper.insert_to_file (   template,
  target,
  replace_dict 
)
Update the template file based on the replace_dict, and write to the target.

Definition at line 44 of file o2o_helper.py.

44 def insert_to_file(template, target, replace_dict):
45  '''Update the template file based on the replace_dict, and write to the target.'''
46  logging.debug('Creating "%s" from template "%s" using dictionary:'%(target, template))
47  logging.debug(replace_dict)
48  with open(template, 'r') as input_file:
49  config=input_file.read()
50  with open(target, 'w') as output_file:
51  for key, value in six.iteritems(replace_dict):
52  config = config.replace(key, value)
53  output_file.write(config)
54  return config
55 

◆ kill_subproc_noexcept()

def o2o_helper.kill_subproc_noexcept (   p)
Kill a subprocess without throwing OSError.
Used for cleaning up subprocesses when the main script crashes.

Definition at line 16 of file o2o_helper.py.

17  '''Kill a subprocess without throwing OSError.
18  Used for cleaning up subprocesses when the main script crashes.'''
19  try:
20  p.terminate()
21  except OSError:
22  pass
23 
24 

◆ send_mail()

def o2o_helper.send_mail (   subject,
  message,
  send_to,
  send_from,
  text_attachments = [] 
)
Send an email. [send_to] needs to be a list.

Definition at line 132 of file o2o_helper.py.

132 def send_mail(subject, message, send_to, send_from, text_attachments=[]):
133  '''Send an email. [send_to] needs to be a list.'''
134  msg = MIMEMultipart()
135  msg['Subject'] = subject
136  msg['From'] = send_from
137  msg['To'] = ','.join(send_to)
138  msg.attach(MIMEText(message))
139 
140  for fn in text_attachments:
141  with open(fn, 'rb') as txtfile:
142  attachment = MIMEText(txtfile.read())
143  attachment.add_header('Content-Disposition', 'attachment', filename=os.path.basename(fn))
144  msg.attach(attachment)
145 
146  s = smtplib.SMTP('localhost')
147  s.sendmail(send_from, send_to, msg.as_string())
148  s.quit()
149 

References join().

◆ upload_payload()

def o2o_helper.upload_payload (   dbFile,
  inputTag,
  destTags,
  destDb,
  since,
  userText 
)
Upload payload using conditionUploader. 

Definition at line 88 of file o2o_helper.py.

88 def upload_payload(dbFile, inputTag, destTags, destDb, since, userText):
89  '''Upload payload using conditionUploader. '''
90  if isinstance(destTags, str):
91  destTags = [destTags]
92  metadataFilename = dbFile.replace('.db', '.txt')
93  create_metadata(metadataFilename, inputTag, destTags, destDb, since, userText)
94  logging.info('Uploading tag [%s] from %s to [%s] in %s:' % (inputTag, dbFile, ','.join(destTags), destDb))
95  command = "uploadConditions.py %s" % dbFile
96  pipe = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
97  out = pipe.communicate()[0]
98  logging.info(out)
99  logging.info('@@@Upload return code = %d@@@' % pipe.returncode)
100  if pipe.returncode != 0:
101  raise RuntimeError('Upload FAILED!')
102 
103 

References create_metadata(), and join().

filterCSVwithJSON.copy
copy
Definition: filterCSVwithJSON.py:36
o2o_helper.insert_to_file
def insert_to_file(template, target, replace_dict)
Definition: o2o_helper.py:44
join
static std::string join(char **cmd)
Definition: RemoteFile.cc:17
o2o_helper.kill_subproc_noexcept
def kill_subproc_noexcept(p)
Definition: o2o_helper.py:16
o2o_helper.exists_iov
def exists_iov(dbFile, tag)
Definition: o2o_helper.py:150
o2o_helper.configLogger
def configLogger(logfile, loglevel=logging.INFO)
Definition: o2o_helper.py:25
createfilelist.int
int
Definition: createfilelist.py:10
o2o_helper.upload_payload
def upload_payload(dbFile, inputTag, destTags, destDb, since, userText)
Definition: o2o_helper.py:88
format
o2o_helper.send_mail
def send_mail(subject, message, send_to, send_from, text_attachments=[])
Definition: o2o_helper.py:132
o2o_helper.copy_payload
def copy_payload(dbFile, inputTag, destTags, destDb, since, userText)
Definition: o2o_helper.py:104
o2o_helper.create_metadata
def create_metadata(metadataFilename, inputTag, destTags, destDb, since, userText)
Definition: o2o_helper.py:56