2 Helper Script for StripO2O 11 from email.mime.multipart
import MIMEMultipart
12 from email.mime.text
import MIMEText
16 '''Kill a subprocess without throwing OSError. 17 Used for cleaning up subprocesses when the main script crashes.''' 25 '''Setting up logging to both file and console. 26 @see: https://docs.python.org/2/howto/logging-cookbook.html 29 logging.basicConfig(level=loglevel,
30 format=
'[%(asctime)s] %(levelname)s: %(message)s',
34 console = logging.StreamHandler()
35 console.setLevel(loglevel)
37 formatter = logging.Formatter(
'[%(levelname)s] %(message)s')
39 console.setFormatter(formatter)
41 logging.getLogger(
'').addHandler(console)
44 '''Update the template file based on the replace_dict, and write to the target.''' 45 logging.debug(
'Creating "%s" from template "%s" using dictionary:'%(target, template))
46 logging.debug(replace_dict)
47 with open(template,
'r') as input_file: 48 config=input_file.read() 49 with open(target, 'w')
as output_file:
50 for key, value
in replace_dict.iteritems():
51 config = config.replace(key, value)
52 output_file.write(config)
55 def create_metadata(metadataFilename, inputTag, destTags, destDb, since, userText):
56 '''Create metadata file for the conditionsUpload service. 57 @see: uploadConditions.runWizard() 58 @see: https://twiki.cern.ch/twiki/bin/view/CMS/DropBox 61 metadataFilename -- output metadata filename 62 inputTag -- input tag name 63 destTags -- a list of destination tags 64 destDb -- [destinationDatabase] in metadata 65 since -- [since] in metadata 66 userText -- [userText] in metadata 68 if isinstance(destTags, str):
73 for destinationTag
in destTags:
74 destinationTags[destinationTag] = {}
76 'destinationDatabase': destDb,
77 'destinationTags': destinationTags,
82 logging.info(
'Writing metadata in %s', metadataFilename)
83 logging.debug(metadata)
84 with open(metadataFilename,
'wb')
as metadataFile:
85 metadataFile.write(json.dumps(metadata, sort_keys=
True, indent=4))
88 '''Upload payload using conditionUploader. ''' 89 if isinstance(destTags, str):
91 metadataFilename = dbFile.replace(
'.db',
'.txt')
92 create_metadata(metadataFilename, inputTag, destTags, destDb, since, userText)
93 logging.info(
'Uploading tag [%s] from %s to [%s] in %s:' % (inputTag, dbFile,
','.
join(destTags), destDb))
94 command =
"uploadConditions.py %s" % dbFile
95 pipe = subprocess.Popen(command, shell=
True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
96 out = pipe.communicate()[0]
98 logging.info(
'@@@Upload return code = %d@@@' % pipe.returncode)
99 if pipe.returncode != 0:
100 raise RuntimeError(
'Upload FAILED!')
104 '''Upload payload using conddb copy.''' 105 if isinstance(destTags, str):
106 destTags = [destTags]
107 if destDb.lower() ==
'oracle://cms_orcon_prod/cms_conditions':
108 copyDestDb =
'onlineorapro' 109 elif destDb.lower() ==
'oracle://cms_orcoff_prep/cms_conditions':
110 copyDestDb =
'oradev' 115 command =
'conddb --force --yes --db {db} copy {inputTag} {destTag} --destdb {destDb} --synchronize --note "{note}"'.
format(
116 db=dbFile, inputTag=inputTag, destTag=dest, destDb=copyDestDb, note=userText)
117 logging.info(
'Copy tag [%s] from %s to [%s] in %s:' % (inputTag, dbFile, dest, destDb))
118 logging.debug(command)
119 pipe = subprocess.Popen(command, shell=
True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
120 out = pipe.communicate()[0]
122 return pipe.returncode
123 for dest
in destTags:
124 returncode =
copy(dest)
125 if returncode == 0: success += 1
126 logging.info(
'@@@Upload return code = %d@@@' % (success - len(destTags)))
127 if success != len(destTags):
128 raise RuntimeError(
'Upload FAILED!')
131 def send_mail(subject, message, send_to, send_from, text_attachments=[]):
132 '''Send an email. [send_to] needs to be a list.''' 133 msg = MIMEMultipart()
134 msg[
'Subject'] = subject
135 msg[
'From'] = send_from
136 msg[
'To'] =
','.
join(send_to)
137 msg.attach(MIMEText(message))
139 for fn
in text_attachments:
140 with open(fn,
'rb')
as txtfile:
141 attachment = MIMEText(txtfile.read())
142 attachment.add_header(
'Content-Disposition',
'attachment', filename=os.path.basename(fn))
143 msg.attach(attachment)
145 s = smtplib.SMTP(
'localhost')
146 s.sendmail(send_from, send_to, msg.as_string())
150 '''Check if there exists any IOV for a specific tag in the given sqlite file.''' 151 dataConnection = sqlite3.connect(dbFile)
152 dataCursor = dataConnection.cursor()
153 dataCursor.execute(
'select SINCE from IOV where TAG_NAME=:tag_name', {
'tag_name' : tag})
154 return len(dataCursor.fetchall()) > 0
def create_metadata(metadataFilename, inputTag, destTags, destDb, since, userText)
def exists_iov(dbFile, tag)
def configLogger(logfile, loglevel=logging.INFO)
def send_mail(subject, message, send_to, send_from, text_attachments=[])
static std::string join(char **cmd)
def insert_to_file(template, target, replace_dict)
def upload_payload(dbFile, inputTag, destTags, destDb, since, userText)
def copy_payload(dbFile, inputTag, destTags, destDb, since, userText)
def kill_subproc_noexcept(p)