source: fedd/fedd_ns2topdl.py @ d0912be

compt_changesinfo-ops
Last change on this file since d0912be was 2e46f35, checked in by mikeryan <mikeryan@…>, 13 years ago

switch to /usr/bin/env python to run python

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