def loadModules(self, *modNames, **userCtx): # Build a list of available modules if not modNames: modNames = {} for mibSource in self.__mibSources: for modName in mibSource.listdir(): modNames[modName] = None modNames = list(modNames.keys()) if not modNames: raise error.MibNotFoundError('No MIB module to load at %s' % (self, )) for modName in modNames: try: self.loadModule(modName, **userCtx) except error.MibNotFoundError: if self.__mibCompiler: debug.logger & debug.flagBld and debug.logger( 'loadModules: calling MIB compiler for %s' % modName) status = self.__mibCompiler.compile( modName, genTexts=self.loadTexts) errs = '; '.join([ hasattr(x, 'error') and str(x.error) or x for x in status.values() if x in ('failed', 'missing') ]) if errs: raise error.MibNotFoundError( '%s compilation error(s): %s' % (modName, errs)) # compilation suceeded, MIB might load now self.loadModule(modName, **userCtx) return self
def loadModule(self, modName, **userCtx): """Load and execute MIB modules as Python code""" for mibSource in self._mibSources: debug.logger & debug.FLAG_BLD and debug.logger( 'loadModule: trying %s at %s' % (modName, mibSource)) try: codeObj, sfx = mibSource.read(modName) except IOError as exc: debug.logger & debug.FLAG_BLD and debug.logger( 'loadModule: read %s from %s failed: ' '%s' % (modName, mibSource, exc)) continue modPath = mibSource.fullPath(modName, sfx) if modPath in self._modPathsSeen: debug.logger & debug.FLAG_BLD and debug.logger( 'loadModule: seen %s' % modPath) break else: self._modPathsSeen.add(modPath) debug.logger & debug.FLAG_BLD and debug.logger( 'loadModule: evaluating %s' % modPath) g = {'mibBuilder': self, 'userCtx': userCtx} try: exec(codeObj, g) except Exception: self._modPathsSeen.remove(modPath) raise error.MibLoadError( 'MIB module "%s" load error: ' '%s' % (modPath, traceback.format_exception(*sys.exc_info()))) self._modSeen[modName] = modPath debug.logger & debug.FLAG_BLD and debug.logger( 'loadModule: loaded %s' % modPath) break if modName not in self._modSeen: raise error.MibNotFoundError( 'MIB file "%s" not found in search path ' '(%s)' % (modName and modName + ".py[co]", ', '.join( [str(x) for x in self._mibSources]))) return self
def loadModule(self, modName, **userCtx): for mibSource in self.__mibSources: debug.logger & debug.flagBld and debug.logger( 'loadModule: trying %s at %s' % (modName, mibSource)) try: modData, sfx = mibSource.read(modName) except IOError: debug.logger & debug.flagBld and debug.logger( 'loadModule: read %s from %s failed: %s' % (modName, mibSource, sys.exc_info()[1])) continue modPath = mibSource.fullPath(modName, sfx) if modPath in self.__modPathsSeen: debug.logger & debug.flagBld and debug.logger( 'loadModule: seen %s' % modPath) break else: self.__modPathsSeen[modPath] = 1 debug.logger & debug.flagBld and debug.logger( 'loadModule: evaluating %s' % modPath) g = {'mibBuilder': self, 'userCtx': userCtx} try: exec(modData, g) except Exception: del self.__modPathsSeen[modPath] raise error.MibLoadError( 'MIB module \"%s\" load error: %s' % (modPath, traceback.format_exception(*sys.exc_info()))) self.__modSeen[modName] = modPath debug.logger & debug.flagBld and debug.logger( 'loadModule: loaded %s' % modPath) break if modName not in self.__modSeen: raise error.MibNotFoundError( 'MIB file \"%s\" not found in search path (%s)' % (modName and modName + ".py[co]", ', '.join( [str(x) for x in self.__mibSources]))) return self
def importSymbols(self, modName, *symNames, **userCtx): if not modName: raise error.SmiError('importSymbols: empty MIB module name') r = () for symName in symNames: if modName not in self.mibSymbols: self.loadModules(modName, **userCtx) if modName not in self.mibSymbols: raise error.MibNotFoundError('No module %s loaded at %s' % (modName, self)) if symName not in self.mibSymbols[modName]: raise error.SmiError('No symbol %s::%s at %s' % (modName, symName, self)) r = r + (self.mibSymbols[modName][symName], ) return r
def unloadModules(self, *modNames): if not modNames: modNames = list(self.mibSymbols.keys()) for modName in modNames: if modName not in self.mibSymbols: raise error.MibNotFoundError('No module %s at %s' % (modName, self)) self.unexportSymbols(modName) del self.__modPathsSeen[self.__modSeen[modName]] del self.__modSeen[modName] debug.logger & debug.flagBld and debug.logger('unloadModules: ' % (modName)) return self
def unloadModules(self, *modNames): if not modNames: modNames = list(self.mibSymbols) for modName in modNames: if modName not in self.mibSymbols: raise error.MibNotFoundError('No module %s at %s' % (modName, self)) self.unexportSymbols(modName) self._modPathsSeen.remove(self._modSeen[modName]) del self._modSeen[modName] debug.logger & debug.FLAG_BLD and debug.logger( 'unloadModules: %s' % modName) return self