source: fedd/fedd_ns2topdl.py

Last change on this file 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
RevLine 
[2e46f35]1#!/usr/bin/env python
[d743d60]2
3import sys
4
[6bedbdba]5from deter import topdl
6
[e83f2f2]7from federation.proof import proof
[d743d60]8from federation.remote_service import service_caller
9from federation.client_lib import client_opts, exit_with_fault, RPCException, \
[c259a77]10        wrangle_standard_options, do_rpc, get_experiment_names, save_certfile,\
11        ns_service_re
[d743d60]12
13class ns_topdl_opts(client_opts):
14    def __init__(self):
15        client_opts.__init__(self)
16        self.add_option("--file", dest="file", 
[62f3dd9]17                action='callback', callback=self.expand_file, type='str',
[d743d60]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
[a0c2866]25try:
26    cert, fid, url = wrangle_standard_options(opts)
27except RuntimeError, e:
28    sys.exit("%s" %e)
[c259a77]29svcs = []
30contents = ''
[d743d60]31if opts.file:
32    try:
[c259a77]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))
[d743d60]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, 
[5d854e1]48            url, opts.transport, cert, opts.trusted, 
[d743d60]49            serialize_only=opts.serialize_only,
50            tracefile=opts.tracefile,
51            caller=service_caller('Ns2Topdl'),
52            responseBody="Ns2TopdlResponseBody")
53except RPCException, e:
[e83f2f2]54    exit_with_fault(e, 'Ns2Topdl', opts)
[d743d60]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
[c259a77]69if len(svcs) > 0 :
70    comments = '<!--\n%s\n-->' % '\n'.join(svcs)
71else:
72    comments = ''
73
[d743d60]74if opts.outfile:
75    try:
76        f = open(opts.outfile, "w")
[c259a77]77        print >>f, comments
[d743d60]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:
[c259a77]83    print comments
[d743d60]84    print topdl.topology_to_xml(top, top="experiment")
[e83f2f2]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.