Changeset c259a77
- Timestamp:
- Jun 3, 2014 4:29:31 PM (11 years ago)
- Branches:
- master
- Children:
- 0b217d1
- Parents:
- ba07149
- Location:
- fedd
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
fedd/fedd_create.py
rba07149 rc259a77 17 17 from federation.client_lib import client_opts, exit_with_fault, RPCException, \ 18 18 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 20 21 from federation.util import abac_split_cert, abac_context_to_creds 21 22 … … 58 59 self.set_defaults(delegate=True) 59 60 60 def parse_service(svc):61 """62 Pasre a service entry into a hash representing a service entry in a63 message. The format is:64 svc_name:exporter(s):importer(s):attr=val,attr=val65 The svc_name is teh service name, exporter is the exporting testbeds66 (comma-separated) importer is the importing testbeds (if any) and the rest67 are attr=val pairs that will become attributes of the service. These68 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 svcd90 91 61 def project_export_service(master, project): 92 62 """ … … 156 126 157 127 # Main line 158 service_re = re.compile('^\\s*#\\s*SERVICE:\\s*([\\S]+)')159 128 parser = fedd_create_opts() 160 129 (opts, args) = parser.parse_args() … … 173 142 try: 174 143 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) 175 146 except EnvironmentError: 176 147 # Can't read the file, fail now … … 186 157 # Parse all the strings that we can pull out of the file using the 187 158 # 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)]) 190 161 except EnvironmentError: 191 162 sys.exit("Cannot read description file (%s)" %opts.file) -
fedd/fedd_ns2topdl.py
rba07149 rc259a77 8 8 from federation.remote_service import service_caller 9 9 from 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 11 12 12 13 class ns_topdl_opts(client_opts): … … 26 27 except RuntimeError, e: 27 28 sys.exit("%s" %e) 28 29 svcs = [] 30 contents = '' 29 31 if opts.file: 30 32 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)) 32 37 except EnvironmentError, e: 33 38 sys.exit("Can't read %s: %s" % (opts.file, e)) … … 62 67 sys.exit("Bad response. %s" % e.message) 63 68 69 if len(svcs) > 0 : 70 comments = '<!--\n%s\n-->' % '\n'.join(svcs) 71 else: 72 comments = '' 73 64 74 if opts.outfile: 65 75 try: 66 76 f = open(opts.outfile, "w") 77 print >>f, comments 67 78 print >>f, topdl.topology_to_xml(top, top="experiment") 68 79 f.close() … … 70 81 sys.exit("Can't write to %s: %s" % (opts.outfile, e)) 71 82 else: 83 print comments 72 84 print topdl.topology_to_xml(top, top="experiment") 73 85 proof = proof.from_dict(resp_dict.get('proof', {})) -
fedd/federation/client_lib.py
rba07149 rc259a77 4 4 import pwd 5 5 import os 6 import re 6 7 import os.path 8 import xml.parsers.expat 7 9 8 10 from string import join 9 11 from datetime import datetime 10 11 12 12 13 from deter import fedid … … 137 138 return rv 138 139 140 ns_service_re = re.compile('^\\s*#\\s*SERVICE:\\s*([\\S]+)') 141 xml_service_re = re.compile('SERVICE:\\s*([\\S]+)\\s*$') 142 143 def 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 174 def 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 191 def 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 139 223 def wrangle_standard_options(opts): 140 224 """
Note: See TracChangeset
for help on using the changeset viewer.