00001
00002
00003
00004
00005
00006
00007
00008 """
00009 DBS data discovery command line interface
00010 """
00011
00012 import httpslib, 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("--host",action="store",type="string",dest="host",
00030 help="specify a host name of Data Discovery service, e.g. httpss://cmsweb.cern.ch/dbs_discovery/")
00031 self.parser.add_option("--details",action="store_true",dest="details",
00032 help="show detailed output")
00033 self.parser.add_option("--case",action="store",default="on",type="string",dest="case",
00034 help="specify if your input is case sensitive of not, default is on.")
00035 self.parser.add_option("--page",action="store",type="string",default="0",dest="page",
00036 help="specify output page, should come together with --limit and --details")
00037 self.parser.add_option("--limit",action="store",type="string",default="10",dest="limit",
00038 help="specify a limit on output, e.g. 50 results, should come together with --page and --details")
00039 def getOpt(self):
00040 """
00041 Returns parse list of options
00042 """
00043 return self.parser.parse_args()
00044
00045 def sendMessage(host,port,dbsInst,userInput,page,limit,xml=0,case='on',details=0,debug=0):
00046 """
00047 Send message to server, message should be an well formed XML document.
00048 """
00049 if xml:
00050 xml=1
00051 else:
00052 xml=0
00053 input=urllib.quote(userInput)
00054 if debug:
00055 httpslib.HTTPConnection.debuglevel = 1
00056 print "Contact",host,port
00057 _port=443
00058 if host.find("https://")!=-1:
00059 _port=80
00060 if host.find("httpss://")!=-1:
00061 _port=443
00062 host=host.replace("https://","").replace("httpss://","")
00063 if host.find(":")==-1:
00064 port=_port
00065 prefix_path=""
00066 if host.find("/")!=-1:
00067 hs=host.split("/")
00068 host=hs[0]
00069 prefix_path='/'.join(hs[1:])
00070 port=int(port)
00071 if port==443:
00072 https_conn = httpslib.HTTPS(host,port)
00073 else:
00074 https_conn = httpslib.HTTP(host,port)
00075 if details:
00076 details=1
00077 else:
00078 details=0
00079 path='/aSearch?dbsInst=%s&html=0&caseSensitive=%s&_idx=%s&pagerStep=%s&userInput=%s&xml=%s&details=%s'%(dbsInst,case,page,limit,input,xml,details)
00080
00081 if prefix_path:
00082 path="/"+prefix_path+path[1:]
00083 https_conn.putrequest('POST',path)
00084 https_conn.putheader('Host',host)
00085 https_conn.putheader('Content-Type','text/html; charset=utf-8')
00086 https_conn.putheader('Content-Length',str(len(input)))
00087 https_conn.endheaders()
00088 https_conn.send(input)
00089
00090 (status_code,msg,reply)=https_conn.getreply()
00091 data=https_conn.getfile().read()
00092 if debug or msg!="OK":
00093 print
00094 print https_conn.headers
00095 print "*** Send message ***"
00096 print input
00097 print "************************************************************************"
00098 print "status code:",status_code
00099 print "message:",msg
00100 print "************************************************************************"
00101 print reply
00102 return data
00103
00104
00105
00106
00107 if __name__ == "__main__":
00108 host= "cmsweb.cern.ch/dbs_discovery/"
00109 port= 443
00110 dbsInst="cms_dbs_prod_global"
00111 optManager = DDOptionParser()
00112 (opts,args) = optManager.getOpt()
00113 if opts.host: host=opts.host
00114 if host.find("https://")!=-1:
00115 host=host.replace("https://","")
00116 if host[-1]!="/":
00117 host+="/"
00118 if opts.dbsInst: dbsInst=opts.dbsInst
00119 if opts.input:
00120 if os.path.isfile(opts.input):
00121 input=open(opts.input,'r').readline()
00122 else:
00123 input=opts.input
00124 else:
00125 print "\nUsage: ./python_dbs.py --help"
00126 sys.exit(0)
00127 result = sendMessage(host,port,dbsInst,input,opts.page,opts.limit,opts.xml,opts.case,opts.details,opts.verbose)
00128 print result