[python-debian/master] add new example of dependency parsing usage

Stefano Zacchiroli zack at upsilon.cc
Thu Jun 19 15:02:24 UTC 2008


---
 TODO                     |    1 -
 debian/changelog         |    3 +-
 examples/deb822/depgraph |   56 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+), 2 deletions(-)
 create mode 100755 examples/deb822/depgraph

diff --git a/TODO b/TODO
index aac68ba..0d9ee79 100644
--- a/TODO
+++ b/TODO
@@ -3,7 +3,6 @@
   that for reading Packages files Deb822 should be used instead.
 - parsed dependencies support:
   - add tests
-  - add examples
   - change key name to access package name from "name" to "package", that way
     it is more consistent with legacy package names
   - instead of adding top-level methods, add a single top-level property
diff --git a/debian/changelog b/debian/changelog
index 61cb692..0045bee 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -13,8 +13,9 @@ python-debian (0.1.11) UNRELEASED; urgency=low
   [ Stefano Zacchiroli ]
   * debian_bundle/debfile.py: add has_key (same implementation of
     __contains__) to better emulate dictionary containers
-  * debian_budnel/deb822.py: add support for "parsed" dependencies and other
+  * debian_bundle/deb822.py: add support for "parsed" dependencies and other
     inter-package relationships. See examples/deb822/depgraph for sample usage
+  * examples/deb822/ add new example "depgraph"
 
  -- John Wright <jsw at debian.org>  Thu, 12 Jun 2008 12:16:53 +0100
 
diff --git a/examples/deb822/depgraph b/examples/deb822/depgraph
new file mode 100755
index 0000000..7697e74
--- /dev/null
+++ b/examples/deb822/depgraph
@@ -0,0 +1,56 @@
+#!/usr/bin/python
+
+# depgraph
+# Copyright (C) 2008 Stefano Zacchiroli <zack at debian.org>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+"""Graph the dependencies of all packages contained in a given Packages
+file.
+
+Only consider Depends fields. Versioned dependencies are considered as they
+were not versioned. The graph is retourned in output as a (non normalized)
+graphviz graph suitable to be processed by dot (for an unstable/main Packages
+file, generating the final graph will take a while ...)."""
+
+import sys
+from debian_bundle import deb822
+
+__fresh_id = 0
+
+def main():
+    if len(sys.argv) != 2:
+        print "Usage: depgraph PACKAGES_FILE"
+        sys.exit(2)
+
+    def get_id():
+        global __fresh_id
+        __fresh_id += 1
+        return ("NODE_%d" % __fresh_id)
+
+    def emit_arc(node1, node2):
+        print '  "%s" -> "%s" ;' % (node1, node2)
+    def emit_node(node, dsc):
+        print '  "%s" [label="%s"] ;' % (node, dsc)
+
+    print "digraph depgraph {"
+    for pkg in deb822.Packages.iter_paragraphs(file(sys.argv[1])):
+        name = pkg['package']
+        if pkg.has_key('depends'):
+            for deps in pkg.depends():
+                if len(deps) == 1:
+                    emit_arc(name, deps[0]['name'])
+                else:   # output an OR node
+                    or_node = get_id()
+                    emit_arc(name, or_node)
+                    emit_node(or_node, 'OR')
+                    for dep in deps:
+                        emit_arc(or_node, dep['name'])
+    print "}"
+
+if __name__ == '__main__':
+    main()
+
-- 
1.5.4.2





More information about the pkg-python-debian-commits mailing list