33 import os, sys, re, das_client
35 if not os.environ.has_key(
'DD_SOURCE'):
36 os.environ[
'DD_SOURCE'] =
'das'
37 if not os.environ.has_key(
'DD_RELEASE'):
38 os.environ[
'DD_RELEASE'] =
''
39 if not os.environ.has_key(
'DD_SAMPLE'):
40 os.environ[
'DD_SAMPLE'] =
''
41 if not os.environ.has_key(
'DD_COND'):
42 os.environ[
'DD_COND'] =
''
43 if not os.environ.has_key(
'DD_TIER'):
44 os.environ[
'DD_TIER'] =
''
45 if not os.environ.has_key(
'DD_TIER_SECONDARY'):
46 os.environ[
'DD_TIER_SECONDARY'] =
''
47 if not os.environ.has_key(
'DD_RUN'):
48 os.environ[
'DD_RUN'] =
''
50 dd_release_re = re.compile(os.environ[
'DD_RELEASE'].
replace(
'*',
'.*')) ;
51 dd_sample_re = re.compile(os.environ[
'DD_SAMPLE'].
replace(
'*',
'.*')) ;
52 dd_cond_re = re.compile(os.environ[
'DD_COND'].
replace(
'*',
'.*')) ;
53 dd_run_re = re.compile(os.environ[
'DD_RUN'].
replace(
'*',
'.*')) ;
57 dd_tier_re = re.compile(dd_tier.replace(
'*',
'.*')) ;
59 if os.environ[
'DD_SOURCE'] ==
"das":
61 query =
"dataset instance=cms_dbs_prod_global"
62 if os.environ[
'DD_RELEASE'] !=
"" :
63 query = query +
" release=" + os.environ[
'DD_RELEASE']
64 if os.environ[
'DD_SAMPLE'] !=
"":
65 query = query +
" primary_dataset=" + os.environ[
'DD_SAMPLE']
67 query = query +
" tier=" + dd_tier
68 if os.environ[
'DD_COND'] !=
"":
69 query = query +
" dataset=*" + os.environ[
'DD_COND'] +
"*"
70 if os.environ[
'DD_RUN'] !=
"":
71 query = query +
" run=" + os.environ[
'DD_RUN']
83 data = das_client.json.loads(das_client.get_data(
'https://cmsweb.cern.ch',query,0,0,0))
85 if data[
'nresults']==0:
86 print '[electronDataDiscovery.py] No DAS dataset for query:', query
88 if data[
'nresults']>1:
89 print '[electronDataDiscovery.py] Several DAS datasets for query:', query
92 dataset = data[
'data'][0][
'dataset'][0][
'name']
94 query =
"file instance=cms_dbs_prod_global dataset="+dataset
105 data = das_client.json.loads(das_client.get_data(
'https://cmsweb.cern.ch',query,0,0,0))
107 if data[
'nresults']==0:
108 print '[electronDataDiscovery.py] No DAS file in dataset:', dataset
112 for i
in range(0,data[
'nresults']):
113 result.append(str(data[
'data'][i][
'file'][0][
'name']))
115 elif os.environ[
'DD_SOURCE'] ==
"dbs":
118 separator =
" where "
119 if os.environ[
'DD_RELEASE'] !=
"":
120 input = input + separator +
"release = " + os.environ[
'DD_RELEASE']
122 if os.environ[
'DD_SAMPLE'] !=
"":
123 input = input + separator +
"primds = " + os.environ[
'DD_SAMPLE']
125 if os.environ[
'DD_RUN'] !=
"":
126 input = input + separator +
"run = " + os.environ[
'DD_RUN']
128 input = input + separator +
"dataset like *" + os.environ[
'DD_COND'] +
"*" + dd_tier +
"*"
130 data = os.popen(
'dbs search --url="http://cmsdbsprod.cern.ch/cms_dbs_prod_global/servlet/DBSServlet" --query "'+input+
'"')
131 datalines = data.readlines()
134 for line
in datalines:
136 if line !=
"" and line[0] ==
"/":
139 elif os.environ[
'DD_SOURCE'] ==
"http":
142 separator =
" where "
143 if os.environ[
'DD_RELEASE'] !=
"":
144 input = input + separator +
"release = " + os.environ[
'DD_RELEASE']
146 if os.environ[
'DD_SAMPLE'] !=
"":
147 input = input + separator +
"primds = " + os.environ[
'DD_SAMPLE']
149 if os.environ[
'DD_RUN'] !=
"":
150 input = input + separator +
"run = " + os.environ[
'DD_RUN']
152 input = input + separator +
"dataset like *" + os.environ[
'DD_COND'] +
"*" + dd_tier +
"*"
154 url =
"https://cmsweb.cern.ch:443/dbs_discovery/aSearch"
155 final_input = urllib.quote(input) ;
157 agent =
"Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)"
158 ctypes =
"text/plain"
159 headers = {
'User-Agent':agent,
'Accept':ctypes}
160 params = {
'dbsInst':
'cms_dbs_prod_global',
161 'html':0,
'caseSensitive':
'on',
'_idx':0,
'pagerStep':-1,
162 'userInput':final_input,
163 'xml':0,
'details':0,
'cff':0,
'method':
'dbsapi'}
164 data = urllib.urlencode(params,doseq=
True)
165 req = urllib2.Request(url, data, headers)
169 response = urllib2.urlopen(req)
170 data = response.read()
171 except urllib2.HTTPError, e:
179 datalines = data.readlines()
182 for line
in datalines:
184 if line !=
"" and line[0] ==
"/":
187 elif os.environ[
'DD_SOURCE'] ==
"lsf":
189 dbs_path =
'/'+os.environ[
'DD_SAMPLE']+
'/'+os.environ[
'DD_RELEASE']+
'-'+os.environ[
'DD_COND']+
'/'+os.environ[
'DD_TIER']+
'"'
190 if __name__ ==
"__main__":
191 print 'dbs path:',dbs_path
192 data = os.popen(
'dbs lsf --path="'+dbs_path+
'"')
193 datalines = data.readlines()
196 for line
in datalines:
198 if line !=
"" and line[0] ==
"/":
201 elif os.environ[
'DD_SOURCE'].startswith(
'/castor/cern.ch/cms/'):
203 castor_dir = os.environ[
'DD_SOURCE'].
replace(
'/castor/cern.ch/cms/',
'/',1)
205 data = os.popen(
'rfdir /castor/cern.ch/cms'+castor_dir)
206 subdirs = data.readlines()
211 subdir = line.split()[8]
212 data = os.popen(
'rfdir /castor/cern.ch/cms'+castor_dir+
'/'+subdir)
213 datalines = data.readlines()
214 for line
in datalines:
216 file = line.split()[8]
218 result.append(castor_dir+
'/'+subdir+
'/'+file)
221 elif os.environ[
'DD_SOURCE'].startswith(
'/eos/cms/'):
223 data = os.popen(
'/afs/cern.ch/project/eos/installation/pro/bin/eos.select find -f '+os.environ[
'DD_SOURCE'])
224 lines = data.readlines()
228 line = line.strip().
replace(
'/eos/cms/',
'/',1)
229 if line ==
"":
continue
230 if dd_sample_re.search(line) ==
None:
continue
231 if dd_cond_re.search(line) ==
None:
continue
232 if dd_tier_re.search(line) ==
None:
continue
233 if dd_run_re.search(line) ==
None:
continue
239 for line
in open(os.environ[
'DD_SOURCE']).readlines():
240 line = os.path.expandvars(line.strip())
241 if line ==
"":
continue
242 if dd_sample_re.search(line) ==
None:
continue
243 if dd_cond_re.search(line) ==
None:
continue
244 if dd_tier_re.search(line) ==
None:
continue
245 if dd_run_re.search(line) ==
None:
continue
249 diag =
'[electronDataDiscovery.py] No more files after filtering with :'
250 if os.environ[
'DD_SAMPLE']!=
'': diag +=
' ' + os.environ[
'DD_SAMPLE']
251 if os.environ[
'DD_COND']!=
'': diag +=
' ' + os.environ[
'DD_COND']
252 if dd_tier!=
'': diag +=
' ' + dd_tier
253 if os.environ[
'DD_RUN']!=
'': diag +=
' ' + os.environ[
'DD_RUN']