[Python-checkins] distutils2: small changes, PEP8 compliance
tarek.ziade
python-checkins at python.org
Mon May 31 01:56:09 CEST 2010
tarek.ziade pushed f75c222e1b04 to distutils2:
http://hg.python.org/distutils2/rev/f75c222e1b04
changeset: 186:f75c222e1b04
user: Josip Djolonga
date: Mon May 31 01:43:44 2010 +0200
summary: small changes, PEP8 compliance
files: src/distutils2/_backport/pkgutil.py, src/distutils2/depgraph.py
diff --git a/src/distutils2/_backport/pkgutil.py b/src/distutils2/_backport/pkgutil.py
--- a/src/distutils2/_backport/pkgutil.py
+++ b/src/distutils2/_backport/pkgutil.py
@@ -22,6 +22,7 @@
'provides_distribution', 'obsoletes_distribution',
]
+
def read_code(stream):
# This helper is needed in order for the PEP 302 emulation to
# correctly handle compiled files
@@ -38,6 +39,7 @@
def simplegeneric(func):
"""Make a trivial single-dispatch generic function"""
registry = {}
+
def wrapper(*args, **kw):
ob = args[0]
try:
@@ -48,6 +50,7 @@
mro = cls.__mro__
except AttributeError:
try:
+
class cls(cls, object):
pass
mro = cls.__mro__[1:]
@@ -129,7 +132,7 @@
# don't traverse path items we've seen before
path = [p for p in path if not seen(p)]
- for item in walk_packages(path, name+'.', onerror):
+ for item in walk_packages(path, name + '.', onerror):
yield item
@@ -207,7 +210,7 @@
for fn in filenames:
modname = inspect.getmodulename(fn)
- if modname=='__init__' or modname in yielded:
+ if modname == '__init__' or modname in yielded:
continue
path = os.path.join(self.path, fn)
@@ -217,7 +220,7 @@
modname = fn
for fn in os.listdir(path):
subname = inspect.getmodulename(fn)
- if subname=='__init__':
+ if subname == '__init__':
ispkg = True
break
else:
@@ -256,7 +259,7 @@
def _reopen(self):
if self.file and self.file.closed:
mod_type = self.etc[2]
- if mod_type==imp.PY_SOURCE:
+ if mod_type == imp.PY_SOURCE:
self.file = open(self.filename, 'rU')
elif mod_type in (imp.PY_COMPILED, imp.C_EXTENSION):
self.file = open(self.filename, 'rb')
@@ -271,22 +274,22 @@
def is_package(self, fullname):
fullname = self._fix_name(fullname)
- return self.etc[2]==imp.PKG_DIRECTORY
+ 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:
+ if mod_type == imp.PY_SOURCE:
source = self.get_source(fullname)
self.code = compile(source, self.filename, 'exec')
- elif mod_type==imp.PY_COMPILED:
+ elif mod_type == imp.PY_COMPILED:
self._reopen()
try:
self.code = read_code(self.file)
finally:
self.file.close()
- elif mod_type==imp.PKG_DIRECTORY:
+ elif mod_type == imp.PKG_DIRECTORY:
self.code = self._get_delegate().get_code()
return self.code
@@ -294,29 +297,28 @@
fullname = self._fix_name(fullname)
if self.source is None:
mod_type = self.etc[2]
- if mod_type==imp.PY_SOURCE:
+ if mod_type == imp.PY_SOURCE:
self._reopen()
try:
self.source = self.file.read()
finally:
self.file.close()
- elif mod_type==imp.PY_COMPILED:
+ elif mod_type == imp.PY_COMPILED:
if os.path.exists(self.filename[:-1]):
f = open(self.filename[:-1], 'rU')
self.source = f.read()
f.close()
- elif mod_type==imp.PKG_DIRECTORY:
+ elif mod_type == imp.PKG_DIRECTORY:
self.source = self._get_delegate().get_source()
return self.source
-
def _get_delegate(self):
return ImpImporter(self.filename).find_module('__init__')
def get_filename(self, fullname=None):
fullname = self._fix_name(fullname)
mod_type = self.etc[2]
- if self.etc[2]==imp.PKG_DIRECTORY:
+ if self.etc[2] == imp.PKG_DIRECTORY:
return self._get_delegate().get_filename()
elif self.etc[2] in (imp.PY_SOURCE, imp.PY_COMPILED, imp.C_EXTENSION):
return self.filename
@@ -340,16 +342,16 @@
fn = fn[plen:].split(os.sep)
- if len(fn)==2 and fn[1].startswith('__init__.py'):
+ if len(fn) == 2 and fn[1].startswith('__init__.py'):
if fn[0] not in yielded:
yielded[fn[0]] = 1
yield fn[0], True
- if len(fn)!=1:
+ if len(fn) != 1:
continue
modname = inspect.getmodulename(fn[0])
- if modname=='__init__':
+ if modname == '__init__':
continue
if modname and '.' not in modname and modname not in yielded:
@@ -437,6 +439,7 @@
if '.' not in fullname:
yield ImpImporter()
+
def get_loader(module_or_name):
"""Get a PEP 302 "loader" object for module_or_name
@@ -462,6 +465,7 @@
fullname = module_or_name
return find_loader(fullname)
+
def find_loader(fullname):
"""Find a PEP 302 "loader" object for fullname
@@ -552,6 +556,7 @@
return path
+
def get_data(package, resource):
"""Get a resource from a package.
@@ -595,6 +600,7 @@
DIST_FILES = ('INSTALLER', 'METADATA', 'RECORD', 'REQUESTED',)
+
class Distribution(object):
"""Created with the *path* of the ``.dist-info`` directory provided to the
constructor. It reads the metadata contained in METADATA when it is
@@ -621,7 +627,7 @@
RECORD = os.path.join(self.path, 'RECORD')
record_reader = csv_reader(open(RECORD, 'rb'), delimiter=',')
for row in record_reader:
- path, md5, size = row[:] + [ None for i in xrange(len(row), 3) ]
+ path, md5, size = row[:] + [None for i in xrange(len(row), 3)]
if local:
path = path.replace('/', os.sep)
path = os.path.join(sys.prefix, path)
@@ -636,7 +642,6 @@
A local absolute path is an absolute path in which occurrences of
``'/'`` have been replaced by the system separator given by ``os.sep``.
-
:parameter local: flag to say if the path should be returned a local
absolute path
:type local: boolean
@@ -644,7 +649,6 @@
"""
return self._get_records(local)
-
def uses(self, path):
"""
Returns ``True`` if path is listed in RECORD. *path* can be a local
@@ -664,8 +668,8 @@
``file`` instance for the file pointed by *path*.
:parameter path: a ``'/'``-separated path relative to the ``.dist-info``
- directory or an absolute path; If *path* is an absolute
- path and doesn't start with the ``.dist-info``
+ directory or an absolute path; If *path* is an
+ absolute path and doesn't start with the ``.dist-info``
directory path, a :class:`DistutilsError` is raised
:type path: string
:parameter binary: If *binary* is ``True``, opens the file in read-only
@@ -710,7 +714,8 @@
for path, md5, size in self._get_records(local):
yield path
-class EggInfoDistribution:
+
+class EggInfoDistribution(object):
"""Created with the *path* of the ``.egg-info`` directory or file provided
to the constructor. It reads the metadata contained in the file itself, or
if the given path happens to be a directory, the metadata is read from the
@@ -734,11 +739,13 @@
def uses(self, path):
return False
+
def _normalize_dist_name(name):
"""Returns a normalized name from the given *name*.
:rtype: string"""
return name.replace('-', '_')
+
def distinfo_dirname(name, version):
"""
The *name* and *version* parameters are converted into their
@@ -767,7 +774,8 @@
normalized_version = version
return '-'.join([name, normalized_version]) + file_extension
-def get_distributions(use_egg_info = False):
+
+def get_distributions(use_egg_info=False):
"""
Provides an iterator that looks for ``.dist-info`` directories in
``sys.path`` and returns :class:`Distribution` instances for each one of
@@ -788,7 +796,8 @@
dist = EggInfoDistribution(os.path.join(realpath, dir))
yield dist
-def get_distribution(name, use_egg_info = False):
+
+def get_distribution(name, use_egg_info=False):
"""
Scans all elements in ``sys.path`` and looks for all directories ending with
``.dist-info``. Returns a :class:`Distribution` corresponding to the
@@ -816,7 +825,8 @@
break
return found
-def obsoletes_distribution(name, version=None, use_egg_info = False):
+
+def obsoletes_distribution(name, version=None, use_egg_info=False):
"""
Iterates over all distributions to find which distributions obsolete *name*.
If a *version* is provided, it will be used to filter the results.
@@ -845,7 +855,8 @@
yield dist
break
-def provides_distribution(name, version=None, use_egg_info = False):
+
+def provides_distribution(name, version=None, use_egg_info=False):
"""
Iterates over all distributions to find which distributions provide *name*.
If a *version* is provided, it will be used to filter the results. Scans
@@ -891,6 +902,7 @@
yield dist
break
+
def get_file_users(path):
"""
Iterates over all distributions to find out which distributions uses
@@ -903,4 +915,3 @@
for dist in get_distributions():
if dist.uses(path):
yield dist
-
diff --git a/src/distutils2/depgraph.py b/src/distutils2/depgraph.py
--- a/src/distutils2/depgraph.py
+++ b/src/distutils2/depgraph.py
@@ -10,7 +10,7 @@
__all__ = ['DependencyGraph', 'generate_graph']
-class DependencyGraph:
+class DependencyGraph(object):
"""
Represents a dependency graph between distributions.
@@ -22,9 +22,6 @@
requirements that were not provided by any other distributions.
"""
- adjacency_list = {}
- missing = {} # maps distributions to a list of unfulfilled requirements
-
def __init__(self):
self.adjacency_list = {}
self.missing = {}
@@ -63,7 +60,7 @@
"""
self.missing[distribution].append(requirement)
- def to_dot(self, f, skip_disconnected = True):
+ def to_dot(self, f, skip_disconnected=True):
"""
Writes a DOT output for the graph to the provided *file*.
If *skip_disconnected* is set to ``True``, then all distributions
@@ -83,8 +80,8 @@
disconnected.append(dist)
for (other, label) in adjs:
if not label is None:
- f.write('"%s" -> "%s" [label="%s"]\n' % (dist.name,
- other.name, label))
+ f.write('"%s" -> "%s" [label="%s"]\n' %
+ (dist.name, other.name, label))
else:
f.write('"%s" -> "%s"\n' % (dist.name, other.name))
if not skip_disconnected and len(disconnected) > 0:
@@ -98,6 +95,7 @@
f.write('}\n')
f.write('}\n')
+
def generate_graph(dists):
"""
Generates a dependency graph from the given distributions.
@@ -110,7 +108,7 @@
graph = DependencyGraph()
provided = {} # maps names to lists of (version, dist) tuples
- dists = list(dists) # maybe use generator_tools to copy generators in future
+ dists = list(dists) # maybe use generator_tools in future
missing = [] # a list of (instance, requirement) tuples
@@ -150,10 +148,11 @@
return graph
+
def dependent_dists(dists, dist):
"""
- Recursively generate a list of distributions from *dists* that are dependent
- on *dist*.
+ Recursively generate a list of distributions from *dists* that are
+ dependent on *dist*.
:param dists: a list of distributions
:param dist: a distribution, member of *dists* for which we are interested
@@ -183,4 +182,3 @@
", ".join(reqs)))
f = open('output.dot', 'w')
graph.to_dot(f, True)
-
--
Repository URL: http://hg.python.org/distutils2
More information about the Python-checkins
mailing list