[2e46f35] | 1 | #!/usr/bin/env python |
---|
[d743d60] | 2 | |
---|
| 3 | import sys |
---|
| 4 | |
---|
[6bedbdba] | 5 | from deter import topdl |
---|
| 6 | |
---|
[e83f2f2] | 7 | from federation.proof import proof |
---|
[d743d60] | 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", |
---|
[62f3dd9] | 16 | action='callback', callback=self.expand_file, type='str', |
---|
[d743d60] | 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 | |
---|
[a0c2866] | 24 | try: |
---|
| 25 | cert, fid, url = wrangle_standard_options(opts) |
---|
| 26 | except RuntimeError, e: |
---|
| 27 | sys.exit("%s" %e) |
---|
[d743d60] | 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, |
---|
[5d854e1] | 43 | url, opts.transport, cert, opts.trusted, |
---|
[d743d60] | 44 | serialize_only=opts.serialize_only, |
---|
| 45 | tracefile=opts.tracefile, |
---|
| 46 | caller=service_caller('Ns2Topdl'), |
---|
| 47 | responseBody="Ns2TopdlResponseBody") |
---|
| 48 | except RPCException, e: |
---|
[e83f2f2] | 49 | exit_with_fault(e, 'Ns2Topdl', opts) |
---|
[d743d60] | 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") |
---|
[e83f2f2] | 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) |
---|