Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:09:33

0001 #!/usr/bin/env python3
0002 #-*- coding: ISO-8859-1 -*-
0003 #
0004 #
0005 # Author:  Valentin Kuznetsov, 2008
0006 
0007 """
0008 DBS data discovery command line interface
0009 """
0010 from __future__ import print_function
0011 
0012 import httplib, urllib, urllib2, types, string, os, sys
0013 from optparse import OptionParser
0014 
0015 class DDOptionParser: 
0016   """
0017      DDOptionParser is main class to parse options for L{DDHelper} and L{DDServer}.
0018   """
0019   def __init__(self):
0020     self.parser = OptionParser()
0021     self.parser.add_option("--dbsInst",action="store", type="string", dest="dbsInst",
0022          help="specify DBS instance to use, e.g. --dbsInst=cms_dbs_prod_global")
0023     self.parser.add_option("-v","--verbose",action="store", type="int", default=0, dest="verbose",
0024          help="specify verbosity level, 0-none, 1-info, 2-debug")
0025     self.parser.add_option("--input",action="store", type="string", default=False, dest="input",
0026          help="specify input for your request.")
0027     self.parser.add_option("--xml",action="store_true",dest="xml",
0028          help="request output in XML format")
0029     self.parser.add_option("--cff",action="store_true",dest="cff",
0030          help="request output for files in CMS cff format")
0031     self.parser.add_option("--host",action="store",type="string",dest="host",
0032          help="specify a host name of Data Discovery service, e.g. https://cmsweb.cern.ch/dbs_discovery/")
0033     self.parser.add_option("--port",action="store",type="string",dest="port",
0034          help="specify a port to be used by Data Discovery host")
0035     self.parser.add_option("--iface",action="store",default="dbsapi",type="string",dest="iface",
0036          help="specify which interface to use for queries dd or dbsapi, default is dbsapi.")
0037     self.parser.add_option("--details",action="store_true",dest="details",
0038          help="show detailed output")
0039     self.parser.add_option("--case",action="store",default="on",type="string",dest="case",
0040          help="specify if your input is case sensitive of not, default is on.")
0041     self.parser.add_option("--page",action="store",type="string",default="0",dest="page",
0042          help="specify output page, should come together with --limit and --details")
0043     self.parser.add_option("--limit",action="store",type="string",default="10",dest="limit",
0044          help="specify a limit on output, e.g. 50 results, the --limit=-1 will list all results")
0045   def getOpt(self):
0046     """
0047         Returns parse list of options
0048     """
0049     return self.parser.parse_args()
0050 
0051 def sendMessage(host,port,dbsInst,userInput,page,limit,xml=0,case='on',iface='dbsapi',details=0,cff=0,debug=0):
0052     if xml: xml=1
0053     else:   xml=0
0054     if cff: cff=1
0055     else:   cff=0
0056     input=urllib.quote(userInput)
0057     if debug:
0058        httplib.HTTPConnection.debuglevel = 1
0059        print("Contact",host,port)
0060     _port=443
0061     if host.find("http://")!=-1:
0062        _port=80
0063     if host.find("https://")!=-1:
0064        _port=443
0065     host=host.replace("http://","").replace("https://","")
0066     if host.find(":")==-1:
0067        port=_port
0068     prefix_path=""
0069     if host.find("/")!=-1:
0070        hs=host.split("/")
0071        host=hs[0]
0072        prefix_path='/'.join(hs[1:])
0073     if host.find(":")!=-1:
0074        host,port=host.split(":")
0075     port=int(port)
0076     path="/aSearch"
0077     if prefix_path:
0078        path="/"+prefix_path+path[1:]
0079     if port==443:
0080        url = "https://"+host+":%s"%port+path
0081     else:
0082        url = "http://"+host+":%s"%port+path
0083     if details: details=1
0084     else:       details=0
0085     params  = {'dbsInst':dbsInst,'html':0,'caseSensitive':case,'_idx':page,'pagerStep':limit,'userInput':input,'xml':xml,'details':details,'cff':cff,'method':iface}
0086     agent   = "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)"
0087     ctypes  = "text/plain"
0088     headers = { 'User-Agent':agent, 'Accept':ctypes}
0089     data    = urllib.urlencode(params,doseq=True)
0090     if  debug:
0091         print(url,data,headers)
0092     req     = urllib2.Request(url, data, headers)
0093     data    = ""
0094     try:
0095         response = urllib2.urlopen(req)
0096         data = response.read()
0097     except urllib2.HTTPError as e:
0098         if e.code==201:
0099            print(e.headers)       
0100            print(e.msg)
0101            pass
0102         else:
0103            raise e
0104     return data
0105 
0106 #
0107 # main
0108 #
0109 if __name__ == "__main__":
0110     host= "cmsweb.cern.ch/dbs_discovery/"
0111     port= 443
0112     dbsInst="cms_dbs_prod_global"
0113     optManager  = DDOptionParser()
0114     (opts,args) = optManager.getOpt()
0115     if opts.host: host=opts.host
0116     if host.find("http://")!=-1:
0117        host=host.replace("http://","")
0118 #    if host.find(":")!=-1:
0119 #       host,port=host.split(":")
0120     if host[-1]!="/":
0121        host+="/"
0122     if opts.port:
0123        port = opts.port
0124     if opts.dbsInst: dbsInst=opts.dbsInst
0125     if opts.input:
0126        if os.path.isfile(opts.input):
0127           input=open(opts.input,'r').readline()
0128        else:
0129           input=opts.input
0130     else:
0131        print("\nUsage: %s --help"%sys.argv[0])
0132        sys.exit(0)
0133     result = sendMessage(host,port,dbsInst,input,opts.page,opts.limit,opts.xml,opts.case,opts.iface,opts.details,opts.cff,opts.verbose)
0134     print(result)