Пример #1
0
    def getData(self, mibname, zipBlob=None):
        debug.logger & debug.flagReader and debug.logger('looking for MIB %s at %s' % (mibname, self._name))

        if self._pendingError:
            raise self._pendingError

        if not self._members:
            raise error.PySmiReaderFileNotFoundError('source MIB %s not found' % mibname, reader=self)

        for mibalias, mibfile in self.getMibVariants(mibname):

            debug.logger & debug.flagReader and debug.logger('trying MIB %s' % mibfile)

            try:
                refs = self._members[mibfile]

            except KeyError:
                continue

            mibData, mtime = self._readZipFile(refs)

            if not mibData:
                continue

            debug.logger & debug.flagReader and debug.logger(
                'source MIB %s, mtime %s, read from %s/%s' % (mibfile, time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(mtime)), self._name, mibfile)
            )

            if len(mibData) == self.maxMibSize:
                raise IOError('MIB %s/%s too large' % (self._name, mibfile))

            return MibInfo(path='zip://%s/%s' % (self._name, mibfile),
                           file=mibfile, name=mibalias, mtime=mtime), decode(mibData)

        raise error.PySmiReaderFileNotFoundError('source MIB %s not found' % mibname, reader=self)
Пример #2
0
    def fileExists(self, mibname, mtime, rebuild=False):
        if rebuild:
            debug.logger & debug.flagSearcher and debug.logger('pretend %s is very old' % mibname)
            return

        mibname = decode(mibname)
        basename = os.path.join(self._path, mibname)

        for sfx in self.exts:
            f = basename + sfx
            if not os.path.exists(f) or not os.path.isfile(f):
                debug.logger & debug.flagSearcher and debug.logger('%s not present or not a file' % f)
                continue

            try:
                fileTime = os.stat(f)[8]

            except OSError:
                raise error.PySmiSearcherError('failure opening compiled file %s: %s' % (f, sys.exc_info()[1]),
                                               searcher=self)

            debug.logger & debug.flagSearcher and debug.logger(
                'found %s, mtime %s' % (f, time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(fileTime))))

            if fileTime >= mtime:
                raise error.PySmiFileNotModifiedError()

        raise error.PySmiFileNotFoundError('no compiled file %s found' % mibname, searcher=self)
Пример #3
0
    def fileExists(self, mibname, mtime, rebuild=False):
        if rebuild:
            debug.logger & debug.flagSearcher and debug.logger(
                'pretend %s is very old' % mibname)
            return
        mibname = decode(mibname)
        pyfile = os.path.join(self._path, mibname)
        for fmt in imp.PY_COMPILED, imp.PY_SOURCE:
            for pySfx, pyMode in self.suffixes[fmt]:
                f = pyfile + pySfx
                if not os.path.exists(f) or not os.path.isfile(f):
                    debug.logger & debug.flagSearcher and debug.logger(
                        '%s not present or not a file' % f)
                    continue
                if fmt == imp.PY_COMPILED:
                    try:
                        pyData = open(f, pyMode).read(8)
                    except IOError:
                        raise error.PySmiSearcherError(
                            'failure opening compiled file %s: %s' %
                            (f, sys.exc_info()[1]),
                            searcher=self)
                    if pyData[:4] == imp.get_magic():
                        pyData = pyData[4:]
                        pyTime = struct.unpack('<L', pyData[:4])[0]
                        debug.logger & debug.flagSearcher and debug.logger(
                            'found %s, mtime %s' %
                            (f,
                             time.strftime("%a, %d %b %Y %H:%M:%S GMT",
                                           time.gmtime(pyTime))))
                        if pyTime >= mtime:
                            raise error.PySmiFileNotModifiedError()
                        else:
                            raise error.PySmiFileNotFoundError(
                                'older file %s exists %s' % mibname,
                                searcher=self)
                    else:
                        debug.logger & debug.flagSearcher and debug.logger(
                            'bad magic in %s' % f)
                        continue
                else:
                    try:
                        pyTime = os.stat(f)[8]
                    except OSError:
                        raise error.PySmiSearcherError(
                            'failure opening compiled file %s: %s' %
                            (f, sys.exc_info()[1]),
                            searcher=self)

                    debug.logger & debug.flagSearcher and debug.logger(
                        'found %s, mtime %s' %
                        (f,
                         time.strftime("%a, %d %b %Y %H:%M:%S GMT",
                                       time.gmtime(pyTime))))
                    if pyTime >= mtime:
                        raise error.PySmiFileNotModifiedError()

        raise error.PySmiFileNotFoundError('no compiled file %s found' %
                                           mibname,
                                           searcher=self)
Пример #4
0
    def putData(self, mibname, data, comments=[], dryRun=False):
        if dryRun:
            debug.logger & debug.flagWriter and debug.logger('dry run mode')
            return
        if not os.path.exists(self._path):
            try:
                os.makedirs(self._path)
            except OSError:
                raise error.PySmiWriterError(
                    'failure creating destination directory %s: %s' %
                    (self._path, sys.exc_info()[1]),
                    writer=self)

        if comments:
            data = '#\n' + ''.join(['# %s\n' % x
                                    for x in comments]) + '#\n' + data

        pyfile = os.path.join(
            self._path, decode(mibname)) + self.suffixes[imp.PY_SOURCE][0][0]

        try:
            fd, tfile = tempfile.mkstemp(dir=self._path)
            os.write(fd, encode(data))
            os.close(fd)
            os.rename(tfile, pyfile)
        except (OSError, IOError, UnicodeEncodeError):
            exc = sys.exc_info()
            try:
                os.unlink(tfile)
            except OSError:
                pass
            raise error.PySmiWriterError('failure writing file %s: %s' %
                                         (pyfile, exc[1]),
                                         file=pyfile,
                                         writer=self)

        debug.logger & debug.flagWriter and debug.logger(
            'created file %s' % pyfile)

        if self.pyCompile:
            try:
                if sys.version_info[0:2] > (3, 1):
                    py_compile.compile(pyfile,
                                       doraise=True,
                                       optimize=self.pyOptimizationLevel)
                else:
                    py_compile.compile(pyfile, doraise=True)
            except (SyntaxError, py_compile.PyCompileError):
                pass  # XXX
            except:
                try:
                    os.unlink(pyfile)
                except Exception:
                    pass
                raise error.PySmiWriterError('failure compiling %s: %s' %
                                             (pyfile, sys.exc_info()[1]),
                                             file=mibname,
                                             writer=self)

        debug.logger & debug.flagWriter and debug.logger('%s stored' % mibname)
Пример #5
0
    def getData(self, mibname, **options):
        debug.logger & debug.flagReader and debug.logger('looking for MIB %s at %s' % (mibname, self._name))

        if self._pendingError:
            raise self._pendingError

        if not self._members:
            raise error.PySmiReaderFileNotFoundError('source MIB %s not found' % mibname, reader=self)

        for mibalias, mibfile in self.getMibVariants(mibname, **options):

            debug.logger & debug.flagReader and debug.logger('trying MIB %s' % mibfile)

            try:
                refs = self._members[mibfile]

            except KeyError:
                continue

            mibData, mtime = self._readZipFile(refs)

            if not mibData:
                continue

            debug.logger & debug.flagReader and debug.logger(
                'source MIB %s, mtime %s, read from %s/%s' % (mibfile, time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(mtime)), self._name, mibfile)
            )

            if len(mibData) == self.maxMibSize:
                raise IOError('MIB %s/%s too large' % (self._name, mibfile))

            return MibInfo(path='zip://%s/%s' % (self._name, mibfile),
                           file=mibfile, name=mibalias, mtime=mtime), decode(mibData)

        raise error.PySmiReaderFileNotFoundError('source MIB %s not found' % mibname, reader=self)
Пример #6
0
    def getData(self, mibname):
        debug.logger & debug.flagReader and debug.logger(
            '%slooking for MIB %s' % (self._recursive and 'recursively ' or '', mibname))
        for path in self.getSubdirs(self._path, self._recursive,
                                    self._ignoreErrors):
            for mibalias, mibfile in self.getMibVariants(mibname):
                f = os.path.join(decode(path), decode(mibfile))
                debug.logger & debug.flagReader and debug.logger('trying MIB %s' % f)
                if os.path.exists(f) and os.path.isfile(f):
                    try:
                        mtime = os.stat(f)[8]
                        debug.logger & debug.flagReader and debug.logger(
                            'source MIB %s mtime is %s, fetching data...' % (
                                f, time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(mtime))))
                        return MibInfo(path='file://%s' % f, file=mibfile, name=mibalias, mtime=mtime), decode(
                            open(f, mode='rb').read(self.maxMibSize))
                    except (OSError, IOError):
                        debug.logger & debug.flagReader and debug.logger(
                            'source file %s open failure: %s' % (f, sys.exc_info()[1]))
                        if not self._ignoreErrors:
                            raise error.PySmiError('file %s access error: %s' % (f, sys.exc_info()[1]))

                    raise error.PySmiReaderFileNotModifiedError('source MIB %s is older than needed' % f, reader=self)

        raise error.PySmiReaderFileNotFoundError('source MIB %s not found' % mibname, reader=self)
Пример #7
0
    def getData(self, mibname, **kwargs):
        if bool(kwargs.get('genTexts')) != self.genTexts:
            debug.logger & debug.flagBorrower and debug.logger('skipping incompatible borrower %s for file %s' % (self, mibname))
            raise error.PySmiFileNotFoundError(mibname=mibname, reader=self._reader)

        debug.logger & debug.flagBorrower and debug.logger('trying to borrow file %s from %s' % (mibname, self._reader))
        return self._reader.getData(mibname)
Пример #8
0
    def getData(self, mibname, **kwargs):
        if bool(kwargs.get('genTexts')) != self.genTexts:
            debug.logger & debug.flagBorrower and debug.logger(
                'skipping incompatible borrower %s for file %s' % (self, mibname))
            raise error.PySmiFileNotFoundError(mibname=mibname, reader=self._reader)

        debug.logger & debug.flagBorrower and (
            debug.logger('trying to borrow file %s from %s' % (mibname, self._reader))
        )

        return self._reader.getData(mibname)
Пример #9
0
    def putData(self, mibname, data, comments=[], dryRun=False):
        if dryRun:
            debug.logger & debug.flagWriter and debug.logger('dry run mode')
            return

        try:
            self._cbFun(mibname, data, self._cbCtx)
        except Exception:
            raise error.PySmiWriterError('user callback %s failure writing %s: %s' % (self._cbFun, mibname, sys.exc_info()[1]), writer=self)

        debug.logger & debug.flagWriter and debug.logger('user callback for %s succeeded' % mibname)
Пример #10
0
    def putData(self, mibname, data, comments=(), dryRun=False):
        if dryRun:
            debug.logger & debug.flagWriter and debug.logger('dry run mode')
            return
        if not os.path.exists(self._path):
            try:
                os.makedirs(self._path)
            except OSError:
                raise error.PySmiWriterError(
                    'failure creating destination directory %s: %s' % (self._path, sys.exc_info()[1]), writer=self)

        if comments:
            data = '#\n' + ''.join(['# %s\n' % x for x in comments]) + '#\n' + data

        pyfile = os.path.join(self._path, decode(mibname)) + self.suffixes[imp.PY_SOURCE][0][0]

        tfile = None

        try:
            fd, tfile = tempfile.mkstemp(dir=self._path)
            os.write(fd, encode(data))
            os.close(fd)
            os.rename(tfile, pyfile)
        except (OSError, IOError, UnicodeEncodeError):
            exc = sys.exc_info()
            if tfile:
                try:
                    os.unlink(tfile)
                except OSError:
                    pass
            raise error.PySmiWriterError('failure writing file %s: %s' % (pyfile, exc[1]), file=pyfile, writer=self)

        debug.logger & debug.flagWriter and debug.logger('created file %s' % pyfile)

        if self.pyCompile:
            try:
                if sys.version_info[0:2] > (3, 1):
                    # noinspection PyArgumentList
                    py_compile.compile(pyfile, doraise=True, optimize=self.pyOptimizationLevel)
                else:
                    py_compile.compile(pyfile, doraise=True)
            except (SyntaxError, py_compile.PyCompileError):
                pass  # XXX
            except:
                try:
                    os.unlink(pyfile)
                except Exception:
                    pass
                raise error.PySmiWriterError('failure compiling %s: %s' % (pyfile, sys.exc_info()[1]), file=mibname,
                                             writer=self)

        debug.logger & debug.flagWriter and debug.logger('%s stored' % mibname)
Пример #11
0
    def getData(self, mibname, **options):
        debug.logger & debug.flagReader and debug.logger(
            '%slooking for MIB %s' %
            (self._recursive and 'recursively ' or '', mibname))

        for path in self.getSubdirs(self._path, self._recursive,
                                    self._ignoreErrors):

            for mibalias, mibfile in self.getMibVariants(mibname, **options):
                f = os.path.join(decode(path), decode(mibfile))

                debug.logger & debug.flagReader and debug.logger(
                    'trying MIB %s' % f)

                if os.path.exists(f) and os.path.isfile(f):
                    try:
                        mtime = os.stat(f)[8]

                        debug.logger & debug.flagReader and debug.logger(
                            'source MIB %s mtime is %s, fetching data...' %
                            (f,
                             time.strftime("%a, %d %b %Y %H:%M:%S GMT",
                                           time.gmtime(mtime))))

                        fp = open(f, mode='rb')
                        mibData = fp.read(self.maxMibSize)
                        fp.close()

                        if len(mibData) == self.maxMibSize:
                            raise IOError('MIB %s too large' % f)

                        return MibInfo(path='file://%s' % f,
                                       file=mibfile,
                                       name=mibalias,
                                       mtime=mtime), decode(mibData)

                    except (OSError, IOError):
                        debug.logger & debug.flagReader and debug.logger(
                            'source file %s open failure: %s' %
                            (f, sys.exc_info()[1]))

                        if not self._ignoreErrors:
                            raise error.PySmiError('file %s access error: %s' %
                                                   (f, sys.exc_info()[1]))

                    raise error.PySmiReaderFileNotModifiedError(
                        'source MIB %s is older than needed' % f, reader=self)

        raise error.PySmiReaderFileNotFoundError('source MIB %s not found' %
                                                 mibname,
                                                 reader=self)
Пример #12
0
    def genCode(self, ast, symbolTable, **kwargs):
        self.genRules['text'] = kwargs.get('genTexts', False)
        self._rows.clear()
        self._cols.clear()
        self._parentOids.clear()
        self._symsOrder = []
        self._postponedSyms.clear()
        self._importMap.clear()
        self._out = {}  # should be new object, do not use `clear` method
        self.moduleName[0], moduleOid, imports, declarations = ast

        out, importedModules = self.genImports(imports or {})

        for declr in declarations or []:
            if declr:
                clausetype = declr[0]
                classmode = clausetype == 'typeDeclaration'
                self.handlersTable[declr[0]](self, self.prepData(declr[1:], classmode), classmode)

        if self._postponedSyms:
            raise error.PySmiSemanticError('Unknown parents for symbols: %s' % ', '.join(self._postponedSyms))

        for sym in self._parentOids:
            if sym not in self._out and sym not in self._importMap:
                raise error.PySmiSemanticError('Unknown parent symbol: %s' % sym)

        self._out['_symtable_order'] = list(self._symsOrder)
        self._out['_symtable_cols'] = list(self._cols)
        self._out['_symtable_rows'] = list(self._rows)

        debug.logger & debug.flagCodegen and debug.logger(
            'canonical MIB name %s (%s), imported MIB(s) %s, Symbol table size %s symbols' % (
                self.moduleName[0], moduleOid, ','.join(importedModules) or '<none>', len(self._out)))

        return MibInfo(oid=None, name=self.moduleName[0], imported=tuple([x for x in importedModules])), self._out
Пример #13
0
    def buildIndex(self, processedMibs, **options):
        platform_info, user_info = self._get_system_info()

        comments = [
            'Produced by %s-%s at %s' % (packageName, packageVersion, time.asctime()),
            'On host %s platform %s version %s by user %s' % (platform_info[1], platform_info[0],
                                                              platform_info[2], user_info[0]),
            'Using Python version %s' % sys.version.split('\n')[0]
        ]

        try:
            self._writer.putData(
                self.indexFile,
                self._codegen.genIndex(
                    processedMibs,
                    comments=comments,
                    old_index_data=self._writer.getData(self.indexFile)
                ),
                dryRun=options.get('dryRun')
            )
        except error.PySmiError:
            exc_class, exc, tb = sys.exc_info()
            exc.msg += ' at MIB index %s' % self.indexFile

            debug.logger & debug.flagCompiler and debug.logger('error %s when building %s' % (exc, self.indexFile))

            if options.get('ignoreErrors'):
                return

            if hasattr(exc, 'with_traceback'):
                raise exc.with_traceback(tb)
            else:
                raise exc
Пример #14
0
    def fileWrite(self, fileName, data, comments=[], dryRun=False):
        fileName = fileName.replace('-','_')
        if dryRun:
            debug.logger & debug.flagWriter and debug.logger('dry run mode')
            return
        if not os.path.exists(self._path):
            try:
                os.makedirs(self._path)
            except OSError:
                raise error.PySmiWriterError('failure creating destination directory %s: %s' % (self._path, sys.exc_info()[1]), writer=self)
        if comments:
            data = '//\n' + ''.join(['//%s\n'% x for x in comments]) + '//\n' + data
        fileName = os.path.join(self._path,decode(fileName))

        try:
            fd, tfile = tempfile.mkstemp(dir = self._path)
            os.write(fd, encode(data))
            os.close(fd)
            if(os.path.isfile(fileName)):
                os.remove(fileName)
            os.rename(tfile, fileName)
        except (OSError, IOError, UnicodeEncodeError):
            exc = sys.exc_info()
            try:
                os.unlink(tfile)
            except OSError:
                pass
            raise error.PySmiWriterError('failure writing file %s: %s' % (fileName, exc[1]),file=fileName,write=self)
Пример #15
0
    def genCode(self, ast, symbolTable, **kwargs):
        mibInfo, context = IntermediateCodeGen.genCode(self, ast, symbolTable,
                                                       **kwargs)

        # TODO: reduce code duplication with the other codegens

        searchPath = os.path.join(os.path.dirname(__file__), 'templates')

        dstTemplate = kwargs.get('dstTemplate')
        if dstTemplate:
            searchPath.insert(0, os.path.dirname(os.path.abspath(dstTemplate)))

        env = jinja2.Environment(loader=jinja2.FileSystemLoader(searchPath),
                                 trim_blocks=True,
                                 lstrip_blocks=True)

        env.filters['capfirst'] = jfilters.capfirst

        try:
            tmpl = env.get_template(dstTemplate or self.TEMPLATE_NAME)
            text = tmpl.render(mib=context)

        except jinja2.exceptions.TemplateError:
            err = sys.exc_info()[1]
            raise error.PySmiCodegenError(
                'Jinja template rendering error: %s' % err)

        debug.logger & debug.flagCodegen and debug.logger(
            'canonical MIB name %s (%s), imported MIB(s) %s, rendered from '
            '%s, JSON document size %d bytes' %
            (mibInfo.name, mibInfo.identity, ','.join(mibInfo.imported)
             or '<none>', dstTemplate, len(text)))

        return mibInfo, text
Пример #16
0
 def genCode(self, ast, symbolTable, **kwargs):
     self.genRules['text'] = kwargs.get('genTexts', False)
     self.symbolTable = symbolTable
     out = ''
     importedModules = ()
     self._rows.clear()
     self._cols.clear()
     self._exports.clear()
     self._presentedSyms.clear()
     self._importMap.clear()
     self._out.clear()
     self.moduleName[0], moduleOid,imports, declarations = ast
     out, importModules = self.genImports(imports and imports or {})
     for declr in declarations and declarations or []:
         if declr:
             clausetype = declr[0]
             classmode = clausetype == 'typeDeclaration'
             self.handlersTable[declr[0]](self,self.prepData(declr[1:],classmode),classmode)
     for sym in self.symbolTable[self.moduleName[0]]['_symtable_order']:
         if sym not in self._out:
             raise error.PySmiCodegenError('No generated code for symbol %s' % sym)
         out += self._out[sym]
     out += self.genExports()
     if 'comments' in kwargs:
         out = ''.join(['# %s\n' % x for x in kwargs['comments']]) + '#\n' + out
         out = '#\n# NetSnmp MIB module' + out
     debug.logger & debug.flagCodegen and debug.logger('canonical MIB name %s (%s), imported MIB(s) %s, C code size %s bytes' % (self.moduleName[0],moduleOid, ','.join(importedModules) or '<none>', len(out)))
     return MibInfo(oid=None, NameError=self.moduleName[0], importedModules=tuple([x for x in importedModules if x not in fakedMibs])), out
Пример #17
0
 def genCode(self, ast, symbolTable, **kwargs):
     self.genRules["text"] = kwargs.get("genTexts", False)
     self._rows.clear()
     self._cols.clear()
     self._parentOids.clear()
     self._symsOrder = []
     self._postponedSyms.clear()
     self._importMap.clear()
     self._out = {}  # should be new object, do not use `clear` method
     self.moduleName[0], moduleOid, imports, declarations = ast
     out, importedModules = self.genImports(imports and imports or {})
     for declr in declarations and declarations or []:
         if declr:
             clausetype = declr[0]
             classmode = clausetype == "typeDeclaration"
             self.handlersTable[declr[0]](self, self.prepData(declr[1:], classmode), classmode)
     if self._postponedSyms:
         raise error.PySmiSemanticError("Unknown parents for symbols: %s" % ", ".join(self._postponedSyms))
     for sym in self._parentOids:
         if sym not in self._out and sym not in self._importMap:
             raise error.PySmiSemanticError("Unknown parent symbol: %s" % sym)
     self._out["_symtable_order"] = list(self._symsOrder)
     self._out["_symtable_cols"] = list(self._cols)
     self._out["_symtable_rows"] = list(self._rows)
     debug.logger & debug.flagCodegen and debug.logger(
         "canonical MIB name %s (%s), imported MIB(s) %s, Symbol table size %s symbols"
         % (self.moduleName[0], moduleOid, ",".join(importedModules) or "<none>", len(self._out))
     )
     return MibInfo(oid=None, name=self.moduleName[0], imported=tuple([x for x in importedModules])), self._out
Пример #18
0
    def genCode(self, ast, symbolTable, **kwargs):
        mibInfo, context = IntermediateCodeGen.genCode(self, ast, symbolTable, **kwargs)

        # TODO: reduce code duplication with the other codegens

        searchPath = os.path.join(os.path.dirname(__file__), 'templates')

        dstTemplate = kwargs.get('dstTemplate')
        if dstTemplate:
            searchPath.insert(0, os.path.dirname(os.path.abspath(dstTemplate)))

        env = jinja2.Environment(loader=jinja2.FileSystemLoader(searchPath),
                                 trim_blocks=True, lstrip_blocks=True)

        env.filters['capfirst'] = jfilters.capfirst

        try:
            tmpl = env.get_template(dstTemplate or self.TEMPLATE_NAME)
            text = tmpl.render(mib=context)

        except jinja2.exceptions.TemplateError:
            err = sys.exc_info()[1]
            raise error.PySmiCodegenError('Jinja template rendering error: %s' % err)

        debug.logger & debug.flagCodegen and debug.logger(
            'canonical MIB name %s (%s), imported MIB(s) %s, rendered from '
            '%s, JSON document size %d bytes' % (
                mibInfo.name, mibInfo.identity,
                ','.join(mibInfo.imported) or '<none>',
                dstTemplate, len(text)))

        return mibInfo, text
Пример #19
0
    def __init__(self, path, ignoreErrors=True):
        """Create an instance of *ZipReader* serving a ZIP archive.

           Args:
               path (str): path to ZIP archive containing MIB files

           Keyword Args:
               ignoreErrors (bool): ignore ZIP archive access errors
        """
        self._name = path
        self._members = {}
        self._pendingError = None

        try:
            self._members = self._readZipDirectory(fileObj=open(path, 'rb'))

        except Exception:
            debug.logger & debug.flagReader and debug.logger(
                'ZIP file %s open failure: %s' %
                (self._name, sys.exc_info()[1]))

            if not ignoreErrors:
                self._pendingError = error.PySmiError(
                    'file %s access error: %s' %
                    (self._name, sys.exc_info()[1]))
Пример #20
0
 def buildIndex(self, processedMibs, **options):
     comments = [
         'Produced by %s-%s at %s' % (packageName, packageVersion, time.asctime()),
         'On host %s platform %s version %s by user %s' % (hasattr(os, 'uname') and os.uname()[1] or '?', hasattr(os, 'uname') and os.uname()[0] or '?', hasattr(os, 'uname') and os.uname()[2] or '?', hasattr(os, 'getuid') and getpwuid(os.getuid())[0]) or '?',
         'Using Python version %s' % sys.version.split('\n')[0]
     ]
     try:
         self._writer.putData(
             self.indexFile,
             self._codegen.genIndex(
                 dict([(x, x.oid) for x in processedMibs if hasattr(x, 'oid')]),
                 comments=comments
             ),
             dryRun=options.get('dryRun')
         )
     except error.PySmiError:
         exc_class, exc, tb = sys.exc_info()
         exc.msg += ' at MIB index %s' % self.indexFile
         debug.logger & debug.flagCompiler and debug.logger('error %s when building %s' % (exc, self.indexFile))
         if options.get('ignoreErrors'):
             return
         if hasattr(exc, 'with_traceback'):
             raise exc.with_traceback(tb)
         else:
             raise exc
Пример #21
0
 def buildIndex(self, processedMibs, **options):
     comments = [
         'Produced by %s-%s at %s' %
         (packageName, packageVersion, time.asctime()),
         'On host %s platform %s version %s by user %s' %
         (hasattr(os, 'uname') and os.uname()[1]
          or '?', hasattr(os, 'uname') and os.uname()[0]
          or '?', hasattr(os, 'uname') and os.uname()[2] or '?',
          hasattr(os, 'getuid') and getpwuid(os.getuid())[0]) or '?',
         'Using Python version %s' % sys.version.split('\n')[0]
     ]
     try:
         self._writer.putData(self.indexFile,
                              self._codegen.genIndex(dict([
                                  (x, x.oid) for x in processedMibs
                                  if hasattr(x, 'oid')
                              ]),
                                                     comments=comments),
                              dryRun=options.get('dryRun'))
     except error.PySmiError:
         exc_class, exc, tb = sys.exc_info()
         exc.msg += ' at MIB index %s' % self.indexFile
         debug.logger & debug.flagCompiler and debug.logger(
             'error %s when building %s' % (exc, self.indexFile))
         if options.get('ignoreErrors'):
             return
         if hasattr(exc, 'with_traceback'):
             raise exc.with_traceback(tb)
         else:
             raise exc
Пример #22
0
    def buildIndex(self, processedMibs, **options):
        platform_info, user_info = self._get_system_info()

        comments = [
            'Produced by %s-%s at %s' %
            (packageName, packageVersion, time.asctime()),
            'On host %s platform %s version %s by user %s' %
            (platform_info[1], platform_info[0], platform_info[2],
             user_info[0]),
            'Using Python version %s' % sys.version.split('\n')[0]
        ]

        try:
            self._writer.putData(self.indexFile,
                                 self._codegen.genIndex(
                                     processedMibs,
                                     comments=comments,
                                     old_index_data=self._writer.getData(
                                         self.indexFile)),
                                 dryRun=options.get('dryRun'))
        except error.PySmiError:
            exc_class, exc, tb = sys.exc_info()
            exc.msg += ' at MIB index %s' % self.indexFile

            debug.logger & debug.flagCompiler and debug.logger(
                'error %s when building %s' % (exc, self.indexFile))

            if options.get('ignoreErrors'):
                return

            if hasattr(exc, 'with_traceback'):
                raise exc.with_traceback(tb)
            else:
                raise exc
Пример #23
0
    def getData(self, mibname, **options):
        debug.logger & debug.flagReader and debug.logger('calling user callback %s for MIB %s' % (self._cbFun, mibname))

        res = self._cbFun(mibname, self._cbCtx)
        if res:
            return MibInfo(path='file:///dev/stdin', file='', name=mibname, mtime=time.time()), res

        raise error.PySmiReaderFileNotFoundError(mibname=mibname, reader=self)
Пример #24
0
 def parse(self, data, **kwargs):
     debug.logger & debug.flagParser and debug.logger('source MIB size is %s characters, first 50 characters are "%s..."' % (len(data), data[:50]))
     ast = self.parser.parse(data, lexer=self.lexer.lexer)
     self.reset()
     if ast and ast[0] == 'mibFile' and ast[1]:   # mibfile is not empty
         return ast[1]
     else:
         return []
Пример #25
0
    def fileExists(self, mibname, mtime, rebuild=False):
        if mibname in self._mibnames:
            debug.logger & debug.flagSearcher and debug.logger('pretend compiled %s exists and is very new' % mibname)
            raise error.PySmiFileNotModifiedError('compiled file %s is among %s' % (mibname, ', '.join(self._mibnames)),
                                                  searcher=self)

        raise error.PySmiFileNotFoundError('no compiled file %s found among %s' % (mibname, ', '.join(self._mibnames)),
                                           searcher=self)
Пример #26
0
    def getData(self, mibname):
        debug.logger & debug.flagReader and debug.logger('calling user callback %s for MIB %s' % (self._cbFun, mibname))

        res = self._cbFun(mibname, self._cbCtx)
        if res:
            return MibInfo(path='file:///dev/stdin', file='', name=mibname, mtime=time.time()), res

        raise error.PySmiReaderFileNotFoundError(mibname=mibname, reader=self)
Пример #27
0
    def putData(self, mibname, data, comments=(), dryRun=False):
        if dryRun:
            debug.logger & debug.flagWriter and debug.logger('dry run mode')
            return

        if not os.path.exists(self._path):
            try:
                os.makedirs(self._path)

            except OSError:
                raise error.PySmiWriterError(
                    'failure creating destination directory %s: %s' %
                    (self._path, sys.exc_info()[1]),
                    writer=self)

        if comments:
            data = '#\n' + ''.join(['# %s\n' % x
                                    for x in comments]) + '#\n' + data

        filename = os.path.join(self._path, decode(mibname)) + self.suffix

        tfile = None

        try:
            fd, tfile = tempfile.mkstemp(dir=self._path)
            os.write(fd, encode(data))
            os.close(fd)
            os.rename(tfile, filename)

        except (OSError, IOError, UnicodeEncodeError):
            exc = sys.exc_info()
            if tfile:
                try:
                    os.unlink(tfile)

                except OSError:
                    pass

            raise error.PySmiWriterError('failure writing file %s: %s' %
                                         (filename, exc[1]),
                                         file=filename,
                                         writer=self)

        debug.logger & debug.flagWriter and debug.logger('%s stored in %s' %
                                                         (mibname, filename))
Пример #28
0
    def getData(self, mibname):
        if self._ssl:
            conn = ftplib.FTP_TLS()
        else:
            conn = ftplib.FTP()

        try:
            conn.connect(self._host, self._port, self._timeout)
        except ftplib.all_errors:
            raise error.PySmiReaderFileNotFoundError(
                'failed to connect to FTP server %s:%s: %s' % (self._host, self._port, sys.exc_info()[1]), reader=self)

        try:
            conn.login(self._user, self._password)
        except ftplib.all_errors:
            conn.close()
            raise error.PySmiReaderFileNotFoundError('failed to log in to FTP server %s:%s as %s/%s: %s' % (
            self._host, self._port, self._user, self._password, sys.exc_info()[1]), reader=self)

        mibname = decode(mibname)

        debug.logger & debug.flagReader and debug.logger('looking for MIB %s' % mibname)

        for mibalias, mibfile in self.getMibVariants(mibname):
            location = self._locationTemplate.replace('@mib@', mibfile)
            mtime = time.time()
            debug.logger & debug.flagReader and debug.logger(
                'trying to fetch MIB %s from %s:%s' % (location, self._host, self._port))
            data = []
            try:
                try:
                    response = conn.sendcmd('MDTM %s' % location)
                except ftplib.all_errors:
                    debug.logger & debug.flagReader and debug.logger(
                        'server %s:%s does not support MDTM command, fetching file %s' % (
                        self._host, self._port, location))
                else:
                    debug.logger & debug.flagReader and debug.logger(
                        'server %s:%s MDTM response is %s' % (self._host, self._port, response))
                    if response[:3] == 213:
                        mtime = time.mktime(time.strptime(response[4:], "%Y%m%d%H%M%S"))
                debug.logger & debug.flagReader and debug.logger('fetching source MIB %s, mtime %s' % (
                location, time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(mtime))))
                conn.retrlines('RETR %s' % location, lambda x, y=data: y.append(x))
            except ftplib.all_errors:
                debug.logger & debug.flagReader and debug.logger(
                    'failed to fetch MIB %s from %s:%s: %s' % (location, self._host, self._port, sys.exc_info()[1]))
                continue

            data = decode('\n'.join(data))

            debug.logger & debug.flagReader and debug.logger('fetched %s bytes in %s' % (len(data), location))

            conn.close()
            return MibInfo(path='ftp://%s%s' % (self._host, location), file=mibfile, name=mibalias, mtime=mtime), data

        conn.close()

        raise error.PySmiReaderFileNotFoundError('source MIB %s not found' % mibname, reader=self)
Пример #29
0
    def fileExists(self, mibname, mtime, rebuild=False):
        if rebuild:
            debug.logger & debug.flagSearcher and debug.logger('pretend %s is very old' % mibname)
            return
        mibname = decode(mibname)
        pyfile = os.path.join(self._path, mibname)
        for fmt in imp.PY_COMPILED, imp.PY_SOURCE:
            for pySfx, pyMode in self.suffixes[fmt]:
                f = pyfile + pySfx
                if not os.path.exists(f) or not os.path.isfile(f):
                    debug.logger & debug.flagSearcher and debug.logger('%s not present or not a file' % f)
                    continue
                if fmt == imp.PY_COMPILED:
                    try:
                        pyData = open(f, pyMode).read(8)
                    except IOError:
                        raise error.PySmiSearcherError('failure opening compiled file %s: %s' % (f, sys.exc_info()[1]),
                                                       searcher=self)
                    if pyData[:4] == imp.get_magic():
                        pyData = pyData[4:]
                        pyTime = struct.unpack('<L', pyData[:4])[0]
                        debug.logger & debug.flagSearcher and debug.logger(
                            'found %s, mtime %s' % (f, time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(pyTime))))
                        if pyTime >= mtime:
                            raise error.PySmiFileNotModifiedError()
                        else:
                            raise error.PySmiFileNotFoundError('older file %s exists' % mibname, searcher=self)
                    else:
                        debug.logger & debug.flagSearcher and debug.logger('bad magic in %s' % f)
                        continue
                else:
                    try:
                        pyTime = os.stat(f)[8]
                    except OSError:
                        raise error.PySmiSearcherError('failure opening compiled file %s: %s' % (f, sys.exc_info()[1]),
                                                       searcher=self)

                    debug.logger & debug.flagSearcher and debug.logger(
                        'found %s, mtime %s' % (f, time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(pyTime))))
                    if pyTime >= mtime:
                        raise error.PySmiFileNotModifiedError()

        raise error.PySmiFileNotFoundError('no compiled file %s found' % mibname, searcher=self)
Пример #30
0
 def parse(self, data, **kwargs):
     debug.logger & debug.flagParser and debug.logger(
         'source MIB size is %s characters, first 50 characters are "%s..."'
         % (len(data), data[:50]))
     ast = self.parser.parse(data, lexer=self.lexer.lexer)
     self.reset()
     if ast and ast[0] == 'mibFile' and ast[1]:  # mibfile is not empty
         return ast[1]
     else:
         return []
Пример #31
0
    def genCode(self, ast, symbolTable, **kwargs):
        self.genRules['text'] = kwargs.get('genTexts', False)
        self.textFilter = kwargs.get('textFilter') or (
            lambda symbol, text: re.sub('\s+', ' ', text))
        self.symbolTable = symbolTable
        self._rows.clear()
        self._cols.clear()
        self._exports.clear()
        self._seenSyms.clear()
        self._importMap.clear()
        self._out.clear()
        self._moduleIdentityOid = None
        self.moduleName[0], moduleOid, imports, declarations = ast

        out, importedModules = self.genImports(imports or {})

        for declr in declarations or []:
            if declr:
                clausetype = declr[0]
                classmode = clausetype == 'typeDeclaration'
                self.handlersTable[declr[0]](self,
                                             self.prepData(
                                                 declr[1:], classmode),
                                             classmode)

        for sym in self.symbolTable[self.moduleName[0]]['_symtable_order']:
            if sym not in self._out:
                raise error.PySmiCodegenError(
                    'No generated code for symbol %s' % sym)
            out += self._out[sym]

        out += self.genExports()

        if 'comments' in kwargs:
            out = ''.join(['# %s\n' % x
                           for x in kwargs['comments']]) + '#\n' + out
            out = '#\n# PySNMP MIB module %s (http://snmplabs.com/pysmi)\n' % self.moduleName[
                0] + out

        debug.logger & debug.flagCodegen and debug.logger(
            'canonical MIB name %s (%s), imported MIB(s) %s, Python code size %s bytes'
            % (self.moduleName[0], moduleOid, ','.join(importedModules)
               or '<none>', len(out)))

        return MibInfo(oid=moduleOid,
                       identity=self._moduleIdentityOid,
                       name=self.moduleName[0],
                       revision=self._moduleRevision,
                       oids=[],
                       enterprise=None,
                       compliance=[],
                       imported=tuple([
                           x for x in importedModules if x not in self.fakeMibs
                       ])), out
Пример #32
0
    def fileExists(self, mibname, mtime, rebuild=False):
        if rebuild:
            debug.logger & debug.flagSearcher and debug.logger('pretend %s is very old' % mibname)
            return
        mibname = decode(mibname)
        try:
            p = __import__(self._package, globals(), locals(), ['__init__'])
            if hasattr(p, '__loader__') and hasattr(p.__loader__, '_files'):
                self.__loader = p.__loader__
                self._package = self._package.replace('.', os.sep)
                debug.logger & debug.flagSearcher and debug.logger(
                    '%s is an importable egg at %s' % (self._package, os.path.split(p.__file__)[0]))
            elif hasattr(p, '__file__'):
                debug.logger & debug.flagSearcher and debug.logger(
                    '%s is not an egg, trying it as a package directory' % self._package)
                return PyFileSearcher(os.path.split(p.__file__)[0]).fileExists(mibname, mtime, rebuild=rebuild)
            else:
                raise error.PySmiFileNotFoundError('%s is neither importable nor a file' % self._package, searcher=self)

        except ImportError:
            raise error.PySmiFileNotFoundError('%s is not importable, trying as a path' % self._package, searcher=self)

        for fmt in imp.PY_COMPILED, imp.PY_SOURCE:
            for pySfx, pyMode in self.suffixes[fmt]:
                f = os.path.join(self._package, mibname.upper()) + pySfx
                if f not in self.__loader._files:
                    debug.logger & debug.flagSearcher and debug.logger('%s is not in %s' % (f, self._package))
                    continue
                if fmt == imp.PY_COMPILED:
                    pyData = self.__loader.get_data(f)
                    if pyData[:4] == imp.get_magic():
                        pyData = pyData[4:]
                        pyTime = struct.unpack('<L', pyData[:4])[0]
                        debug.logger & debug.flagSearcher and debug.logger(
                            'found %s, mtime %s' % (f, time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(pyTime))))
                        if pyTime >= mtime:
                            raise error.PySmiFileNotModifiedError()
                        else:
                            raise error.PySmiFileNotFoundError('older file %s exists' % mibname, searcher=self)
                    else:
                        debug.logger & debug.flagSearcher and debug.logger('bad magic in %s' % f)
                        continue
                else:
                    pyTime = self._parseDosTime(
                        self.__loader._files[f][6],
                        self.__loader._files[f][5]
                    )

                    debug.logger & debug.flagSearcher and debug.logger(
                        'found %s, mtime %s' % (f, time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(pyTime))))
                    if pyTime >= mtime:
                        raise error.PySmiFileNotModifiedError()
                    else:
                        raise error.PySmiFileNotFoundError('older file %s exists' % mibname, searcher=self)

        raise error.PySmiFileNotFoundError('no file %s found' % mibname, searcher=self)
Пример #33
0
    def loadIndex(self, indexFile):
        mibIndex = {}
        if os.path.exists(indexFile):
            try:
                mibIndex = dict(
                    [x.split()[:2] for x in open(indexFile).readlines()]
                )
                debug.logger & debug.flagReader and debug.logger('loaded MIB index map from %s file, %s entries' % (indexFile, len(mibIndex)))
            except IOError:
                pass

        return mibIndex
Пример #34
0
 def genIndex(self, mibsMap, **kwargs):
     out = '\nfrom pysnmp.proto.rfc1902 import ObjectName\n\noidToMibMap = {\n'
     count = 0
     for name, oid in mibsMap:
         out += 'ObjectName("%s"): "%s",\n' % (oid, name)
         count += 1
     out += '}\n'
     if 'comments' in kwargs:
         out = ''.join(['# %s\n' % x for x in kwargs['comments']]) + '#\n' + out
         out = '#\n# PySNMP MIB indices (http://pysnmp.sf.net)\n' + out
     debug.logger & debug.flagCodegen and debug.logger('OID->MIB index built, %s entries, %s bytes' % (count, len(out)))
     return out
Пример #35
0
    def loadIndex(self, indexFile):
        mibIndex = {}
        if os.path.exists(indexFile):
            try:
                mibIndex = dict(
                    [x.split()[:2] for x in open(indexFile).readlines()]
                )
                debug.logger & debug.flagReader and debug.logger('loaded MIB index map from %s file, %s entries' % (indexFile, len(mibIndex)))
            except IOError:
                pass

        return mibIndex
Пример #36
0
    def getMibVariants(self, mibname):
        if self.useIndexFile:
            if not self._indexLoaded:
                self._mibIndex = self.loadIndex(
                    os.path.join(self._path, self.indexFile)
                )
                self._indexLoaded = True

            if mibname in self._mibIndex:
                debug.logger & debug.flagReader and debug.logger('found %s in MIB index: %s' % (mibname, self._mibIndex[mibname]))
                return [(mibname, self._mibIndex[mibname])]

        return super(FileReader, self).getMibVariants(mibname)
Пример #37
0
    def fileExists(self, mibname, mtime, rebuild=False):
        if mibname in self._mibnames:
            debug.logger & debug.flagSearcher and debug.logger(
                'pretend compiled %s exists and is very new' % mibname)
            raise error.PySmiFileNotModifiedError(
                'compiled file %s is among %s' %
                (mibname, ', '.join(self._mibnames)),
                searcher=self)

        raise error.PySmiFileNotFoundError(
            'no compiled file %s found among %s' %
            (mibname, ', '.join(self._mibnames)),
            searcher=self)
Пример #38
0
    def getMibVariants(self, mibname):
        if self.useIndexFile:
            if not self._indexLoaded:
                self._mibIndex = self.loadIndex(
                    os.path.join(self._path, self.indexFile)
                )
                self._indexLoaded = True

            if mibname in self._mibIndex:
                debug.logger & debug.flagReader and debug.logger('found %s in MIB index: %s' % (mibname, self._mibIndex[mibname]))
                return [(mibname, self._mibIndex[mibname])]

        return super(FileReader, self).getMibVariants(mibname)
Пример #39
0
    def putData(self, mibname, data, comments=(), dryRun=False):
        if dryRun:
            debug.logger & debug.flagWriter and debug.logger('dry run mode')
            return

        if not os.path.exists(self._path):
            try:
                os.makedirs(self._path)

            except OSError:
                raise error.PySmiWriterError(
                    'failure creating destination directory %s: %s' % (self._path, sys.exc_info()[1]), writer=self)

        if comments:
            data = '#\n' + ''.join(['# %s\n' % x for x in comments]) + '#\n' + data

        filename = os.path.join(self._path, decode(mibname)) + self.suffix

        tfile = None

        try:
            fd, tfile = tempfile.mkstemp(dir=self._path)
            os.write(fd, encode(data))
            os.close(fd)
            os.rename(tfile, filename)

        except (OSError, IOError, UnicodeEncodeError):
            exc = sys.exc_info()
            if tfile:
                try:
                    os.unlink(tfile)

                except OSError:
                    pass

            raise error.PySmiWriterError('failure writing file %s: %s' % (filename, exc[1]), file=filename, writer=self)

        debug.logger & debug.flagWriter and debug.logger('%s stored in %s' % (mibname, filename))
Пример #40
0
 def genIndex(self, mibsMap, **kwargs):
     out = '\nfrom pysnmp.proto.rfc1902 import ObjectName\n\noidToMibMap = {\n'
     count = 0
     for name, oid in mibsMap:
         out += 'ObjectName("%s"): "%s",\n' % (oid, name)
         count += 1
     out += '}\n'
     if 'comments' in kwargs:
         out = ''.join(['# %s\n' % x
                        for x in kwargs['comments']]) + '#\n' + out
         out = '#\n# PySNMP MIB indices (http://pysnmp.sf.net)\n' + out
     debug.logger & debug.flagCodegen and debug.logger(
         'OID->MIB index built, %s entries, %s bytes' % (count, len(out)))
     return out
Пример #41
0
    def __init__(self, path, ignoreErrors=True):
        self._name = path
        self._members = {}
        self._pendingError = None

        try:
            self._members = self._readZipDirectory(fileObj=open(path, 'rb'))

        except Exception:
            debug.logger & debug.flagReader and debug.logger(
                'ZIP file %s open failure: %s' % (self._name, sys.exc_info()[1]))

            if not ignoreErrors:
                self._pendingError = error.PySmiError('file %s access error: %s' % (self._name, sys.exc_info()[1]))
Пример #42
0
    def genCode(self, ast, symbolTable, **kwargs):
        self.genRules['text'] = kwargs.get('genTexts', False)
        self.textFilter = kwargs.get('textFilter') or (
            lambda symbol, text: re.sub('\s+', ' ', text))
        self.symbolTable = symbolTable
        self._rows.clear()
        self._cols.clear()
        self._seenSyms.clear()
        self._importMap.clear()
        self._out.clear()
        self._moduleIdentityOid = None
        self._enterpriseOid = None
        self._oids = set()
        self._complianceOids = []
        self.moduleName[0], moduleOid, imports, declarations = ast

        outDict, importedModules = self.genImports(imports and imports or {})

        for declr in declarations or []:
            if declr:
                self.handlersTable[declr[0]](self, self.prepData(declr[1:]))

        for sym in self.symbolTable[self.moduleName[0]]['_symtable_order']:
            if sym not in self._out:
                raise error.PySmiCodegenError(
                    'No generated code for symbol %s' % sym)

            outDict[sym] = self._out[sym]

        outDict['meta'] = OrderedDict()
        outDict['meta']['module'] = self.moduleName[0]

        if 'comments' in kwargs:
            outDict['meta']['comments'] = kwargs['comments']

        debug.logger & debug.flagCodegen and debug.logger(
            'canonical MIB name %s (%s), imported MIB(s) %s' %
            (self.moduleName[0], moduleOid, ','.join(importedModules)
             or '<none>'))

        return MibInfo(oid=moduleOid,
                       identity=self._moduleIdentityOid,
                       name=self.moduleName[0],
                       revision=self._moduleRevision,
                       oids=self._oids,
                       enterprise=self._enterpriseOid,
                       compliance=self._complianceOids,
                       imported=tuple([
                           x for x in importedModules if x not in self.fakeMibs
                       ])), outDict
Пример #43
0
    def fileExists(self, mibname, mtime, rebuild=False):
        if rebuild:
            debug.logger & debug.flagSearcher and debug.logger(
                'pretend %s is very old' % mibname)
            return

        mibname = decode(mibname)
        basename = os.path.join(self._path, mibname)

        for sfx in self.exts:
            f = basename + sfx
            if not os.path.exists(f) or not os.path.isfile(f):
                debug.logger & debug.flagSearcher and debug.logger(
                    '%s not present or not a file' % f)
                continue

            try:
                fileTime = os.stat(f)[8]

            except OSError:
                raise error.PySmiSearcherError(
                    'failure opening compiled file %s: %s' %
                    (f, sys.exc_info()[1]),
                    searcher=self)

            debug.logger & debug.flagSearcher and debug.logger(
                'found %s, mtime %s' %
                (f,
                 time.strftime("%a, %d %b %Y %H:%M:%S GMT",
                               time.gmtime(fileTime))))

            if fileTime >= mtime:
                raise error.PySmiFileNotModifiedError()

        raise error.PySmiFileNotFoundError('no compiled file %s found' %
                                           mibname,
                                           searcher=self)
Пример #44
0
    def getData(self, mibname):
        headers = {'Accept': 'text/plain'}
        if sys.version_info[:2] < (2, 6):
            conn = httplib.HTTPConnection(self._host, self._port)
        else:
            conn = httplib.HTTPConnection(self._host,
                                          self._port,
                                          timeout=self._timeout)

        mibname = decode(mibname)

        debug.logger & debug.flagReader and debug.logger(
            'looking for MIB %s' % mibname)

        for mibalias, mibfile in self.getMibVariants(mibname):
            location = self._locationTemplate.replace('@mib@', mibfile)
            debug.logger & debug.flagReader and debug.logger(
                'trying to fetch MIB from %s://%s:%s%s' %
                (self._schema, self._host, self._port, location))
            try:
                conn.request('GET', location, '', headers)
                response = conn.getresponse()
            except Exception:
                debug.logger & debug.flagReader and debug.logger(
                    'failed to fetch MIB from %s://%s:%s%s: %s' %
                    (self._schema, self._host, self._port, location,
                     sys.exc_info()[1]))
                continue

            debug.logger & debug.flagReader and debug.logger(
                'HTTP response %s' % response.status)

            if response.status == 200:
                try:
                    mtime = time.mktime(
                        time.strptime(response.getheader('Last-Modified'),
                                      "%a, %d %b %Y %H:%M:%S %Z"))
                except Exception:
                    debug.logger & debug.flagReader and debug.logger(
                        'malformed HTTP headers: %s' % sys.exc_info()[1])
                    mtime = time.time()

                debug.logger & debug.flagReader and debug.logger(
                    'fetching source MIB %s, mtime %s' %
                    (location, response.getheader('Last-Modified')))

                return MibInfo(
                    path='%s://%s:%s%s' %
                    (self._schema, self._host, self._port, location),
                    file=mibfile,
                    name=mibalias,
                    mtime=mtime), decode(response.read(self.maxMibSize))

        raise error.PySmiReaderFileNotFoundError('source MIB %s not found' %
                                                 mibname,
                                                 reader=self)
Пример #45
0
    def getData(self, mibname):
        headers = {'Accept': 'text/plain', 'User-Agent': self._user_agent}

        mibname = decode(mibname)

        debug.logger & debug.flagReader and debug.logger(
            'looking for MIB %s' % mibname)

        for mibalias, mibfile in self.getMibVariants(mibname):
            if self.MIB_MAGIC in self._url:
                url = self._url.replace(self.MIB_MAGIC, mibfile)
            else:
                url = self._url + mibfile

            debug.logger & debug.flagReader and debug.logger(
                'trying to fetch MIB from %s' % url)

            try:
                req = Request(url, headers=headers)
                response = urlopen(req)

            except Exception:
                debug.logger & debug.flagReader and debug.logger(
                    'failed to fetch MIB from %s: %s' %
                    (url, sys.exc_info()[1]))
                continue

            debug.logger & debug.flagReader and debug.logger(
                'HTTP response %s' % response.code)

            if response.code == 200:
                try:
                    mtime = time.mktime(
                        time.strptime(response.getheader('Last-Modified'),
                                      "%a, %d %b %Y %H:%M:%S %Z"))

                except Exception:
                    debug.logger & debug.flagReader and debug.logger(
                        'malformed HTTP headers: %s' % sys.exc_info()[1])
                    mtime = time.time()

                debug.logger & debug.flagReader and debug.logger(
                    'fetching source MIB %s, mtime %s' %
                    (url, response.getheader('Last-Modified')))

                return MibInfo(path=url,
                               file=mibfile,
                               name=mibalias,
                               mtime=mtime), decode(
                                   response.read(self.maxMibSize))

        raise error.PySmiReaderFileNotFoundError('source MIB %s not found' %
                                                 mibname,
                                                 reader=self)
Пример #46
0
    def _readZipFile(self, refs):

        for fileObj, filename, mtime in refs:

            if not fileObj:
                fileObj = FileLike(dataObj, name=self._name)

            archive = zipfile.ZipFile(fileObj)

            try:
                dataObj = archive.read(filename)

            except Exception:
                debug.logger & debug.flagReader and debug.logger('ZIP read component %s read error: %s' % (fileObj.name, sys.exc_info()[1]))
                return '', 0

        return dataObj, mtime
Пример #47
0
    def _readZipFile(self, refs):

        for fileObj, filename, mtime in refs:

            if not fileObj:
                fileObj = FileLike(dataObj, name=self._name)

            archive = zipfile.ZipFile(fileObj)

            try:
                dataObj = archive.read(filename)

            except Exception:
                debug.logger & debug.flagReader and debug.logger('ZIP read component %s read error: %s' % (fileObj.name, sys.exc_info()[1]))
                return '', 0

        return dataObj, mtime
Пример #48
0
    def addSources(self, *sources):
        """Add more ASN.1 MIB source repositories.

        MibCompiler.compile will invoke each of configured source objects
        in order of their addition asking each to fetch MIB module specified
        by name.

        Args:
            sources: reader object(s)

        Returns:
            reference to itself (can be used for call chaining)

        """
        self._sources.extend(sources)
        debug.logger & debug.flagCompiler and debug.logger('current MIB source(s): %s' % ', '.join([str(x) for x in self._sources]))
        return self
Пример #49
0
    def addSearchers(self, *searchers):
        """Add more transformed MIBs repositories.

        MibCompiler.compile will invoke each of configured searcher objects
        in order of their addition asking each if already transformed MIB
        module already exists and is more recent than specified.

        Args:
            searchers: searcher object(s)

        Returns:
            reference to itself (can be used for call chaining)

        """
        self._searchers.extend(searchers)
        debug.logger & debug.flagCompiler and debug.logger('current compiled MIBs location(s): %s' % ', '.join([str(x) for x in self._searchers]))
        return self
Пример #50
0
    def addSources(self, *sources):
        """Add more ASN.1 MIB source repositories.

        MibCompiler.compile will invoke each of configured source objects
        in order of their addition asking each to fetch MIB module specified
        by name.

        Args:
            sources: reader object(s)

        Returns:
            reference to itself (can be used for call chaining)

        """
        self._sources.extend(sources)
        debug.logger & debug.flagCompiler and debug.logger('current MIB source(s): %s' % ', '.join([str(x) for x in self._sources]))
        return self
Пример #51
0
    def addSearchers(self, *searchers):
        """Add more transformed MIBs repositories.

        MibCompiler.compile will invoke each of configured searcher objects
        in order of their addition asking each if already transformed MIB
        module already exists and is more recent than specified.

        Args:
            searchers: searcher object(s)

        Returns:
            reference to itself (can be used for call chaining)

        """
        self._searchers.extend(searchers)
        debug.logger & debug.flagCompiler and debug.logger('current compiled MIBs location(s): %s' % ', '.join([str(x) for x in self._searchers]))
        return self
Пример #52
0
    def genIndex(self, processed, **kwargs):
        out = '\nfrom pysnmp.proto.rfc1902 import ObjectName\n\noidToMibMap = {\n'
        count = 0
        for module, status in processed.items():
            value = getattr(status, 'oid', None)
            if value:
                out += 'ObjectName("%s"): "%s",\n' % (value, module)
                count += 1
        out += '}\n'

        if 'comments' in kwargs:
            out = ''.join(['# %s\n' % x for x in kwargs['comments']]) + '#\n' + out
            out = '#\n# PySNMP MIB indices (http://pysnmp.sf.net)\n' + out

        debug.logger & debug.flagCodegen and debug.logger(
            'OID->MIB index built, %s entries, %s bytes' % (count, len(out)))

        return out
Пример #53
0
    def addBorrowers(self, *borrowers):
        """Add more transformed MIBs repositories to borrow MIBs from.

        Whenever MibCompiler.compile encounters MIB module which neither of
        the *searchers* can find or fetched ASN.1 MIB module can not be
        parsed (due to syntax errors), these *borrowers* objects will be
        invoked in order of their addition asking each if already transformed
        MIB can be fetched (borrowed).

        Args:
            borrowers: borrower object(s)

        Returns:
            reference to itself (can be used for call chaining)

        """
        self._borrowers.extend(borrowers)
        debug.logger & debug.flagCompiler and debug.logger('current MIB borrower(s): %s' % ', '.join([str(x) for x in self._borrowers]))
        return self
Пример #54
0
    def addBorrowers(self, *borrowers):
        """Add more transformed MIBs repositories to borrow MIBs from.

        Whenever MibCompiler.compile encounters MIB module which neither of
        the *searchers* can find or fetched ASN.1 MIB module can not be
        parsed (due to syntax errors), these *borrowers* objects will be
        invoked in order of their addition asking each if already transformed
        MIB can be fetched (borrowed).

        Args:
            borrowers: borrower object(s)

        Returns:
            reference to itself (can be used for call chaining)

        """
        self._borrowers.extend(borrowers)
        debug.logger & debug.flagCompiler and debug.logger('current MIB borrower(s): %s' % ', '.join([str(x) for x in self._borrowers]))
        return self
Пример #55
0
 def genCode(self, ast, symbolTable, **kwargs):
     self.genRules['text'] = kwargs.get('genTexts', False)
     self.symbolTable = symbolTable
     out = ''
     importedModules = ()
     self._rows.clear()
     self._cols.clear()
     self._exports.clear()
     self._presentedSyms.clear()
     self._importMap.clear()
     self._out.clear()
     self.moduleName[0], moduleOid, imports, declarations = ast
     out, importModules = self.genImports(imports and imports or {})
     for declr in declarations and declarations or []:
         if declr:
             clausetype = declr[0]
             classmode = clausetype == 'typeDeclaration'
             self.handlersTable[declr[0]](self,
                                          self.prepData(
                                              declr[1:], classmode),
                                          classmode)
     for sym in self.symbolTable[self.moduleName[0]]['_symtable_order']:
         if sym not in self._out:
             raise error.PySmiCodegenError(
                 'No generated code for symbol %s' % sym)
         out += self._out[sym]
     out += self.genExports()
     if 'comments' in kwargs:
         out = ''.join(['# %s\n' % x
                        for x in kwargs['comments']]) + '#\n' + out
         out = '#\n# NetSnmp MIB module' + out
     debug.logger & debug.flagCodegen and debug.logger(
         'canonical MIB name %s (%s), imported MIB(s) %s, C code size %s bytes'
         % (self.moduleName[0], moduleOid, ','.join(importedModules)
            or '<none>', len(out)))
     return MibInfo(oid=None,
                    NameError=self.moduleName[0],
                    importedModules=tuple([
                        x for x in importedModules if x not in fakedMibs
                    ])), out
Пример #56
0
    def __init__(self, path, ignoreErrors=True):
        """Create an instance of *ZipReader* serving a ZIP archive.

           Args:
               path (str): path to ZIP archive containing MIB files

           Keyword Args:
               ignoreErrors (bool): ignore ZIP archive access errors
        """
        self._name = path
        self._members = {}
        self._pendingError = None

        try:
            self._members = self._readZipDirectory(fileObj=open(path, 'rb'))

        except Exception:
            debug.logger & debug.flagReader and debug.logger(
                'ZIP file %s open failure: %s' % (self._name, sys.exc_info()[1]))

            if not ignoreErrors:
                self._pendingError = error.PySmiError('file %s access error: %s' % (self._name, sys.exc_info()[1]))
Пример #57
0
    def getData(self, mibname, **options):
        headers = {
            'Accept': 'text/plain',
            'User-Agent': self._user_agent
        }

        mibname = decode(mibname)

        debug.logger & debug.flagReader and debug.logger('looking for MIB %s' % mibname)

        for mibalias, mibfile in self.getMibVariants(mibname, **options):
            if self.MIB_MAGIC in self._url:
                url = self._url.replace(self.MIB_MAGIC, mibfile)
            else:
                url = self._url + mibfile

            debug.logger & debug.flagReader and debug.logger('trying to fetch MIB from %s' % url)

            try:
                req = Request(url, headers=headers)
                response = urlopen(req)

            except Exception:
                debug.logger & debug.flagReader and debug.logger('failed to fetch MIB from %s: %s' % (url, sys.exc_info()[1]))
                continue

            debug.logger & debug.flagReader and debug.logger('HTTP response %s' % response.code)

            if response.code == 200:
                try:
                    mtime = time.mktime(time.strptime(response.getheader('Last-Modified'), "%a, %d %b %Y %H:%M:%S %Z"))

                except Exception:
                    debug.logger & debug.flagReader and debug.logger('malformed HTTP headers: %s' % sys.exc_info()[1])
                    mtime = time.time()

                debug.logger & debug.flagReader and debug.logger(
                    'fetching source MIB %s, mtime %s' % (url, response.getheader('Last-Modified')))

                return MibInfo(path=url, file=mibfile, name=mibalias, mtime=mtime), decode(response.read(self.maxMibSize))

        raise error.PySmiReaderFileNotFoundError('source MIB %s not found' % mibname, reader=self)
Пример #58
0
    def getData(self, mibname):
        headers = {
            'Accept': 'text/plain'
        }
        if sys.version_info[:2] < (2, 6):
            conn = httplib.HTTPConnection(self._host, self._port)
        else:
            conn = httplib.HTTPConnection(self._host, self._port, timeout=self._timeout)

        mibname = decode(mibname)

        debug.logger & debug.flagReader and debug.logger('looking for MIB %s' % mibname)

        for mibalias, mibfile in self.getMibVariants(mibname):
            location = self._locationTemplate.replace('@mib@', mibfile)
            debug.logger & debug.flagReader and debug.logger(
                'trying to fetch MIB from %s://%s:%s%s' % (self._schema, self._host, self._port, location))
            try:
                conn.request('GET', location, '', headers)
                response = conn.getresponse()
            except Exception:
                debug.logger & debug.flagReader and debug.logger('failed to fetch MIB from %s://%s:%s%s: %s' % (
                    self._schema, self._host, self._port, location, sys.exc_info()[1]))
                continue

            debug.logger & debug.flagReader and debug.logger('HTTP response %s' % response.status)

            if response.status == 200:
                try:
                    mtime = time.mktime(time.strptime(response.getheader('Last-Modified'), "%a, %d %b %Y %H:%M:%S %Z"))
                except Exception:
                    debug.logger & debug.flagReader and debug.logger('malformed HTTP headers: %s' % sys.exc_info()[1])
                    mtime = time.time()

                debug.logger & debug.flagReader and debug.logger(
                    'fetching source MIB %s, mtime %s' % (location, response.getheader('Last-Modified')))

                return MibInfo(path='%s://%s:%s%s' % (self._schema, self._host, self._port, location), file=mibfile,
                               name=mibalias, mtime=mtime), decode(response.read(self.maxMibSize))

        raise error.PySmiReaderFileNotFoundError('source MIB %s not found' % mibname, reader=self)
Пример #59
0
    def genIndex(self, processed, **kwargs):
        outDict = {
            'meta': {},
            'identity': {},
            'enterprise': {},
            'compliance': {},
            'oids': {},
        }
        if kwargs.get('old_index_data'):
            try:
                outDict.update(
                    json.loads(kwargs['old_index_data'])
                )

            except Exception:
                raise error.PySmiCodegenError('Index load error: %s' % sys.exc_info()[1])

        def order(top):
            if isinstance(top, dict):
                new_top = OrderedDict()
                try:
                    # first try to sort keys as OIDs
                    for k in sorted(top, key=lambda x: [int(y) for y in x.split('.')]):
                        new_top[k] = order(top[k])

                except ValueError:
                    for k in sorted(top):
                        new_top[k] = order(top[k])

                return new_top
            elif isinstance(top, list):
                new_top = []
                for e in sorted(set(top)):
                    new_top.append(order(e))

                return new_top

            return top

        for module, status in processed.items():
            modData = outDict['identity']
            identity_oid = getattr(status, 'identity', None)
            if identity_oid:
                if identity_oid not in modData:
                    modData[identity_oid] = []

                modData[identity_oid].append(module)

            modData = outDict['enterprise']
            enterprise_oid = getattr(status, 'enterprise', None)
            if enterprise_oid:
                if enterprise_oid not in modData:
                    modData[enterprise_oid] = []

                modData[enterprise_oid].append(module)

            modData = outDict['compliance']
            compliance_oids = getattr(status, 'compliance', ())
            for compliance_oid in compliance_oids:
                if compliance_oid not in modData:
                    modData[compliance_oid] = []
                modData[compliance_oid].append(module)

            modData = outDict['oids']
            objects_oids = getattr(status, 'oids', ())
            for object_oid in objects_oids:
                if object_oid not in modData:
                    modData[object_oid] = []

                modData[object_oid].append(module)

            if modData:
                unique_prefixes = {}
                for oid in sorted(modData, key=lambda x: x.count('.')):
                    for oid_prefix, modules in unique_prefixes.items():
                        if oid.startswith(oid_prefix) and set(modules).issuperset(modData[oid]):
                            break
                    else:
                        unique_prefixes[oid] = modData[oid]

                outDict['oids'] = unique_prefixes

        if 'comments' in kwargs:
            outDict['meta']['comments'] = kwargs['comments']

        debug.logger & debug.flagCodegen and debug.logger(
            'OID->MIB index built, %s entries' % len(processed))

        return json.dumps(order(outDict), indent=2)