source: fedd/fedd_ns2topdl.py @ 56816c9

axis_examplecompt_changesinfo-opsversion-3.01version-3.02
Last change on this file since 56816c9 was d743d60, checked in by Ted Faber <faber@…>, 14 years ago

Totally refactor fedd_client.py into component scripts. The previous layout
have become a twisty hell of misdirected OOP and learning python run amok.
This version is actually pretty readable and will be much easier to build on.

  • Property mode set to 100755
File size: 1.9 KB
Line 
1#!/usr/local/bin/python
2
3import sys
4
5from federation import topdl
6from federation.remote_service import service_caller
7from federation.client_lib import client_opts, exit_with_fault, RPCException, \
8        wrangle_standard_options, do_rpc, get_experiment_names, save_certfile
9
10class 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
18parser = ns_topdl_opts()
19(opts, args) = parser.parse_args()
20
21cert, fid = wrangle_standard_options(opts)
22
23if 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))
28else:
29    sys.exit("Must specify an experiment description (--file)")
30
31msg = { 'description': { 'ns2description': contents }, }
32
33if opts.debug > 1: print >>sys.stderr, msg
34
35try:
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")
42except RPCException, e:
43    exit_with_fault(e)
44except RuntimeError, e:
45    sys.exit("Error processing RPC: %s" % e)
46
47if '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")
53elif opts.serialize_only:
54    sys.exit(0)
55else:
56    sys.exit("Bad response. %s" % e.message)
57
58if 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))
65else:
66    print topdl.topology_to_xml(top, top="experiment")
Note: See TracBrowser for help on using the repository browser.