Package iceprod :: Package core :: Module translator
[hide private]
[frames] | no frames]

Source Code for Module iceprod.core.translator

  1  #! /usr/bin/env python 
  2  # 
  3   
  4  """ 
  5     @version: $Revision: $ 
  6     @date: $Date: $ 
  7     @author: Juan Carlos Diaz Velez <juancarlos@icecube.wisc.edu> 
  8  """ 
  9   
 10  import sys,os 
 11  import string 
 12  import time 
 13  import re,getopt 
 14  import glob 
 15  import lex 
 16  from dataclasses import * 
 17  from xmlparser import IceTrayXMLParser 
 18  from xmlwriter import IceTrayXMLWriter 
 19  from os.path import expandvars 
 20  import logging 
 21  import iceprod.core.logger 
 22   
23 -class LoadLibraryError(Exception):
24 - def __init__(self, value):
25 self.value = value
26
27 - def __str__(self):
28 return repr(self.value)
29
30 -class UndefinedVariableError(Exception):
31 - def __init__(self, value):
32 self.value = value
33
34 - def __str__(self):
35 return repr(self.value)
36 37
38 -class Translator:
39 40 loaded_libs = {} 41
42 - def __init__(self,steering,outfile,opts):
43 self.steering = steering 44 self.baseurl = 'http://x2100.icecube.wisc.edu/downloads' 45 self.opts = opts 46 self.pdburl = None 47 self.sysopts = self.GetSystemDefaults(steering) 48 self.paramdict = self.steering.get_param_dict() 49 self.expparser = lex.ExpParser(self.opts,self.steering,noeval=True) 50 self.output_fd = outfile 51 self.logger = logging.getLogger('Translator')
52
53 - def SetURL(self,url):
54 self.pdburl = url
55
56 - def fetch_dependencies(self,indent=''):
57 58 from iceprod.core.functions import isurl 59 deps = [] 60 for d in self.steering.GetDependencies(): 61 fetchurl = d.name 62 if not isurl(fetchurl): fetchurl = "$args(fetch)/%s" % fetchurl 63 deps.append('"%s"' % fetchurl) 64 if not deps: return 65 print >> self.output_fd, "# Fetch dependencies" 66 print >> self.output_fd, "from iceprod.core.functions import wget" 67 print >> self.output_fd, "if cmd_opts['tray'] == 0 and cmd_opts['iter'] == 0:" 68 69 print >> self.output_fd, indent + "dependencies = [\n\t%s\n\t]" % ",\n\t".join(deps) 70 print >> self.output_fd, indent + "for d in dependencies:" 71 print >> self.output_fd, indent + " if wget(exparser.parse(d)):" 72 print >> self.output_fd, indent + " print 'failed to retrieve', d" 73 print >> self.output_fd, indent + " os._exit(1)" 74 print >> self.output_fd, indent + " file = os.path.basename(exparser.parse(d)) " 75 print >> self.output_fd, indent + " if file.endswith('.tgz') or file.endswith('tar.gz'): " 76 print >> self.output_fd, indent + " os.system('tar xzf ' + file) " 77 return
78
79 - def load_library(self,library,tray,indent=''):
80 """ 81 Load library. If library depends on other libraries, 82 recursively load them first (can't have closed loops) 83 @param library: library to be loaded 84 """ 85 for dep_name in library.GetDependencies(): 86 dependency = tray.GetProject(dep_name) 87 if dependency: 88 depname = 'lib'+dependency.GetName() 89 if self.loaded_libs.has_key(depname): 90 continue 91 else: 92 self.load_library(dependency,tray,indent) 93 else: 94 raise LoadLibraryError, "could not load dependency \'%s\'" % dep_name 95 96 libname = 'lib'+library.GetName() 97 if not libname in self.loaded_libs.keys(): 98 print >> self.output_fd, "%sload(\"%s\")" % (indent,str(libname)) 99 self.loaded_libs[libname] = True
100 101
102 - def LoadLibraries(self,tray,indent=''):
103 """ 104 Load libraries into memory 105 """ 106 107 # We have the library names so load them... 108 # ...but prevent loading the same library (from service elements) 109 print >> self.output_fd, indent + "# Load libraries " 110 self.loaded_libs = {} 111 metaprojects = tray.GetMetaProjectList() 112 for mp in metaprojects: 113 projects = mp.GetProjectList() 114 for lib in projects: 115 self.load_library(lib,tray,indent) 116 117 projects = tray.GetProjectList() 118 for lib in projects: 119 self.load_library(lib,tray,indent)
120
121 - def ClearLibraries(self):
122 """ 123 Remove libraries from dictionary 124 """ 125 self.loaded_libs = {}
126
127 - def format_string(self,pstring,pformat):
128 """ 129 Format string using the arguments in pformat 130 """ 131 return_string = pstring 132 if not pformat: 133 return return_string 134 135 try: 136 fstrings = re.findall(r'\%[0-9]*.{0,1}[0-9]*[csridufegExXo]',pstring) 137 pformat = map(self.cast_string,fstrings,pformat) 138 return_string = pstring % tuple(pformat) 139 except TypeError,e: 140 self.logger.error( str(e) ) 141 self.logger.error('Unable to format string parameter ' \ 142 + pstring + ' with args ' +str(pformat)) 143 excinfo = sys.exc_info() 144 sys.excepthook(excinfo[0],excinfo[1],excinfo[2]) 145 146 return return_string
147
148 - def cast_string(self,fstring,arg):
149 if not fstring: return arg 150 if fstring.endswith('c'): return str(arg) 151 elif fstring.endswith('s'): return str(arg) 152 elif fstring.endswith('r'): return repr(arg) 153 elif fstring.endswith('i'): return int(arg) 154 elif fstring.endswith('d'): return int(arg) 155 elif fstring.endswith('u'): return int(arg) 156 elif fstring.endswith('f'): return float(arg) 157 elif fstring.endswith('e'): return float(arg) 158 elif fstring.endswith('g'): return float(arg) 159 elif fstring.endswith('E'): return float(arg) 160 elif fstring.endswith('x'): return int('0x%s' % arg, 0) 161 elif fstring.endswith('X'): return int('0x%s' % arg, 0) 162 elif fstring.endswith('o'): return int('0%s' % arg, 0) 163 else: 164 self.logger.warn('Unable to cast %s using format %s' (arg,fstring)) 165 return arg
166
167 - def GetSystemDefaults(self,steering):
168 """ 169 Generate defaul system options 170 @return: dictonary with default sysopts 171 """ 172 steering.AddSysOpt(SysOpt("platform","$PLATFORM")) 173 steering.AddSysOpt(SysOpt("rootsys","$ROOTSY")) 174 steering.AddSysOpt(SysOpt("photontablesdir","$PHOTON_TABLES")) 175 steering.AddSysOpt(SysOpt("javahome","$JAVA_HOME")) 176 steering.AddSysOpt(SysOpt("scratch","$SCRATCH")) 177 steering.AddSysOpt(SysOpt("targeturl","$TARGETURL")) 178 steering.AddSysOpt(SysOpt("lib_url","$args(fetch)")) 179 steering.AddSysOpt(SysOpt("globus_proxy","$X509_USER_PROXY")) 180 steering.AddSysOpt(SysOpt("globus_location","$GLOBUS_LOCATION")) 181 steering.AddSysOpt(SysOpt("localcp","False")) 182 steering.AddSysOpt(SysOpt("cache","$PWD/")) 183 return steering.GetSysOpts()
184
185 - def parseval(self,pvalue):
186 """ 187 Parse parameter value string to see if it is an external 188 variable and replace it with its value if found. 189 """ 190 parsedval = "" 191 try: 192 parsedval = self.expparser.parse(pvalue) 193 except: pass 194 195 if parsedval == pvalue: 196 return pvalue 197 else: 198 return "exparser.parse(\"%s\")" % pvalue
199
200 - def EvaluateParameter(self,val,stype):
201 if stype in VectorTypes: 202 retvals = [] 203 vals = map(lambda x: self.EvaluateParameter(x,stype[:-1]),val) 204 for v in vals: 205 if v.startswith('"$glob('): 206 retvals += map(lambda x: '\"%s\"' % x, 207 glob.glob(v[len('"$glob('):len(v)-2])) 208 else: 209 retvals.append(v) 210 if len(",".join(retvals)) > 100: 211 return "[%s]" % ",\n\t\t\t".join(retvals) 212 else: 213 return "[%s]" % ",".join(retvals) 214 215 if stype == 'OMKey': 216 stringid = self.parseval(val.stringid) 217 stringid = self.TypeValue(stringid,'int',None) 218 omid = self.parseval(val.omid) 219 omid = self.TypeValue(omid,'int',None) 220 return "OMKey(%s,%s)" % (stringid,omid) 221 222 pvalue = val.value 223 punit = val.unit 224 pformat = val.format 225 226 if stype == 'string' and not pformat == None: #format string 227 pformat = map(self.parseval,pformat.split(',')) 228 pvalue = self.format_string(pvalue,pformat) 229 230 #parse value string 231 pvalue = self.parseval(pvalue) 232 pvalue = self.TypeValue(pvalue, stype, punit) 233 return str(pvalue)
234 235
236 - def ConfigureServices(self,tray,indent=''):
237 if tray.GetModules(): 238 print >> self.output_fd, "" 239 print >> self.output_fd, indent + "# Configure IceTray services" 240 for serv in tray.GetServices(): 241 sname = serv.GetName() 242 sclass = serv.GetClass() 243 if not serv.GetParameters(): 244 print >> self.output_fd, indent + "tray.AddService(\"%s\",\"%s\")" % (sclass, sname) 245 else: 246 print >> self.output_fd, indent + "tray.AddService(\"%s\",\"%s\")(" % (sclass, sname) 247 248 for param in serv.GetParameters(): 249 # If there is an environmental variable, expand it 250 try: 251 pname = param.GetName() 252 stype = param.GetType() 253 pvalue = param.GetValue() 254 pvalue = self.EvaluateParameter(pvalue,stype) 255 print >> self.output_fd, indent + " (\"%s\",%s)," % (pname,pvalue) 256 except TypeError,e: 257 self.logger.error('%s: Type error thrown: %s' % (pname,str(e))) 258 excinfo = sys.exc_info() 259 sys.excepthook(excinfo[0],excinfo[1],excinfo[2]) 260 os._exit(1) 261 262 print >> self.output_fd, indent + ")" 263 print >> self.output_fd, " "
264 265
266 - def ConfigureModules(self,tray,indent=''):
267 print >> self.output_fd, "" 268 if tray.GetModules(): 269 print >> self.output_fd, indent + "# Configure IceTray modules " 270 for mod in tray.GetModules(): 271 mname = mod.GetName() 272 mclass = mod.GetClass() 273 if not mod.GetParameters(): 274 print >> self.output_fd, indent + "tray.AddModule(\"%s\",\"%s\")" % (mclass, mname) 275 else: 276 print >> self.output_fd, indent + "tray.AddModule(\"%s\",\"%s\")(" % (mclass, mname) 277 278 for param in mod.GetParameters(): 279 try: 280 # If there is an environmental variable, expand it 281 pname = param.GetName() 282 stype = param.GetType() 283 pvalue = param.GetValue() 284 285 pvalue = self.EvaluateParameter(pvalue,stype) 286 print >> self.output_fd, indent + " (\"%s\",%s)," % (pname,pvalue) 287 except TypeError,e: 288 self.logger.error('%s: Type error thrown: %s' % (pname,str(e))) 289 excinfo = sys.exc_info() 290 sys.excepthook(excinfo[0],excinfo[1],excinfo[2]) 291 os._exit(1) 292 print >> self.output_fd, indent + ")" 293 print >> self.output_fd, " "
294
295 - def ConfigurePre(self,tray,indent=''):
296 if tray.GetIceProdPres(): 297 print >> self.output_fd, indent + "# Configure PreTray modules " 298 for mod in tray.GetIceProdPres(): 299 mname = mod.GetName() 300 mclass = mod.GetClass() 301 self.logger.debug("loading class '%s' as module '%s'" % (mclass,mname)) 302 if not mod.GetParameters(): 303 print >> self.output_fd, indent + "pre.AddModule(\"%s\",\"%s\")" % (mclass, mname) 304 else: 305 print >> self.output_fd, indent + "pre.AddModule(\"%s\",\"%s\")(" % (mclass, mname) 306 307 for param in mod.GetParameters(): 308 try: 309 pname = param.GetName() 310 stype = param.GetType() 311 pvalue = param.GetValue() 312 313 pvalue = self.EvaluateParameter(pvalue,stype) 314 print >> self.output_fd, indent + " (\"%s\",%s)," % (pname,pvalue) 315 except TypeError,e: 316 self.logger.error('%s: Type error thrown: %s' % (pname,str(e))) 317 excinfo = sys.exc_info() 318 sys.excepthook(excinfo[0],excinfo[1],excinfo[2]) 319 os._exit(1) 320 print >> self.output_fd, indent + ")" 321 print >> self.output_fd, indent + "pre.SetParser(\"%s\",exparser)" % mname 322 print >> self.output_fd, indent + " " 323 print >> self.output_fd, " "
324
325 - def ConfigurePost(self,tray,indent=''):
326 if tray.GetIceProdPosts(): 327 print >> self.output_fd, indent + "# Configure PostTray modules " 328 for mod in tray.GetIceProdPosts(): 329 mname = mod.GetName() 330 mclass = mod.GetClass() 331 self.logger.debug("loading class '%s' as module '%s'" % (mclass,mname)) 332 if not mod.GetParameters(): 333 print >> self.output_fd, indent + "post.AddModule(\"%s\",\"%s\")" % (mclass, mname) 334 else: 335 print >> self.output_fd, indent + "post.AddModule(\"%s\",\"%s\")(" % (mclass, mname) 336 337 for param in mod.GetParameters(): 338 try: 339 pname = param.GetName() 340 stype = param.GetType() 341 pvalue = param.GetValue() 342 343 pvalue = self.EvaluateParameter(pvalue,stype) 344 print >> self.output_fd, indent + " (\"%s\",%s)," % (pname,pvalue) 345 except TypeError,e: 346 self.logger.error('%s: Type error thrown: %s' % (pname,str(e))) 347 excinfo = sys.exc_info() 348 sys.excepthook(excinfo[0],excinfo[1],excinfo[2]) 349 os._exit(1) 350 print >> self.output_fd, indent + ")" 351 print >> self.output_fd, indent + "post.SetParser(\"%s\",exparser)" % mname 352 print >> self.output_fd, indent + " " 353 print >> self.output_fd, " "
354 355
356 - def ConnectBoxes(self,tray,indent=''):
357 for conn in tray.GetConnections(): 358 outbox = conn.GetOutbox() 359 from_module = outbox.GetModule() 360 inbox = conn.GetInbox() 361 to_module = inbox.GetModule() 362 print >> self.output_fd, indent + "tray.ConnectBoxes(\"%s\",\"%s\",\"%s\")" % \ 363 (from_module, outbox.GetBoxName(), to_module)
364
365 - def units(self,pvalue,punit):
366 """ 367 Parse I3Units 368 @param pvalue: parameter type (string) 369 @param punit: optional parameter I3Unit (string) 370 """ 371 if punit and globals().has_key(punit): 372 return globals()[punit](pvalue) 373 elif not punit == None: 374 return pvalue 375 else: 376 return pvalue
377
378 - def TypeValue(self,pvalue, ptype,punit=None):
379 """ 380 Convert string to a typed value 381 @param pvalue: parameter value (string) 382 @param ptype: parameter type (string) 383 @param punit: optional parameter I3Unit (string) 384 """ 385 386 if not pvalue.startswith('exparser.parse'): 387 if ptype == 'string': 388 if '$' in pvalue: 389 return 'expandvars("%s")' % pvalue 390 else: 391 return '"%s"' % pvalue 392 else: 393 return pvalue 394 395 if ptype == 'bool': 396 return "boolcast(%s)" % pvalue 397 398 if ptype == 'int': 399 return "%s(%s)" % (ptype,pvalue) 400 if ptype == 'long': 401 return "%s(%s)" % (ptype,pvalue) 402 if ptype == 'float' or ptype == 'double': 403 if punit: 404 return "float(%s)*I3Units.%s" % (pvalue,punit) 405 else: 406 return "%s(%s)" % (ptype,pvalue) 407 if ptype == 'string': 408 return "str(%s)" % (pvalue) 409 else: 410 self.logger.error('%s: Unsupported type' % host) 411 sys.exit()
412
413 - def WriteHeader(self, steeringmod=None):
414 """ 415 Basic Header 416 """ 417 418 print >> self.output_fd, "#!/usr/bin/env python" 419 print >> self.output_fd, "# " 420 print >> self.output_fd, "# Code automatically generated by iceprod translator " 421 print >> self.output_fd, "# on %s " % time.strftime("%Y-%m-%dT%H:%M:%S") 422 print >> self.output_fd, "# " 423 print >> self.output_fd, "#" * 70 424 425 print >> self.output_fd, "import os" 426 print >> self.output_fd, "import sys,getopt" 427 print >> self.output_fd, "import logging" 428 print >> self.output_fd, "from os.path import expandvars\n" 429 print >> self.output_fd, "from I3Tray import *" 430 print >> self.output_fd, "import iceprod.modules" 431 print >> self.output_fd, "from iceprod.core import lex " 432 print >> self.output_fd, "from iceprod.core.dataclasses import Steering,Parameter,I3PreTray,I3PostTray,SysOpt " 433 if steeringmod: 434 print >> self.output_fd, "from %s import *" % steeringmod 435 print >> self.output_fd, "" 436
437 - def WriteSteering(self):
438 """ 439 Steering Module 440 """ 441 442 print >> self.output_fd, "logging.basicConfig()" 443 print >> self.output_fd, "" 444 print >> self.output_fd, "" 445 print >> self.output_fd, "# Setup command-line options" 446 print >> self.output_fd, "oplist = [ " 447 print >> self.output_fd, " 'dataset=', 'fetch=', 'key=', 'pyhome=', 'platform=', 'ping=', " 448 print >> self.output_fd, " 'url=', 'seed=', 'randdelay', 'procnum=', 'cache=', 'nproc=', " 449 print >> self.output_fd, " 'nocopy=', 'mktar', 'noclean', 'tray=','iter=' ] " 450 print >> self.output_fd, "" 451 print >> self.output_fd, "# Default values" 452 print >> self.output_fd, "cmd_opts = { " 453 print >> self.output_fd, " 'seed':0, 'procnum':0, 'nproc':1, 'tray':0, 'iter':0, " 454 print >> self.output_fd, " 'dataset': 0, " 455 print >> self.output_fd, " 'fetch': 'http://x2100.icecube.wisc.edu/downloads', " 456 print >> self.output_fd, " } " 457 print >> self.output_fd, "" 458 print >> self.output_fd, "opts,args = getopt.getopt(sys.argv[1:], '', oplist )" 459 print >> self.output_fd, "" 460 print >> self.output_fd, "for o,a in opts: " 461 print >> self.output_fd, " cmd_opts[o.replace('--','')] = a " 462 print >> self.output_fd, "" 463 print >> self.output_fd, "" 464 print >> self.output_fd, "# Add steering parameters " 465 print >> self.output_fd, "steering = Steering()" 466 for p in self.steering.GetParameters(): 467 ptype = p.GetType() 468 pname = p.GetName() 469 pvalue = p.GetValue().replace("\"","\\\"") 470 print >> self.output_fd, """steering.AddParameter("%s","%s","%s")""" % (ptype,pname,pvalue) 471 print >> self.output_fd, "" 472 for opt in self.steering.GetSysOpts(): #Add system-specific options 473 print >> self.output_fd, """steering.AddSysOpt(SysOpt("%s","%s"))""" % (opt.GetName(),opt.GetValue()) 474 print >> self.output_fd, "" 475 print >> self.output_fd, "# Instantiate parameters expression parser " 476 print >> self.output_fd, "exparser = lex.ExpParser(cmd_opts,steering)" 477 print >> self.output_fd, "" 478 print >> self.output_fd, 'def boolcast(s): return s not in ["False","0","","[]"]' 479 print >> self.output_fd, 'double = float' 480 print >> self.output_fd, '' 481 try: 482 self.fetch_dependencies(indent=' ') 483 except Exception,e: 484 self.logger.error( str(e)) 485 excinfo = sys.exc_info() 486 sys.excepthook(excinfo[0],excinfo[1],excinfo[2]) 487 print >> self.output_fd, '' 488 489 490
491 - def Translate(self,itray=-1,indent=''):
492 """ 493 Execute icetray 494 """ 495 if itray < 0: 496 itray = int(self.opts['tray']) 497 else: 498 self.opts['tray'] = itray 499 500 tray = self.steering.GetTrays()[itray] 501 max_iter = int(tray.GetIterations()) -1 502 self.opts['max_iter'] = max_iter 503 prev_max_iter = 1 504 if itray > 0: 505 prev_max_iter = int(self.steering.GetTrays()[itray-1].GetIterations())-1 506 self.opts['prev_max_iter'] = prev_max_iter 507 508 print >> self.output_fd, indent + '##'+'#'*30+' Tray %d '%itray+'#'*30 509 510 try: 511 self.LoadLibraries(tray,indent) 512 except LoadLibraryError,e: 513 self.logger.error( 'tray %u: %s' % ( itray, str(e) )) 514 515 if tray.GetModules(): 516 print >> self.output_fd, indent + "# Instantiate a tray " 517 print >> self.output_fd, indent + "tray = I3Tray()" 518 print >> self.output_fd, indent + "" 519 if tray.GetIceProdPres(): 520 print >> self.output_fd, indent + "# Pre-processing" 521 print >> self.output_fd, indent + "pre = I3PreTray()" 522 print >> self.output_fd, indent + "" 523 if tray.GetIceProdPosts(): 524 print >> self.output_fd, indent + "# Post-processing" 525 print >> self.output_fd, indent + "post = I3PostTray()\n" 526 527 try: 528 self.ConfigurePre(tray,indent) 529 except UndefinedVariableError,e: 530 self.logger.error( 'tray %u: %s' % ( itray, str(e) )) 531 532 if tray.GetIceProdPres(): 533 print >> self.output_fd, indent + "" 534 print >> self.output_fd, indent + "pre.Execute()" 535 print >> self.output_fd, indent + "" 536 537 try: 538 self.ConfigureServices(tray,indent) 539 except UndefinedVariableError,e: 540 self.logger.error( 'tray %u: %s' % ( itray, str(e) )) 541 542 try: 543 self.ConfigureModules(tray,indent) 544 except UndefinedVariableError,e: 545 self.logger.error( 'tray %u: %s' % ( itray, str(e) )) 546 547 self.ConnectBoxes(tray,indent) 548 549 if tray.GetModules(): 550 print >> self.output_fd, indent + "" 551 print >> self.output_fd, indent + "# Execute the Tray" 552 if int(tray.GetEvents()) > 0: 553 print >> self.output_fd, indent + "tray.Execute(%s)" % tray.GetEvents() 554 else: 555 print >> self.output_fd, indent + "tray.Execute()" 556 print >> self.output_fd, indent + "tray.Finish()" 557 print >> self.output_fd, indent + "" 558 559 try: 560 self.ConfigurePost(tray,indent) 561 except UndefinedVariableError,e: 562 self.logger.error( 'tray %u: %s' % ( itray, str(e) )) 563 if tray.GetIceProdPosts(): 564 print >> self.output_fd, indent + "" 565 print >> self.output_fd, indent + "# Execute the PostTray" 566 print >> self.output_fd, indent + "post.Execute()" 567 print >> self.output_fd, indent + "" 568 print >> self.output_fd, indent + "\n\n" 569 570 self.ClearLibraries() 571 572
573 -class CheckConfig(Translator):
574
575 - def __init__(self,steering,opts):
576 577 outfile = open('/dev/null','w') # don't need the output 578 Translator.__init__(self,steering,outfile,opts) 579 self.sysopts = self.GetSystemDefaults(steering) 580 self.expparser = lex.ExpParser(self.opts,self.steering,noeval=False) 581 self.logger = logging.getLogger('CheckConfig')
582
583 - def parseval(self,pvalue):
584 """ 585 Parse parameter value string to see if it is an external 586 variable and replace it with its value if found. 587 """ 588 retval = '' 589 try: 590 retval = self.expparser.parse(pvalue) 591 except Exception,e: 592 raise UndefinedVariableError,str(e) 593 return retval
594