Changeset c259a77


Ignore:
Timestamp:
Jun 3, 2014 4:29:31 PM (11 years ago)
Author:
Ted Faber <faber@…>
Branches:
master
Children:
0b217d1
Parents:
ba07149
Message:

Service info in xml files.

Location:
fedd
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • fedd/fedd_create.py

    rba07149 rc259a77  
    1717from federation.client_lib import client_opts, exit_with_fault, RPCException, \
    1818        wrangle_standard_options, do_rpc, get_experiment_names, save_certfile,\
    19         get_abac_certs, log_authentication
     19        get_abac_certs, log_authentication, parse_service,\
     20        service_dict_to_line, ns_service_re, extract_services_from_xml
    2021from federation.util import abac_split_cert, abac_context_to_creds
    2122
     
    5859        self.set_defaults(delegate=True)
    5960
    60 def parse_service(svc):
    61     """
    62     Pasre a service entry into a hash representing a service entry in a
    63     message.  The format is:
    64         svc_name:exporter(s):importer(s):attr=val,attr=val
    65     The svc_name is teh service name, exporter is the exporting testbeds
    66     (comma-separated) importer is the importing testbeds (if any) and the rest
    67     are attr=val pairs that will become attributes of the service.  These
    68     include parameters and other such stuff.
    69     """
    70 
    71     terms = svc.split(':')
    72     svcd = { }
    73     if len(terms) < 2 or len(terms[0]) == 0 or len(terms[1]) == 0:
    74         sys.exit("Bad service description '%s': Not enough terms" % svc)
    75    
    76     svcd['name'] = terms[0]
    77     svcd['export'] = terms[1].split(",")
    78     if len(terms) > 2 and len(terms[2]) > 0:
    79         svcd['import'] = terms[2].split(",")
    80     if len(terms) > 3 and len(terms[3]) > 0:
    81         svcd['fedAttr'] = [ ]
    82         for t in terms[3].split(";"):
    83             i = t.find("=")
    84             if i != -1 :
    85                 svcd['fedAttr'].append(
    86                         {'attribute': t[0:i], 'value': t[i+1:]})
    87             else:
    88                 sys.exit("Bad service attribute '%s': no equals sign" % t)
    89     return svcd
    90 
    9161def project_export_service(master, project):
    9262    """
     
    156126
    157127# Main line
    158 service_re = re.compile('^\\s*#\\s*SERVICE:\\s*([\\S]+)')
    159128parser = fedd_create_opts()
    160129(opts, args) = parser.parse_args()
     
    173142    try:
    174143        top = topdl.topology_from_xml(filename=opts.file, top='experiment')
     144        # Pull any service descriptions from the file as well
     145        svcs = extract_services_from_xml(filename=opts.file)
    175146    except EnvironmentError:
    176147        # Can't read the file, fail now
     
    186157            # Parse all the strings that we can pull out of the file using the
    187158            # service_re.
    188             svcs.extend([parse_service(service_re.match(l).group(1)) \
    189                     for l in lines if service_re.match(l)])
     159            svcs.extend([parse_service(ns_service_re.match(l).group(1)) \
     160                    for l in lines if ns_service_re.match(l)])
    190161        except EnvironmentError:
    191162            sys.exit("Cannot read description file (%s)" %opts.file)
  • fedd/fedd_ns2topdl.py

    rba07149 rc259a77  
    88from federation.remote_service import service_caller
    99from federation.client_lib import client_opts, exit_with_fault, RPCException, \
    10         wrangle_standard_options, do_rpc, get_experiment_names, save_certfile
     10        wrangle_standard_options, do_rpc, get_experiment_names, save_certfile,\
     11        ns_service_re
    1112
    1213class ns_topdl_opts(client_opts):
     
    2627except RuntimeError, e:
    2728    sys.exit("%s" %e)
    28 
     29svcs = []
     30contents = ''
    2931if opts.file:
    3032    try:
    31         contents = "".join([l for l in open(opts.file, "r")])
     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))
    3237    except EnvironmentError, e:
    3338        sys.exit("Can't read %s: %s" % (opts.file, e))
     
    6267    sys.exit("Bad response. %s" % e.message)
    6368
     69if len(svcs) > 0 :
     70    comments = '<!--\n%s\n-->' % '\n'.join(svcs)
     71else:
     72    comments = ''
     73
    6474if opts.outfile:
    6575    try:
    6676        f = open(opts.outfile, "w")
     77        print >>f, comments
    6778        print >>f, topdl.topology_to_xml(top, top="experiment")
    6879        f.close()
     
    7081        sys.exit("Can't write to %s: %s" % (opts.outfile, e))
    7182else:
     83    print comments
    7284    print topdl.topology_to_xml(top, top="experiment")
    7385proof = proof.from_dict(resp_dict.get('proof', {}))
  • fedd/federation/client_lib.py

    rba07149 rc259a77  
    44import pwd
    55import os
     6import re
    67import os.path
     8import xml.parsers.expat
    79
    810from string import join
    911from datetime import datetime
    10 
    1112
    1213from deter import fedid
     
    137138    return rv
    138139
     140ns_service_re = re.compile('^\\s*#\\s*SERVICE:\\s*([\\S]+)')
     141xml_service_re = re.compile('SERVICE:\\s*([\\S]+)\\s*$')
     142
     143def parse_service(svc):
     144    """
     145    Pasre a service entry into a hash representing a service entry in a
     146    message.  The format is:
     147        svc_name:exporter(s):importer(s):attr=val,attr=val
     148    The svc_name is the service name, exporter is the exporting testbeds
     149    (comma-separated) importer is the importing testbeds (if any) and the rest
     150    are attr=val pairs that will become attributes of the service.  These
     151    include parameters and other such stuff.
     152    """
     153
     154    terms = svc.split(':')
     155    svcd = { }
     156    if len(terms) < 2 or len(terms[0]) == 0 or len(terms[1]) == 0:
     157        sys.exit("Bad service description '%s': Not enough terms" % svc)
     158
     159    svcd['name'] = terms[0]
     160    svcd['export'] = terms[1].split(",")
     161    if len(terms) > 2 and len(terms[2]) > 0:
     162        svcd['import'] = terms[2].split(",")
     163    if len(terms) > 3 and len(terms[3]) > 0:
     164        svcd['fedAttr'] = [ ]
     165        for t in terms[3].split(";"):
     166            i = t.find("=")
     167            if i != -1 :
     168                svcd['fedAttr'].append(
     169                        {'attribute': t[0:i], 'value': t[i+1:]})
     170            else:
     171                sys.exit("Bad service attribute '%s': no equals sign" % t)
     172    return svcd
     173
     174def service_dict_to_line(d):
     175    """
     176    Convert a dict containing a service description into a colon separated
     177    service description suitable for inclusion in a file or command line.
     178    """
     179
     180    return 'SERVICE: %s' % (
     181            ':'.join([
     182                d.get('name', ''),
     183                ','.join(d.get('export','')),
     184                ','.join(d.get('import','')),
     185                ','.join([
     186                    '='.join((dd.get('attribute', ''), dd.get('value',''))) \
     187                        for dd in d.get('fedAttr', [])
     188                ])
     189            ]))
     190
     191def extract_services_from_xml(string=None, file=None, filename=None):
     192    class parser:
     193        def __init__(self):
     194            self.svcs = []
     195        def comment_handler(self, data):
     196            for l in data.split('\n'):
     197                if xml_service_re.match(l):
     198                    self.svcs.append(
     199                            parse_service(xml_service_re.match(l).group(1)))
     200
     201    p = parser()
     202    xp = xml.parsers.expat.ParserCreate()
     203
     204    xp.CommentHandler = p.comment_handler
     205
     206    num_set = len([ x for x in (string, filename, file)\
     207            if x is not None ])
     208
     209    if num_set != 1:
     210        raise RuntimeError("Exactly one one of file, filename and string " + \
     211                "must be set")
     212    elif filename:
     213        f = open(filename, "r")
     214        xp.ParseFile(f)
     215        f.close()
     216    elif file:
     217        xp.ParseFile(file)
     218    elif string:
     219        xp.Parse(string, True)
     220
     221    return p.svcs
     222
    139223def wrangle_standard_options(opts):
    140224    """
Note: See TracChangeset for help on using the changeset viewer.