1 Star 1 Fork 3

张小农 / 某扫描器核心反编译

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
pkgutil.py 20.55 KB
一键复制 编辑 原始数据 按行查看 历史
张小农 提交于 2019-03-21 13:47 . first code
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: pkgutil.py
"""Utilities to support packages."""
from collections import namedtuple
from functools import singledispatch as simplegeneric
import importlib, importlib.util, importlib.machinery, os, os.path, sys
from types import ModuleType
import warnings
__all__ = [
"get_importer",
"iter_importers",
"get_loader",
"find_loader",
"walk_packages",
"iter_modules",
"get_data",
"ImpImporter",
"ImpLoader",
"read_code",
"extend_path",
"ModuleInfo",
]
ModuleInfo = namedtuple("ModuleInfo", "module_finder name ispkg")
ModuleInfo.__doc__ = "A namedtuple with minimal info about a module."
def _get_spec(finder, name):
"""Return the finder-specific module spec."""
try:
find_spec = finder.find_spec
except AttributeError:
loader = finder.find_module(name)
if loader is None:
return
return importlib.util.spec_from_loader(name, loader)
return find_spec(name)
def read_code(stream):
import marshal
magic = stream.read(4)
if magic != importlib.util.MAGIC_NUMBER:
return
else:
stream.read(8)
return marshal.load(stream)
def walk_packages(path=None, prefix="", onerror=None):
"""Yields ModuleInfo for all modules recursively
on path, or, if path is None, all accessible modules.
'path' should be either None or a list of paths to look for
modules in.
'prefix' is a string to output on the front of every module name
on output.
Note that this function must import all *packages* (NOT all
modules!) on the given path, in order to access the __path__
attribute to find submodules.
'onerror' is a function which gets called with one argument (the
name of the package which was being imported) if any exception
occurs while trying to import a package. If no onerror function is
supplied, ImportErrors are caught and ignored, while all other
exceptions are propagated, terminating the search.
Examples:
# list all modules python can access
walk_packages()
# list all submodules of ctypes
walk_packages(ctypes.__path__, ctypes.__name__+'.')
"""
def seen(p, m={}):
if p in m:
return True
m[p] = True
for info in iter_modules(path, prefix):
yield info
if info.ispkg:
try:
__import__(info.name)
except ImportError:
if onerror is not None:
onerror(info.name)
except Exception:
if onerror is not None:
onerror(info.name)
else:
raise
path = getattr(sys.modules[info.name], "__path__", None) or []
path = [p for p in path if not seen(p)]
yield from walk_packages(path, info.name + ".", onerror)
def iter_modules(path=None, prefix=""):
"""Yields ModuleInfo for all submodules on path,
or, if path is None, all top-level modules on sys.path.
'path' should be either None or a list of paths to look for
modules in.
'prefix' is a string to output on the front of every module name
on output.
"""
if path is None:
importers = iter_importers()
else:
importers = map(get_importer, path)
yielded = {}
for i in importers:
for name, ispkg in iter_importer_modules(i, prefix):
if name not in yielded:
yielded[name] = 1
yield ModuleInfo(i, name, ispkg)
@simplegeneric
def iter_importer_modules(importer, prefix=""):
if not hasattr(importer, "iter_modules"):
return []
else:
return importer.iter_modules(prefix)
def _iter_file_finder_modules(importer, prefix=""):
if importer.path is None or not os.path.isdir(importer.path):
return
yielded = {}
import inspect
try:
filenames = os.listdir(importer.path)
except OSError:
filenames = []
filenames.sort()
for fn in filenames:
modname = inspect.getmodulename(fn)
if not modname == "__init__":
if modname in yielded:
continue
path = os.path.join(importer.path, fn)
ispkg = False
if not modname and os.path.isdir(path) and "." not in fn:
modname = fn
try:
dircontents = os.listdir(path)
except OSError:
dircontents = []
for fn in dircontents:
subname = inspect.getmodulename(fn)
if subname == "__init__":
ispkg = True
break
continue
if modname:
if "." not in modname:
yielded[modname] = 1
yield (prefix + modname, ispkg)
iter_importer_modules.register(
importlib.machinery.FileFinder, _iter_file_finder_modules
)
def _import_imp():
global imp
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
imp = importlib.import_module("imp")
class ImpImporter:
"""PEP 302 Finder that wraps Python's "classic" import algorithm
ImpImporter(dirname) produces a PEP 302 finder that searches that
directory. ImpImporter(None) produces a PEP 302 finder that searches
the current sys.path, plus any modules that are frozen or built-in.
Note that ImpImporter does not currently support being used by placement
on sys.meta_path.
"""
def __init__(self, path=None):
warnings.warn(
"This emulation is deprecated, use 'importlib' instead", DeprecationWarning
)
_import_imp()
self.path = path
def find_module(self, fullname, path=None):
subname = fullname.split(".")[-1]
if subname != fullname:
if self.path is None:
return
if self.path is None:
path = None
else:
path = [os.path.realpath(self.path)]
try:
file, filename, etc = imp.find_module(subname, path)
except ImportError:
return
return ImpLoader(fullname, file, filename, etc)
def iter_modules(self, prefix=""):
if self.path is None or not os.path.isdir(self.path):
return
yielded = {}
import inspect
try:
filenames = os.listdir(self.path)
except OSError:
filenames = []
filenames.sort()
for fn in filenames:
modname = inspect.getmodulename(fn)
if not modname == "__init__":
if modname in yielded:
continue
path = os.path.join(self.path, fn)
ispkg = False
if not modname and os.path.isdir(path) and "." not in fn:
modname = fn
try:
dircontents = os.listdir(path)
except OSError:
dircontents = []
for fn in dircontents:
subname = inspect.getmodulename(fn)
if subname == "__init__":
ispkg = True
break
continue
if modname:
if "." not in modname:
yielded[modname] = 1
yield (prefix + modname, ispkg)
class ImpLoader:
"""PEP 302 Loader that wraps Python's "classic" import algorithm
"""
code = source = None
def __init__(self, fullname, file, filename, etc):
warnings.warn(
"This emulation is deprecated, use 'importlib' instead", DeprecationWarning
)
_import_imp()
self.file = file
self.filename = filename
self.fullname = fullname
self.etc = etc
def load_module(self, fullname):
self._reopen()
try:
mod = imp.load_module(fullname, self.file, self.filename, self.etc)
finally:
if self.file:
self.file.close()
return mod
def get_data(self, pathname):
with open(pathname, "rb") as (file):
return file.read()
def _reopen(self):
if self.file:
if self.file.closed:
mod_type = self.etc[2]
if mod_type == imp.PY_SOURCE:
self.file = open(self.filename, "r")
else:
if mod_type in (imp.PY_COMPILED, imp.C_EXTENSION):
self.file = open(self.filename, "rb")
def _fix_name(self, fullname):
if fullname is None:
fullname = self.fullname
else:
if fullname != self.fullname:
raise ImportError(
"Loader for module %s cannot handle module %s"
% (self.fullname, fullname)
)
return fullname
def is_package(self, fullname):
fullname = self._fix_name(fullname)
return self.etc[2] == imp.PKG_DIRECTORY
def get_code(self, fullname=None):
fullname = self._fix_name(fullname)
if self.code is None:
mod_type = self.etc[2]
if mod_type == imp.PY_SOURCE:
source = self.get_source(fullname)
self.code = compile(source, self.filename, "exec")
else:
if mod_type == imp.PY_COMPILED:
self._reopen()
try:
self.code = read_code(self.file)
finally:
self.file.close()
else:
if mod_type == imp.PKG_DIRECTORY:
self.code = self._get_delegate().get_code()
return self.code
def get_source(self, fullname=None):
fullname = self._fix_name(fullname)
if self.source is None:
mod_type = self.etc[2]
if mod_type == imp.PY_SOURCE:
self._reopen()
try:
self.source = self.file.read()
finally:
self.file.close()
if mod_type == imp.PY_COMPILED:
if os.path.exists(self.filename[:-1]):
with open(self.filename[:-1], "r") as (f):
self.source = f.read()
else:
if mod_type == imp.PKG_DIRECTORY:
self.source = self._get_delegate().get_source()
return self.source
def _get_delegate(self):
finder = ImpImporter(self.filename)
spec = _get_spec(finder, "__init__")
return spec.loader
def get_filename(self, fullname=None):
fullname = self._fix_name(fullname)
mod_type = self.etc[2]
if mod_type == imp.PKG_DIRECTORY:
return self._get_delegate().get_filename()
if mod_type in (imp.PY_SOURCE, imp.PY_COMPILED, imp.C_EXTENSION):
return self.filename
try:
import zipimport
from zipimport import zipimporter
def iter_zipimport_modules(importer, prefix=""):
dirlist = sorted(zipimport._zip_directory_cache[importer.archive])
_prefix = importer.prefix
plen = len(_prefix)
yielded = {}
import inspect
for fn in dirlist:
if not fn.startswith(_prefix):
continue
fn = fn[plen:].split(os.sep)
if len(fn) == 2:
if fn[1].startswith("__init__.py"):
if fn[0] not in yielded:
yielded[fn[0]] = 1
yield (prefix + fn[0], True)
if len(fn) != 1:
continue
modname = inspect.getmodulename(fn[0])
if modname == "__init__":
continue
if modname:
if "." not in modname:
if modname not in yielded:
yielded[modname] = 1
yield (prefix + modname, False)
iter_importer_modules.register(zipimporter, iter_zipimport_modules)
except ImportError:
pass
def get_importer(path_item):
"""Retrieve a finder for the given path item
The returned finder is cached in sys.path_importer_cache
if it was newly created by a path hook.
The cache (or part of it) can be cleared manually if a
rescan of sys.path_hooks is necessary.
"""
try:
importer = sys.path_importer_cache[path_item]
except KeyError:
for path_hook in sys.path_hooks:
try:
importer = path_hook(path_item)
sys.path_importer_cache.setdefault(path_item, importer)
break
except ImportError:
pass
else:
importer = None
return importer
def iter_importers(fullname=""):
"""Yield finders for the given module name
If fullname contains a '.', the finders will be for the package
containing fullname, otherwise they will be all registered top level
finders (i.e. those on both sys.meta_path and sys.path_hooks).
If the named module is in a package, that package is imported as a side
effect of invoking this function.
If no module name is specified, all top level finders are produced.
"""
if fullname.startswith("."):
msg = ("Relative module name {!r} not supported").format(fullname)
raise ImportError(msg)
if "." in fullname:
pkg_name = fullname.rpartition(".")[0]
pkg = importlib.import_module(pkg_name)
path = getattr(pkg, "__path__", None)
if path is None:
return
else:
yield from sys.meta_path
path = sys.path
for item in path:
yield get_importer(item)
def get_loader(module_or_name):
"""Get a "loader" object for module_or_name
Returns None if the module cannot be found or imported.
If the named module is not already imported, its containing package
(if any) is imported, in order to establish the package __path__.
"""
if module_or_name in sys.modules:
module_or_name = sys.modules[module_or_name]
if module_or_name is None:
return
if isinstance(module_or_name, ModuleType):
module = module_or_name
loader = getattr(module, "__loader__", None)
if loader is not None:
return loader
if getattr(module, "__spec__", None) is None:
return
fullname = module.__name__
else:
fullname = module_or_name
return find_loader(fullname)
def find_loader(fullname):
"""Find a "loader" object for fullname
This is a backwards compatibility wrapper around
importlib.util.find_spec that converts most failures to ImportError
and only returns the loader rather than the full spec
"""
if fullname.startswith("."):
msg = ("Relative module name {!r} not supported").format(fullname)
raise ImportError(msg)
try:
spec = importlib.util.find_spec(fullname)
except (ImportError, AttributeError, TypeError, ValueError) as ex:
msg = "Error while finding loader for {!r} ({}: {})"
raise ImportError(msg.format(fullname, type(ex), ex)) from ex
if spec is not None:
return spec.loader
def extend_path(path, name):
"""Extend a package's path.
Intended use is to place the following code in a package's __init__.py:
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
This will add to the package's __path__ all subdirectories of
directories on sys.path named after the package. This is useful
if one wants to distribute different parts of a single logical
package as multiple directories.
It also looks for *.pkg files beginning where * matches the name
argument. This feature is similar to *.pth files (see site.py),
except that it doesn't special-case lines starting with 'import'.
A *.pkg file is trusted at face value: apart from checking for
duplicates, all entries found in a *.pkg file are added to the
path, regardless of whether they are exist the filesystem. (This
is a feature.)
If the input path is not a list (as is the case for frozen
packages) it is returned unchanged. The input path is not
modified; an extended copy is returned. Items are only appended
to the copy at the end.
It is assumed that sys.path is a sequence. Items of sys.path that
are not (unicode or 8-bit) strings referring to existing
directories are ignored. Unicode items of sys.path that cause
errors when used as filenames may cause this function to raise an
exception (in line with os.path.isdir() behavior).
"""
if not isinstance(path, list):
return path
else:
sname_pkg = name + ".pkg"
path = path[:]
parent_package, _, final_name = name.rpartition(".")
if parent_package:
try:
search_path = sys.modules[parent_package].__path__
except (KeyError, AttributeError):
return path
else:
search_path = sys.path
for dir in search_path:
if not isinstance(dir, str):
continue
finder = get_importer(dir)
if finder is not None:
portions = []
if hasattr(finder, "find_spec"):
spec = finder.find_spec(final_name)
if spec is not None:
portions = spec.submodule_search_locations or []
else:
if hasattr(finder, "find_loader"):
_, portions = finder.find_loader(final_name)
for portion in portions:
if portion not in path:
path.append(portion)
pkgfile = os.path.join(dir, sname_pkg)
if os.path.isfile(pkgfile):
try:
f = open(pkgfile)
except OSError as msg:
sys.stderr.write("Can't open %s: %s\n" % (pkgfile, msg))
with f:
for line in f:
line = line.rstrip("\n")
if not not line:
if line.startswith("#"):
pass
else:
path.append(line)
return path
def get_data(package, resource):
"""Get a resource from a package.
This is a wrapper round the PEP 302 loader get_data API. The package
argument should be the name of a package, in standard module format
(foo.bar). The resource argument should be in the form of a relative
filename, using '/' as the path separator. The parent directory name '..'
is not allowed, and nor is a rooted name (starting with a '/').
The function returns a binary string, which is the contents of the
specified resource.
For packages located in the filesystem, which have already been imported,
this is the rough equivalent of
d = os.path.dirname(sys.modules[package].__file__)
data = open(os.path.join(d, resource), 'rb').read()
If the package cannot be located or loaded, or it uses a PEP 302 loader
which does not support get_data(), then None is returned.
"""
spec = importlib.util.find_spec(package)
if spec is None:
return
else:
loader = spec.loader
if loader is None or not hasattr(loader, "get_data"):
return
mod = sys.modules.get(package) or importlib._bootstrap._load(spec)
if mod is None or not hasattr(mod, "__file__"):
return
parts = resource.split("/")
parts.insert(0, os.path.dirname(mod.__file__))
resource_name = os.path.join(*parts)
return loader.get_data(resource_name)
Python
1
https://gitee.com/zhanghk668/opsrv_extracted.git
git@gitee.com:zhanghk668/opsrv_extracted.git
zhanghk668
opsrv_extracted
某扫描器核心反编译
master

搜索帮助