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']
84 data = das_client.json.loads(das_client.get_data(
'https://cmsweb.cern.ch',query,0,0,0))
86 if data[
'nresults']==0:
87 print '[electronDataDiscovery.py] No DAS dataset for query:', query
89 while data[
'nresults']>1:
90 if data[
'data'][0][
'dataset'][0][
'name']==data[
'data'][1][
'dataset'][0][
'name']:
94 print '[electronDataDiscovery.py] Several DAS datasets for query:', query
95 for i
in range(data[
'nresults']):
96 print '[electronDataDiscovery.py] dataset['+str(i)+
']: '+data[
'data'][i][
'dataset'][0][
'name']
99 dataset = data[
'data'][0][
'dataset'][0][
'name']
101 query =
"file instance=cms_dbs_prod_global dataset="+dataset
112 data = das_client.json.loads(das_client.get_data(
'https://cmsweb.cern.ch',query,0,0,0))
114 if data[
'nresults']==0:
115 print '[electronDataDiscovery.py] No DAS file in dataset:', dataset
119 for i
in range(0,data[
'nresults']):
120 result.append(str(data[
'data'][i][
'file'][0][
'name']))
122 elif os.environ[
'DD_SOURCE'] ==
"dbs":
125 separator =
" where "
126 if os.environ[
'DD_RELEASE'] !=
"":
127 input = input + separator +
"release = " + os.environ[
'DD_RELEASE']
129 if os.environ[
'DD_SAMPLE'] !=
"":
130 input = input + separator +
"primds = " + os.environ[
'DD_SAMPLE']
132 if os.environ[
'DD_RUN'] !=
"":
133 input = input + separator +
"run = " + os.environ[
'DD_RUN']
135 input = input + separator +
"dataset like *" + os.environ[
'DD_COND'] +
"*" + dd_tier +
"*"
137 data = os.popen(
'dbs search --url="http://cmsdbsprod.cern.ch/cms_dbs_prod_global/servlet/DBSServlet" --query "'+input+
'"')
138 datalines = data.readlines()
141 for line
in datalines:
143 if line !=
"" and line[0] ==
"/":
146 elif os.environ[
'DD_SOURCE'] ==
"http":
149 separator =
" where "
150 if os.environ[
'DD_RELEASE'] !=
"":
151 input = input + separator +
"release = " + os.environ[
'DD_RELEASE']
153 if os.environ[
'DD_SAMPLE'] !=
"":
154 input = input + separator +
"primds = " + os.environ[
'DD_SAMPLE']
156 if os.environ[
'DD_RUN'] !=
"":
157 input = input + separator +
"run = " + os.environ[
'DD_RUN']
159 input = input + separator +
"dataset like *" + os.environ[
'DD_COND'] +
"*" + dd_tier +
"*"
161 url =
"https://cmsweb.cern.ch:443/dbs_discovery/aSearch"
162 final_input = urllib.quote(input) ;
164 agent =
"Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)"
165 ctypes =
"text/plain"
166 headers = {
'User-Agent':agent,
'Accept':ctypes}
167 params = {
'dbsInst':
'cms_dbs_prod_global',
168 'html':0,
'caseSensitive':
'on',
'_idx':0,
'pagerStep':-1,
169 'userInput':final_input,
170 'xml':0,
'details':0,
'cff':0,
'method':
'dbsapi'}
171 data = urllib.urlencode(params,doseq=
True)
172 req = urllib2.Request(url, data, headers)
176 response = urllib2.urlopen(req)
177 data = response.read()
178 except urllib2.HTTPError, e:
186 datalines = data.readlines()
189 for line
in datalines:
191 if line !=
"" and line[0] ==
"/":
194 elif os.environ[
'DD_SOURCE'] ==
"lsf":
196 dbs_path =
'/'+os.environ[
'DD_SAMPLE']+
'/'+os.environ[
'DD_RELEASE']+
'-'+os.environ[
'DD_COND']+
'/'+os.environ[
'DD_TIER']+
'"'
197 if __name__ ==
"__main__":
198 print 'dbs path:',dbs_path
199 data = os.popen(
'dbs lsf --path="'+dbs_path+
'"')
200 datalines = data.readlines()
203 for line
in datalines:
205 if line !=
"" and line[0] ==
"/":
208 elif os.environ[
'DD_SOURCE'].startswith(
'/castor/cern.ch/cms/'):
210 castor_dir = os.environ[
'DD_SOURCE'].
replace(
'/castor/cern.ch/cms/',
'/',1)
212 data = os.popen(
'rfdir /castor/cern.ch/cms'+castor_dir)
213 subdirs = data.readlines()
218 subdir = line.split()[8]
219 data = os.popen(
'rfdir /castor/cern.ch/cms'+castor_dir+
'/'+subdir)
220 datalines = data.readlines()
221 for line
in datalines:
223 file = line.split()[8]
225 result.append(castor_dir+
'/'+subdir+
'/'+file)
228 elif os.environ[
'DD_SOURCE'].startswith(
'/eos/cms/'):
230 data = os.popen(
'/afs/cern.ch/project/eos/installation/pro/bin/eos.select find -f '+os.environ[
'DD_SOURCE'])
231 lines = data.readlines()
235 line = line.strip().
replace(
'/eos/cms/',
'/',1)
236 if line ==
"":
continue
237 if dd_sample_re.search(line) ==
None:
continue
238 if dd_cond_re.search(line) ==
None:
continue
239 if dd_tier_re.search(line) ==
None:
continue
240 if dd_run_re.search(line) ==
None:
continue
246 for line
in open(os.environ[
'DD_SOURCE']).readlines():
247 line = os.path.expandvars(line.strip())
248 if line ==
"":
continue
249 if dd_sample_re.search(line) ==
None:
continue
250 if dd_cond_re.search(line) ==
None:
continue
251 if dd_tier_re.search(line) ==
None:
continue
252 if dd_run_re.search(line) ==
None:
continue
256 diag =
'[electronDataDiscovery.py] No more files after filtering with :'
257 if os.environ[
'DD_SAMPLE']!=
'': diag +=
' ' + os.environ[
'DD_SAMPLE']
258 if os.environ[
'DD_COND']!=
'': diag +=
' ' + os.environ[
'DD_COND']
259 if dd_tier!=
'': diag +=
' ' + dd_tier
260 if os.environ[
'DD_RUN']!=
'': diag +=
' ' + os.environ[
'DD_RUN']