source: fedd/fedd_ns2topdl.py @ 62f3dd9

axis_examplecompt_changesinfo-ops
Last change on this file since 62f3dd9 was 62f3dd9, checked in by Ted Faber <faber@…>, 13 years ago

allow command line progams to expand tildes. Added a class derived from OptionParser? to make that easily available.

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