from . import _imp from ._imp import PY_COMPILED, PY_FROZEN, PY_SOURCE, find_module
__all__ = ['Require', 'find_module']
class Require: """A prerequisite to building or installing a distribution"""
def __init__(
self, name, requested_version, module, homepage='', attribute=None, format=None
): if format isNoneand requested_version isnotNone:
format = Version
if format isnotNone:
requested_version = format(requested_version) if attribute isNone:
attribute = '__version__'
self.__dict__.update(locals()) del self.self
def full_name(self): """Return full package/distribution name, w/version""" if self.requested_version isnotNone: return'%s-%s' % (self.name, self.requested_version) return self.name
def version_ok(self, version): """Is 'version' sufficiently up-to-date?""" return (
self.attribute isNone or self.format isNone or str(version) != "unknown" and self.format(version) >= self.requested_version
)
def get_version(self, paths=None, default="unknown"): """Get version number of installed module, 'None', or 'default'
Search 'paths'for module. Ifnot found, return'None'. If found, return the extracted version attribute, or'default'if no version
attribute was specified, or the value cannot be determined without
importing the module. The version is formatted according to the
requirement's version format (if any), unless it is 'None' or the
supplied 'default'. """
if self.attribute isNone: try:
f, p, i = find_module(self.module, paths) except ImportError: returnNone if f:
f.close() return default
v = get_module_constant(self.module, self.attribute, default, paths)
if v isnotNoneand v isnot default and self.format isnotNone: return self.format(v)
return v
def is_present(self, paths=None): """Return true if dependency is present on 'paths'""" return self.get_version(paths) isnotNone
def is_current(self, paths=None): """Return true if dependency is present and up-to-date on 'paths'"""
version = self.get_version(paths) if version isNone: returnFalse return self.version_ok(str(version))
# Some objects are not available on some platforms. # XXX it'd be better to test assertions about bytecode instead. ifnot sys.platform.startswith('java') and sys.platform != 'cli':
def get_module_constant(module, symbol, default=-1, paths=None): """Find 'module' by searching 'paths', and extract 'symbol'
Return'None'if'module' does not exist on 'paths', or it does not define 'symbol'. If the module defines 'symbol'as a constant, return the
constant. Otherwise, return'default'."""
with maybe_close(f): if kind == PY_COMPILED:
f.read(8) # skip magic & date
code = marshal.load(f) elif kind == PY_FROZEN:
code = _imp.get_frozen_object(module, paths) elif kind == PY_SOURCE:
code = compile(f.read(), path, 'exec') else: # Not something we can parse; we'll have to import it. :(
imported = _imp.get_module(module, paths, info) return getattr(imported, symbol, None)
return extract_constant(code, symbol, default)
def extract_constant(code, symbol, default=-1): """Extract the constant value of 'symbol' from 'code'
If the name 'symbol'is bound to a constant value by the Python code
object 'code', return that value. If'symbol'is bound to an expression, return'default'. Otherwise, return'None'.
Return value is based on the first assignment to 'symbol'. 'symbol' must
be a global, or at least a non-"fast" local in the code block. That is,
only 'STORE_NAME'and'STORE_GLOBAL' opcodes are checked, and'symbol'
must be present in'code.co_names'. """ if symbol notin code.co_names: # name's not there, can't possibly be an assignment returnNone
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.