236 parser = optparse.OptionParser(description=
"CMS Conditions Upload Script in CondDBFW.",
237 usage =
'Usage: %prog [options] <file>')
240 parser.add_option(
"-i",
"--inputTag", type=str,\
241 help=
"Tag to take IOVs + Payloads from in --sourceDB.")
242 parser.add_option(
"-t",
"--destinationTag", type=str,\
243 help=
"Tag to copy IOVs + Payloads to in --destDB.")
244 parser.add_option(
"-D",
"--destinationDatabase", type=str,\
245 help=
"Database to copy IOVs + Payloads to.")
246 parser.add_option(
"-s",
"--since", type=int,\
247 help=
"Since to take IOVs from.")
248 parser.add_option(
"-u",
"--userText", type=str,\
249 help=
"Description of --destTag (can be empty).")
252 parser.add_option(
"-m",
"--metadataFile", type=str, help=
"Metadata file to take metadata from.")
254 parser.add_option(
"-d",
"--debug", action=
"store_true", default=
False)
255 parser.add_option(
"-v",
"--verbose", action=
"store_true", default=
False)
256 parser.add_option(
"-T",
"--testing", action=
"store_true")
257 parser.add_option(
"--fcsr-filter", type=str, help=
"Synchronization to take FCSR from for local filtering of IOVs.")
259 parser.add_option(
"-n",
"--netrc", help =
'The netrc host (machine) from where the username and password will be read.')
261 parser.add_option(
"-a",
"--authPath", help =
'The path of the .netrc file for the authentication. Default: $HOME')
263 parser.add_option(
"-H",
"--hashToUse")
265 parser.add_option(
"-S",
"--server")
267 parser.add_option(
"-o",
"--review-options", action=
"store_true")
269 parser.add_option(
"-r",
"--replay-file")
271 (command_line_data, arguments) = parser.parse_args()
273 if len(arguments) < 1:
274 if command_line_data.hashToUse ==
None:
278 command_line_data.sourceDB = arguments[0]
280 if command_line_data.replay_file:
281 dictionary = json.loads(
"".
join(open(command_line_data.replay_file,
"r").readlines()))
282 command_line_data.tier0_response = dictionary["tier0_response"]
285 server_alias_to_url = {
286 "prep" :
"https://cms-conddb-dev.cern.ch/cmsDbCondUpload/",
287 "dev" :
"https://cms-conddb-dev.cern.ch/cmsDbCondUpload/",
288 "prod" :
"https://cms-conddb.cern.ch/cmsDbCondUpload/"
293 if command_line_data.server
in server_alias_to_url.keys():
294 command_line_data.server = server_alias_to_url[command_line_data.server]
297 database_alias_to_connection = {
298 "prep":
"oracle://cms_orcoff_prep/CMS_CONDITIONS",
299 "dev":
"oracle://cms_orcoff_prep/CMS_CONDITIONS",
300 "prod":
"oracle://cms_orcon_adg/CMS_CONDITIONS"
303 if command_line_data.destinationDatabase
in database_alias_to_connection.keys():
304 command_line_data.destinationDatabase = database_alias_to_connection[command_line_data.destinationDatabase]
309 netrc_file = command_line_data.netrc
310 auth_path = command_line_data.authPath
311 if not auth_path
is None:
312 if netrc_file
is None:
313 netrc_file = os.path.join(auth_path,
'.netrc')
315 netrc_file = os.path.join(auth_path, netrc_file)
317 netrc_authenticators = netrc.netrc(netrc_file).authenticators(
"ConditionUploader")
318 if netrc_authenticators ==
None:
319 print(
"Your netrc file must contain the key 'ConditionUploader'.")
320 manual_input = raw_input(
"Do you want to try to type your credentials? ")
321 if manual_input ==
"y":
323 username = raw_input(
"Username: ")
324 password = getpass.getpass(
"Password: ")
328 username = netrc_authenticators[0]
329 password = netrc_authenticators[2]
331 print(
"Couldn't obtain your credentials (either from netrc or manual input).")
334 command_line_data.username = username
335 command_line_data.password = password
338 command_line_data.destinationTags = {command_line_data.destinationTag:{}}
341 Construct metadata_dictionary:
342 Currently, this is 3 cases:
344 1) An IOV is being appended to an existing Tag with an existing Payload.
345 In this case, we just take all data from the command line.
347 2) No metadata file is given, so we assume that ALL upload metadata is coming from the command line.
349 3) A metadata file is given, hence we parse the file, and then iterate through command line arguments
350 since these override the options set in the metadata file.
355 if command_line_data.hashToUse !=
None:
356 command_line_data.userText =
""
357 metadata_dictionary = command_line_data.__dict__
358 elif command_line_data.metadataFile ==
None:
359 if command_line_data.sourceDB !=
None and (command_line_data.inputTag ==
None or command_line_data.destinationTag ==
None or command_line_data.destinationDatabase ==
None):
360 basepath = command_line_data.sourceDB.rsplit(
'.db', 1)[0].rsplit(
'.txt', 1)[0]
361 basename = os.path.basename(basepath)
362 dataFilename =
'%s.db' % basepath
363 metadataFilename =
'%s.txt' % basepath
367 with open(dataFilename,
'rb')
as dataFile:
370 errMsg =
'Impossible to open SQLite data file %s' %dataFilename
373 ret[
'error'] = errMsg
378 command_line_data.sourceDB = dataFilename
381 with open(metadataFilename,
'rb')
as metadataFile:
384 if e.errno != errno.ENOENT:
385 errMsg =
'Impossible to open file %s (for other reason than not existing)' %metadataFilename
388 ret[
'error'] = errMsg
391 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':
392 errMsg =
'Metadata file %s does not exist' %metadataFilename
395 ret[
'error'] = errMsg
398 runWizard(basename, dataFilename, metadataFilename)
399 command_line_data.metadataFile = metadataFilename
401 command_line_data.userText = command_line_data.userText\
402 if command_line_data.userText !=
None\
403 else str(raw_input(
"Tag's description [can be empty]:"))
404 metadata_dictionary = command_line_data.__dict__
406 if command_line_data.metadataFile !=
None:
407 metadata_dictionary = json.loads(
"".
join(open(os.path.abspath(command_line_data.metadataFile),
"r").readlines()))
408 metadata_dictionary["username"] = username
409 metadata_dictionary[
"password"] = password
410 metadata_dictionary[
"userText"] = metadata_dictionary.get(
"userText")\
411 if metadata_dictionary.get(
"userText") !=
None\
412 else str(raw_input(
"Tag's description [can be empty]:"))
415 for (option_name, option_value)
in command_line_data.__dict__.items():
417 if option_name !=
"destinationTags":
418 if option_value !=
None or (option_value ==
None and not(option_name
in metadata_dictionary.keys())):
422 metadata_dictionary[option_name] = option_value
424 if option_value != {
None:{}}:
425 metadata_dictionary[
"destinationTags"] = {option_value:{}}
426 elif option_value == {
None:{}}
and not(
"destinationTags" in metadata_dictionary.keys()):
427 metadata_dictionary[
"destinationTags"] = {
None:{}}
429 if command_line_data.review_options:
431 "since" :
"Since of first IOV",
432 "userText" :
"Populated by upload process",
433 "netrc" :
"None given",
434 "fcsr_filter" :
"Don't apply",
435 "hashToUse" :
"Using local SQLite file instead"
437 print(
"Configuration to use for the upload:")
438 for key
in metadata_dictionary:
439 if not(key)
in [
"username",
"password",
"destinationTag"]:
440 value_to_print = metadata_dictionary[key]
if metadata_dictionary[key] !=
None else defaults[key]
441 print(
"\t%s : %s" % (key, value_to_print))
443 if raw_input(
"\nDo you want to continue? [y/n] ") !=
"y":
446 if metadata_dictionary[
"server"] ==
None:
447 if metadata_dictionary[
"destinationDatabase"] ==
"oracle://cms_orcoff_prep/CMS_CONDITIONS":
448 metadata_dictionary[
"server"] = server_alias_to_url[
"prep"]
450 metadata_dictionary[
"server"] = server_alias_to_url[
"prod"]
452 return metadata_dictionary