[Python-checkins] distutils2: make depgraph.main print out the dependencies in the standard output -
tarek.ziade
python-checkins at python.org
Sun Oct 3 11:50:25 CEST 2010
tarek.ziade pushed 9faea5e66727 to distutils2:
http://hg.python.org/distutils2/rev/9faea5e66727
changeset: 738:9faea5e66727
tag: tip
user: Tarek Ziade <tarek at ziade.org>
date: Sun Oct 03 11:50:18 2010 +0200
summary: make depgraph.main print out the dependencies in the standard output - outputing a dot file is done via -d
files: distutils2/depgraph.py, distutils2/tests/test_depgraph.py
diff --git a/distutils2/depgraph.py b/distutils2/depgraph.py
--- a/distutils2/depgraph.py
+++ b/distutils2/depgraph.py
@@ -1,7 +1,8 @@
"""Analyse the relationships between the distributions in the system
and generate a dependency graph.
"""
-
+import sys
+from StringIO import StringIO
from distutils2.errors import DistutilsError
from distutils2.version import VersionPredicate
@@ -65,6 +66,20 @@
"""
self.missing[distribution].append(requirement)
+ def __repr__(self):
+ """Representation of the graph"""
+ def _repr_dist(dist):
+ return '%s %s' % (dist.name, dist.metadata['Version'])
+ output = []
+ for dist, adjs in self.adjacency_list.iteritems():
+ output.append(_repr_dist(dist))
+ for other, label in adjs:
+ dist = _repr_dist(other)
+ if label is not None:
+ dist = '%s [%s]' % (dist, label)
+ output.append(' %s' % dist)
+ return '\n'.join(output)
+
def graph_to_dot(graph, f, skip_disconnected=True):
"""Writes a DOT output for the graph to the provided file *f*.
@@ -176,13 +191,52 @@
dep.pop(0) # remove dist from dep, was there to prevent infinite loops
return dep
-if __name__ == '__main__':
+def main():
from distutils2._backport.pkgutil import get_distributions
- dists = list(get_distributions(use_egg_info=True))
- graph = generate_graph(dists)
+ tempout = StringIO()
+ try:
+ old = sys.stderr
+ sys.stderr = tempout
+ try:
+ dists = list(get_distributions(use_egg_info=True))
+ graph = generate_graph(dists)
+ finally:
+ sys.stderr = old
+ except Exception, e:
+ tempout.seek(0)
+ tempout = tempout.read()
+ print('Could not generate the graph\n%s\n%s\n' % (tempout, str(e)))
+ sys.exit(1)
+
for dist, reqs in graph.missing.iteritems():
if len(reqs) > 0:
- print("Missing dependencies for %s: %s" % (dist.name,
+ print("Warning: Missing dependencies for %s: %s" % (dist.name,
", ".join(reqs)))
- f = open('output.dot', 'w')
- graph_to_dot(graph, f, True)
+ # XXX replace with argparse
+ if len(sys.argv) == 1:
+ print('Dependency graph:')
+ print(' ' + repr(graph).replace('\n', '\n '))
+ sys.exit(0)
+ elif len(sys.argv) > 1 and sys.argv[1] in ('-d', '--dot'):
+ if len(sys.argv) > 2:
+ filename = sys.argv[2]
+ else:
+ filename = 'depgraph.dot'
+
+ f = open(filename, 'w')
+ try:
+ graph_to_dot(graph, f, True)
+ finally:
+ f.close()
+ tempout.seek(0)
+ tempout = tempout.read()
+ print(tempout)
+ print('Dot file written at "%s"' % filename)
+ sys.exit(0)
+ else:
+ print('Supported option: -d [filename]')
+ sys.exit(1)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/distutils2/tests/test_depgraph.py b/distutils2/tests/test_depgraph.py
--- a/distutils2/tests/test_depgraph.py
+++ b/distutils2/tests/test_depgraph.py
@@ -21,6 +21,11 @@
r'"(?P<from>.*)" -> "(?P<to>.*)" \[label="(?P<label>.*)"\]'
)
+ def tearDown(self):
+ super(DepGraphTestCase, self).tearDown()
+ pkgutil.enable_cache()
+ sys.path = self.sys_path
+
def checkLists(self, l1, l2):
""" Compare two lists without taking the order into consideration """
self.assertListEqual(sorted(l1), sorted(l2))
@@ -176,10 +181,26 @@
self.checkLists(matches, expected)
- def tearDown(self):
- super(DepGraphTestCase, self).tearDown()
- pkgutil.enable_cache()
- sys.path = self.sys_path
+ def test_main(self):
+ tempout = StringIO.StringIO()
+ old = sys.stdout
+ sys.stdout = tempout
+ oldargv = sys.argv[:]
+ sys.argv[:] = ['script.py']
+ try:
+ try:
+ depgraph.main()
+ except SystemExit:
+ pass
+ finally:
+ sys.stdout = old
+ sys.argv[:] = oldargv
+
+ # checks what main did XXX could do more here
+ tempout.seek(0)
+ res = tempout.read()
+ self.assertTrue('towel' in res)
+
def test_suite():
return unittest.makeSuite(DepGraphTestCase)
--
Repository URL: http://hg.python.org/distutils2
More information about the Python-checkins
mailing list