7 from datetime
import datetime
9 confFileName =
'popcon2dropbox.json'
10 fileNameForDropBox =
'input_for_dropbox'
11 dbFileForDropBox =
'%s.db' %fileNameForDropBox
12 dbLogFile =
'%s_log.db' %fileNameForDropBox
13 errorInUploadFileFolder =
'upload_errors'
14 dateformatForFolder =
"%y-%m-%d-%H-%M-%S"
15 dateformatForLabel =
"%y-%m-%d %H:%M:%S"
20 class CondMetaData(object):
21 def __init__( self, fileName ):
23 self.datef = datetime.now()
24 with open(fileName) as jf:
26 self.md = json.load(jf)
27 except ValueError as e:
28 errorMessage = 'CondMetaData.__init__: Problem in decoding JSON file. Original error: ' + e.message
29 raise ValueError(errorMessage)
33 if self.md.has_key('authenticationPath'):
34 apath = self.md.get('authenticationPath')
39 if self.md.has_key('authenticationSys'):
40 asys = self.md.get('authenticationSystem')
43 def destinationDatabase( self ):
44 return self.md.get('destinationDatabase')
46 def logDbFileName( self ):
47 return self.md.get('logDbFileName')
50 return self.md.get('records')
52 def dumpMetadataForUpload( self, inputtag, desttag, comment ):
54 uploadMd['destinationDatabase'] = self.destinationDatabase()
57 tags[ desttag ] = tagInfo
58 uploadMd['destinationTags'] = tags
59 uploadMd['inputTag'] = inputtag
60 uploadMd['since'] = None
61 datelabel = self.datef.strftime(dateformatForLabel)
62 uploadMd['userText'] = '%s : %s' %(datelabel,comment)
63 with open( '%s.txt' %fileNameForDropBox, 'wb') as jf:
64 jf.write( json.dumps( uploadMd, sort_keys=True, indent = 2 ) )
66 def dumpMetadataForUpload( destDb, destTag, comment ):
68 uploadMd['destinationDatabase'] = destDb
71 tags[ destTag ] = tagInfo
72 uploadMd['destinationTags'] = tags
73 uploadMd['inputTag'] = destTag
74 uploadMd['since'] = None
75 datef = datetime.now()
76 #datelabel = datef.strftime(dateformatForLabel)
77 uploadMd['userText'] = '%s : %s' %(datelabel,comment)
78 with open( '%s.txt' %fileNameForDropBox, 'wb') as jf:
79 jf.write( json.dumps( uploadMd, sort_keys=True, indent = 2 ) )
83 def upload( destDb, destTag, comment, authPath ):
85 datef = datetime.now()
88 if not os.path.exists( dbFileForDropBox ):
89 print 'The input sqlite file has not been produced.'
94 dbcon = sqlite3.connect( dbFileForDropBox )
95 dbcur = dbcon.cursor()
96 dbcur.execute(
'SELECT * FROM IOV')
97 rows = dbcur.fetchall()
102 print 'The input sqlite file produced contains no data. The upload will be skipped.'
104 except Exception
as e:
105 print 'Check on input data failed: %s' %str(e)
109 if os.path.exists(
'%s.txt' %fileNameForDropBox ):
110 os.remove(
'%s.txt' %fileNameForDropBox )
115 uploadMd[
'destinationDatabase'] = destDb
118 tags[ destTag ] = tagInfo
119 uploadMd[
'destinationTags'] = tags
120 uploadMd[
'inputTag'] = destTag
121 uploadMd[
'since'] =
None
122 datelabel = datef.strftime(dateformatForLabel)
124 if not comment
is None:
126 uploadMd[
'userText'] =
'%s : %s' %(datelabel,commentStr)
127 with open(
'%s.txt' %fileNameForDropBox,
'wb')
as jf:
128 jf.write( json.dumps( uploadMd, sort_keys=
True, indent = 2 ) )
132 uploadCommand =
'uploadConditions.py %s' %fileNameForDropBox
133 if not authPath
is None:
134 uploadCommand +=
' -a %s' %authPath
136 pipe = subprocess.Popen( uploadCommand, shell=
True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
137 stdout = pipe.communicate()[0]
139 retCode = pipe.returncode
142 leafFolderName = datef.strftime(dateformatForFolder)
143 fileFolder = os.path.join( errorInUploadFileFolder, leafFolderName)
144 if not os.path.exists(fileFolder):
145 os.makedirs(fileFolder)
146 df=
'%s.db' %fileNameForDropBox
147 mf=
'%s.txt' %fileNameForDropBox
148 dataDestFile = os.path.join( fileFolder, df)
149 if not os.path.exists(dataDestFile):
150 shutil.copy2(df, dataDestFile)
151 shutil.copy2(mf,os.path.join(fileFolder,mf))
152 print "Upload failed. Data file and metadata saved in folder '%s'" %os.path.abspath(fileFolder)
154 except Exception
as e:
160 if os.path.exists(
'%s.db' %fileNameForDropBox ):
161 print "Removing files with name %s" %fileNameForDropBox
162 os.remove(
'%s.db' %fileNameForDropBox )
163 if os.path.exists(
'%s.txt' %fileNameForDropBox ):
164 os.remove(
'%s.txt' %fileNameForDropBox )
165 command =
'cmsRun %s ' %args.job_file
166 command +=
' destinationDatabase=%s' %args.destDb
167 command +=
' destinationTag=%s' %args.destTag
169 pipe = subprocess.Popen( command, shell=
True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
170 stdout = pipe.communicate()[0]
171 retCode = pipe.returncode
173 print 'Return code is: %s' %retCode
175 print 'O2O job failed. Skipping upload.'
177 return upload( args.destDb, args.destTag, args.comment, args.auth )