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