flexdataclient.in 1.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#!/usr/local/bin/python

import sys
import getopt
import xmlrpclib
import traceback
import string

DEF_HOST = 'ops.emulab.net'
DEF_PORT = 3993

def usage(progname):
    print "Usage: %s [-hp <arg>]" % str(progname)
    print "  -h <name>    Connect to this server (default: %s)" % DEF_HOST
    print "  -p <port>    Connect to this port (default: %d)" % DEF_PORT
    sys.exit(2)
    pass

def main():
    try:
        opts,args = getopt.getopt(sys.argv[1:],"h:p:")
    except getopt.GetoptError:
        traceback.print_exc()
        usage(sys.argv[0])
        pass

    (host,port) = (DEF_HOST,DEF_PORT)

    for (o,v) in opts:
        if o == '-h':
            host = v
            pass
        elif o == '-p':
            port = int(v)
            pass
        pass

    if len(args) < 1:
        usage(sys.argv[0])
        pass

    method = 'flexlab.' + args.pop(0)
    params = dict({})
    for param in args:
        plist = string.split(param, "=", 1)
        if len(plist) != 2:
            print ("error: Parameter, '"
                   + param
                   + "', is not of the form: param=value!")
            return -1
        value = plist[1]

        #
        # Evaluate the arg no matter what (so we get int/float/str, in addition
        # to list/dict).
        #
        try:
            value = eval(value)
        except:
            print "Error while parsing argument into python variable:"
            traceback.print_exc()
            sys.exit(3)
            pass
        
        params[plist[0]] = value
        pass

    #
    # Setup the server, grab the method
    #
    surl = "http://%s:%d/" % (host,port)
    server = xmlrpclib.ServerProxy(surl,allow_none=1)

    meth = getattr(server,method)
    
    #
    # Make the call. 
    #
    try:
        response = apply(meth,[params])
        pass
    except xmlrpclib.Fault, e:
        print e.faultString
        return -1

    print str(response)
    
    sys.exit(0)


if __name__ == '__main__':
    main()
    pass