CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/PKGTOOLS/cmsDownloadME.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 #
00004 # June 21, 2008
00005 # dlange, LLNL
00006 #
00007 # Simple script to retrieve ME directory listings
00008 #
00009 
00010 import md5
00011 import os
00012 from os.path import join, getsize
00013 import sys
00014 import optparse
00015 import urllib2
00016 
00017 usage=\
00018 """%prog <TYPE> [options].
00019 Examples:
00020 %prog -arch slc4_ia32_gcc345 -gen madgraph --cat QCD --energy 10TeV --version 4.2.11-cms1
00021 """
00022 
00023 parser=optparse.OptionParser(usage)
00024 
00025 parser.add_option("-a","--arch",
00026                   help="The CMSSW architecture (default=slc4_ia32_gcc345)",
00027                   default="slc4_ia32_gcc345",
00028                   dest="arch");
00029 parser.add_option("-g","--gen",
00030                   help="The generator to retrieve MEs for",
00031                   default="",
00032                   dest="gen");
00033 parser.add_option("-c","--cat",
00034                   help="The category of MEs (eg, QCD)",
00035                   default="",
00036                   dest="cat");
00037 parser.add_option("-e","--energy",
00038                   help="The energy of MEs (eg, 10TeV)",
00039                   default="",
00040                   dest="energy");
00041 parser.add_option("-v","--ver",
00042                   help="The version of MEs (eg, 4.2.11-cms1)",
00043                   default="",
00044                   dest="ver");
00045 
00046 
00047 (options,args) = parser.parse_args() # by default the arg is sys.argv[1:]
00048 
00049 
00050 baseDirWeb='http://cmsrep.cern.ch/cmssw/ME/'
00051 listFileEnding='list'
00052 cmsswbase=os.environ.get('CMS_PATH')
00053 #hack for cern software area
00054 if cmsswbase=='/afs/cern.ch/cms':
00055     cmsswbase='/afs/cern.ch/cms/sw'
00056 
00057 if cmsswbase is None:
00058     print 'Missing CMSSW_BASE variable to define SW area'
00059     sys.exit(1)
00060 
00061 req=urllib2.Request(baseDirWeb)
00062 try: website=urllib2.urlopen(req)
00063 except IOError, e:
00064     print 'Can not talk to cmsrep web server. Network problem?'
00065     sys.exit()    
00066 
00067 dlist=website.read()
00068 
00069 archList={}
00070 genList={}
00071 catList={}
00072 energyList={}
00073 versionList={}
00074 
00075 for line in dlist.split('\n'):
00076     if ( line.find(listFileEnding)>-1):
00077         href=line.split('<a')[1].split('</a>')[0].split('">')[1]
00078         archList[href]=href.split(':')[0]
00079         genList[href]=href.split(':')[1]
00080         energyList[href]=href.split(':')[2]
00081         catList[href]=href.split(':')[3]
00082         versionList[href]=(href.split(':')[4])[0:-5]
00083 
00084 if options.gen == '':
00085     print 'no generator (--gen) specified. For this arch, choices are:'
00086     tmpDict={}
00087     for ent in archList:
00088         if archList[ent] == options.arch:
00089             gen=genList[ent]
00090             if gen not in tmpDict:
00091                 tmpDict[gen]=1
00092                 print '    ' + gen
00093     sys.exit(0)            
00094 
00095 if options.energy == '':
00096     print 'no energy (--energy) specified. For this arch/generator, choices are:'
00097     tmpDict={}
00098     for ent in archList:
00099         if archList[ent] == options.arch:
00100             if genList[ent] == options.gen:
00101                 energy=energyList[ent]
00102                 if energy not in tmpDict:
00103                     tmpDict[energy]=1
00104                     print '    ' + energy
00105     sys.exit(0)            
00106 
00107 if options.cat == '':
00108     print 'no category (--cat) specified. For this arch/generator/energy, choices are:'
00109     tmpDict={}
00110     for ent in archList:
00111         if archList[ent] == options.arch:
00112             if genList[ent] == options.gen:
00113                 if energyList[ent] == options.energy:
00114                     cat=catList[ent]
00115                     if cat not in tmpDict:
00116                         tmpDict[cat]=1
00117                         print '    ' + cat
00118     sys.exit(0)            
00119 
00120 if options.ver == '':
00121     print 'no version (--ver) specified. For this arch/generator/category, choices are:'
00122     tmpDict={}
00123     for ent in archList:
00124         if archList[ent] == options.arch:
00125             if genList[ent] == options.gen:
00126                 if energyList[ent] == options.energy:
00127                     if catList[ent] == options.cat:
00128                         ver=versionList[ent]
00129                         if ver not in tmpDict:
00130                             tmpDict[ver]=1
00131                             print '    ' + ver
00132     sys.exit(0)            
00133 
00134 fileExpected=options.arch+':'+options.gen+':'+options.energy+':'+options.cat+':'+options.ver+'.'+ listFileEnding
00135 
00136 if ( fileExpected not in archList):
00137     print 'No MEs for arch='+options.arch+' generator='+options.gen+' energy='+options.energy+' category='+options.cat+' version='+options.ver+' found'
00138     print 'Remove --ver, --cat, --energy and/or --gen arguments to find available'
00139     print 'options for this architecture'
00140     sys.exit(0)
00141 
00142  # otherwise we have work to do.
00143 
00144  
00145 req2=urllib2.Request(baseDirWeb+'/'+fileExpected)
00146 try: listFile=urllib2.urlopen(req2)
00147 except IOError, e:
00148     print 'Can not talk to cmsrep web server. Network problem?'
00149     sys.exit()    
00150 
00151 listing=listFile.read()
00152 
00153 for line in listing.split('\n'):
00154     if ( len(line.split(' '))==2):
00155         file=line.split(' ')[0]
00156         md5Server=line.split(' ')[1]
00157         fileOut=cmsswbase+'/'+file[file.find(baseDirWeb)+len(baseDirWeb):]
00158         print 'Considering: '+fileOut
00159         
00160         needToGet=0
00161         if ( os.path.exists(fileOut)):
00162             mysum=md5.md5(open(fileOut).read()).hexdigest()
00163             if ( mysum == md5Server):
00164                 print '    File already downloaded'
00165             else:
00166                 print '    Refetching file (changed on server)....'
00167                 needToGet=1
00168         else:        
00169             print '    Fetching file (new)....'
00170             needToGet=1
00171             dir=os.path.dirname(fileOut)
00172             if not os.path.exists(dir):
00173                 os.makedirs(dir)
00174             if not os.path.exists(dir):
00175                 print 'Could not create directory to download file'
00176                 print dir
00177                 print 'Permissions ok?'
00178                 sys.exit(1)
00179 
00180 # do we need to fetch the file
00181         if ( needToGet==1):
00182         
00183             req3=urllib2.Request(file)
00184             try: listFile=urllib2.urlopen(req3)
00185             except IOError, e:
00186                 print 'Can not talk to cmsrep web server. Network problem?'
00187                 sys.exit()    
00188         
00189             fout=open(fileOut,'w')
00190             fout.write(listFile.read())
00191             fout.close()
00192             print '    done.'
00193     else:
00194         if ( line!=''):
00195             print 'Unknown line.. skipping'
00196             print line