def __init__(self, mib_list, src_dir_list, http_sources=None): if "BFT_DEBUG" in os.environ: self.dbg = os.environ.get('BFT_DEBUG') else: self.dbg = "" if "yy" in self.dbg: # VERY verbose, but essential for spotting # possible ASN.1 errors from pysmi import debug debug.setLogger(debug.Debug('reader', 'compiler')) # Initialize compiler infrastructure mibCompiler = MibCompiler(SmiStarParser(), JsonCodeGen(), CallbackWriter(self.callback_func)) # search for source MIBs here mibCompiler.addSources(*[FileReader(x) for x in src_dir_list]) if http_sources: # search for source MIBs at Web sites mibCompiler.addSources(*[HttpReader(*x) for x in http_sources]) # never recompile MIBs with MACROs mibCompiler.addSearchers(StubSearcher(*JsonCodeGen.baseMibs)) # run recursive MIB compilation mibCompiler.compile(*mib_list)
def _compile_mib_to_json(mib, source_mib_directories, destination_directory, source, compiled_mibs_path): from pysmi.borrower import AnyFileBorrower from pysmi.codegen import JsonCodeGen from pysmi.compiler import MibCompiler from pysmi.parser import SmiV1CompatParser from pysmi.reader import getReadersFromUrls from pysmi.searcher import AnyFileSearcher, StubSearcher from pysmi.writer import FileWriter mib_stubs = JsonCodeGen.baseMibs compile_documentation = True # Compiler infrastructure code_generator = JsonCodeGen() file_writer = FileWriter(destination_directory).setOptions(suffix='.json') mib_compiler = MibCompiler(SmiV1CompatParser(tempdir=''), code_generator, file_writer) # use source_mib_directories as mibs source sources = [source] sources.extend(source_mib_directories) mib_compiler.addSources( *getReadersFromUrls(*sources, **dict(fuzzyMatching=True))) searchers = [ AnyFileSearcher(destination_directory).setOptions(exts=['.json']), StubSearcher(*mib_stubs) ] mib_compiler.addSearchers(*searchers) # borrowers, aka compiled mibs source borrowers = [ AnyFileBorrower(borrower_reader, genTexts=True).setOptions(exts=['.json']) for borrower_reader in getReadersFromUrls( *[compiled_mibs_path], **dict(lowcaseMatching=False)) ] mib_compiler.addBorrowers(borrowers) processed = mib_compiler.compile( mib, **dict( noDeps=False, rebuild=False, dryRun=False, dstTemplate=None, genTexts=compile_documentation, textFilter=False and (lambda symbol, text: text) or None, writeMibs=True, ignoreErrors=False, )) return processed
def _compile_mib_to_json(mib, source_mib_directories, destination_directory): from pysmi.codegen import JsonCodeGen from pysmi.compiler import MibCompiler from pysmi.parser import SmiV1CompatParser from pysmi.reader import FileReader, HttpReader from pysmi.searcher import AnyFileSearcher, StubSearcher from pysmi.writer import FileWriter mib_stubs = JsonCodeGen.baseMibs compile_documentation = True # Compiler infrastructure searchers = [ AnyFileSearcher(destination_directory).setOptions(exts=['.json']), StubSearcher(*mib_stubs) ] code_generator = JsonCodeGen() file_writer = FileWriter(destination_directory).setOptions(suffix='.json') mib_compiler = MibCompiler(SmiV1CompatParser(tempdir=''), code_generator, file_writer) # use source_mib_directories as mibs source for source_directory in source_mib_directories: mib_compiler.addSources(FileReader(source_directory)) # use snmp mibs repo as mibs source reader = HttpReader('raw.githubusercontent.com', 80, '/projx/snmp-mibs/master/@mib@') mib_compiler.addSources(reader) mib_compiler.addSearchers(*searchers) processed = mib_compiler.compile( mib, **dict( noDeps=False, rebuild=False, dryRun=False, dstTemplate=None, genTexts=compile_documentation, textFilter=False and (lambda symbol, text: text) or None, writeMibs=True, ignoreErrors=False, )) return processed
def __init__(self, mib_list, src_dir_list, http_sources=None): if "BFT_DEBUG" in os.environ: self.dbg = os.environ.get('BFT_DEBUG') else: self.dbg = "" if "yy" in self.dbg: # VERY verbose, but essential for spotting # possible ASN.1 errors from pysmi import debug debug.setLogger(debug.Debug('reader', 'compiler')) # Initialize compiler infrastructure mibCompiler = MibCompiler(SmiStarParser(), JsonCodeGen(), CallbackWriter(self.callback_func)) # search for source MIBs here mibCompiler.addSources(*[FileReader(x) for x in src_dir_list]) if http_sources: # search for source MIBs at Web sites mibCompiler.addSources(*[HttpReader(*x) for x in http_sources]) # never recompile MIBs with MACROs mibCompiler.addSearchers(StubSearcher(*JsonCodeGen.baseMibs)) # run recursive MIB compilation mib_dict = mibCompiler.compile(*mib_list) err = False if mib_dict is None or mib_dict == {}: print( "ERROR: failed on mib compilation (mibCompiler.compile returned an empty dictionary)" ) err = True for key, value in mib_dict.iteritems(): if value == 'unprocessed': print("ERROR: failed on mib compilation: " + key + ": " + value) err = True if err: raise Exception("SnmpMibs failed to initialize.") elif 'BFT_DEBUG' in os.environ: print('# %d MIB modules compiled' % len(mib_dict))
def transform_mibs_to_json(self, specific_mibs: object = None, exclude_mibs: object = []) -> object: mib_names = [] if specific_mibs is None: for filename in os.listdir(self.local_mib_dir): try: filename.rindex('.my', -3) except: continue mib_names.append(filename) else: mib_names = specific_mibs.copy() logging.info('Compiling MIBs to JSON.') try: mib_compiler = MibCompiler( SmiV1CompatParser(), JsonCodeGen(), FileWriter(self.local_json_dir).setOptions(suffix='.json')) mib_compiler.addSources( FileReader(self.local_mib_dir, recursive=True)) mib_stubs = JsonCodeGen.baseMibs searchers = [ AnyFileSearcher( self.local_json_dir).setOptions(exts=['.json']), StubSearcher(*mib_stubs) ] mib_compiler.addSearchers(*searchers) if not os.path.isdir(self.local_json_dir): os.makedirs(self.local_json_dir) processed = mib_compiler.compile( *mib_names, **dict(noDeps=False, rebuild=True, genTexts=True, writeMibs=True, ignoreErrors=True)) mib_compiler.buildIndex(processed, ignoreErrors=True) except error.PySmiError: logging.error('ERROR: %s', str(sys.exc_info()[1])) sys.exit(1) logging.info('Finished compiling MIBs to JSON.')
if sys.platform[:3] == 'win': dstDirectory = os.path.join(dstDirectory, 'PySNMP Configuration', 'mibs') else: dstDirectory = os.path.join(dstDirectory, '.pysnmp', 'mibs') # Compiler infrastructure borrowers = [PyFileBorrower(x[1], genTexts=mibBorrowers[x[0]][1]) for x in enumerate(getReadersFromUrls(*[m[0] for m in mibBorrowers], **dict(lowcaseMatching=False)))] searchers = [PyFileSearcher(dstDirectory)] for mibSearcher in mibSearchers: searchers.append(PyPackageSearcher(mibSearcher)) searchers.append(StubSearcher(*mibStubs)) codeGenerator = PySnmpCodeGen() fileWriter = PyFileWriter(dstDirectory).setOptions(pyCompile=pyCompileFlag, pyOptimizationLevel=pyOptimizationLevel) elif dstFormat == 'json': if not mibStubs: mibStubs = JsonCodeGen.baseMibs if not mibBorrowers: mibBorrowers = [('http://mibs.snmplabs.com/json/notexts/@mib@', False), ('http://mibs.snmplabs.com/json/fulltexts/@mib@', True)] if not dstDirectory:
# Compiler infrastructure borrowers = [ PyFileBorrower(x[1], genTexts=mibBorrowers[x[0]][1]) for x in enumerate( getReadersFromUrls(*[m[0] for m in mibBorrowers], **dict(lowcaseMatching=False))) ] searchers = [PyFileSearcher(dstDirectory)] for mibSearcher in mibSearchers: searchers.append(PyPackageSearcher(mibSearcher)) searchers.append(StubSearcher(*mibStubs)) codeGenerator = PySnmpCodeGen() fileWriter = PyFileWriter(dstDirectory).setOptions( pyCompile=pyCompileFlag, pyOptimizationLevel=pyOptimizationLevel) elif dstFormat == 'json': if not mibStubs: mibStubs = JsonCodeGen.baseMibs if not mibBorrowers: mibBorrowers = [('http://mibs.snmplabs.com/json/notexts/@mib@', False), ('http://mibs.snmplabs.com/json/fulltexts/@mib@', True) ]
from pysmi.reader import HttpReader from pysmi.searcher import StubSearcher from pysmi.writer import CallbackWriter from pysmi.parser import SmiStarParser from pysmi.codegen import JsonCodeGen from pysmi.compiler import MibCompiler inputMibs = ['IF-MIB', 'IP-MIB'] httpSources = [('mibs.snmplabs.com', 80, '/asn1/@mib@')] # store compiled MIBs by calling this function def store_mibs(mibName, jsonDoc, cbCtx): print('# MIB module %s' % mibName) print(jsonDoc) mibCompiler = MibCompiler(SmiStarParser(), JsonCodeGen(), CallbackWriter(store_mibs)) # pull ASN.1 MIBs over HTTP mibCompiler.addSources(*[HttpReader(*x) for x in httpSources]) # never recompile MIBs with ASN.1 MACROs mibCompiler.addSearchers(StubSearcher(*JsonCodeGen.baseMibs)) status = mibCompiler.compile(*inputMibs) print(status)
import sys from pysmi.reader import CallbackReader from pysmi.searcher import StubSearcher from pysmi.writer import CallbackWriter from pysmi.parser import SmiV2Parser from pysmi.codegen import PySnmpCodeGen from pysmi.compiler import MibCompiler inputMibs = ['IF-MIB', 'IP-MIB'] srcDir = '/usr/share/snmp/mibs/' # we will read MIBs from here # Initialize compiler infrastructure mibCompiler = MibCompiler( SmiV2Parser(), PySnmpCodeGen(), # out own callback function stores results in its own way CallbackWriter(lambda m, d, c: sys.stdout.write(d))) # our own callback function serves as a MIB source here mibCompiler.addSources( CallbackReader(lambda m, c: open(srcDir + m + '.txt').read())) # never recompile MIBs with MACROs mibCompiler.addSearchers(StubSearcher(*PySnmpCodeGen.baseMibs)) # run non-recursive MIB compilation results = mibCompiler.compile(*inputMibs, **dict(noDeps=True)) print('Results: %s' % ', '.join(['%s:%s' % (x, results[x]) for x in results]))