Skip to content

Commit

Permalink
Replace imp with importlib is available
Browse files Browse the repository at this point in the history
  • Loading branch information
etingof committed Apr 2, 2019
1 parent e5fb119 commit 5b903a7
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 104 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Revision 0.3.4, XX-01-2019
--------------------------

- Added `implied` key to JSON SNMP table index structure
- Rebased MIB importing code onto `importlib` because `imp` is long
deprecated

Revision 0.3.3, 29-12-2018
--------------------------
Expand Down
16 changes: 11 additions & 5 deletions pysmi/borrower/pyfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@
# Copyright (c) 2015-2019, Ilya Etingof <[email protected]>
# License: http://snmplabs.com/pysmi/license.html
#
import imp
try:
import importlib

SOURCE_SUFFIXES = importlib.machinery.SOURCE_SUFFIXES

except ImportError:
import imp

SOURCE_SUFFIXES = [imp.PY_SOURCE]

from pysmi.borrower.base import AbstractBorrower


class PyFileBorrower(AbstractBorrower):
"""Create PySNMP MIB file borrowing object"""
for sfx, mode, typ in imp.get_suffixes():
if typ == imp.PY_SOURCE:
exts = [sfx]
break
exts = SOURCE_SUFFIXES
104 changes: 57 additions & 47 deletions pysmi/searcher/pyfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,19 @@
import os
import sys
import time
import imp
import struct
try:
import importlib

SOURCE_SUFFIXES = importlib.machinery.SOURCE_SUFFIXES
BYTECODE_SUFFIXES = importlib.machinery.BYTECODE_SUFFIXES

except ImportError:
import imp

SOURCE_SUFFIXES = [imp.PY_SOURCE]
BYTECODE_SUFFIXES = [imp.PY_COMPILED]

from pysmi.searcher.base import AbstractSearcher
from pysmi.compat import decode
from pysmi import debug
Expand All @@ -19,12 +30,6 @@ class PyFileSearcher(AbstractSearcher):
"""Figures out if given Python file (source or bytecode) exists at given
location.
"""
suffixes = {}
for sfx, mode, typ in imp.get_suffixes():
if typ not in suffixes:
suffixes[typ] = []
suffixes[typ].append((sfx, mode))

def __init__(self, path):
"""Create an instance of *PyFileSearcher* bound to specific directory.
Expand All @@ -44,49 +49,54 @@ def fileExists(self, mibname, mtime, rebuild=False):
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:
fp = open(f, pyMode)
pyData = fp.read(8)
fp.close()

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
for pySfx in BYTECODE_SUFFIXES:
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

try:
fp = open(f, 'rb')
pyData = fp.read(8)
fp.close()

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:
try:
pyTime = os.stat(f)[8]
raise error.PySmiFileNotFoundError('older file %s exists' % mibname, searcher=self)

else:
debug.logger & debug.flagSearcher and debug.logger('bad magic in %s' % f)
continue

for pySfx in SOURCE_SUFFIXES:
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

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

except OSError:
raise error.PySmiSearcherError('failure opening compiled file %s: %s' % (f, sys.exc_info()[1]),
searcher=self)
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))))
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()
if pyTime >= mtime:
raise error.PySmiFileNotModifiedError()

raise error.PySmiFileNotFoundError('no compiled file %s found' % mibname, searcher=self)
95 changes: 53 additions & 42 deletions pysmi/searcher/pypackage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,21 @@
#
import os
import time
import imp
import struct
try:
import importlib

PY_MAGIC_NUMBER = importlib.util.MAGIC_NUMBER
SOURCE_SUFFIXES = importlib.machinery.SOURCE_SUFFIXES
BYTECODE_SUFFIXES = importlib.machinery.BYTECODE_SUFFIXES

except ImportError:
import imp

PY_MAGIC_NUMBER = imp.get_magic()
SOURCE_SUFFIXES = [imp.PY_SOURCE]
BYTECODE_SUFFIXES = [imp.PY_COMPILED]

from pysmi.searcher.base import AbstractSearcher
from pysmi.searcher.pyfile import PyFileSearcher
from pysmi.compat import decode
Expand All @@ -21,13 +34,6 @@ class PyPackageSearcher(AbstractSearcher):
Python package must be importable.
"""
suffixes = {}
for sfx, mode, typ in imp.get_suffixes():
if typ not in suffixes:
suffixes[typ] = []

suffixes[typ].append((sfx, mode))

def __init__(self, package):
"""Create an instance of *PyPackageSearcher* bound to specific Python
package.
Expand Down Expand Up @@ -82,41 +88,46 @@ def fileExists(self, mibname, mtime, rebuild=False):
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
for pySfx in BYTECODE_SUFFIXES:
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

pyData = self.__loader.get_data(f)
if pyData[:4] == PY_MAGIC_NUMBER:
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:
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('older file %s exists' % mibname, searcher=self)

else:
debug.logger & debug.flagSearcher and debug.logger('bad magic in %s' % f)
continue

for pySfx in SOURCE_SUFFIXES:

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

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)
22 changes: 12 additions & 10 deletions pysmi/writer/pyfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@
#
import os
import sys
import imp
import tempfile
import py_compile
try:
import importlib

SOURCE_SUFFIXES = importlib.machinery.SOURCE_SUFFIXES

except ImportError:
import imp

SOURCE_SUFFIXES = [imp.PY_SOURCE]

from pysmi.writer.base import AbstractWriter
from pysmi.compat import encode, decode
from pysmi import debug
Expand All @@ -24,14 +33,6 @@ class PyFileWriter(AbstractWriter):
pyCompile = True
pyOptimizationLevel = -1

suffixes = {}

for sfx, mode, typ in imp.get_suffixes():
if typ not in suffixes:
suffixes[typ] = []

suffixes[typ].append((decode(sfx), mode))

def __init__(self, path):
"""Creates an instance of *PyFileWriter* class.
Expand Down Expand Up @@ -59,7 +60,8 @@ def putData(self, mibname, data, comments=(), dryRun=False):
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]
pyfile = os.path.join(self._path, decode(mibname))
pyfile += SOURCE_SUFFIXES[0]

tfile = None

Expand Down

0 comments on commit 5b903a7

Please sign in to comment.