distro_info.py: Add validity check method.

This commit is contained in:
Benjamin Drung 2011-06-15 00:01:49 +02:00
parent 16300e471f
commit de32133e75
3 changed files with 21 additions and 1 deletions

View File

@ -106,6 +106,10 @@ class DistroInfo(object):
"""Get list of all supported distributions based on the given date.""" """Get list of all supported distributions based on the given date."""
raise NotImplementedError() raise NotImplementedError()
def valid(self, codename):
"""Check if the given codename is known."""
return codename in self.all
def unsupported(self, date=None): def unsupported(self, date=None):
"""Get list of all unsupported distributions based on the given date.""" """Get list of all unsupported distributions based on the given date."""
if date is None: if date is None:
@ -167,6 +171,11 @@ class DebianDistroInfo(DistroInfo):
raise DistroDataOutdated() raise DistroDataOutdated()
return distros[-2]["series"] return distros[-2]["series"]
def valid(self, codename):
"""Check if the given codename is known."""
return DistroInfo.valid(self, codename) or \
codename in ["unstable", "testing", "stable", "old"]
class UbuntuDistroInfo(DistroInfo): class UbuntuDistroInfo(DistroInfo):
"""provides information about Ubuntu's distributions""" """provides information about Ubuntu's distributions"""

View File

@ -177,5 +177,5 @@ def codename_to_distribution(codename):
if not info: if not info:
continue continue
if codename in info().all: if info().valid(codename):
return distro return distro

View File

@ -58,6 +58,12 @@ class DebianDistroInfoTestCase(unittest.TestCase):
"""Test: Get latest testing Debian distribution.""" """Test: Get latest testing Debian distribution."""
self.assertEqual(self._distro_info.testing(self._date), "squeeze") self.assertEqual(self._distro_info.testing(self._date), "squeeze")
def test_valid(self):
"""Test: Check for valid Debian distribution."""
self.assertTrue(self._distro_info.valid("sid"))
self.assertTrue(self._distro_info.valid("stable"))
self.assertFalse(self._distro_info.valid("foobar"))
def test_unsupported(self): def test_unsupported(self):
"""Test: List all unsupported Debian distribution.""" """Test: List all unsupported Debian distribution."""
unsupported = ["buzz", "rex", "bo", "hamm", "slink", "potato", "woody", unsupported = ["buzz", "rex", "bo", "hamm", "slink", "potato", "woody",
@ -111,3 +117,8 @@ class UbuntuDistroInfoTestCase(unittest.TestCase):
"gutsy", "intrepid", "jaunty"]) "gutsy", "intrepid", "jaunty"])
self.assertEqual(unsupported - self.assertEqual(unsupported -
set(self._distro_info.unsupported()), set()) set(self._distro_info.unsupported()), set())
def test_valid(self):
"""Test: Check for valid Ubuntu distribution."""
self.assertTrue(self._distro_info.valid("lucid"))
self.assertFalse(self._distro_info.valid("42"))