Source code for utah.client.package_info
# Ubuntu Testing Automation Harness
# Copyright 2012 Canonical Ltd.
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
"""Get package information in the system under test."""
import collections
import apt
[docs]class InstalledPackages(collections.Mapping):
"""Query apt cache for installed packages and their version."""
def __init__(self):
cache = apt.cache.Cache()
self._data = {
package.name: package.installed.version
for package in cache
if package.is_installed
}
def __iter__(self):
"""Delegate iteration to internal data dictionary.
:returns: Sequence of installed packages
:rtype: iterable(str)
"""
return iter(self._data)
def __len__(self):
"""Delegate length calculation to internal data dictionary.
:returns: Installed packages count
:rtype: int
"""
return len(self._data)
def __getitem__(self, key):
"""Delegate key retrieval to internal data dictionary.
:param key: Package name to look up
:type key: str
:returns: Installed package version
:rtype: str
"""
return self._data[key]
[docs]class DeltaPackages(object):
"""Calculate delta between to set of installed packages.
:param start: Packages installed when test run started
:type start: :class:`InstalledPackages`
:param end: Packages installed when test run ended
:type end: :class:`InstalledPackages`
:var installed: Packages that were installed in between
:type installed: list(str)
:var uninstalled: Packages that were uninstalled in between
:type uninstalled: list(str)
:var updated: Packages that were uninstalled in between
:type updated: tuple(str, str, str)
"""
def __init__(self, start, end):
start_names = set(start)
end_names = set(end)
self.installed = sorted(end_names - start_names)
self.uninstalled = sorted(start_names - end_names)
self.updated = [
{'name': name,
'old_version': start[name],
'new_version': end[name]}
for name in sorted(start_names & end_names)
if end[name] != start[name]
]
def __iter__(self):
"""Yield attributes in key/value pairs.
This method is expected to be used as a way to convert DeltaPackages
objects to dictionaries easily.
"""
attributes = ['installed', 'uninstalled', 'updated']
for attribute in attributes:
yield attribute, getattr(self, attribute)