CMS 3D CMS Logo

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 
00082 os.system('rm -f '+TMPTAG)
00083 os.system('cmscond_tagtree_list -c '+CONNSTRINGGLOBTAG+' -T '+GLOBTAG+' > '+TMPTAG)
00084 
00085 nlines=0
00086 tmp=open(TMPTAG,'r')
00087 while tmp:
00088     line=tmp.readline()
00089     if len(line)==0:
00090         break
00091     nlines=nlines+1
00092     line=string.strip(line)
00093     if line.find('leafnode')==-1:
00094         out=myparser(line,'root:')
00095         if out!='-1':
00096             root=out
00097         out=myparser(line,'node:')
00098         if out!='-1':
00099             node=out
00100         out=myparser(line,'parent:')
00101         if out!='-1':
00102             globparent=out
00103     else:
00104         leafnode.append(myparser(line,'leafnode:'))
00105         parent.append(myparser(line,'parent:'))
00106         tag.append(myparser(line,'tag:'))
00107         pfn.append(myparser(line,'pfn:'))
00108 
00109 #    print nlines,line
00110 
00111 tmp.close()
00112 print 'Read '+str(nlines)+' lines...'
00113 print 'Read ',len(leafnode),' leafnodes'
00114 print 'Read ',len(parent),' parent'
00115 print 'Read ',len(tag),' tag'
00116 print 'Read ',len(pfn),' pfn'
00117 
00118 if len(leafnode)!=len(parent) or len(leafnode)!=len(tag) or len(leafnode)!=len(pfn):
00119     print "# of leafnodes different from parent/tag/pfn"
00120     sys.exit()
00121 
00122 #output
00123 #print root,node,globparent
00124 
00125 
00126 for i in range(0,len(leafnode)):
00127     command='cmscond_taginventory_list -c '+CONNSTRINGGLOBTAG+' -t '+tag[i]
00128     #    print "COMMAND=",command
00129     # put in a loop until it succeed
00130     for ntime in range(0,MAXRETRIES):
00131         fullout=commands.getoutput(command)
00132     # 'cmscond_taginventory_list -c'+CONNSTRINGGLOBTAG+' -t '+tag[i])
00133         linesout=fullout.split('\n')
00134         #        print fullout
00135         # print len(linesout)
00136         if(len(linesout)>1):
00137             # success
00138             break
00139         print "Try: ",ntime, ".....",tag[i]
00140         time.sleep(0.5)
00141     if(len(linesout)<=1):
00142         print "Unable to get information on tag:",tag[i]," after ", MAXRETRIES, "retries"
00143         print "Giving up here..."
00144         sys.exit(1)
00145 
00146     for i2 in range(0,len(linesout)):
00147         if linesout[i2].split()[2]==pfn[i]:
00148                #same pfn and tag
00149            object.append(linesout[i2].split()[3])
00150            record.append(linesout[i2].split()[4])
00151            if CONNREP!='':
00152                connstring.append(CONNREP)
00153            else:
00154                connstring.append(pfn[i].split('/CMS_COND')[0])
00155            account.append('CMS_COND'+pfn[i].split('/CMS_COND')[1])
00156 
00157 #    print "Leafnode:",i,leafnode[i]
00158 #    print "Parent=",parent[i]
00159 #    print "Tag=",tag[i]
00160 #    print "Pfn=",pfn[i]
00161 #    print "Object=",object[i]
00162 #    print "Record=",record[i]
00163 #    print "Connstring=",connstring[i]
00164 #    print "Account=",account[i]
00165 #    print "=================================="
00166                                
00167 
00168 # open output conf file
00169 conf=open(CONFFILE,'w')
00170 conf.write('[COMMON]\n')
00171 conf.write('connect=sqlite_file:dbtoconf.db\n')
00172 conf.write('#connect=oracle://cms_orcoff_int2r/'+ACCOUNT+'\n')
00173 conf.write('#connect=oracle://cms_orcon_prod/'+ACCOUNT+'\n')
00174 conf.write('\n')
00175 conf.write('[TAGINVENTORY]\n')
00176 conf.write('tagdata=\n')
00177 for iline in range(0,len(leafnode)):
00178 #    print iline
00179     outline=' '+tag[iline]+'{pfn='+connstring[iline]+'/'+account[iline]+',objectname='+object[iline]+',recordname='+record[iline]+'}'
00180     if iline != len(leafnode)-1:
00181         outline=outline+';'
00182     outline=outline+'\n'
00183     conf.write(outline)
00184 
00185 conf.write("\n")
00186 if TAGREP=='':
00187     conf.write('[TAGTREE '+GLOBTAG+']\n')
00188 else:
00189     conf.write('[TAGTREE '+TAGREP+']\n')
00190 
00191 conf.write('root='+root+'\n')
00192 conf.write('nodedata='+node+'{parent='+globparent+'}\n')
00193 conf.write('leafdata=\n')
00194 for ileaf in range(0,len(leafnode)):
00195     outline=' '+leafnode[ileaf]+'{parent='+parent[ileaf]+',tagname='+tag[ileaf]+',pfn='+connstring[ileaf]+'/'+account[ileaf]+'}'
00196     if ileaf!=len(leafnode)-1:
00197         outline=outline+';'
00198     outline=outline+'\n'
00199     conf.write(outline)
00200 
00201 conf.close()
00202 
00203 print CONFFILE+' ready. Plase have a look'

Generated on Tue Jun 9 17:26:14 2009 for CMSSW by  doxygen 1.5.4