#!/usr/local/bin/python import sys from federation import topdl from federation.remote_service import service_caller from federation.client_lib import client_opts, exit_with_fault, RPCException, \ wrangle_standard_options, do_rpc, get_experiment_names, save_certfile class ns_topdl_opts(client_opts): def __init__(self): client_opts.__init__(self) self.add_option("--file", dest="file", help="experiment description file") self.add_option("--output", dest="outfile", type="string", help="output topdl file") parser = ns_topdl_opts() (opts, args) = parser.parse_args() cert, fid = wrangle_standard_options(opts) if opts.file: try: contents = "".join([l for l in open(opts.file, "r")]) except EnvironmentError, e: sys.exit("Can't read %s: %s" % (opts.file, e)) else: sys.exit("Must specify an experiment description (--file)") msg = { 'description': { 'ns2description': contents }, } if opts.debug > 1: print >>sys.stderr, msg try: resp_dict = do_rpc(msg, opts.url, opts.transport, cert, opts.trusted, serialize_only=opts.serialize_only, tracefile=opts.tracefile, caller=service_caller('Ns2Topdl'), responseBody="Ns2TopdlResponseBody") except RPCException, e: exit_with_fault(e) except RuntimeError, e: sys.exit("Error processing RPC: %s" % e) if 'experimentdescription' in resp_dict: if 'topdldescription' in resp_dict['experimentdescription']: exp = resp_dict['experimentdescription']['topdldescription'] top = topdl.Topology(**exp) else: sys.exit("Bad response: could not translate") elif opts.serialize_only: sys.exit(0) else: sys.exit("Bad response. %s" % e.message) if opts.outfile: try: f = open(opts.outfile, "w") print >>f, topdl.topology_to_xml(top, top="experiment") f.close() except EnvironmentError, e: sys.exit("Can't write to %s: %s" % (opts.outfile, e)) else: print topdl.topology_to_xml(top, top="experiment")