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