1 |
#!/usr/bin/env python
|
2 |
#---------------------------------------------------------------------------------------------------
|
3 |
# Stole this from Valery [Author: Valentin Kuznetsov, 2008]
|
4 |
#
|
5 |
# Probably it could do everything I wanted but I hacked it together.
|
6 |
#---------------------------------------------------------------------------------------------------
|
7 |
import os,sys,types,string
|
8 |
import httplib,urllib
|
9 |
from optparse import OptionParser
|
10 |
|
11 |
"""
|
12 |
DBS data discovery command line interface
|
13 |
"""
|
14 |
|
15 |
class DDOptionParser:
|
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("--details",action="store_true",dest="details",
|
34 |
help="show detailed output")
|
35 |
self.parser.add_option("--case",action="store",default="on",type="string",dest="case",
|
36 |
help="specify if your input is case sensitive of not, default is on.")
|
37 |
self.parser.add_option("--page",action="store",type="string",default="0",dest="page",
|
38 |
help="specify output page, should come together with --limit and --details")
|
39 |
self.parser.add_option("--limit",action="store",type="string",default="10",dest="limit",
|
40 |
help="specify a limit on output, e.g. 50 results, the --limit=-1 will list all results")
|
41 |
def getOpt(self):
|
42 |
"""
|
43 |
Returns parse list of options
|
44 |
"""
|
45 |
return self.parser.parse_args()
|
46 |
|
47 |
def sendMessage(host,port,dbsInst,userInput,page,limit,xml=0,case='on',details=0,cff=0,debug=0):
|
48 |
"""
|
49 |
Send message to server, message should be an well formed XML document.
|
50 |
"""
|
51 |
if xml:
|
52 |
xml=1
|
53 |
else:
|
54 |
xml=0
|
55 |
if cff:
|
56 |
cff=1
|
57 |
else:
|
58 |
cff=0
|
59 |
input = urllib.quote(userInput)
|
60 |
|
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 |
port=int(port)
|
78 |
if port==443:
|
79 |
http_conn = httplib.HTTPS(host,port)
|
80 |
else:
|
81 |
http_conn = httplib.HTTP(host,port)
|
82 |
if details:
|
83 |
details=1
|
84 |
else:
|
85 |
details=0
|
86 |
|
87 |
path='/getLFNsForSite?dbsInst=%s&site=*&datasetPath=%s&what=txt&userMode=user&run=*' \
|
88 |
%(dbsInst,input)
|
89 |
|
90 |
## path='/aSearch?dbsInst=%s&html=0&caseSensitive=%s&_idx=%s&pagerStep=%s&userInput=%s&xml=%s&details=%s&cff=%s'%(dbsInst,case,page,limit,input,xml,details,cff)
|
91 |
|
92 |
if prefix_path:
|
93 |
path="/"+prefix_path+path[1:]
|
94 |
|
95 |
# print ' HOST: ' + host
|
96 |
# print ' PATH: ' + path
|
97 |
http_conn.putrequest('POST',path)
|
98 |
http_conn.putheader('Host',host)
|
99 |
http_conn.putheader('Content-Type','text/html; charset=utf-8')
|
100 |
http_conn.putheader('Content-Length',str(len(input)))
|
101 |
http_conn.endheaders()
|
102 |
|
103 |
# print ' IPUT: ' + input
|
104 |
http_conn.send(input)
|
105 |
|
106 |
(status_code,msg,reply) = http_conn.getreply()
|
107 |
data = http_conn.getfile().read()
|
108 |
if debug or msg != "OK":
|
109 |
print
|
110 |
print http_conn.headers
|
111 |
print "*** Send message ***"
|
112 |
print input
|
113 |
print "************************************************************************"
|
114 |
print "status code:",status_code
|
115 |
print "message:",msg
|
116 |
print "************************************************************************"
|
117 |
print reply
|
118 |
return data
|
119 |
|
120 |
#---------------------------------------------------------------------------------------------------
|
121 |
# main
|
122 |
#---------------------------------------------------------------------------------------------------
|
123 |
if __name__ == "__main__":
|
124 |
host= "cmsweb.cern.ch/dbs_discovery/"
|
125 |
port= 443
|
126 |
dbsInst="cms_dbs_prod_global"
|
127 |
optManager = DDOptionParser()
|
128 |
(opts,args) = optManager.getOpt()
|
129 |
if opts.host:
|
130 |
host=opts.host
|
131 |
if host.find("http://") != -1:
|
132 |
host=host.replace("http://","")
|
133 |
if host[-1] != "/":
|
134 |
host += "/"
|
135 |
if opts.dbsInst:
|
136 |
dbsInst = opts.dbsInst
|
137 |
if opts.input:
|
138 |
if os.path.isfile(opts.input):
|
139 |
input = open(opts.input,'r').readline()
|
140 |
else:
|
141 |
input = opts.input
|
142 |
else:
|
143 |
print "\nUsage: %s --help"%sys.argv[0]
|
144 |
sys.exit(0)
|
145 |
|
146 |
result = sendMessage(host,port,dbsInst,input,
|
147 |
opts.page,opts.limit,opts.xml,opts.case,opts.details,opts.cff,opts.verbose)
|
148 |
print result
|