CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/DQMOffline/EGamma/python/electronDbsDiscovery.py

Go to the documentation of this file.
00001 
00002 #===================================================================
00003 # This python module is querying https://cmsweb.cern.ch/dbs_discovery/
00004 # so to get the list of input files. One must call :
00005 #   search(), to get the list of primary files
00006 #   search2(), to get the list of eventual secondary files
00007 # 
00008 # The selection of files is configured thanks to shell
00009 # environment variables: 
00010 # 
00011 #   DBS_RELEASE, for example CMSSW_2_2_0_pre1
00012 #   DBS_SAMPLE, for example RelValSingleElectronPt35
00013 #   DBS_RUN, for example Any
00014 #   DBS_COND , for example MC_31X_V2-v1
00015 #   DBS_TIER , for example RECO
00016 #   DBS_TIER_SECONDARY, for eventual secondary files
00017 #   
00018 #   DBS_STRATEGY:
00019 #     search: use dbs search
00020 #     castor: use rfdir
00021 #     lsf: use dbs lsf
00022 #     local: look within electronDbsDiscovery.txt
00023 #
00024 # In DBS_COND, DBS_TIER and DBS_TIER_SECONDARY,
00025 # one can use wildcard *.
00026 #===================================================================
00027 
00028 #import httpslib, urllib, urllib2, types, string, os, sys
00029 import os, sys
00030 
00031 if not os.environ.has_key('DBS_RELEASE'):
00032   os.environ['DBS_RELEASE'] = "Any"
00033 if not os.environ.has_key('DBS_SAMPLE'):
00034   os.environ['DBS_SAMPLE'] = "Any"
00035 if not os.environ.has_key('DBS_RUN'):
00036   os.environ['DBS_RUN'] = "Any"
00037 if not os.environ.has_key('DBS_TIER_SECONDARY'):
00038   os.environ['DBS_TIER_SECONDARY'] = ""
00039 if not os.environ.has_key('DBS_STRATEGY'):
00040   os.environ['DBS_STRATEGY'] = "search"
00041 
00042 def common_search(dbs_tier):
00043 
00044   if os.environ['DBS_STRATEGY'] == "local":
00045   
00046     result = []
00047     for line in  open('electronDbsDiscovery.txt').readlines():
00048       line = os.path.expandvars(line.strip())
00049       if line == "": continue
00050       if os.environ['DBS_SAMPLE'] != "Any" and line.find(os.environ['DBS_SAMPLE'])== -1: continue
00051       if line.find(os.environ['DBS_COND'])== -1: continue
00052       if line.find(dbs_tier)== -1: continue
00053       result.append('file:'+line)
00054       
00055   elif os.environ['DBS_STRATEGY'] == "castor":
00056   
00057     castor_dir = '/castor/cern.ch/cms/store/relval/'+os.environ['DBS_RELEASE']+'/'+os.environ['DBS_SAMPLE']+'/'+os.environ['DBS_TIER']+'/'+os.environ['DBS_COND']+'/'
00058     if __name__ == "__main__":
00059       print 'castor dir:',castor_dir
00060     result = []
00061     data = os.popen('rfdir '+castor_dir)
00062     subdirs = data.readlines()
00063     data.close()
00064     datalines = []
00065     for line in subdirs:
00066       line = line.rstrip()
00067       subdir = line.split()[8]
00068       data = os.popen('rfdir '+castor_dir+'/'+subdir)
00069       datalines = data.readlines()
00070       for line in datalines:
00071         line = line.rstrip()
00072         file = line.split()[8]
00073         if file != "":
00074           result.append('/store/relval/'+os.environ['DBS_RELEASE']+'/'+os.environ['DBS_SAMPLE']+'/'+os.environ['DBS_TIER']+'/'+os.environ['DBS_COND']+'/'+subdir+'/'+file)
00075       data.close()
00076       
00077   elif os.environ['DBS_STRATEGY'] == "lsf":
00078   
00079     dbs_path = '/'+os.environ['DBS_SAMPLE']+'/'+os.environ['DBS_RELEASE']+'-'+os.environ['DBS_COND']+'/'+os.environ['DBS_TIER']+'"'
00080     if __name__ == "__main__":
00081       print 'dbs path:',dbs_path
00082     data = os.popen('dbs lsf --path="'+dbs_path+'"')
00083     datalines = data.readlines()
00084     data.close()
00085     result = []
00086     for line in datalines:
00087       line = line.rstrip()
00088       if line != "" and line[0] =="/":
00089         result.append(line)
00090       
00091   else:
00092   
00093     input = "find file"
00094     separator = " where "
00095     if os.environ['DBS_RELEASE'] != "Any":
00096       input = input + separator + "release = " + os.environ['DBS_RELEASE']
00097       separator = " and "
00098     if os.environ['DBS_SAMPLE'] != "Any":
00099       input = input + separator + "primds = " + os.environ['DBS_SAMPLE']
00100       separator = " and "
00101     if os.environ['DBS_RUN'] != "Any":
00102       input = input + separator + "run = " + os.environ['DBS_RUN']
00103       separator = " and "
00104     input = input + separator + "dataset like *" + os.environ['DBS_COND'] + "*" + dbs_tier + "*"
00105     
00106     #url = "https://cmsweb.cern.ch:443/dbs_discovery/aSearch"
00107     #final_input = urllib.quote(input) ;
00108     #
00109     #agent   = "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)"
00110     #ctypes  = "text/plain"
00111     #headers = { 'User-Agent':agent, 'Accept':ctypes}
00112     #params  = {'dbsInst':'cms_dbs_prod_global',
00113     #           'html':0,'caseSensitive':'on','_idx':0,'pagerStep':-1,
00114     #           'userInput':final_input,
00115     #           'xml':0,'details':0,'cff':0,'method':'dbsapi'}
00116     #data    = urllib.urlencode(params,doseq=True)
00117     #req     = urllib2.Request(url, data, headers)
00118     #data    = ""
00119     #
00120     #try:
00121     #  response = urllib2.urlopen(req)
00122     #  data = response.read()
00123     #except urllib2.HTTPError, e:
00124     #  if e.code==201:
00125     #    print e.headers       
00126     #    print e.msg
00127     #    pass
00128     #  else:
00129     #    raise e
00130 
00131     data = os.popen('dbs search --url="https://cmsdbsprod.cern.ch/cms_dbs_prod_global/servlet/DBSServlet" --query "'+input+'"')
00132     datalines = data.readlines()
00133     data.close()
00134     result = []
00135     for line in datalines:
00136       line = line.rstrip()
00137       if line != "" and line[0] =="/":
00138         result.append(line)
00139     
00140   return result
00141 
00142 def search():
00143   return common_search(os.environ['DBS_TIER'])
00144 
00145 def search2():
00146   return common_search(os.environ['DBS_TIER_SECONDARY'])
00147 
00148         
00149         
00150