source: fedd/fedd_ns2topdl.py @ c259a77

Last change on this file since c259a77 was c259a77, checked in by Ted Faber <faber@…>, 10 years ago

Service info in xml files.

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