Package Ganga :: Package Utility :: Module Runtime
[hide private]
[frames] | no frames]

Source Code for Module Ganga.Utility.Runtime

  1  # 05 Aug 2005 - KH : Added functions getSearchPath and getScriptPath 
  2   
  3  # 16 Aug 2005 - KH : Added method RuntimePackage.loadTemplates 
  4   
  5  # 30 Aug 2006 - KH : Modified function getSearchPath, to expand ~ and 
  6  #                    environment variables, and to allow paths to be 
  7  #                    specified relative to Ganga top directory 
  8   
  9  # 19 Oct 2006 - KH : Modified function getScriptPath, to expand ~ and 
 10  #                    environment variables 
 11   
 12  # 19 Oct 2006 - KH : Generalised function getSearchPath, allowing 
 13  #                    configuration parameter defining search path 
 14  #                    to be passed as arument 
 15   
 16  from Ganga.Utility.util import importName 
 17   
 18  from Ganga.Utility.external.ordereddict import oDict 
 19  allRuntimes = oDict() 
 20   
 21  import Ganga.Utility.logging 
 22  logger = Ganga.Utility.logging.getLogger(modulename=1) 
 23   
24 -def getScriptPath( name = "", searchPath = "" ):
25 """Determine path to a script 26 27 Arguments: 28 name - Name of a script 29 searchPath - String of colon-separated directory names, 30 defining locations to be searched for script 31 32 If 'name' already gives the path to the script, then 33 'searchPath' is ignored. 34 35 Return value: String giving path to script (success) 36 or empty string (script not found)""" 37 38 import os 39 40 scriptPath = "" 41 42 if name: 43 fullName = os.path.expanduser( os.path.expandvars( name ) ) 44 if os.path.exists( fullName ): 45 scriptPath = fullName 46 else: 47 if searchPath: 48 script = os.path.basename( fullName ) 49 pathList = searchPath.split( ":" ) 50 for directory in pathList : 51 if os.path.isdir( directory ): 52 dirPath = os.path.abspath( directory ) 53 if script in os.listdir( dirPath ): 54 scriptPath = os.sep.join( [ dirPath, script ] ) 55 break 56 57 return scriptPath
58
59 -def getSearchPath( configPar = "SCRIPTS_PATH" ):
60 """Determine search path from configuration parameter 61 62 Argument: 63 configPar : Name of configuration parameter defining search path 64 65 Return value: Search path""" 66 67 import os 68 from Ganga.Utility.Config import Config, ConfigError, getConfig 69 70 config = getConfig( "Configuration" ) 71 72 utilityDir = os.path.dirname( os.path.abspath( __file__ ) ) 73 gangaRoot = os.path.dirname( os.path.dirname( utilityDir ) ) 74 75 pathString1 = "" 76 if configPar: 77 try: 78 pathString1 = str( config[ configPar ] ) 79 except ConfigError: 80 logger.error( "Option '%s' not defined in 'Configuration'" % \ 81 configPar ) 82 83 # always have . in the path in the first position! 84 pathList1 = ['.']+pathString1.split( ":" ) 85 pathList2 = [] 86 87 for path in pathList1: 88 if ( "." != path ): 89 path = os.path.expanduser( os.path.expandvars( path ) ) 90 if ( 0 != path.find( "/" ) ): 91 path = os.path.join( gangaRoot, path ) 92 pathList2.append( path ) 93 94 pathString2 = ":".join( pathList2 ) 95 return pathString2
96
97 -class RuntimePackage:
98 - def __init__(self,path):
99 import os.path,sys 100 import Ganga.Utility.Config 101 102 self.path = os.path.normpath(path.rstrip('/')) 103 self.name = os.path.basename(self.path) 104 self.syspath = os.path.dirname(self.path) 105 self.mod = None 106 self.modpath = '' 107 108 showpath = self.syspath 109 if not showpath: 110 showpath = '<defaultpath>' 111 logger.info("initializing runtime: '%s' '%s'",self.name,showpath) 112 113 if allRuntimes.has_key(self.name): 114 if allRuntimes[self.name].path != self.path: 115 logger.warning('possible clash: runtime "%s" already exists at path "%s"',self.name,allRuntimes[self.name].path) 116 117 allRuntimes[self.name] = self 118 119 if self.syspath: 120 # FIXME: not sure if I really want to modify sys.path (side effects!!) 121 #allow relative paths to GANGA_PYTHONPATH 122 if not os.path.isabs(self.syspath): 123 self.syspath = os.path.join(Ganga.Utility.Config.getConfig('System')['GANGA_PYTHONPATH'], self.syspath) 124 sys.path.insert(0,self.syspath) 125 126 self.config = {} #Ganga.Utility.Config.getConfig('Runtime_'+self.name) 127 128 try: 129 self.mod = __import__(self.name) 130 self.modpath = os.path.dirname(os.path.normpath(os.path.abspath(self.mod.__file__))) 131 if self.syspath: 132 if self.modpath.find(self.syspath) == -1: 133 logger.warning("runtime '%s' imported from '%s' but specified path is '%s'. You might be getting different code than expected!",self.name,self.modpath,self.syspath) 134 else: 135 logger.info("runtime package %s imported from %s",self.name,self.modpath) 136 137 # import the <PACKAGE>/PACKAGE.py module 138 # @see Ganga/PACKAGE.py for description of this magic module 139 ## in this way we enforce any initialization of module is performed 140 ## (e.g PackageSetup.setPlatform() is called) 141 __import__(self.name+".PACKAGE") 142 143 except ImportError,x: 144 logger.warning("cannot import runtime package %s: %s",self.name,str(x))
145
146 - def getEnvironment(self):
147 # FIXME: pass the configuration object 148 g = importName(self.name,'getEnvironment') 149 if g: 150 return g(self.config) 151 else: 152 logger.debug("no environment defined for runtime package %s",self.name) 153 return {}
154
155 - def loadPlugins(self):
156 g = importName(self.name,'loadPlugins') 157 if g: g(self.config) 158 else: 159 logger.debug("no plugins defined for runtime package %s",self.name)
160
161 - def bootstrap(self,globals):
162 try: 163 import os.path 164 # import fixes logging problem: the logger belongs to the original 165 # packages rather than to the package defined by the "globals" 166 # namespace (Ganga.GPI) 167 ##exec("from %s.BOOT import *"%self.name,globals) 168 169 # do not import names from BOOT file automatically, use exportToGPI() function explicitly 170 exec("import %s.BOOT"%self.name) 171 except ImportError, x: 172 logger.debug("problems with bootstrap of runtime package %s",self.name) 173 logger.debug(x) 174 except IOError,x: 175 logger.debug("problems with bootstrap of runtime package %s",self.name) 176 logger.debug(x)
177
178 - def loadTemplates( self, globals ):
179 try: 180 import os.path 181 execfile( os.path.join( self.modpath, 'TEMPLATES.py' ), globals ) 182 except IOError, x: 183 logger.debug\ 184 ( "Problems adding templates for runtime package %s", self.name) 185 logger.debug(x)
186
187 - def shutdown(self):
188 g = importName(self.name,'shutdown') 189 if g: g() 190 else: 191 logger.debug("no shutdown procedure in runtime package %s",self.name)
192 193 # this hook is called after the Ganga bootstrap procedure completed
194 - def postBootstrapHook(self):
195 g = importName(self.name,'postBootstrapHook') 196 if g: g() 197 else: 198 logger.debug("no postBootstrapHook() in runtime package %s",self.name)
199