CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
dbs_discovery.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import httplib, urllib, urllib2, types, string, os, sys
4 
5 # return the list of files obtained from the data discovery and based upon environnement variables:
6 # DBS_RELEASE, for example CMSSW_2_2_0_pre1
7 # DBS_SAMPLE, for example RelValSingleElectronPt35
8 # DBS_LIKE , for example *RECO
9 
10 def search():
11 
12  if os.environ['DBS_LIKE'] == "LOCAL":
13  fileIn = open("Listfiles.txt","r")
14  line = fileIn.readline()
15  result = []
16  while line:
17  for fragment in line.split("\n"):
18  if fragment != "":
19  result.append(fragment)
20  line = fileIn.readline()
21  else:
22  url = "https://cmsweb.cern.ch:443/dbs_discovery/aSearch"
23  input = "find file where release = " + os.environ['DBS_RELEASE']
24  input = input + " and primds = " + os.environ['DBS_SAMPLE']
25  input = input + " and dataset like " + os.environ['DBS_LIKE']
26  final_input = urllib.quote(input) ;
27 
28  agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)"
29  ctypes = "text/plain"
30  headers = { 'User-Agent':agent, 'Accept':ctypes}
31  params = {'dbsInst':'cms_dbs_prod_global',
32  'html':0,'caseSensitive':'on','_idx':0,'pagerStep':-1,
33  'userInput':final_input,
34  'xml':0,'details':0,'cff':0,'method':'dbsapi'}
35  data = urllib.urlencode(params,doseq=True)
36  req = urllib2.Request(url, data, headers)
37  data = ""
38 
39  try:
40  response = urllib2.urlopen(req)
41  data = response.read()
42  except urllib2.HTTPError, e:
43  if e.code==201:
44  print e.headers
45  print e.msg
46  pass
47  else:
48  raise e
49 
50  result = []
51  for line in data.split("\n"):
52  if line != "" and line[0] =="/":
53  result.append(line)
54 
55  return result
56 
57 if __name__ == "__main__":
58  for file in search():
59  print file
60 
61 
62 
63