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