Skip to content

Commit

Permalink
Merge pull request #118 from mulkieran/master-hawkey
Browse files Browse the repository at this point in the history
Master hawkey
  • Loading branch information
mulkieran committed May 21, 2015
2 parents f60a2a9 + ce805b0 commit 93d0e21
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 13 deletions.
30 changes: 19 additions & 11 deletions blivet/tasks/availability.py
Expand Up @@ -21,8 +21,7 @@

import abc
from distutils.version import LooseVersion
import rpm
import six
import hawkey

from six import add_metaclass

Expand Down Expand Up @@ -136,19 +135,28 @@ def __init__(self, package=None):

@property
def packageVersion(self):
""" Returns the version of the package.
""" Returns the version of the installed package.
:returns: the package version
:rtype: LooseVersion
:raises AvailabilityError: on failure to obtain package version
"""
ts = rpm.TransactionSet()
info = ts.dbMatch("provides", self.package.package_name)
if len(info) == 0:
raise AvailabilityError("Could not determine package version for %s" % self.package.package_name)
version = next(info)['version']
if six.PY3:
version = version.decode()
return LooseVersion(version)
sack = hawkey.Sack()

try:
sack.load_system_repo()
except IOError as e:
# hawkey has been observed allowing an IOError to propagate to
# caller with message "Failed calculating RPMDB checksum."
# See: https://bugzilla.redhat.com/show_bug.cgi?id=1223914
raise AvailabilityError("Could not determine package version for %s: %s" % (self.package.package_name, e))

query = hawkey.Query(sack).filter(name=self.package.package_name, latest=True)
packages = query.run()
if len(packages) != 1:
raise AvailabilityError("Could not determine package version for %s: unable to obtain package information from repo" % self.package.package_name)

return LooseVersion(packages[0].version)

def availabilityErrors(self, resource):
if self._availabilityErrors is not None and CACHE_AVAILABILITY:
Expand Down
4 changes: 2 additions & 2 deletions python-blivet.spec
Expand Up @@ -43,7 +43,7 @@ Requires: libselinux-python
Requires: libblockdev >= %{libblockdevver}
Requires: libblockdev-plugins-all >= %{libblockdevver}
Requires: libselinux-python
Requires: rpm-python
Requires: python-hawkey

%description
The python-blivet package is a python module for examining and modifying
Expand All @@ -65,7 +65,7 @@ Requires: util-linux >= %{utillinuxver}
Requires: dosfstools
Requires: e2fsprogs >= %{e2fsver}
Requires: lsof
Requires: rpm-python3
Requires: python3-hawkey

%description -n python3-%{realname}
The python3-%{realname} is a python3 package for examining and modifying storage
Expand Down
Empty file added tests/tasks_test/__init__.py
Empty file.
41 changes: 41 additions & 0 deletions tests/tasks_test/basic_tests.py
@@ -0,0 +1,41 @@
#!/usr/bin/python
import unittest

import blivet.tasks.task as task
import blivet.tasks.availability as availability

class BasicUnavailableApplication(task.BasicApplication):
ext = availability.unavailable_resource("unavailable")
description = "unavailable application"

def doTask(self):
pass

class BasicAvailableApplication(task.BasicApplication):
ext = availability.available_resource("available")
description = "available application"

def doTask(self):
pass

class ResourceTestCase(unittest.TestCase):

def testAvailabililty(self):
unavailable_resource = availability.unavailable_resource("unavailable")
self.assertNotEqual(unavailable_resource.availabilityErrors, [])
self.assertFalse(unavailable_resource.available)

available_resource = availability.available_resource("available")
self.assertEqual(available_resource.availabilityErrors, [])
self.assertTrue(available_resource.available)

class TasksTestCase(unittest.TestCase):

def testAvailability(self):
unavailable_app = BasicUnavailableApplication()
self.assertFalse(unavailable_app.available)
self.assertNotEqual(unavailable_app.availabilityErrors, [])

available_app = BasicAvailableApplication()
self.assertTrue(available_app.available)
self.assertEqual(available_app.availabilityErrors, [])

0 comments on commit 93d0e21

Please sign in to comment.