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