#!/usr/bin/env python import sys from deter import topdl from federation.proof import proof 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", action='callback', callback=self.expand_file, type='str', 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() try: cert, fid, url = wrangle_standard_options(opts) except RuntimeError, e: sys.exit("%s" %e) 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, 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, 'Ns2Topdl', opts) 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") proof = proof.from_dict(resp_dict.get('proof', {})) if proof and opts.auth_log: log_authentication(opts.auth_log, 'New (create)', 'succeeded', proof)