9 DBS data discovery command line interface 12 import httplib, urllib, types, string, os, sys
13 from optparse
import OptionParser
17 DDOptionParser is main class to parse options for L{DDHelper} and L{DDServer}. 21 self.parser.add_option(
"--dbsInst",action=
"store", type=
"string", dest=
"dbsInst",
22 help=
"specify DBS instance to use, e.g. --dbsInst=cms_dbs_prod_global")
23 self.parser.add_option(
"-v",
"--verbose",action=
"store", type=
"int", default=0, dest=
"verbose",
24 help=
"specify verbosity level, 0-none, 1-info, 2-debug")
25 self.parser.add_option(
"--input",action=
"store", type=
"string", default=
False, dest=
"input",
26 help=
"specify input for your request.")
27 self.parser.add_option(
"--xml",action=
"store_true",dest=
"xml",
28 help=
"request output in XML format")
29 self.parser.add_option(
"--cff",action=
"store_true",dest=
"cff",
30 help=
"request output for files in CMS cff format")
31 self.parser.add_option(
"--host",action=
"store",type=
"string",dest=
"host",
32 help=
"specify a host name of Data Discovery service, e.g. https://cmsweb.cern.ch/dbs_discovery/")
33 self.parser.add_option(
"--port",action=
"store",type=
"string",dest=
"port",
34 help=
"specify a port to be used by Data Discovery host")
35 self.parser.add_option(
"--iface",action=
"store",default=
"dd",type=
"string",dest=
"iface",
36 help=
"specify which interface to use for queries dd or dbsapi, default is dbsapi.")
37 self.parser.add_option(
"--details",action=
"store_true",dest=
"details",
38 help=
"show detailed output")
39 self.parser.add_option(
"--case",action=
"store",default=
"on",type=
"string",dest=
"case",
40 help=
"specify if your input is case sensitive of not, default is on.")
41 self.parser.add_option(
"--page",action=
"store",type=
"string",default=
"0",dest=
"page",
42 help=
"specify output page, should come together with --limit and --details")
43 self.parser.add_option(
"--limit",action=
"store",type=
"string",default=
"10",dest=
"limit",
44 help=
"specify a limit on output, e.g. 50 results, the --limit=-1 will list all results")
47 Returns parse list of options 49 return self.parser.parse_args()
51 def sendMessage(host,port,dbsInst,userInput,page,limit,xml=0,case='on',iface='dd',details=0,cff=0,debug=0):
53 Send message to server, message should be an well formed XML document. 59 input=urllib.quote(userInput)
61 httplib.HTTPConnection.debuglevel = 1
62 print "Contact",host,port
64 if host.find(
"http://")!=-1:
66 if host.find(
"https://")!=-1:
68 host=host.replace(
"http://",
"").
replace(
"https://",
"")
69 if host.find(
":")==-1:
72 if host.find(
"/")!=-1:
75 prefix_path=
'/'.
join(hs[1:])
76 if host.find(
":")!=-1:
77 host,port=host.split(
":")
81 http_conn = httplib.HTTPS(host,port)
83 http_conn = httplib.HTTP(host,port)
86 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)
88 path=
"/"+prefix_path+path[1:]
89 http_conn.putrequest(
'POST',path)
90 http_conn.putheader(
'Host',host)
91 http_conn.putheader(
'Content-Type',
'text/html; charset=utf-8')
92 http_conn.putheader(
'Content-Length',
str(len(input)))
93 http_conn.endheaders()
96 (status_code,msg,reply)=http_conn.getreply()
97 data=http_conn.getfile().read()
98 if debug
or msg!=
"OK":
100 print http_conn.headers
101 print "*** Send message ***" 103 print "************************************************************************" 104 print "status code:",status_code
106 print "************************************************************************" 113 if __name__ ==
"__main__":
114 host=
"cmsweb.cern.ch/dbs_discovery/" 116 dbsInst=
"cms_dbs_prod_global" 118 (opts,args) = optManager.getOpt()
119 if opts.host: host=opts.host
120 if host.find(
"http://")!=-1:
121 host=host.replace(
"http://",
"")
128 if opts.dbsInst: dbsInst=opts.dbsInst
130 if os.path.isfile(opts.input):
131 input=open(opts.input,
'r').readline() 135 print "\nUsage: %s --help"%sys.argv[0]
137 result =
sendMessage(host,port,dbsInst,input,opts.page,opts.limit,opts.xml,opts.case,opts.iface,opts.details,opts.cff,opts.verbose)
def replace(string, replacements)
static std::string join(char **cmd)
def sendMessage(host, port, dbsInst, userInput, page, limit, xml=0, case='on', iface='dd', details=0, cff=0, debug=0)