File indexing completed on 2024-11-26 02:34:12
0001
0002
0003
0004
0005
0006
0007 """
0008 DBS data discovery command line interface
0009 """
0010
0011 import httplib, urllib, urllib2, types, string, os, sys
0012 from optparse import OptionParser
0013
0014 class DDOptionParser:
0015 """
0016 DDOptionParser is main class to parse options for L{DDHelper} and L{DDServer}.
0017 """
0018 def __init__(self):
0019 self.parser = OptionParser()
0020 self.parser.add_option("--dbsInst",action="store", type="string", dest="dbsInst",
0021 help="specify DBS instance to use, e.g. --dbsInst=cms_dbs_prod_global")
0022 self.parser.add_option("-v","--verbose",action="store", type="int", default=0, dest="verbose",
0023 help="specify verbosity level, 0-none, 1-info, 2-debug")
0024 self.parser.add_option("--input",action="store", type="string", default=False, dest="input",
0025 help="specify input for your request.")
0026 self.parser.add_option("--xml",action="store_true",dest="xml",
0027 help="request output in XML format")
0028 self.parser.add_option("--cff",action="store_true",dest="cff",
0029 help="request output for files in CMS cff format")
0030 self.parser.add_option("--host",action="store",type="string",dest="host",
0031 help="specify a host name of Data Discovery service, e.g. https://cmsweb.cern.ch/dbs_discovery/")
0032 self.parser.add_option("--port",action="store",type="string",dest="port",
0033 help="specify a port to be used by Data Discovery host")
0034 self.parser.add_option("--iface",action="store",default="dbsapi",type="string",dest="iface",
0035 help="specify which interface to use for queries dd or dbsapi, default is dbsapi.")
0036 self.parser.add_option("--details",action="store_true",dest="details",
0037 help="show detailed output")
0038 self.parser.add_option("--case",action="store",default="on",type="string",dest="case",
0039 help="specify if your input is case sensitive of not, default is on.")
0040 self.parser.add_option("--page",action="store",type="string",default="0",dest="page",
0041 help="specify output page, should come together with --limit and --details")
0042 self.parser.add_option("--limit",action="store",type="string",default="10",dest="limit",
0043 help="specify a limit on output, e.g. 50 results, the --limit=-1 will list all results")
0044 def getOpt(self):
0045 """
0046 Returns parse list of options
0047 """
0048 return self.parser.parse_args()
0049
0050 def sendMessage(host,port,dbsInst,userInput,page,limit,xml=0,case='on',iface='dbsapi',details=0,cff=0,debug=0):
0051 if xml: xml=1
0052 else: xml=0
0053 if cff: cff=1
0054 else: cff=0
0055 input=urllib.quote(userInput)
0056 if debug:
0057 httplib.HTTPConnection.debuglevel = 1
0058 print("Contact",host,port)
0059 _port=443
0060 if host.find("http://")!=-1:
0061 _port=80
0062 if host.find("https://")!=-1:
0063 _port=443
0064 host=host.replace("http://","").replace("https://","")
0065 if host.find(":")==-1:
0066 port=_port
0067 prefix_path=""
0068 if host.find("/")!=-1:
0069 hs=host.split("/")
0070 host=hs[0]
0071 prefix_path='/'.join(hs[1:])
0072 if host.find(":")!=-1:
0073 host,port=host.split(":")
0074 port=int(port)
0075 path="/aSearch"
0076 if prefix_path:
0077 path="/"+prefix_path+path[1:]
0078 if port==443:
0079 url = "https://"+host+":%s"%port+path
0080 else:
0081 url = "http://"+host+":%s"%port+path
0082 if details: details=1
0083 else: details=0
0084 params = {'dbsInst':dbsInst,'html':0,'caseSensitive':case,'_idx':page,'pagerStep':limit,'userInput':input,'xml':xml,'details':details,'cff':cff,'method':iface}
0085 agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)"
0086 ctypes = "text/plain"
0087 headers = { 'User-Agent':agent, 'Accept':ctypes}
0088 data = urllib.urlencode(params,doseq=True)
0089 if debug:
0090 print(url,data,headers)
0091 req = urllib2.Request(url, data, headers)
0092 data = ""
0093 try:
0094 response = urllib2.urlopen(req)
0095 data = response.read()
0096 except urllib2.HTTPError as e:
0097 if e.code==201:
0098 print(e.headers)
0099 print(e.msg)
0100 pass
0101 else:
0102 raise e
0103 return data
0104
0105
0106
0107
0108 if __name__ == "__main__":
0109 host= "cmsweb.cern.ch/dbs_discovery/"
0110 port= 443
0111 dbsInst="cms_dbs_prod_global"
0112 optManager = DDOptionParser()
0113 (opts,args) = optManager.getOpt()
0114 if opts.host: host=opts.host
0115 if host.find("http://")!=-1:
0116 host=host.replace("http://","")
0117
0118
0119 if host[-1]!="/":
0120 host+="/"
0121 if opts.port:
0122 port = opts.port
0123 if opts.dbsInst: dbsInst=opts.dbsInst
0124 if opts.input:
0125 if os.path.isfile(opts.input):
0126 input=open(opts.input,'r').readline()
0127 else:
0128 input=opts.input
0129 else:
0130 print("\nUsage: %s --help"%sys.argv[0])
0131 sys.exit(0)
0132 result = sendMessage(host,port,dbsInst,input,opts.page,opts.limit,opts.xml,opts.case,opts.iface,opts.details,opts.cff,opts.verbose)
0133 print(result)