CMS 3D CMS Logo

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 from __future__ import print_function
12 
13 import httplib, urllib, types, string, os, sys
14 from optparse import OptionParser
15 
17  """
18  DDOptionParser is main class to parse options for L{DDHelper} and L{DDServer}.
19  """
20  def __init__(self):
21  self.parser = OptionParser()
22  self.parser.add_option("--dbsInst",action="store", type="string", dest="dbsInst",
23  help="specify DBS instance to use, e.g. --dbsInst=cms_dbs_prod_global")
24  self.parser.add_option("-v","--verbose",action="store", type="int", default=0, dest="verbose",
25  help="specify verbosity level, 0-none, 1-info, 2-debug")
26  self.parser.add_option("--input",action="store", type="string", default=False, dest="input",
27  help="specify input for your request.")
28  self.parser.add_option("--xml",action="store_true",dest="xml",
29  help="request output in XML format")
30  self.parser.add_option("--cff",action="store_true",dest="cff",
31  help="request output for files in CMS cff format")
32  self.parser.add_option("--host",action="store",type="string",dest="host",
33  help="specify a host name of Data Discovery service, e.g. https://cmsweb.cern.ch/dbs_discovery/")
34  self.parser.add_option("--port",action="store",type="string",dest="port",
35  help="specify a port to be used by Data Discovery host")
36  self.parser.add_option("--iface",action="store",default="dd",type="string",dest="iface",
37  help="specify which interface to use for queries dd or dbsapi, default is dbsapi.")
38  self.parser.add_option("--details",action="store_true",dest="details",
39  help="show detailed output")
40  self.parser.add_option("--case",action="store",default="on",type="string",dest="case",
41  help="specify if your input is case sensitive of not, default is on.")
42  self.parser.add_option("--page",action="store",type="string",default="0",dest="page",
43  help="specify output page, should come together with --limit and --details")
44  self.parser.add_option("--limit",action="store",type="string",default="10",dest="limit",
45  help="specify a limit on output, e.g. 50 results, the --limit=-1 will list all results")
46  def getOpt(self):
47  """
48  Returns parse list of options
49  """
50  return self.parser.parse_args()
51 
52 def sendMessage(host,port,dbsInst,userInput,page,limit,xml=0,case='on',iface='dd',details=0,cff=0,debug=0):
53  """
54  Send message to server, message should be an well formed XML document.
55  """
56  if xml: xml=1
57  else: xml=0
58  if cff: cff=1
59  else: cff=0
60  input=urllib.quote(userInput)
61  if debug:
62  httplib.HTTPConnection.debuglevel = 1
63  print("Contact",host,port)
64  _port=443
65  if host.find("http://")!=-1:
66  _port=80
67  if host.find("https://")!=-1:
68  _port=443
69  host=host.replace("http://","").replace("https://","")
70  if host.find(":")==-1:
71  port=_port
72  prefix_path=""
73  if host.find("/")!=-1:
74  hs=host.split("/")
75  host=hs[0]
76  prefix_path='/'.join(hs[1:])
77  if host.find(":")!=-1:
78  host,port=host.split(":")
79  port=int(port)
80 # print "\n\n+++",host,port
81  if port==443:
82  http_conn = httplib.HTTPS(host,port)
83  else:
84  http_conn = httplib.HTTP(host,port)
85  if details: details=1
86  else: details=0
87  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  if prefix_path:
89  path="/"+prefix_path+path[1:]
90  http_conn.putrequest('POST',path)
91  http_conn.putheader('Host',host)
92  http_conn.putheader('Content-Type','text/html; charset=utf-8')
93  http_conn.putheader('Content-Length',str(len(input)))
94  http_conn.endheaders()
95  http_conn.send(input)
96 
97  (status_code,msg,reply)=http_conn.getreply()
98  data=http_conn.getfile().read()
99  if debug or msg!="OK":
100  print()
101  print(http_conn.headers)
102  print("*** Send message ***")
103  print(input)
104  print("************************************************************************")
105  print("status code:",status_code)
106  print("message:",msg)
107  print("************************************************************************")
108  print(reply)
109  return data
110 
111 #
112 # main
113 #
114 if __name__ == "__main__":
115  host= "cmsweb.cern.ch/dbs_discovery/"
116  port= 443
117  dbsInst="cms_dbs_prod_global"
118  optManager = DDOptionParser()
119  (opts,args) = optManager.getOpt()
120  if opts.host: host=opts.host
121  if host.find("http://")!=-1:
122  host=host.replace("http://","")
123 # if host.find(":")!=-1:
124 # host,port=host.split(":")
125  if host[-1]!="/":
126  host+="/"
127  if opts.port:
128  port = opts.port
129  if opts.dbsInst: dbsInst=opts.dbsInst
130  if opts.input:
131  if os.path.isfile(opts.input):
132  input=open(opts.input,'r').readline()
133  else:
134  input=opts.input
135  else:
136  print("\nUsage: %s --help"%sys.argv[0])
137  sys.exit(0)
138  result = sendMessage(host,port,dbsInst,input,opts.page,opts.limit,opts.xml,opts.case,opts.iface,opts.details,opts.cff,opts.verbose)
139  print(result)
join
static std::string join(char **cmd)
Definition: RemoteFile.cc:17
str
#define str(s)
Definition: TestProcessor.cc:51
query.DDOptionParser.parser
parser
Definition: query.py:21
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
createfilelist.int
int
Definition: createfilelist.py:10
query.DDOptionParser
Definition: query.py:16
main.parse_args
def parse_args(args)
Definition: main.py:63
query.DDOptionParser.getOpt
def getOpt(self)
Definition: query.py:46
query.sendMessage
def sendMessage(host, port, dbsInst, userInput, page, limit, xml=0, case='on', iface='dd', details=0, cff=0, debug=0)
Definition: query.py:52
readEcalDQMStatus.read
read
Definition: readEcalDQMStatus.py:38
query.DDOptionParser.__init__
def __init__(self)
Definition: query.py:20
python.rootplot.root2matplotlib.replace
def replace(string, replacements)
Definition: root2matplotlib.py:444