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 | action='callback', callback=self.expand_file, type='str', |
---|
15 | help="experiment description file") |
---|
16 | self.add_option("--output", dest="outfile", type="string", |
---|
17 | help="output topdl file") |
---|
18 | |
---|
19 | parser = ns_topdl_opts() |
---|
20 | (opts, args) = parser.parse_args() |
---|
21 | |
---|
22 | try: |
---|
23 | cert, fid, url = wrangle_standard_options(opts) |
---|
24 | except RuntimeError, e: |
---|
25 | sys.exit("%s" %e) |
---|
26 | |
---|
27 | if opts.file: |
---|
28 | try: |
---|
29 | contents = "".join([l for l in open(opts.file, "r")]) |
---|
30 | except EnvironmentError, e: |
---|
31 | sys.exit("Can't read %s: %s" % (opts.file, e)) |
---|
32 | else: |
---|
33 | sys.exit("Must specify an experiment description (--file)") |
---|
34 | |
---|
35 | msg = { 'description': { 'ns2description': contents }, } |
---|
36 | |
---|
37 | if opts.debug > 1: print >>sys.stderr, msg |
---|
38 | |
---|
39 | try: |
---|
40 | resp_dict = do_rpc(msg, |
---|
41 | url, opts.transport, cert, opts.trusted, |
---|
42 | serialize_only=opts.serialize_only, |
---|
43 | tracefile=opts.tracefile, |
---|
44 | caller=service_caller('Ns2Topdl'), |
---|
45 | responseBody="Ns2TopdlResponseBody") |
---|
46 | except RPCException, e: |
---|
47 | exit_with_fault(e) |
---|
48 | except RuntimeError, e: |
---|
49 | sys.exit("Error processing RPC: %s" % e) |
---|
50 | |
---|
51 | if 'experimentdescription' in resp_dict: |
---|
52 | if 'topdldescription' in resp_dict['experimentdescription']: |
---|
53 | exp = resp_dict['experimentdescription']['topdldescription'] |
---|
54 | top = topdl.Topology(**exp) |
---|
55 | else: |
---|
56 | sys.exit("Bad response: could not translate") |
---|
57 | elif opts.serialize_only: |
---|
58 | sys.exit(0) |
---|
59 | else: |
---|
60 | sys.exit("Bad response. %s" % e.message) |
---|
61 | |
---|
62 | if opts.outfile: |
---|
63 | try: |
---|
64 | f = open(opts.outfile, "w") |
---|
65 | print >>f, topdl.topology_to_xml(top, top="experiment") |
---|
66 | f.close() |
---|
67 | except EnvironmentError, e: |
---|
68 | sys.exit("Can't write to %s: %s" % (opts.outfile, e)) |
---|
69 | else: |
---|
70 | print topdl.topology_to_xml(top, top="experiment") |
---|