CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Functions
o2o_helper Namespace Reference

Functions

def configLogger
 
def copy_payload
 
def create_metadata
 
def exists_iov
 
def insert_to_file
 
def kill_subproc_noexcept
 
def send_mail
 
def upload_payload
 

Detailed Description

Helper Script for StripO2O
@author: Huilin Qu

Function Documentation

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 24 of file o2o_helper.py.

24 
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)
def configLogger
Definition: o2o_helper.py:24
def o2o_helper.copy_payload (   dbFile,
  inputTag,
  destTags,
  destDb,
  since,
  userText 
)
Upload payload using conddb copy.

Definition at line 103 of file o2o_helper.py.

References filterCSVwithJSON.copy.

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 
def copy_payload
Definition: o2o_helper.py:103
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 55 of file o2o_helper.py.

Referenced by upload_payload().

55 
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))
def create_metadata
Definition: o2o_helper.py:55
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 149 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
def exists_iov
Definition: o2o_helper.py:149
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 43 of file o2o_helper.py.

43 
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 replace_dict.items():
52  config = config.replace(key, value)
53  output_file.write(config)
54  return config
def insert_to_file
Definition: o2o_helper.py:43
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 15 of file o2o_helper.py.

15 
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 
def kill_subproc_noexcept
Definition: o2o_helper.py:15
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 131 of file o2o_helper.py.

References join().

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()
def send_mail
Definition: o2o_helper.py:131
static std::string join(char **cmd)
Definition: RemoteFile.cc:19
def o2o_helper.upload_payload (   dbFile,
  inputTag,
  destTags,
  destDb,
  since,
  userText 
)
Upload payload using conditionUploader. 

Definition at line 87 of file o2o_helper.py.

References create_metadata(), and join().

87 
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 
def upload_payload
Definition: o2o_helper.py:87
def create_metadata
Definition: o2o_helper.py:55
static std::string join(char **cmd)
Definition: RemoteFile.cc:19