ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/PKGTOOLS/cmsDownloadME.py
Revision: 1.1
Committed: Sat Jun 21 14:09:15 2008 UTC (16 years, 10 months ago) by dlange
Content type: text/x-python
Branch: MAIN
Log Message:
first version of scripts to setup and download ME generator files

File Contents

# User Rev Content
1 dlange 1.1 #!/usr/bin/env python
2    
3     #
4     # June 21, 2008
5     # dlange, LLNL
6     #
7     # Simple script to retrieve ME directory listings
8     #
9    
10     import md5
11     import os
12     from os.path import join, getsize
13     import sys
14     import optparse
15     import urllib2
16    
17     usage=\
18     """%prog <TYPE> [options].
19     Examples:
20     %prog -arch slc4_ia32_gcc345 -gen madgraph --cat QCD
21     """
22    
23     parser=optparse.OptionParser(usage)
24    
25     parser.add_option("-a","--arch",
26     help="The CMSSW architecture (default=slc4_ia32_gcc345)",
27     default="slc4_ia32_gcc345",
28     dest="arch");
29     parser.add_option("-g","--gen",
30     help="The generator to retrieve MEs for",
31     default="",
32     dest="gen");
33     parser.add_option("-c","--cat",
34     help="The category of MEs (eg, QCD)",
35     default="",
36     dest="cat");
37    
38    
39     (options,args) = parser.parse_args() # by default the arg is sys.argv[1:]
40    
41    
42     baseDirWeb='http://cmsrep.cern.ch/cmssw/ME/'
43     listFileEnding='list'
44     cmsswbase=os.environ.get('CMS_PATH')
45     #hack for cern software area
46     if cmsswbase='/afs/cern.ch/cms':
47     cmsswbase='/afs/cern.ch/cms/sw
48    
49     if cmsswbase is None:
50     print 'Missing CMSSW_BASE variable to define SW area'
51     sys.exit(1)
52    
53     req=urllib2.Request(baseDirWeb)
54     try: website=urllib2.urlopen(req)
55     except IOError, e:
56     print 'Can not talk to cmsrep web server. Network problem?'
57     sys.exit()
58    
59     dlist=website.read()
60    
61     archList={}
62     genList={}
63     catList={}
64    
65     for line in dlist.split('\n'):
66     if ( line.find(listFileEnding)>-1):
67     href=line.split('<a')[1].split('</a>')[0].split('">')[1]
68     archList[href]=href.split('-')[0]
69     genList[href]=href.split('-')[1]
70     catList[href]=href.split('-')[2].split('.')[0]
71    
72     if options.gen == '':
73     print 'no generator (--gen) specified. For this arch, choices are:'
74     tmpDict={}
75     for ent in archList:
76     if archList[ent] == options.arch:
77     gen=genList[ent]
78     if gen not in tmpDict:
79     tmpDict[gen]=1
80     print ' ' + gen
81     sys.exit(0)
82    
83     if options.cat == '':
84     print 'no category (--cat) specified. For this arch/generator, choices are:'
85     tmpDict={}
86     for ent in archList:
87     if archList[ent] == options.arch:
88     if genList[ent] == options.gen:
89     cat=catList[ent]
90     if cat not in tmpDict:
91     tmpDict[cat]=1
92     print ' ' + cat
93     sys.exit(0)
94    
95     fileExpected=options.arch+'-'+options.gen+'-'+options.cat+'.'+ listFileEnding
96    
97     if ( fileExpected not in archList):
98     print 'No MEs for arch='+options.arch+' generator='+options.gen+' category='+options.cat+' found'
99     print 'Remove --cat and/or --gen arguments to find available'
100     print 'options for this architecture'
101     sys.exit(0)
102    
103     # otherwise we have work to do.
104    
105    
106     req2=urllib2.Request(baseDirWeb+'/'+fileExpected)
107     try: listFile=urllib2.urlopen(req2)
108     except IOError, e:
109     print 'Can not talk to cmsrep web server. Network problem?'
110     sys.exit()
111    
112     listing=listFile.read()
113    
114     for line in listing.split('\n'):
115     if ( len(line.split(' '))==2):
116     file=line.split(' ')[0]
117     md5Server=line.split(' ')[1]
118     fileOut=cmsswbase+'/'+file[file.find(baseDirWeb)+len(baseDirWeb):]
119     print 'Considering: '+fileOut
120    
121     needToGet=0
122     if ( os.path.exists(fileOut)):
123     mysum=md5.md5(open(fileOut).read()).hexdigest()
124     if ( mysum == md5Server):
125     print ' File already downloaded'
126     else:
127     print ' Refetching file (changed on server)....'
128     needToGet=1
129     else:
130     print ' Fetching file (new)....'
131     needToGet=1
132     dir=os.path.dirname(fileOut)
133     if not os.path.exists(dir):
134     os.makedirs(dir)
135     if not os.path.exists(dir):
136     print 'Could not create directory to download file'
137     print dir
138     print 'Permissions ok?'
139     sys.exit(1)
140    
141     # do we need to fetch the file
142     if ( needToGet==1):
143    
144     req3=urllib2.Request(file)
145     try: listFile=urllib2.urlopen(req3)
146     except IOError, e:
147     print 'Can not talk to cmsrep web server. Network problem?'
148     sys.exit()
149    
150     fout=open(fileOut,'w')
151     fout.write(listFile.read())
152     fout.close()
153     print ' done.'
154     else:
155     if ( line!=''):
156     print 'Unknown line.. skipping'
157     print line