CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
dbtoweb.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import os,string,sys,commands,time,ConfigParser,operator
3 
4 from operator import itemgetter
5 MAXRETRIES=10 # number of retries before giving up
6 
7 CONFIGFILE='dbtoweb.cfg'
8 CONFIG = ConfigParser.ConfigParser()
9 print 'Reading configuration file from ',CONFIGFILE
10 CONFIG.read(CONFIGFILE)
11 
12 # this is for [COMMON] part of the myconf.conf
13 
14 print " "
15 print "dbtoconf.py:"
16 ACCOUNT=CONFIG.get('Common','Account')
17 CONNSTRINGGLOBTAG=CONFIG.get('Common','Conn_string_gtag')
18 GLOBTAG=CONFIG.get('Common','Globtag')
19 ROOTDIR=CONFIG.get('Common','Rootdir')
20 HTMLNAME=CONFIG.get('Common','HTMLName')
21 
22 AUTHPATH=''
23 try:
24  AUTHPATH=CONFIG.get('Common','AuthPath')
25 except:
26  print "WARNING: No authpath fount in config file"
27 
28 
29 print
30 print "Configuration:"
31 print "================================"
32 print "Account:",ACCOUNT
33 print "CONNSTRING:",CONNSTRINGGLOBTAG
34 print "Auth. Path:",AUTHPATH
35 print "GLOBALTAG:",GLOBTAG
36 print "Root dir:",ROOTDIR
37 
38 print "================================"
39 
40 
41 def myparser(input,parstag):
42  out='-1'
43 
44  if input.find(parstag)!=-1:
45  first=input.split(parstag)
46  second=first[1].split()
47 # print second
48  if (len(second)>=1):
49  out=second[0]
50 
51  return out
52 
53 
54 
55 def single(currgtag):
56 
57  root=''
58  node=''
59  globparent=''
60  leafnode=[]
61  parent=[]
62  tag=[]
63  pfn=[]
64  object=[]
65  record=[]
66  connstring=[]
67  account=[]
68 
69  htmltag=open(currgtag+'.html','w')
70  htmltag.write('<html>\n')
71  htmltag.write('<body>\n')
72  htmltag.write('<h3> Tag List for Global Tag: '+currgtag+' </h3>\n')
73  htmltag.write('<h4> The first time you access a tag below you have to login (HN credentials)<br>')
74  htmltag.write('Then you can come back here and access all tags </h4>\n')
75 
76 
77  htmltag.write('<table border="1">\n')
78  htmltag.write('<tr>\n')
79  htmltag.write('<th>Tag</th>\n')
80  htmltag.write('<th>Source</th>\n')
81  htmltag.write('</tr>\n')
82 
83 
84  TMPTAG=commands.getoutput('mktemp')
85  os.system('cmscond_tagtree_list -c '+CONNSTRINGGLOBTAG+' -T '+currgtag+' > '+TMPTAG)
86 # os.system('cat '+TMPTAG)
87  nlines=0
88  tmp=open(TMPTAG,'r')
89  while tmp:
90  line=tmp.readline()
91  if len(line)==0:
92  break
93  nlines=nlines+1
94  line=string.strip(line)
95  if line.find('leafnode')==-1:
96  out=myparser(line,'root:')
97  if out!='-1':
98  root=out
99  out=myparser(line,'node:')
100  if out!='-1':
101  node=out
102  out=myparser(line,'parent:')
103  if out!='-1':
104  globparent=out
105  else:
106  leafnode.append(myparser(line,'leafnode:'))
107  parent.append(myparser(line,'parent:'))
108  tag.append(myparser(line,'tag:'))
109  pfn.append(myparser(line,'pfn:'))
110 
111  # print nlines,line
112 
113  tmp.close()
114  print 'Read '+str(nlines)+' lines...'
115  print 'Read ',len(leafnode),' leafnodes'
116  print 'Read ',len(parent),' parent'
117  print 'Read ',len(tag),' tag'
118  print 'Read ',len(pfn),' pfn'
119 
120  if len(leafnode)!=len(parent) or len(leafnode)!=len(tag) or len(leafnode)!=len(pfn):
121  print "# of leafnodes different from parent/tag/pfn"
122  sys.exit()
123 
124  #output
125  #print root,node,globparent
126  #`create dictionary
127  tagdict={}
128  for i in range(0,len(leafnode)):
129  tagdict[i]=pfn[i]
130 
131  sortindex=sorted(tagdict.items(), key=itemgetter(1))
132 
133  # print tagdict
134  # for i in range(0,len(leafnode)):
135  # print sortindex[i][0]
136 
137  print 'Scanning tags:'
138  for i in range(0,len(leafnode)):
139  index=sortindex[i][0]
140  #lm
141  # command='cmscond_list_iov -c '+pfn[i].replace('frontier:','frontier://cmsfrontier:8000')+' -t '+tag[i]
142  #
143  # for ntime in range(0,MAXRETRIES):
144  # fullout=commands.getoutput(command)
145  # if fullout.count(tag[i])>=1:
146  # # success
147  # break
148  # print "Try: ",ntime, ".....",tag[i]
149  # time.sleep(0.5)
150  # if fullout.count(tag[i])<1:
151  # print "Unable to get information on tag:",tag[i]," after ", MAXRETRIES, "retries"
152  # print "Giving up here..."
153  # sys.exit(1)
154 
155  # adding tag to tag list page with link to condweb page
156  # first define needed parameters
157  TEMP=pfn[index].split('/')
158  ACCOUNT=TEMP[len(TEMP)-1]
159  TEMP=ACCOUNT.split('_')
160  DET=TEMP[len(TEMP)-1]
161  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='
162 # print 'href= '+HREFSTRING
163 
164 
165  htmltag.write('<tr>\n')
166 # htmltag.write('<td><a href="'+tag[i]+'">'+tag[i]+'</a></td>\n')
167  htmltag.write('<td><a href="'+HREFSTRING+'">'+tag[index]+'</a></td>\n')
168  htmltag.write('<td>'+pfn[index]+'</td>\n')
169  htmltag.write('</tr>\n')
170  # open output file
171  #lm
172  #taginfo=open(tag[i],'w')
173  #taginfo.write(pfn[i]+'\n')
174  #taginfo.write('========================================================\n')
175  #taginfo.write(fullout)
176  #taginfo.close()
177 #
178 # print '%-50s - %-30s' % (pfn[i],tag[i])
179  os.system('rm -f '+TMPTAG)
180  htmltag.write('</table>\n')
181  htmltag.write('<h3> This list was created on: '+time.ctime()+'</h3>\n')
182  htmltag.write('</body>\n</html>\n')
183  htmltag.close()
184  return 'done'
185 
186 # main start here
187 ######################################
188 # initialization
189 # first change to root dir
190 TOBECREATED=0
191 try:
192  if not os.path.exists(ROOTDIR):
193  os.mkdir(ROOTDIR)
194  os.chdir(ROOTDIR)
195  TOBECREATED=1
196  BASEDIR=os.getcwd()
197  print 'ROOTDIR created, current dir= '+BASEDIR
198  else:
199  os.chdir(ROOTDIR)
200  BASEDIR=os.getcwd()
201  print 'ROOTDIR exists already, current dir= '+BASEDIR
202 except:
203  print "ERROR: it is impossible to chdir in",ROOTDIR
204  sys.exit(1)
205 
206 HTMLTMP=HTMLNAME+'.tmp'
207 if TOBECREATED==1 :
208  htmlroot=open(HTMLTMP,'w')
209 else:
210  os.system('head -n -4 '+HTMLNAME+' > '+HTMLTMP)
211  htmlroot=open(HTMLTMP,'a')
212 
213 if TOBECREATED==1:
214  # create root html
215  htmlroot.write('<html>\n')
216  htmlroot.write('<body>\n')
217  htmlroot.write('<h3>Global Tag List:</h3>\n')
218  htmlroot.write('<table border="1">\n')
219  htmlroot.write('<tr>\n')
220  htmlroot.write('<th>Global Tag</th>\n')
221  htmlroot.write('<th>Source</th>\n')
222  htmlroot.write('</tr>\n')
223 
224 
225 treelist=[]
226 
227 if GLOBTAG=="All" :
228  # need to create a list of trees
229  TMPTAGLIST=commands.getoutput('mktemp')
230  os.system('cmscond_tagtree_list -c '+CONNSTRINGGLOBTAG+' > '+TMPTAGLIST)
231  ntrees=0
232  tmplist=open(TMPTAGLIST,'r')
233  while tmplist:
234  line=tmplist.readline()
235  if len(line)==0:
236  break
237  line=string.strip(line)
238  if line.find('tree:')!=1:
239  out=myparser(line,'tree:')
240  if out!='-1':
241  treelist.append(out)
242  ntrees=ntrees+1
243  tmplist.close()
244  os.system('rm -f '+TMPTAGLIST)
245 else:
246  treelist.append(GLOBTAG)
247 
248 print "Found trees:"
249 for tree in range(0,len(treelist)):
250  print str(tree)+': Tree= '+treelist[tree]
251  # adding global tag to main page
252  htmlroot.write('<tr>\n')
253  file=treelist[tree]+'/'+treelist[tree]+'.html'
254  htmlroot.write('<td><a href="'+file+'">'+treelist[tree]+'</a></td>\n')
255  htmlroot.write('<td>'+ACCOUNT+'</td>\n')
256  htmlroot.write('</tr>\n')
257 
258  # start creating files
259  os.chdir(BASEDIR)
260  TREEDIR=BASEDIR+'/'+treelist[tree]
261  if not os.path.exists(TREEDIR):
262  os.mkdir(TREEDIR)
263  os.chdir(TREEDIR)
264  print 'TREEDIR created, current dir is '+os.getcwd()
265 
266  single(treelist[tree])
267  os.chdir(BASEDIR)
268 
269 htmlroot.write('</table>\n')
270 htmlroot.write('<h3> This list was created on: '+time.ctime()+'</h3>\n')
271 htmlroot.write('</body>\n</html>\n')
272 htmlroot.close()
273 print 'A new root html has been created: '
274 print BASEDIR+'/'+HTMLTMP
275 print 'Please check and replace (in case)'
276 # os.system('cp '+HTMLTMP+' '+HTMLNAME+' ; rm -f '+HTMLTMP)
def myparser
Definition: dbtoweb.py:41
def single
Definition: dbtoweb.py:55
double split
Definition: MVATrainer.cc:139