CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
query.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #-*- coding: ISO-8859-1 -*-
3 #
4 # Copyright 2008 Cornell University, Ithaca, NY 14853. All rights reserved.
5 #
6 # Author: Valentin Kuznetsov, 2008
7 
8 """
9 DBS data discovery command line interface
10 """
11 
12 import httplib, urllib, types, string, os, sys
13 from optparse import OptionParser
14 
16  """
17  DDOptionParser is main class to parse options for L{DDHelper} and L{DDServer}.
18  """
19  def __init__(self):
20  self.parser = OptionParser()
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")
45  def getOpt(self):
46  """
47  Returns parse list of options
48  """
49  return self.parser.parse_args()
50 
51 def sendMessage(host,port,dbsInst,userInput,page,limit,xml=0,case='on',iface='dd',details=0,cff=0,debug=0):
52  """
53  Send message to server, message should be an well formed XML document.
54  """
55  if xml: xml=1
56  else: xml=0
57  if cff: cff=1
58  else: cff=0
59  input=urllib.quote(userInput)
60  if debug:
61  httplib.HTTPConnection.debuglevel = 1
62  print "Contact",host,port
63  _port=443
64  if host.find("http://")!=-1:
65  _port=80
66  if host.find("https://")!=-1:
67  _port=443
68  host=host.replace("http://","").replace("https://","")
69  if host.find(":")==-1:
70  port=_port
71  prefix_path=""
72  if host.find("/")!=-1:
73  hs=host.split("/")
74  host=hs[0]
75  prefix_path='/'.join(hs[1:])
76  if host.find(":")!=-1:
77  host,port=host.split(":")
78  port=int(port)
79 # print "\n\n+++",host,port
80  if port==443:
81  http_conn = httplib.HTTPS(host,port)
82  else:
83  http_conn = httplib.HTTP(host,port)
84  if details: details=1
85  else: details=0
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)
87  if prefix_path:
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()
94  http_conn.send(input)
95 
96  (status_code,msg,reply)=http_conn.getreply()
97  data=http_conn.getfile().read()
98  if debug or msg!="OK":
99  print
100  print http_conn.headers
101  print "*** Send message ***"
102  print input
103  print "************************************************************************"
104  print "status code:",status_code
105  print "message:",msg
106  print "************************************************************************"
107  print reply
108  return data
109 
110 #
111 # main
112 #
113 if __name__ == "__main__":
114  host= "cmsweb.cern.ch/dbs_discovery/"
115  port= 443
116  dbsInst="cms_dbs_prod_global"
117  optManager = DDOptionParser()
118  (opts,args) = optManager.getOpt()
119  if opts.host: host=opts.host
120  if host.find("http://")!=-1:
121  host=host.replace("http://","")
122 # if host.find(":")!=-1:
123 # host,port=host.split(":")
124  if host[-1]!="/":
125  host+="/"
126  if opts.port:
127  port = opts.port
128  if opts.dbsInst: dbsInst=opts.dbsInst
129  if opts.input:
130  if os.path.isfile(opts.input):
131  input=open(opts.input,'r').readline()
132  else:
133  input=opts.input
134  else:
135  print "\nUsage: %s --help"%sys.argv[0]
136  sys.exit(0)
137  result = sendMessage(host,port,dbsInst,input,opts.page,opts.limit,opts.xml,opts.case,opts.iface,opts.details,opts.cff,opts.verbose)
138  print result
def sendMessage
Definition: query.py:51
static std::string join(char **cmd)
Definition: RemoteFile.cc:18