9 from datetime
import datetime
11 errorInImportFileFolder =
'import_errors' 12 dateformatForFolder =
"%Y-%m-%d-%H-%M-%S" 13 dateformatForLabel =
"%Y-%m-%d %H:%M:%S" 15 auth_path_key =
'COND_AUTH_PATH' 17 messageLevelEnvVar =
'POPCON_LOG_LEVEL' 18 fmt_str =
"[%(asctime)s] %(levelname)s: %(message)s" 19 logLevel = logging.INFO
20 if messageLevelEnvVar
in os.environ:
21 levStr = os.environ[messageLevelEnvVar]
23 logLevel = logging.DEBUG
24 logFormatter = logging.Formatter(fmt_str)
25 logger = logging.getLogger()
26 logger.setLevel(logLevel)
27 consoleHandler = logging.StreamHandler(sys.stdout)
28 consoleHandler.setFormatter(logFormatter)
29 logger.addHandler(consoleHandler)
32 dbFileName =
'%s.db' %dbName
37 if not os.path.exists( dbFileName ):
38 logger.error(
'The file expected as an input %s has not been found.'%dbFileName )
43 dbcon = sqlite3.connect( dbFileName )
44 dbcur = dbcon.cursor()
45 dbcur.execute(
'SELECT * FROM IOV')
46 rows = dbcur.fetchall()
51 logger.warning(
'The file expected as an input %s contains no data. The import will be skipped.'%dbFileName )
54 except Exception
as e:
55 logger.error(
'Check on input data failed: %s' %
str(e))
60 leafFolderName = datef.strftime(dateformatForFolder)
61 fileFolder = os.path.join( errorInImportFileFolder, leafFolderName)
62 if not os.path.exists(fileFolder):
63 os.makedirs(fileFolder)
65 dataDestFile = os.path.join( fileFolder, df)
66 if not os.path.exists(dataDestFile):
67 shutil.copy2(df, dataDestFile)
70 metadataDestFile = os.path.join( fileFolder, mf )
71 if not os.path.exists(metadataDestFile):
72 shutil.copy2(df, metadataDestFile)
73 logger.error(
"Upload failed. Data file and metadata saved in folder '%s'" %os.path.abspath(fileFolder))
77 destTag = args.destTag
78 comment = args.comment
80 datef = datetime.now()
83 if os.path.exists(
'%s.txt' %dbName ):
84 logger.debug(
'Removing already existing file %s' %dbName)
85 os.remove(
'%s.txt' %dbName )
89 uploadMd[
'destinationDatabase'] = destDb
92 tags[ destTag ] = tagInfo
93 uploadMd[
'destinationTags'] = tags
94 uploadMd[
'inputTag'] = destTag
95 uploadMd[
'since'] =
None 96 datelabel = datef.strftime(dateformatForLabel)
98 if not comment
is None:
100 uploadMd[
'userText'] =
'%s : %s' %(datelabel,commentStr)
101 with open(
'%s.txt' %dbName,
'wb')
as jf:
102 jf.write( json.dumps( uploadMd, sort_keys=
True, indent = 2 ) )
106 uploadCommand =
'uploadConditions.py %s' %dbName
109 pipe = subprocess.Popen( uploadCommand, shell=
True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
110 stdout = pipe.communicate()[0]
112 retCode = pipe.returncode
116 except Exception
as e:
122 dbFileName =
'%s.db' %dbName
124 destTag = args.destTag
125 comment = args.comment
127 datef = datetime.now()
128 destMap = {
"oracle://cms_orcoff_prep/cms_conditions":
"oradev",
"oracle://cms_orcon_prod/cms_conditions":
"onlineorapro" }
129 if destDb.lower()
in destMap.keys():
130 destDb = destMap[destDb.lower()]
132 if destDb.startswith(
'sqlite'):
133 destDb = destDb.split(
':')[1]
135 logger.error(
'Destination connection %s is not supported.' %destDb )
138 note =
'"Importing data with O2O execution"' 139 commandOptions =
'--force --yes --db %s copy %s %s --destdb %s --synchronize --note %s' %(dbFileName,destTag,destTag,destDb,note)
140 copyCommand =
'conddb %s' %commandOptions
141 logger.info(
'Executing command: %s' %copyCommand )
143 pipe = subprocess.Popen( copyCommand, shell=
True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
144 stdout = pipe.communicate()[0]
146 retCode = pipe.returncode
150 except Exception
as e:
152 logger.error(
str(e) )
157 dbName = datetime.utcnow().strftime(
'%Y-%m-%d_%H-%M-%S-%f')
158 dbFileName =
'%s.db' %dbName
160 if args.auth
is not None and not args.auth==
'':
161 if auth_path_key
in os.environ:
162 logger.warning(
"Cannot set authentication path to %s in the environment, since it is already set." %args.auth)
164 logger.info(
"Setting the authentication path to %s in the environment." %args.auth)
165 os.environ[auth_path_key]=args.auth
166 if os.path.exists(
'%s.db' %dbName ):
167 logger.info(
"Removing files with name %s" %dbName )
168 os.remove(
'%s.db' %dbName )
169 if os.path.exists(
'%s.txt' %dbName ):
170 os.remove(
'%s.txt' %dbName )
171 command =
'cmsRun %s ' %args.job_file
172 command +=
' targetFile=%s' %dbFileName
173 command +=
' destinationDatabase=%s' %args.destDb
174 command +=
' destinationTag=%s' %args.destTag
176 pipe = subprocess.Popen( command, shell=
True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
177 stdout = pipe.communicate()[0]
178 retCode = pipe.returncode
180 logger.info(
'PopCon Analyzer return code is: %s' %retCode )
182 logger.error(
'O2O job failed. Skipping upload.' )
188 ret =
copy( args, dbName )
190 ret =
upload( args, dbName )
191 os.remove(
'%s.db' %dbName )
def saveFileForImportErrors(datef, dbName, withMetadata=False)