235 parser = optparse.OptionParser(description=
"CMS Conditions Upload Script in CondDBFW.",
236 usage =
'Usage: %prog [options] <file>')
239 parser.add_option(
"-i",
"--inputTag", type=str,\
240 help=
"Tag to take IOVs + Payloads from in --sourceDB.")
241 parser.add_option(
"-t",
"--destinationTag", type=str,\
242 help=
"Tag to copy IOVs + Payloads to in --destDB.")
243 parser.add_option(
"-D",
"--destinationDatabase", type=str,\
244 help=
"Database to copy IOVs + Payloads to.")
245 parser.add_option(
"-s",
"--since", type=int,\
246 help=
"Since to take IOVs from.")
247 parser.add_option(
"-u",
"--userText", type=str,\
248 help=
"Description of --destTag (can be empty).")
251 parser.add_option(
"-m",
"--metadataFile", type=str, help=
"Metadata file to take metadata from.")
253 parser.add_option(
"-d",
"--debug", action=
"store_true", default=
False)
254 parser.add_option(
"-v",
"--verbose", action=
"store_true", default=
False)
255 parser.add_option(
"-T",
"--testing", action=
"store_true")
256 parser.add_option(
"--fcsr-filter", type=str, help=
"Synchronization to take FCSR from for local filtering of IOVs.")
258 parser.add_option(
"-n",
"--netrc", help =
'The netrc host (machine) from where the username and password will be read.')
260 parser.add_option(
"-a",
"--authPath", help =
'The path of the .netrc file for the authentication. Default: $HOME')
262 parser.add_option(
"-H",
"--hashToUse")
264 parser.add_option(
"-S",
"--server")
266 parser.add_option(
"-o",
"--review-options", action=
"store_true")
268 parser.add_option(
"-r",
"--replay-file")
270 (command_line_data, arguments) = parser.parse_args()
272 if len(arguments) < 1:
273 if command_line_data.hashToUse ==
None:
277 command_line_data.sourceDB = arguments[0]
279 if command_line_data.replay_file:
280 dictionary = json.loads(
"".
join(open(command_line_data.replay_file,
"r").readlines()))
281 command_line_data.tier0_response = dictionary[
"tier0_response"]
284 server_alias_to_url = {
285 "prep" :
"https://cms-conddb-dev.cern.ch/cmsDbCondUpload/",
286 "dev" :
"https://cms-conddb-dev.cern.ch/cmsDbCondUpload/",
287 "prod" :
"https://cms-conddb.cern.ch/cmsDbCondUpload/"
292 if command_line_data.server
in server_alias_to_url.keys():
293 command_line_data.server = server_alias_to_url[command_line_data.server]
296 database_alias_to_connection = {
297 "prep":
"oracle://cms_orcoff_prep/CMS_CONDITIONS",
298 "dev":
"oracle://cms_orcoff_prep/CMS_CONDITIONS",
299 "prod":
"oracle://cms_orcon_adg/CMS_CONDITIONS"
302 if command_line_data.destinationDatabase
in database_alias_to_connection.keys():
303 command_line_data.destinationDatabase = database_alias_to_connection[command_line_data.destinationDatabase]
308 netrc_file = command_line_data.netrc
309 auth_path = command_line_data.authPath
310 if not auth_path
is None:
311 if netrc_file
is None:
312 netrc_file = os.path.join(auth_path,
'.netrc')
314 netrc_file = os.path.join(auth_path, netrc_file)
316 netrc_authenticators = netrc.netrc(netrc_file).authenticators(
"ConditionUploader")
317 if netrc_authenticators ==
None:
318 print(
"Your netrc file must contain the key 'ConditionUploader'.")
319 manual_input = raw_input(
"Do you want to try to type your credentials? ")
320 if manual_input ==
"y":
322 username = raw_input(
"Username: ")
323 password = getpass.getpass(
"Password: ")
327 username = netrc_authenticators[0]
328 password = netrc_authenticators[2]
330 print(
"Couldn't obtain your credentials (either from netrc or manual input).")
333 command_line_data.username = username
334 command_line_data.password = password
337 command_line_data.destinationTags = {command_line_data.destinationTag:{}}
340 Construct metadata_dictionary:
341 Currently, this is 3 cases:
343 1) An IOV is being appended to an existing Tag with an existing Payload.
344 In this case, we just take all data from the command line.
346 2) No metadata file is given, so we assume that ALL upload metadata is coming from the command line.
348 3) A metadata file is given, hence we parse the file, and then iterate through command line arguments
349 since these override the options set in the metadata file.
354 if command_line_data.hashToUse !=
None:
355 command_line_data.userText =
""
356 metadata_dictionary = command_line_data.__dict__
357 elif command_line_data.metadataFile ==
None:
358 if command_line_data.sourceDB !=
None and (command_line_data.inputTag ==
None or command_line_data.destinationTag ==
None or command_line_data.destinationDatabase ==
None):
359 basepath = command_line_data.sourceDB.rsplit(
'.db', 1)[0].rsplit(
'.txt', 1)[0]
360 basename = os.path.basename(basepath)
361 dataFilename =
'%s.db' % basepath
362 metadataFilename =
'%s.txt' % basepath
366 with open(dataFilename,
'rb')
as dataFile:
369 errMsg =
'Impossible to open SQLite data file %s' %dataFilename
372 ret[
'error'] = errMsg
377 command_line_data.sourceDB = dataFilename
380 with open(metadataFilename,
'rb')
as metadataFile:
383 if e.errno != errno.ENOENT:
384 errMsg =
'Impossible to open file %s (for other reason than not existing)' %metadataFilename
387 ret[
'error'] = errMsg
390 if getInput(
'y',
'\nIt looks like the metadata file %s does not exist and not enough parameters were received in the command line. Do you want me to create it and help you fill it?\nAnswer [y]: ' % metadataFilename).lower() !=
'y':
391 errMsg =
'Metadata file %s does not exist' %metadataFilename
394 ret[
'error'] = errMsg
397 runWizard(basename, dataFilename, metadataFilename)
398 command_line_data.metadataFile = metadataFilename
400 command_line_data.userText = command_line_data.userText\
401 if command_line_data.userText !=
None\
402 else str(raw_input(
"Tag's description [can be empty]:"))
403 metadata_dictionary = command_line_data.__dict__
405 if command_line_data.metadataFile !=
None:
406 metadata_dictionary = json.loads(
"".
join(open(os.path.abspath(command_line_data.metadataFile),
"r").readlines()))
407 metadata_dictionary[
"username"] = username
408 metadata_dictionary[
"password"] = password
409 metadata_dictionary[
"userText"] = metadata_dictionary.get(
"userText")\
410 if metadata_dictionary.get(
"userText") !=
None\
411 else str(raw_input(
"Tag's description [can be empty]:"))
414 for (option_name, option_value)
in command_line_data.__dict__.items():
416 if option_name !=
"destinationTags":
417 if option_value !=
None or (option_value ==
None and not(option_name
in metadata_dictionary.keys())):
421 metadata_dictionary[option_name] = option_value
423 if option_value != {
None:{}}:
424 metadata_dictionary[
"destinationTags"] = {option_value:{}}
425 elif option_value == {
None:{}}
and not(
"destinationTags" in metadata_dictionary.keys()):
426 metadata_dictionary[
"destinationTags"] = {
None:{}}
428 if command_line_data.review_options:
430 "since" :
"Since of first IOV",
431 "userText" :
"Populated by upload process",
432 "netrc" :
"None given",
433 "fcsr_filter" :
"Don't apply",
434 "hashToUse" :
"Using local SQLite file instead"
436 print(
"Configuration to use for the upload:")
437 for key
in metadata_dictionary:
438 if not(key)
in [
"username",
"password",
"destinationTag"]:
439 value_to_print = metadata_dictionary[key]
if metadata_dictionary[key] !=
None else defaults[key]
440 print(
"\t%s : %s" % (key, value_to_print))
442 if raw_input(
"\nDo you want to continue? [y/n] ") !=
"y":
445 if metadata_dictionary[
"server"] ==
None:
446 if metadata_dictionary[
"destinationDatabase"] ==
"oracle://cms_orcoff_prep/CMS_CONDITIONS":
447 metadata_dictionary[
"server"] = server_alias_to_url[
"prep"]
449 metadata_dictionary[
"server"] = server_alias_to_url[
"prod"]
451 return metadata_dictionary