[python-debian/master] debian_support: Be more careful in case apt_pkg is not available

John Wright jsw at debian.org
Sun Mar 14 11:37:42 UTC 2010


- If apt_pkg is not available, AptPkgVersion now raises
  NotImplementedError on any initialization attempt
- test_debian_support.py now doesn't try to test AptPkgVersion if
  apt_pkg is not availble
---
 debian/changelog             |   10 ++++++++++
 lib/debian/debian_support.py |   12 +++++++++---
 tests/test_debian_support.py |   37 +++++++++++++++++++++++++++++--------
 3 files changed, 48 insertions(+), 11 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index 143f8f5..6ae1751 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,13 @@
+python-debian (0.1.16) UNRELEASED; urgency=low
+
+  * debian_support: Be more careful in case apt_pkg is not available
+    - If apt_pkg is not available, AptPkgVersion now raises
+      NotImplementedError on any initialization attempt
+    - test_debian_support.py now doesn't try to test AptPkgVersion if
+      apt_pkg is not availble
+
+ -- John Wright <jsw at debian.org>  Sun, 14 Mar 2010 05:31:54 -0600
+
 python-debian (0.1.15) unstable; urgency=low
 
   [ John Wright ]
diff --git a/lib/debian/debian_support.py b/lib/debian/debian_support.py
index 3fe8f52..b2bcca9 100644
--- a/lib/debian/debian_support.py
+++ b/lib/debian/debian_support.py
@@ -28,9 +28,9 @@ from deprecation import function_deprecated_by
 try:
     import apt_pkg
     apt_pkg.init()
-    __have_apt_pkg = True
+    _have_apt_pkg = True
 except ImportError:
-    __have_apt_pkg = False
+    _have_apt_pkg = False
 
 class ParseError(Exception):
     """An exception which is used to signal a parse failure.
@@ -161,6 +161,12 @@ class BaseVersion(object):
 class AptPkgVersion(BaseVersion):
     """Represents a Debian package version, using apt_pkg.VersionCompare"""
 
+    def __init__(self, version):
+        if not _have_apt_pkg:
+            raise NotImplementedError("apt_pkg not available; install the "
+                                      "python-apt package")
+        super(AptPkgVersion, self).__init__(version)
+
     def __cmp__(self, other):
         return apt_pkg.VersionCompare(str(self), str(other))
 
@@ -247,7 +253,7 @@ class NativeVersion(BaseVersion):
                     return res
         return 0
 
-if __have_apt_pkg:
+if _have_apt_pkg:
     class Version(AptPkgVersion):
         pass
 else:
diff --git a/tests/test_debian_support.py b/tests/test_debian_support.py
index 29ddb96..51a2e3c 100755
--- a/tests/test_debian_support.py
+++ b/tests/test_debian_support.py
@@ -23,6 +23,7 @@ import unittest
 
 sys.path.insert(0, '../lib/debian/')
 
+import debian_support
 from debian_support import *
 
 
@@ -30,7 +31,12 @@ class VersionTests(unittest.TestCase):
     """Tests for AptPkgVersion and NativeVersion classes in debian_support"""
 
     def _test_version(self, full_version, epoch, upstream, debian):
-        for cls in (AptPkgVersion, NativeVersion):
+        if debian_support._have_apt_pkg:
+            test_classes = [AptPkgVersion, NativeVersion]
+        else:
+            test_classes = [NativeVersion]
+
+        for cls in test_classes:
             v = cls(full_version)
             self.assertEqual(v.full_version, full_version,
                              "%s: full_version broken" % cls)
@@ -68,7 +74,12 @@ class VersionTests(unittest.TestCase):
         self._test_version('2:1.0.4~rc2-1', '2', '1.0.4~rc2', '1')
 
     def test_version_updating(self):
-        for cls in (AptPkgVersion, NativeVersion):
+        if debian_support._have_apt_pkg:
+            test_classes = [AptPkgVersion, NativeVersion]
+        else:
+            test_classes = [NativeVersion]
+
+        for cls in test_classes:
             v = cls('1:1.4.1-1')
 
             v.debian_version = '2'
@@ -112,12 +123,22 @@ class VersionTests(unittest.TestCase):
         This is does the real work for test_comparisons.
         """
 
-        for (cls1, cls2) in [(AptPkgVersion, AptPkgVersion),
-                             (AptPkgVersion, NativeVersion),
-                             (NativeVersion, AptPkgVersion),
-                             (NativeVersion, NativeVersion),
-                             (str, AptPkgVersion), (AptPkgVersion, str),
-                             (str, NativeVersion), (NativeVersion, str)]:
+        if debian_support._have_apt_pkg:
+            test_class_tuples = [
+                (AptPkgVersion, AptPkgVersion),
+                (AptPkgVersion, NativeVersion),
+                (NativeVersion, AptPkgVersion),
+                (NativeVersion, NativeVersion),
+                (str, AptPkgVersion), (AptPkgVersion, str),
+                (str, NativeVersion), (NativeVersion, str),
+            ]
+        else:
+            test_class_tuples = [
+                 (NativeVersion, NativeVersion),
+                 (str, NativeVersion), (NativeVersion, str),
+            ]
+
+        for (cls1, cls2) in test_class_tuples:
             v1 = cls1(v1_str)
             v2 = cls2(v2_str)
             truth_fn = self._get_truth_fn(cmp_oper)
-- 
1.6.3.3




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