Commit e6e3b8df authored by Jason R. Coombs's avatar Jason R. Coombs

Modernize syntax

--HG--
extra : amend_source : eeaee0372ea8d1d39475a722234c03f6a0247722
parent 7c471a14
from __future__ import generators
import sys, imp, marshal
import sys
import imp
import marshal
from imp import PKG_DIRECTORY, PY_COMPILED, PY_SOURCE, PY_FROZEN
from distutils.version import StrictVersion, LooseVersion
from distutils.version import StrictVersion
__all__ = [
'Require', 'find_module', 'get_module_constant', 'extract_constant'
......@@ -10,9 +11,8 @@ __all__ = [
class Require:
"""A prerequisite to building or installing a distribution"""
def __init__(self,name,requested_version,module,homepage='',
attribute=None,format=None
):
def __init__(self, name, requested_version, module, homepage='',
attribute=None, format=None):
if format is None and requested_version is not None:
format = StrictVersion
......@@ -25,20 +25,17 @@ class Require:
self.__dict__.update(locals())
del self.self
def full_name(self):
"""Return full package/distribution name, w/version"""
if self.requested_version is not None:
return '%s-%s' % (self.name,self.requested_version)
return self.name
def version_ok(self,version):
def version_ok(self, version):
"""Is 'version' sufficiently up-to-date?"""
return self.attribute is None or self.format is None or \
str(version) != "unknown" and version >= self.requested_version
def get_version(self, paths=None, default="unknown"):
"""Get version number of installed module, 'None', or 'default'
......@@ -59,20 +56,18 @@ class Require:
except ImportError:
return None
v = get_module_constant(self.module,self.attribute,default,paths)
v = get_module_constant(self.module, self.attribute, default, paths)
if v is not None and v is not default and self.format is not None:
return self.format(v)
return v
def is_present(self,paths=None):
def is_present(self, paths=None):
"""Return true if dependency is present on 'paths'"""
return self.get_version(paths) is not None
def is_current(self,paths=None):
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 is None:
......@@ -113,14 +108,6 @@ def _iter_code(code):
yield op,arg
def find_module(module, paths=None):
"""Just like 'imp.find_module()', but with package support"""
......@@ -140,28 +127,6 @@ def find_module(module, paths=None):
return info
def get_module_constant(module, symbol, default=-1, paths=None):
"""Find 'module' by searching 'paths', and extract 'symbol'
......@@ -171,7 +136,7 @@ def get_module_constant(module, symbol, default=-1, paths=None):
constant. Otherwise, return 'default'."""
try:
f, path, (suffix,mode,kind) = find_module(module,paths)
f, path, (suffix, mode, kind) = find_module(module, paths)
except ImportError:
# Module doesn't exist
return None
......@@ -187,23 +152,17 @@ def get_module_constant(module, symbol, default=-1, paths=None):
else:
# Not something we can parse; we'll have to import it. :(
if module not in sys.modules:
imp.load_module(module,f,path,(suffix,mode,kind))
return getattr(sys.modules[module],symbol,None)
imp.load_module(module, f, path, (suffix, mode, kind))
return getattr(sys.modules[module], symbol, None)
finally:
if f:
f.close()
return extract_constant(code,symbol,default)
return extract_constant(code, symbol, default)
def extract_constant(code,symbol,default=-1):
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
......@@ -236,11 +195,9 @@ def extract_constant(code,symbol,default=-1):
return const
else:
const = default
if sys.platform.startswith('java') or sys.platform == 'cli':
# XXX it'd be better to test assertions about bytecode instead...
del extract_constant, get_module_constant
__all__.remove('extract_constant')
__all__.remove('get_module_constant')
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment