00001
00002 import os,string,sys,commands,time,ConfigParser,operator
00003
00004 from operator import itemgetter
00005 MAXRETRIES=10
00006
00007 CONFIGFILE='dbtoweb.cfg'
00008 CONFIG = ConfigParser.ConfigParser()
00009 print 'Reading configuration file from ',CONFIGFILE
00010 CONFIG.read(CONFIGFILE)
00011
00012
00013
00014 print " "
00015 print "dbtoconf.py:"
00016 ACCOUNT=CONFIG.get('Common','Account')
00017 CONNSTRINGGLOBTAG=CONFIG.get('Common','Conn_string_gtag')
00018 GLOBTAG=CONFIG.get('Common','Globtag')
00019 ROOTDIR=CONFIG.get('Common','Rootdir')
00020 HTMLNAME=CONFIG.get('Common','HTMLName')
00021
00022 AUTHPATH=''
00023 try:
00024 AUTHPATH=CONFIG.get('Common','AuthPath')
00025 except:
00026 print "WARNING: No authpath fount in config file"
00027
00028
00029 print
00030 print "Configuration:"
00031 print "================================"
00032 print "Account:",ACCOUNT
00033 print "CONNSTRING:",CONNSTRINGGLOBTAG
00034 print "Auth. Path:",AUTHPATH
00035 print "GLOBALTAG:",GLOBTAG
00036 print "Root dir:",ROOTDIR
00037
00038 print "================================"
00039
00040
00041 def myparser(input,parstag):
00042 out='-1'
00043
00044 if input.find(parstag)!=-1:
00045 first=input.split(parstag)
00046 second=first[1].split()
00047
00048 if (len(second)>=1):
00049 out=second[0]
00050
00051 return out
00052
00053
00054
00055 def single(currgtag):
00056
00057 root=''
00058 node=''
00059 globparent=''
00060 leafnode=[]
00061 parent=[]
00062 tag=[]
00063 pfn=[]
00064 object=[]
00065 record=[]
00066 connstring=[]
00067 account=[]
00068
00069 htmltag=open(currgtag+'.html','w')
00070 htmltag.write('<html>\n')
00071 htmltag.write('<body>\n')
00072 htmltag.write('<h3> Tag List for Global Tag: '+currgtag+' </h3>\n')
00073 htmltag.write('<h4> The first time you access a tag below you have to login (HN credentials)<br>')
00074 htmltag.write('Then you can come back here and access all tags </h4>\n')
00075
00076
00077 htmltag.write('<table border="1">\n')
00078 htmltag.write('<tr>\n')
00079 htmltag.write('<th>Tag</th>\n')
00080 htmltag.write('<th>Source</th>\n')
00081 htmltag.write('</tr>\n')
00082
00083
00084 TMPTAG=commands.getoutput('mktemp')
00085 os.system('cmscond_tagtree_list -c '+CONNSTRINGGLOBTAG+' -T '+currgtag+' > '+TMPTAG)
00086
00087 nlines=0
00088 tmp=open(TMPTAG,'r')
00089 while tmp:
00090 line=tmp.readline()
00091 if len(line)==0:
00092 break
00093 nlines=nlines+1
00094 line=string.strip(line)
00095 if line.find('leafnode')==-1:
00096 out=myparser(line,'root:')
00097 if out!='-1':
00098 root=out
00099 out=myparser(line,'node:')
00100 if out!='-1':
00101 node=out
00102 out=myparser(line,'parent:')
00103 if out!='-1':
00104 globparent=out
00105 else:
00106 leafnode.append(myparser(line,'leafnode:'))
00107 parent.append(myparser(line,'parent:'))
00108 tag.append(myparser(line,'tag:'))
00109 pfn.append(myparser(line,'pfn:'))
00110
00111
00112
00113 tmp.close()
00114 print 'Read '+str(nlines)+' lines...'
00115 print 'Read ',len(leafnode),' leafnodes'
00116 print 'Read ',len(parent),' parent'
00117 print 'Read ',len(tag),' tag'
00118 print 'Read ',len(pfn),' pfn'
00119
00120 if len(leafnode)!=len(parent) or len(leafnode)!=len(tag) or len(leafnode)!=len(pfn):
00121 print "# of leafnodes different from parent/tag/pfn"
00122 sys.exit()
00123
00124
00125
00126
00127 tagdict={}
00128 for i in range(0,len(leafnode)):
00129 tagdict[i]=pfn[i]
00130
00131 sortindex=sorted(tagdict.items(), key=itemgetter(1))
00132
00133
00134
00135
00136
00137 print 'Scanning tags:'
00138 for i in range(0,len(leafnode)):
00139 index=sortindex[i][0]
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 TEMP=pfn[index].split('/')
00158 ACCOUNT=TEMP[len(TEMP)-1]
00159 TEMP=ACCOUNT.split('_')
00160 DET=TEMP[len(TEMP)-1]
00161 HREFSTRING='https://cmsweb.cern.ch/conddb/IOVManagement/get_iovs?det='+DET+'&service=cms_orcoff_prod&schema='+ACCOUNT+'&viacache=CHECKED&sel_tag='+tag[index]+'&tags=Display+IOV+for+selected+tags&destTag=&firstSince=&lastTill='
00162
00163
00164
00165 htmltag.write('<tr>\n')
00166
00167 htmltag.write('<td><a href="'+HREFSTRING+'">'+tag[index]+'</a></td>\n')
00168 htmltag.write('<td>'+pfn[index]+'</td>\n')
00169 htmltag.write('</tr>\n')
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 os.system('rm -f '+TMPTAG)
00180 htmltag.write('</table>\n')
00181 htmltag.write('<h3> This list was created on: '+time.ctime()+'</h3>\n')
00182 htmltag.write('</body>\n</html>\n')
00183 htmltag.close()
00184 return 'done'
00185
00186
00187
00188
00189
00190 TOBECREATED=0
00191 try:
00192 if not os.path.exists(ROOTDIR):
00193 os.mkdir(ROOTDIR)
00194 os.chdir(ROOTDIR)
00195 TOBECREATED=1
00196 BASEDIR=os.getcwd()
00197 print 'ROOTDIR created, current dir= '+BASEDIR
00198 else:
00199 os.chdir(ROOTDIR)
00200 BASEDIR=os.getcwd()
00201 print 'ROOTDIR exists already, current dir= '+BASEDIR
00202 except:
00203 print "ERROR: it is impossible to chdir in",ROOTDIR
00204 sys.exit(1)
00205
00206 HTMLTMP=HTMLNAME+'.tmp'
00207 if TOBECREATED==1 :
00208 htmlroot=open(HTMLTMP,'w')
00209 else:
00210 os.system('head -n -4 '+HTMLNAME+' > '+HTMLTMP)
00211 htmlroot=open(HTMLTMP,'a')
00212
00213 if TOBECREATED==1:
00214
00215 htmlroot.write('<html>\n')
00216 htmlroot.write('<body>\n')
00217 htmlroot.write('<h3>Global Tag List:</h3>\n')
00218 htmlroot.write('<table border="1">\n')
00219 htmlroot.write('<tr>\n')
00220 htmlroot.write('<th>Global Tag</th>\n')
00221 htmlroot.write('<th>Source</th>\n')
00222 htmlroot.write('</tr>\n')
00223
00224
00225 treelist=[]
00226
00227 if GLOBTAG=="All" :
00228
00229 TMPTAGLIST=commands.getoutput('mktemp')
00230 os.system('cmscond_tagtree_list -c '+CONNSTRINGGLOBTAG+' > '+TMPTAGLIST)
00231 ntrees=0
00232 tmplist=open(TMPTAGLIST,'r')
00233 while tmplist:
00234 line=tmplist.readline()
00235 if len(line)==0:
00236 break
00237 line=string.strip(line)
00238 if line.find('tree:')!=1:
00239 out=myparser(line,'tree:')
00240 if out!='-1':
00241 treelist.append(out)
00242 ntrees=ntrees+1
00243 tmplist.close()
00244 os.system('rm -f '+TMPTAGLIST)
00245 else:
00246 treelist.append(GLOBTAG)
00247
00248 print "Found trees:"
00249 for tree in range(0,len(treelist)):
00250 print str(tree)+': Tree= '+treelist[tree]
00251
00252 htmlroot.write('<tr>\n')
00253 file=treelist[tree]+'/'+treelist[tree]+'.html'
00254 htmlroot.write('<td><a href="'+file+'">'+treelist[tree]+'</a></td>\n')
00255 htmlroot.write('<td>'+ACCOUNT+'</td>\n')
00256 htmlroot.write('</tr>\n')
00257
00258
00259 os.chdir(BASEDIR)
00260 TREEDIR=BASEDIR+'/'+treelist[tree]
00261 if not os.path.exists(TREEDIR):
00262 os.mkdir(TREEDIR)
00263 os.chdir(TREEDIR)
00264 print 'TREEDIR created, current dir is '+os.getcwd()
00265
00266 single(treelist[tree])
00267 os.chdir(BASEDIR)
00268
00269 htmlroot.write('</table>\n')
00270 htmlroot.write('<h3> This list was created on: '+time.ctime()+'</h3>\n')
00271 htmlroot.write('</body>\n</html>\n')
00272 htmlroot.close()
00273 print 'A new root html has been created: '
00274 print BASEDIR+'/'+HTMLTMP
00275 print 'Please check and replace (in case)'
00276