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 expected as an input %s has not been found.'%dbFileName )
44 dbcon = sqlite3.connect( dbFileName )
45 dbcur = dbcon.cursor()
46 dbcur.execute(
'SELECT * FROM IOV')
47 rows = dbcur.fetchall()
52 logger.warning(
'The file expected as an input %s contains no data. The import will be skipped.'%dbFileName )
55 except Exception
as e:
56 logger.error(
'Check on input data failed: %s' %
str(e))
61 leafFolderName = datef.strftime(dateformatForFolder)
62 fileFolder = os.path.join( errorInImportFileFolder, leafFolderName)
63 if not os.path.exists(fileFolder):
64 os.makedirs(fileFolder)
66 dataDestFile = os.path.join( fileFolder, df)
67 if not os.path.exists(dataDestFile):
68 shutil.copy2(df, dataDestFile)
71 metadataDestFile = os.path.join( fileFolder, mf )
72 if not os.path.exists(metadataDestFile):
73 shutil.copy2(df, metadataDestFile)
74 logger.error(
"Upload failed. Data file and metadata saved in folder '%s'" %os.path.abspath(fileFolder))
78 destTag = args.destTag
79 comment = args.comment
81 datef = datetime.now()
84 if os.path.exists(
'%s.txt' %dbName ):
85 logger.debug(
'Removing already existing file %s' %dbName)
86 os.remove(
'%s.txt' %dbName )
90 uploadMd[
'destinationDatabase'] = destDb
93 tags[ destTag ] = tagInfo
94 uploadMd[
'destinationTags'] = tags
95 uploadMd[
'inputTag'] = destTag
96 uploadMd[
'since'] =
None 97 datelabel = datef.strftime(dateformatForLabel)
99 if not comment
is None:
101 uploadMd[
'userText'] =
'%s : %s' %(datelabel,commentStr)
102 with open(
'%s.txt' %dbName,
'wb')
as jf:
103 jf.write( json.dumps( uploadMd, sort_keys=
True, indent = 2 ) )
107 uploadCommand =
'uploadConditions.py %s' %dbName
110 pipe = subprocess.Popen( uploadCommand, shell=
True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
111 stdout = pipe.communicate()[0]
113 retCode = pipe.returncode
117 except Exception
as e:
123 dbFileName =
'%s.db' %dbName
125 destTag = args.destTag
126 comment = args.comment
128 datef = datetime.now()
129 destMap = {
"oracle://cms_orcoff_prep/cms_conditions":
"oradev",
"oracle://cms_orcon_prod/cms_conditions":
"onlineorapro" }
130 if destDb.lower()
in destMap.keys():
131 destDb = destMap[destDb.lower()]
133 if destDb.startswith(
'sqlite'):
134 destDb = destDb.split(
':')[1]
136 logger.error(
'Destination connection %s is not supported.' %destDb )
139 note =
'"Importing data with O2O execution"' 140 commandOptions =
'--force --yes --db %s copy %s %s --destdb %s --synchronize --note %s' %(dbFileName,destTag,destTag,destDb,note)
141 copyCommand =
'conddb %s' %commandOptions
142 logger.info(
'Executing command: %s' %copyCommand )
144 pipe = subprocess.Popen( copyCommand, shell=
True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
145 stdout = pipe.communicate()[0]
147 retCode = pipe.returncode
151 except Exception
as e:
153 logger.error(
str(e) )
158 dbName = datetime.utcnow().strftime(
'%Y-%m-%d_%H-%M-%S-%f')
159 dbFileName =
'%s.db' %dbName
161 if args.auth
is not None and not args.auth==
'':
162 if auth_path_key
in os.environ:
163 logger.warning(
"Cannot set authentication path to %s in the environment, since it is already set." %args.auth)
165 logger.info(
"Setting the authentication path to %s in the environment." %args.auth)
166 os.environ[auth_path_key]=args.auth
167 if os.path.exists(
'%s.db' %dbName ):
168 logger.info(
"Removing files with name %s" %dbName )
169 os.remove(
'%s.db' %dbName )
170 if os.path.exists(
'%s.txt' %dbName ):
171 os.remove(
'%s.txt' %dbName )
172 command =
'cmsRun %s ' %args.job_file
173 command +=
' targetFile=%s' %dbFileName
174 command +=
' destinationDatabase=%s' %args.destDb
175 command +=
' destinationTag=%s' %args.destTag
177 pipe = subprocess.Popen( command, shell=
True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
178 stdout = pipe.communicate()[0]
179 retCode = pipe.returncode
181 logger.info(
'PopCon Analyzer return code is: %s' %retCode )
183 logger.error(
'O2O job failed. Skipping upload.' )
189 ret =
copy( args, dbName )
191 ret =
upload( args, dbName )
192 os.remove(
'%s.db' %dbName )
def saveFileForImportErrors(datef, dbName, withMetadata=False)
S & print(S &os, JobReport::InputFile const &f)