[python-debian/master] deb822: support for pretty printing relations

Stefano Zacchiroli zack at upsilon.cc
Tue Aug 12 20:12:42 UTC 2008


added the reverse function of parse_relations and relevante tests
---
 debian/changelog        |    4 ++++
 debian_bundle/deb822.py |   26 ++++++++++++++++++++++++++
 tests/test_deb822.py    |   13 +++++++++++++
 3 files changed, 43 insertions(+), 0 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index 6f2ff47..7465fcd 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -4,6 +4,10 @@ python-debian (0.1.12) UNRELEASED; urgency=low
   * Add initial support to deb822 for gpg signature checking (Closes: #485829)
   * Suggest gpgv for signature checking
 
+  [ Stefano Zacchiroli ]
+  * deb822: add support for pretty printing structured inter-package
+    relationships (and relevant tests)
+
  -- John Wright <jsw at debian.org>  Sat, 09 Aug 2008 17:15:34 -0600
 
 python-debian (0.1.11) unstable; urgency=low
diff --git a/debian_bundle/deb822.py b/debian_bundle/deb822.py
index 05a6ca4..fbc96eb 100644
--- a/debian_bundle/deb822.py
+++ b/debian_bundle/deb822.py
@@ -31,6 +31,7 @@ except ImportError:
 
 import new
 import re
+import string
 import sys
 import StringIO
 import UserDict
@@ -564,6 +565,31 @@ class PkgRelation(object):
         cnf = map(cls.__pipe_sep_RE.split, tl_deps)
         return map(lambda or_deps: map(parse_rel, or_deps), cnf)
 
+    @staticmethod
+    def str(rels):
+        """Format to string structured inter-package relationships
+        
+        Perform the inverse operation of parse_relations, returning a string
+        suitable to be written in a package stanza.
+        """
+        def pp_arch(arch_spec):
+            (excl, arch) = arch_spec
+            if excl:
+                return arch
+            else:
+                return '!' + arch
+
+        def pp_atomic_dep(dep):
+            s = dep['name']
+            if dep.has_key('version') and dep['version'] is not None:
+                s += ' (%s %s)' % dep['version']
+            if dep.has_key('arch') and dep['arch'] is not None:
+                s += ' [%s]' % string.join(map(pp_arch, dep['arch']))
+            return s
+
+        pp_or_dep = lambda deps: string.join(map(pp_atomic_dep, deps), ' | ')
+        return string.join(map(pp_or_dep, rels), ', ')
+
 
 class _lowercase_dict(dict):
     """Dictionary wrapper which lowercase keys upon lookup."""
diff --git a/tests/test_deb822.py b/tests/test_deb822.py
index 52d6bf8..04498d4 100755
--- a/tests/test_deb822.py
+++ b/tests/test_deb822.py
@@ -633,6 +633,19 @@ class TestPkgRelations(unittest.TestCase):
             [{'arch': None, 'name': 'librss1', 'version': ('>=', '4:3.5.9-2')}]]
         self.assertEqual(dep3, pkg3.relations['depends'])
 
+        bin_rels = ['file, libc6 (>= 2.7-1), libpaper1, psutils']
+        src_rels = ['apache2-src (>= 2.2.9), libaprutil1-dev, ' \
+                'libcap-dev [!kfreebsd-i386 !kfreebsd-amd64 !hurd-i386], ' \
+                'autoconf, debhelper (>> 5.0.0)']
+        for bin_rel in bin_rels:
+            self.assertEqual(bin_rel,
+                    deb822.PkgRelation.str(deb822.PkgRelation.parse_relations(
+                            bin_rel)))
+        for src_rel in src_rels:
+            self.assertEqual(src_rel,
+                    deb822.PkgRelation.str(deb822.PkgRelation.parse_relations( \
+                            src_rel)))
+
     def test_sources(self):
         pkgs = deb822.Sources.iter_paragraphs(file('test_Sources'))
         pkg1 = pkgs.next()
-- 
1.5.5.GIT




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