1 from __future__
import print_function
10 from datetime
import datetime
12 errorInImportFileFolder =
'import_errors'
13 dateformatForFolder =
"%Y-%m-%d-%H-%M-%S"
14 dateformatForLabel =
"%Y-%m-%d %H:%M:%S"
16 auth_path_key =
'COND_AUTH_PATH'
18 messageLevelEnvVar =
'POPCON_LOG_LEVEL'
19 fmt_str =
"[%(asctime)s] %(levelname)s: %(message)s"
20 logLevel = logging.INFO
21 if messageLevelEnvVar
in os.environ:
22 levStr = os.environ[messageLevelEnvVar]
24 logLevel = logging.DEBUG
25 logFormatter = logging.Formatter(fmt_str)
26 logger = logging.getLogger()
27 logger.setLevel(logLevel)
28 consoleHandler = logging.StreamHandler(sys.stdout)
29 consoleHandler.setFormatter(logFormatter)
30 logger.addHandler(consoleHandler)
33 dbFileName =
'%s.db' %dbName
38 if not os.path.exists( dbFileName ):
39 logger.error(
'The file generated by PopCon with the data to be imported %s has not been found.'%dbFileName )
44 dbcon = sqlite3.connect( dbFileName )
45 dbcur = dbcon.cursor()
46 dbcur.execute(
"SELECT name FROM sqlite_master WHERE type='table' AND name='IOV'");
47 if dbcur.fetchone()
is None:
48 logger.error(
'The condition database with the data to be imported has not been found in the file generated by PopCon: %s'%dbFileName )
50 dbcur.execute(
'SELECT * FROM IOV')
51 rows = dbcur.fetchall()
56 logger.warning(
'The data set generated by PopCon contains no data to be imported. The import will be skipped.')
59 except Exception
as e:
60 logger.error(
'Check on input data failed: %s' %
str(e))
65 leafFolderName = datef.strftime(dateformatForFolder)
66 fileFolder = os.path.join( errorInImportFileFolder, leafFolderName)
67 if not os.path.exists(fileFolder):
68 os.makedirs(fileFolder)
70 dataDestFile = os.path.join( fileFolder, df)
71 if not os.path.exists(dataDestFile):
72 shutil.copy2(df, dataDestFile)
75 metadataDestFile = os.path.join( fileFolder, mf )
76 if not os.path.exists(metadataDestFile):
77 shutil.copy2(df, metadataDestFile)
78 logger.error(
"Upload failed. Data file and metadata saved in folder '%s'" %os.path.abspath(fileFolder))
82 destTag = args.destTag
83 comment = args.comment
85 datef = datetime.now()
88 if os.path.exists(
'%s.txt' %dbName ):
89 logger.debug(
'Removing already existing file %s' %dbName)
90 os.remove(
'%s.txt' %dbName )
94 uploadMd[
'destinationDatabase'] = destDb
97 tags[ destTag ] = tagInfo
98 uploadMd[
'destinationTags'] = tags
99 uploadMd[
'inputTag'] = destTag
100 uploadMd[
'since'] =
None
101 datelabel = datef.strftime(dateformatForLabel)
103 if not comment
is None:
105 uploadMd[
'userText'] =
'%s : %s' %(datelabel,commentStr)
106 with open(
'%s.txt' %dbName,
'wb')
as jf:
107 jf.write( json.dumps( uploadMd, sort_keys=
True, indent = 2 ) )
111 uploadCommand =
'uploadConditions.py %s' %dbName
114 pipe = subprocess.Popen( uploadCommand, shell=
True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
115 stdout = pipe.communicate()[0]
117 retCode = pipe.returncode
121 except Exception
as e:
127 dbFileName =
'%s.db' %dbName
129 destTag = args.destTag
130 comment = args.comment
132 datef = datetime.now()
133 destMap = {
"oracle://cms_orcoff_prep/cms_conditions":
"oradev",
"oracle://cms_orcon_prod/cms_conditions":
"onlineorapro" }
134 if destDb.lower()
in destMap.keys():
135 destDb = destMap[destDb.lower()]
137 if destDb.startswith(
'sqlite'):
138 destDb = destDb.split(
':')[1]
140 logger.error(
'Destination connection %s is not supported.' %destDb )
143 note =
'"Importing data with O2O execution"'
144 commandOptions =
'--force --yes --db %s copy %s %s --destdb %s --synchronize --note %s' %(dbFileName,destTag,destTag,destDb,note)
145 copyCommand =
'conddb %s' %commandOptions
146 logger.info(
'Executing command: %s' %copyCommand )
148 pipe = subprocess.Popen( copyCommand, shell=
True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
149 stdout = pipe.communicate()[0]
151 retCode = pipe.returncode
155 except Exception
as e:
157 logger.error(
str(e) )
162 dbName = datetime.utcnow().strftime(
'%Y-%m-%d_%H-%M-%S-%f')
163 dbFileName =
'%s.db' %dbName
165 if args.auth
is not None and not args.auth==
'':
166 if auth_path_key
in os.environ:
167 logger.warning(
"Cannot set authentication path to %s in the environment, since it is already set." %args.auth)
169 logger.info(
"Setting the authentication path to %s in the environment." %args.auth)
170 os.environ[auth_path_key]=args.auth
171 if os.path.exists(
'%s.db' %dbName ):
172 logger.info(
"Removing files with name %s" %dbName )
173 os.remove(
'%s.db' %dbName )
174 if os.path.exists(
'%s.txt' %dbName ):
175 os.remove(
'%s.txt' %dbName )
176 command =
'cmsRun %s ' %args.job_file
177 command +=
' targetFile=%s' %dbFileName
178 command +=
' destinationDatabase=%s' %args.destDb
179 command +=
' destinationTag=%s' %args.destTag
181 logger.info(
'Executing command: %s' %command )
182 pipe = subprocess.Popen( command, shell=
True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
183 stdout = pipe.communicate()[0]
184 retCode = pipe.returncode
186 logger.info(
'PopCon Analyzer return code is: %s' %retCode )
188 logger.error(
'O2O job failed. Skipping upload.' )
194 ret =
copy( args, dbName )
196 ret =
upload( args, dbName )
198 logger.info(
'Deleting local file %s.db' %dbName )
199 os.remove(
'%s.db' %dbName )