CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
electronDbsDiscovery.py
Go to the documentation of this file.
1 
2 #===================================================================
3 # This python module is querying https://cmsweb.cern.ch/dbs_discovery/
4 # so to get the list of input files. One must call :
5 # search(), to get the list of primary files
6 # search2(), to get the list of eventual secondary files
7 #
8 # The selection of files is configured thanks to shell
9 # environment variables:
10 #
11 # DBS_RELEASE, for example CMSSW_2_2_0_pre1
12 # DBS_SAMPLE, for example RelValSingleElectronPt35
13 # DBS_RUN, for example Any
14 # DBS_COND , for example MC_31X_V2-v1
15 # DBS_TIER , for example RECO
16 # DBS_TIER_SECONDARY, for eventual secondary files
17 #
18 # DBS_STRATEGY:
19 # search: use dbs search
20 # castor: use rfdir
21 # lsf: use dbs lsf
22 # local: look within electronDbsDiscovery.txt
23 # DBS_CASTOR_DIR: top dir to scrutiny when the strategy is "castor"
24 # for relvals: '/store/relval/${DBS_RELEASE}/${DBS_SAMPLE}/${DBS_TIER}/${DBS_COND}/'
25 # for harvested dqm: '/store/unmerged/dqm/${DBS_SAMPLE}-${DBS_RELEASE}-${DBS_COND}-DQM-DQMHarvest-OfflineDQM'
26 #
27 # In DBS_COND, DBS_TIER and DBS_TIER_SECONDARY,
28 # one can use wildcard *.
29 #===================================================================
30 
31 #import httplib, urllib, urllib2, types, string, os, sys
32 import os, sys
33 
34 if not os.environ.has_key('DBS_STRATEGY'):
35  os.environ['DBS_STRATEGY'] = "search"
36 if not os.environ.has_key('DBS_RELEASE'):
37  os.environ['DBS_RELEASE'] = "Any"
38 if not os.environ.has_key('DBS_SAMPLE'):
39  os.environ['DBS_SAMPLE'] = "Any"
40 if not os.environ.has_key('DBS_RUN'):
41  os.environ['DBS_RUN'] = "Any"
42 if not os.environ.has_key('DBS_TIER_SECONDARY'):
43  os.environ['DBS_TIER_SECONDARY'] = ""
44 
45 def common_search(dbs_tier):
46 
47  if os.environ['DBS_STRATEGY'] == "local":
48 
49  result = []
50  for line in open('electronDbsDiscovery.txt').readlines():
51  line = os.path.expandvars(line.strip())
52  if line == "": continue
53  if os.environ['DBS_SAMPLE'] != "Any" and line.find(os.environ['DBS_SAMPLE'])== -1: continue
54  if line.find(os.environ['DBS_COND'])== -1: continue
55  if line.find(dbs_tier)== -1: continue
56  result.append('file:'+line)
57 
58  elif os.environ['DBS_STRATEGY'] == "castor":
59 
60  castor_dir = os.environ['DBS_CASTOR_DIR']
61  result = []
62  data = os.popen('rfdir /castor/cern.ch/cms'+castor_dir)
63  subdirs = data.readlines()
64  data.close()
65  datalines = []
66  for line in subdirs:
67  line = line.rstrip()
68  subdir = line.split()[8]
69  data = os.popen('rfdir /castor/cern.ch/cms'+castor_dir+'/'+subdir)
70  datalines = data.readlines()
71  for line in datalines:
72  line = line.rstrip()
73  file = line.split()[8]
74  if file != "":
75  result.append(castor_dir+'/'+subdir+'/'+file)
76  data.close()
77 
78  elif os.environ['DBS_STRATEGY'] == "lsf":
79 
80  dbs_path = '/'+os.environ['DBS_SAMPLE']+'/'+os.environ['DBS_RELEASE']+'-'+os.environ['DBS_COND']+'/'+os.environ['DBS_TIER']+'"'
81  if __name__ == "__main__":
82  print 'dbs path:',dbs_path
83  data = os.popen('dbs lsf --path="'+dbs_path+'"')
84  datalines = data.readlines()
85  data.close()
86  result = []
87  for line in datalines:
88  line = line.rstrip()
89  if line != "" and line[0] =="/":
90  result.append(line)
91 
92  else:
93 
94  input = "find file"
95  separator = " where "
96  if os.environ['DBS_RELEASE'] != "Any":
97  input = input + separator + "release = " + os.environ['DBS_RELEASE']
98  separator = " and "
99  if os.environ['DBS_SAMPLE'] != "Any":
100  input = input + separator + "primds = " + os.environ['DBS_SAMPLE']
101  separator = " and "
102  if os.environ['DBS_RUN'] != "Any":
103  input = input + separator + "run = " + os.environ['DBS_RUN']
104  separator = " and "
105  input = input + separator + "dataset like *" + os.environ['DBS_COND'] + "*" + dbs_tier + "*"
106 
107  #url = "https://cmsweb.cern.ch:443/dbs_discovery/aSearch"
108  #final_input = urllib.quote(input) ;
109  #
110  #agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)"
111  #ctypes = "text/plain"
112  #headers = { 'User-Agent':agent, 'Accept':ctypes}
113  #params = {'dbsInst':'cms_dbs_prod_global',
114  # 'html':0,'caseSensitive':'on','_idx':0,'pagerStep':-1,
115  # 'userInput':final_input,
116  # 'xml':0,'details':0,'cff':0,'method':'dbsapi'}
117  #data = urllib.urlencode(params,doseq=True)
118  #req = urllib2.Request(url, data, headers)
119  #data = ""
120  #
121  #try:
122  # response = urllib2.urlopen(req)
123  # data = response.read()
124  #except urllib2.HTTPError, e:
125  # if e.code==201:
126  # print e.headers
127  # print e.msg
128  # pass
129  # else:
130  # raise e
131 
132  data = os.popen('dbs search --url="http://cmsdbsprod.cern.ch/cms_dbs_prod_global/servlet/DBSServlet" --query "'+input+'"')
133  datalines = data.readlines()
134  data.close()
135  result = []
136  for line in datalines:
137  line = line.rstrip()
138  if line != "" and line[0] =="/":
139  result.append(line)
140 
141  return result
142 
143 def search():
144  return common_search(os.environ['DBS_TIER'])
145 
146 def search2():
147  return common_search(os.environ['DBS_TIER_SECONDARY'])
148 
149 
150 
151