CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/CondCore/TagCollection/scripts/dbtoconf.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 import os,string,sys,commands,time,ConfigParser
00003 
00004 MAXRETRIES=10 # number of retries before giving up
00005 
00006 CONFIGFILE='dbtoconf.cfg'
00007 CONFIG = ConfigParser.ConfigParser()
00008 print 'Reading configuration file from ',CONFIGFILE
00009 CONFIG.read(CONFIGFILE)
00010 
00011 # this is for [COMMON] part of the myconf.conf
00012 
00013 print " "
00014 print "dbtoconf.py:"
00015 ACCOUNT=CONFIG.get('Common','Account')
00016 CONNSTRINGGLOBTAG=CONFIG.get('Common','Conn_string_gtag')
00017 GLOBTAG=CONFIG.get('Common','Globtag')
00018 CONFFILE=CONFIG.get('Common','Confoutput')
00019 AUTHPATH=''
00020 try:
00021     AUTHPATH=CONFIG.get('Common','AuthPath')
00022 except:
00023     print "WARNING: No authpath fount in config file"
00024 
00025 CONNREP=''
00026 try:
00027     CONNREP=CONFIG.get('Common','Conn_rep')
00028 except:
00029     print "WARNING: No CONN_REP fount in config file"
00030 
00031 TAGREP=''
00032 try:
00033     TAGREP=CONFIG.get('Common','Tag_rep')
00034 except:
00035     print "WARNING: No TAG_REP fount in config file"
00036     
00037 print
00038 print "Configuration:"
00039 print "================================"
00040 print "Account:",ACCOUNT
00041 print "CONNSTRING:",CONNSTRINGGLOBTAG
00042 print "GLOBALTAG:",GLOBTAG
00043 print "CONF OUTPUT:",CONFFILE
00044 print "Auth. Path:",AUTHPATH
00045 print "Conn. replacement:",CONNREP
00046 print "TAG  replacement:",TAGREP
00047 print "================================"
00048 
00049 # this is for tags
00050 TMPTAG='tmptag.list'
00051 
00052 
00053 def myparser(input,parstag):
00054     if input.find(parstag)!=-1:
00055         first=input.split(parstag)
00056         second=first[1].split()
00057         out=second[0]
00058     else:
00059         out='-1'
00060     return out
00061 
00062 # main start here
00063 ######################################
00064 # initialization
00065 
00066 
00067 
00068 
00069 
00070 root=''
00071 node=''
00072 globparent=''
00073 leafnode=[]
00074 parent=[]
00075 tag=[]
00076 pfn=[]
00077 object=[]
00078 record=[]
00079 connstring=[]
00080 account=[]
00081 label=[]
00082 
00083 os.system('rm -f '+TMPTAG)
00084 tagtree_cmd = 'cmscond_tagtree_list -c '+CONNSTRINGGLOBTAG+' -T '+GLOBTAG
00085 if AUTHPATH != '':
00086     tagtree_cmd += ' -P ' + AUTHPATH
00087 
00088 os.system(tagtree_cmd +' > '+TMPTAG)
00089 
00090 nlines=0
00091 tmp=open(TMPTAG,'r')
00092 while tmp:
00093     line=tmp.readline()
00094     if len(line)==0:
00095         break
00096     nlines=nlines+1
00097     line=string.strip(line)
00098     if line.find('leafnode')==-1:
00099         out=myparser(line,'root:')
00100         if out!='-1':
00101             root=out
00102         out=myparser(line,'node:')
00103         if out!='-1':
00104             node=out
00105         out=myparser(line,'parent:')
00106         if out!='-1':
00107             globparent=out
00108     else:
00109         leafnode.append(myparser(line,'leafnode:'))
00110         parent.append(myparser(line,'parent:'))
00111         tag.append(myparser(line,'tag:'))
00112         pfn.append(myparser(line,'pfn:'))
00113         record.append(myparser(line,'record:'))
00114         label.append(myparser(line,'label:'))
00115         object.append(myparser(line,'object:'))
00116         connstring.append(myparser(line,'pfn:').split('/CMS_COND')[0])
00117         account.append('CMS_COND'+myparser(line,'pfn:').split('/CMS_COND')[1])
00118 #    print nlines,line
00119 
00120 tmp.close()
00121 print 'Read '+str(nlines)+' lines...'
00122 print 'Read ',len(leafnode),' leafnodes'
00123 print 'Read ',len(parent),' parent'
00124 print 'Read ',len(tag),' tag'
00125 print 'Read ',len(pfn),' pfn'
00126 
00127 if len(leafnode)!=len(parent) or len(leafnode)!=len(tag) or len(leafnode)!=len(pfn):
00128     print "# of leafnodes different from parent/tag/pfn"
00129     sys.exit()
00130 
00131 #output
00132 #print root,node,globparent
00133 
00134 
00135 # for i in range(0,len(leafnode)):
00136 #     command='cmscond_taginventory_list -c '+CONNSTRINGGLOBTAG+' -t '+tag[i]
00137 #     if AUTHPATH != '':
00138 #         command += ' -P ' + AUTHPATH
00139 
00140 #     # print "COMMAND=",command
00141 #     # put in a loop until it succeed
00142 #     for ntime in range(0,MAXRETRIES):
00143 #         fullout=commands.getoutput(command)
00144 #     # 'cmscond_taginventory_list -c'+CONNSTRINGGLOBTAG+' -t '+tag[i])
00145 #         linesout=fullout.split('\n')
00146 #         #        print fullout
00147 #         # print len(linesout)
00148 #         if(len(linesout)>1):
00149 #             # success
00150 #             break
00151 #         print "Try: ",ntime, ".....",tag[i]
00152 #         time.sleep(0.5)
00153 #     if(len(linesout)<=1):
00154 #         print "Unable to get information on tag:",tag[i]," after ", MAXRETRIES, "retries"
00155 #         print "Giving up here..."
00156 #         sys.exit(1)
00157 
00158 #     # print tag[i]
00159 #     for i2 in range(0,len(linesout)):
00160 #         # print linesout[i2]
00161 #         if linesout[i2].split()[2]==pfn[i]:
00162 #                #same pfn and tag
00163 #            object.append(linesout[i2].split()[3])
00164 #            record.append(linesout[i2].split()[4])
00165 #            if CONNREP!='':
00166 #                connstring.append(CONNREP)
00167 #            else:
00168 #                connstring.append(pfn[i].split('/CMS_COND')[0])
00169 #            account.append('CMS_COND'+pfn[i].split('/CMS_COND')[1])
00170 #            #print "TAG: " + tag[i] + " LABEL: " + linesout[i2].split()[5]
00171 #            label.append(linesout[i2].split()[5])
00172 
00173 #    print "Leafnode:",i,leafnode[i]
00174 #    print "Parent=",parent[i]
00175 #    print "Tag=",tag[i]
00176 #    print "Pfn=",pfn[i]
00177 #    print "Object=",object[i]
00178 #    print "Record=",record[i]
00179 #    print "Connstring=",connstring[i]
00180 #    print "Account=",account[i]
00181 #    print "=================================="
00182                                
00183 
00184 # open output conf file
00185 conf=open(CONFFILE,'w')
00186 conf.write('[COMMON]\n')
00187 conf.write('connect=sqlite_file:' + GLOBTAG + '.db\n')
00188 conf.write('#connect=oracle://cms_orcoff_int2r/'+ACCOUNT+'\n')
00189 conf.write('#connect=oracle://cms_orcon_prod/'+ACCOUNT+'\n')
00190 conf.write('\n')
00191 conf.write('[TAGINVENTORY]\n')
00192 conf.write('tagdata=\n')
00193 for iline in range(0,len(leafnode)):
00194     #    print iline
00195     if label[iline] == 'None':
00196         outline=' '+tag[iline]+'{pfn='+connstring[iline]+'/'+account[iline]+',objectname='+object[iline]+',recordname='+record[iline]+'}'
00197     else:
00198         outline=' '+tag[iline]+'{pfn='+connstring[iline]+'/'+account[iline]+',objectname='+object[iline]+',recordname='+record[iline]+',labelname='+label[iline]+'}'
00199         
00200     if iline != len(leafnode)-1:
00201         outline=outline+';'
00202     outline=outline+'\n'
00203     conf.write(outline)
00204 
00205 conf.write("\n")
00206 if TAGREP=='':
00207     conf.write('[TAGTREE '+GLOBTAG+']\n')
00208 else:
00209     conf.write('[TAGTREE '+TAGREP+']\n')
00210 
00211 conf.write('root='+root+'\n')
00212 conf.write('nodedata='+node+'{parent='+globparent+'}\n')
00213 conf.write('leafdata=\n')
00214 for ileaf in range(0,len(leafnode)):
00215     outline=' '+leafnode[ileaf]+'{parent='+parent[ileaf]+',tagname='+tag[ileaf]+',pfn='+connstring[ileaf]+'/'+account[ileaf]+'}'
00216     if ileaf!=len(leafnode)-1:
00217         outline=outline+';'
00218     outline=outline+'\n'
00219     conf.write(outline)
00220 
00221 conf.close()
00222 
00223 print CONFFILE+' ready. Plase have a look'