def read(self, f): pycTime = pyTime = -1 for pycSfx in BYTECODE_SUFFIXES: try: pycData, pycPath = self._getData(f + pycSfx, 'rb') except IOError: why = sys.exc_info()[1] if ENOENT == -1 or why.errno == ENOENT: debug.logger & debug.flagBld and debug.logger( 'file %s access error: %s' % (f + pycSfx, why)) else: raise error.MibLoadError('MIB file %s access error: %s' % (f + pycSfx, why)) else: if PY_MAGIC_NUMBER == pycData[:4]: pycData = pycData[4:] pycTime = struct.unpack('<L', pycData[:4])[0] pycData = pycData[4:] debug.logger & debug.flagBld and debug.logger( 'file %s mtime %d' % (pycPath, pycTime)) break else: debug.logger & debug.flagBld and debug.logger( 'bad magic in %s' % pycPath) for pySfx in SOURCE_SUFFIXES: try: pyTime = self._getTimestamp(f + pySfx) except IOError: why = sys.exc_info()[1] if ENOENT == -1 or why.errno == ENOENT: debug.logger & debug.flagBld and debug.logger( 'file %s access error: %s' % (f + pySfx, why)) else: raise error.MibLoadError('MIB file %s access error: %s' % (f + pySfx, why)) else: debug.logger & debug.flagBld and debug.logger( 'file %s mtime %d' % (f + pySfx, pyTime)) break if pycTime != -1 and pycTime >= pyTime: return marshal.loads(pycData), pycSfx if pyTime != -1: modData, pyPath = self._getData(f + pySfx, 'r') return compile(modData, pyPath, 'exec'), pyPath raise IOError(ENOENT, 'No suitable module found', f)
def read(self, f): pycTime = pyTime = -1 for pycSfx, pycSfxLen, pycMode in self.__sfx[imp.PY_COMPILED]: try: pycData = self._getData(f + pycSfx, pycMode) except IOError: why = sys.exc_info()[1] if ENOENT == -1 or why.errno == ENOENT: debug.logger & debug.flagBld and debug.logger( 'file %s access error: %s' % (f + pycSfx, why) ) else: raise error.MibLoadError('MIB file %s access error: %s' % (f + pycSfx, why)) else: if self.__magic == pycData[:4]: pycData = pycData[4:] pycTime = struct.unpack('<L', pycData[:4])[0] pycData = pycData[4:] debug.logger & debug.flagBld and debug.logger( 'file %s mtime %d' % (f + pycSfx, pycTime) ) break else: debug.logger & debug.flagBld and debug.logger( 'bad magic in %s' % (f + pycSfx,) ) for pySfx, pySfxLen, pyMode in self.__sfx[imp.PY_SOURCE]: try: pyTime = self._getTimestamp(f + pySfx) except IOError: why = sys.exc_info()[1] if ENOENT == -1 or why.errno == ENOENT: debug.logger & debug.flagBld and debug.logger( 'file %s access error: %s' % (f + pySfx, why) ) else: raise error.MibLoadError('MIB file %s access error: %s' % (f + pySfx, why)) else: debug.logger & debug.flagBld and debug.logger( 'file %s mtime %d' % (f + pySfx, pyTime) ) break if pycTime != -1 and pycTime >= pyTime: # noinspection PyUnboundLocalVariable return marshal.loads(pycData), pycSfx if pyTime != -1: # noinspection PyUnboundLocalVariable return self._getData(f + pySfx, pyMode), pySfx raise IOError(ENOENT, 'No suitable module found', f)
def getMibPath(self): paths = () for mibSource in self.getMibSources(): if isinstance(mibSource, DirMibSource): paths += (mibSource.fullPath(), ) else: raise error.MibLoadError( 'MIB source is not a plain directory: %s' % (mibSource, )) return paths
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 _getData(self, f, mode): p = os.path.join(self._srcName, f) try: if f in os.listdir(self._srcName): # make FS case-sensitive fp = open(p, mode) data = fp.read() fp.close() return data except (IOError, OSError): why = sys.exc_info()[1] if why.errno != ENOENT and ENOENT != -1: raise error.MibLoadError('MIB file %s access error: %s' % (p, why)) raise IOError(ENOENT, 'No such file: %s' % sys.exc_info()[1], p)
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 _init(self): try: p = __import__(self._srcName, globals(), locals(), ['__init__']) if hasattr(p, '__loader__') and hasattr(p.__loader__, '_files'): self.__loader = p.__loader__ self._srcName = self._srcName.replace('.', os.sep) return self elif hasattr(p, '__file__'): # Dir relative to PYTHONPATH return DirMibSource(os.path.split(p.__file__)[0]).init() else: raise error.MibLoadError('%s access error' % (p, )) except ImportError: # Dir relative to CWD return DirMibSource(self._srcName).init()
def read(self, f): pycTime = pyTime = -1 for pycSfx, pycSfxLen, pycMode in self._sfx[imp.PY_COMPILED]: pycFile = f + pycSfx try: pycData, pycPath = self._getData(pycFile, pycMode) except IOError as exc: if ENOENT == -1 or exc.errno == ENOENT: debug.logger & debug.FLAG_BLD and debug.logger( 'file %s access error: %s' % (pycFile, exc)) else: raise error.MibLoadError('MIB file %s access error: %s' % (pycFile, exc)) else: if self._magic == pycData[:4]: pycData = pycData[4:] pycTime = struct.unpack('<L', pycData[:4])[0] pycData = pycData[4:] debug.logger & debug.FLAG_BLD and debug.logger( 'file %s mtime %d' % (pycPath, pycTime)) break else: debug.logger & debug.FLAG_BLD and debug.logger( 'bad magic in %s' % pycPath) for pySfx, pySfxLen, pyMode in self._sfx[imp.PY_SOURCE]: pyFile = f + pySfx try: pyTime = self._getTimestamp(pyFile) except IOError as exc: if ENOENT == -1 or exc.errno == ENOENT: debug.logger & debug.FLAG_BLD and debug.logger( 'file %s access error: %s' % (pyFile, exc)) else: raise error.MibLoadError('MIB file %s access error: %s' % (pyFile, exc)) else: debug.logger & debug.FLAG_BLD and debug.logger( 'file %s mtime %d' % (pyFile, pyTime)) break if pycTime != -1 and pycTime >= pyTime: return marshal.loads(pycData), pycSfx if pyTime != -1: modData, pyPath = self._getData(pyFile, pyMode) return compile(modData, pyPath, 'exec'), pyPath raise IOError(ENOENT, 'No suitable module found', f)
def test_exception_during_loading(self): poller = Poller.__new__(Poller) poller.builder = Mock() poller.builder.loadModules.side_effect = error.MibLoadError() poller.load_mibs(["a"])