00001
00002
00003
00004
00005
00006
00007
00008 """
00009 DBS data discovery command line interface
00010 """
00011
00012 import httplib, urllib, types, string, os, sys
00013 from optparse import OptionParser
00014
00015 class DDOptionParser:
00016 """
00017 DDOptionParser is main class to parse options for L{DDHelper} and L{DDServer}.
00018 """
00019 def __init__(self):
00020 self.parser = OptionParser()
00021 self.parser.add_option("--dbsInst",action="store", type="string", dest="dbsInst",
00022 help="specify DBS instance to use, e.g. --dbsInst=cms_dbs_prod_global")
00023 self.parser.add_option("-v","--verbose",action="store", type="int", default=0, dest="verbose",
00024 help="specify verbosity level, 0-none, 1-info, 2-debug")
00025 self.parser.add_option("--input",action="store", type="string", default=False, dest="input",
00026 help="specify input for your request.")
00027 self.parser.add_option("--xml",action="store_true",dest="xml",
00028 help="request output in XML format")
00029 self.parser.add_option("--cff",action="store_true",dest="cff",
00030 help="request output for files in CMS cff format")
00031 self.parser.add_option("--host",action="store",type="string",dest="host",
00032 help="specify a host name of Data Discovery service, e.g. https://cmsweb.cern.ch/dbs_discovery/")
00033 self.parser.add_option("--port",action="store",type="string",dest="port",
00034 help="specify a port to be used by Data Discovery host")
00035 self.parser.add_option("--iface",action="store",default="dd",type="string",dest="iface",
00036 help="specify which interface to use for queries dd or dbsapi, default is dbsapi.")
00037 self.parser.add_option("--details",action="store_true",dest="details",
00038 help="show detailed output")
00039 self.parser.add_option("--case",action="store",default="on",type="string",dest="case",
00040 help="specify if your input is case sensitive of not, default is on.")
00041 self.parser.add_option("--page",action="store",type="string",default="0",dest="page",
00042 help="specify output page, should come together with --limit and --details")
00043 self.parser.add_option("--limit",action="store",type="string",default="10",dest="limit",
00044 help="specify a limit on output, e.g. 50 results, the --limit=-1 will list all results")
00045 def getOpt(self):
00046 """
00047 Returns parse list of options
00048 """
00049 return self.parser.parse_args()
00050
00051 def sendMessage(host,port,dbsInst,userInput,page,limit,xml=0,case='on',iface='dd',details=0,cff=0,debug=0):
00052 """
00053 Send message to server, message should be an well formed XML document.
00054 """
00055 if xml: xml=1
00056 else: xml=0
00057 if cff: cff=1
00058 else: cff=0
00059 input=urllib.quote(userInput)
00060 if debug:
00061 httplib.HTTPConnection.debuglevel = 1
00062 print "Contact",host,port
00063 _port=443
00064 if host.find("http://")!=-1:
00065 _port=80
00066 if host.find("https://")!=-1:
00067 _port=443
00068 host=host.replace("http://","").replace("https://","")
00069 if host.find(":")==-1:
00070 port=_port
00071 prefix_path=""
00072 if host.find("/")!=-1:
00073 hs=host.split("/")
00074 host=hs[0]
00075 prefix_path='/'.join(hs[1:])
00076 if host.find(":")!=-1:
00077 host,port=host.split(":")
00078 port=int(port)
00079
00080 if port==443:
00081 http_conn = httplib.HTTPS(host,port)
00082 else:
00083 http_conn = httplib.HTTP(host,port)
00084 if details: details=1
00085 else: details=0
00086 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)
00087 if prefix_path:
00088 path="/"+prefix_path+path[1:]
00089 http_conn.putrequest('POST',path)
00090 http_conn.putheader('Host',host)
00091 http_conn.putheader('Content-Type','text/html; charset=utf-8')
00092 http_conn.putheader('Content-Length',str(len(input)))
00093 http_conn.endheaders()
00094 http_conn.send(input)
00095
00096 (status_code,msg,reply)=http_conn.getreply()
00097 data=http_conn.getfile().read()
00098 if debug or msg!="OK":
00099 print
00100 print http_conn.headers
00101 print "*** Send message ***"
00102 print input
00103 print "************************************************************************"
00104 print "status code:",status_code
00105 print "message:",msg
00106 print "************************************************************************"
00107 print reply
00108 return data
00109
00110
00111
00112
00113 if __name__ == "__main__":
00114 host= "cmsweb.cern.ch/dbs_discovery/"
00115 port= 443
00116 dbsInst="cms_dbs_prod_global"
00117 optManager = DDOptionParser()
00118 (opts,args) = optManager.getOpt()
00119 if opts.host: host=opts.host
00120 if host.find("http://")!=-1:
00121 host=host.replace("http://","")
00122
00123
00124 if host[-1]!="/":
00125 host+="/"
00126 if opts.port:
00127 port = opts.port
00128 if opts.dbsInst: dbsInst=opts.dbsInst
00129 if opts.input:
00130 if os.path.isfile(opts.input):
00131 input=open(opts.input,'r').readline()
00132 else:
00133 input=opts.input
00134 else:
00135 print "\nUsage: %s --help"%sys.argv[0]
00136 sys.exit(0)
00137 result = sendMessage(host,port,dbsInst,input,opts.page,opts.limit,opts.xml,opts.case,opts.iface,opts.details,opts.cff,opts.verbose)
00138 print result