| 1 | #!/usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | import sys |
|---|
| 4 | |
|---|
| 5 | from deter import topdl |
|---|
| 6 | |
|---|
| 7 | from federation.proof import proof |
|---|
| 8 | from federation.remote_service import service_caller |
|---|
| 9 | from federation.client_lib import client_opts, exit_with_fault, RPCException, \ |
|---|
| 10 | wrangle_standard_options, do_rpc, get_experiment_names, save_certfile |
|---|
| 11 | |
|---|
| 12 | class ns_topdl_opts(client_opts): |
|---|
| 13 | def __init__(self): |
|---|
| 14 | client_opts.__init__(self) |
|---|
| 15 | self.add_option("--file", dest="file", |
|---|
| 16 | action='callback', callback=self.expand_file, type='str', |
|---|
| 17 | help="experiment description file") |
|---|
| 18 | self.add_option("--output", dest="outfile", type="string", |
|---|
| 19 | help="output topdl file") |
|---|
| 20 | |
|---|
| 21 | parser = ns_topdl_opts() |
|---|
| 22 | (opts, args) = parser.parse_args() |
|---|
| 23 | |
|---|
| 24 | try: |
|---|
| 25 | cert, fid, url = wrangle_standard_options(opts) |
|---|
| 26 | except RuntimeError, e: |
|---|
| 27 | sys.exit("%s" %e) |
|---|
| 28 | |
|---|
| 29 | if opts.file: |
|---|
| 30 | try: |
|---|
| 31 | contents = "".join([l for l in open(opts.file, "r")]) |
|---|
| 32 | except EnvironmentError, e: |
|---|
| 33 | sys.exit("Can't read %s: %s" % (opts.file, e)) |
|---|
| 34 | else: |
|---|
| 35 | sys.exit("Must specify an experiment description (--file)") |
|---|
| 36 | |
|---|
| 37 | msg = { 'description': { 'ns2description': contents }, } |
|---|
| 38 | |
|---|
| 39 | if opts.debug > 1: print >>sys.stderr, msg |
|---|
| 40 | |
|---|
| 41 | try: |
|---|
| 42 | resp_dict = do_rpc(msg, |
|---|
| 43 | url, opts.transport, cert, opts.trusted, |
|---|
| 44 | serialize_only=opts.serialize_only, |
|---|
| 45 | tracefile=opts.tracefile, |
|---|
| 46 | caller=service_caller('Ns2Topdl'), |
|---|
| 47 | responseBody="Ns2TopdlResponseBody") |
|---|
| 48 | except RPCException, e: |
|---|
| 49 | exit_with_fault(e, 'Ns2Topdl', opts) |
|---|
| 50 | except RuntimeError, e: |
|---|
| 51 | sys.exit("Error processing RPC: %s" % e) |
|---|
| 52 | |
|---|
| 53 | if 'experimentdescription' in resp_dict: |
|---|
| 54 | if 'topdldescription' in resp_dict['experimentdescription']: |
|---|
| 55 | exp = resp_dict['experimentdescription']['topdldescription'] |
|---|
| 56 | top = topdl.Topology(**exp) |
|---|
| 57 | else: |
|---|
| 58 | sys.exit("Bad response: could not translate") |
|---|
| 59 | elif opts.serialize_only: |
|---|
| 60 | sys.exit(0) |
|---|
| 61 | else: |
|---|
| 62 | sys.exit("Bad response. %s" % e.message) |
|---|
| 63 | |
|---|
| 64 | if opts.outfile: |
|---|
| 65 | try: |
|---|
| 66 | f = open(opts.outfile, "w") |
|---|
| 67 | print >>f, topdl.topology_to_xml(top, top="experiment") |
|---|
| 68 | f.close() |
|---|
| 69 | except EnvironmentError, e: |
|---|
| 70 | sys.exit("Can't write to %s: %s" % (opts.outfile, e)) |
|---|
| 71 | else: |
|---|
| 72 | print topdl.topology_to_xml(top, top="experiment") |
|---|
| 73 | proof = proof.from_dict(resp_dict.get('proof', {})) |
|---|
| 74 | if proof and opts.auth_log: |
|---|
| 75 | log_authentication(opts.auth_log, 'New (create)', 'succeeded', proof) |
|---|