From 00ebd1e79986dad66829edaf8dc906a4c9d1ab6c Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Thu, 30 Jul 2009 14:47:59 +0200 Subject: apt/package.py: Allow to set the candidate of a package (Closes: #523997) + Support assignments to the 'candidate' property of Package objects. + Initial patch by Sebastian Heinlein --- apt/package.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'apt/package.py') diff --git a/apt/package.py b/apt/package.py index ec88a456..f5bdc47d 100644 --- a/apt/package.py +++ b/apt/package.py @@ -486,15 +486,25 @@ class Package(object): def __repr__(self): return '' % (self._pkg.Name, self._pkg.ID) - @property def candidate(self): """Return the candidate version of the package. - - :since: 0.7.9""" + + This property is writeable to allow you to set the candidate version + of the package. Just assign a Version() object, and it will be set as + the candidate version. + """ cand = self._pcache._depcache.GetCandidateVer(self._pkg) if cand is not None: return Version(self, cand) + def __set_candidate(self, version): + """Set the candidate version of the package.""" + self._pcache.cachePreChange() + self._pcache._depcache.SetCandidateVer(self._pkg, version._cand) + self._pcache.cachePostChange() + + candidate = property(candidate, __set_candidate) + @property def installed(self): """Return the currently installed version of the package. -- cgit v1.2.3 From 0cb44e7c914bf98d92a85d83b0bdc15ebc73525d Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Thu, 30 Jul 2009 17:44:49 +0200 Subject: apt/package.py: Make comparisons of Version object more robust. --- apt/package.py | 40 +++++++++++++++++++++++++++++++++++----- debian/changelog | 13 +++++++------ 2 files changed, 42 insertions(+), 11 deletions(-) (limited to 'apt/package.py') diff --git a/apt/package.py b/apt/package.py index f5bdc47d..0b8d4bf8 100644 --- a/apt/package.py +++ b/apt/package.py @@ -189,17 +189,47 @@ class Version(object): self.package = package self._cand = cand + def _cmp(self, other): + try: + return apt_pkg.VersionCompare(self._cand.VerStr, other.version) + except AttributeError: + return apt_pkg.VersionCompare(self._cand.VerStr, other) + def __eq__(self, other): - return self._cand.ID == other._cand.ID + try: + return self._cmp(other) == 0 + except TypeError: + return NotImplemented + + def __ge__(self): + try: + return self._cmp(other) >= 0 + except TypeError: + return NotImplemented def __gt__(self, other): - return apt_pkg.VersionCompare(self.version, other.version) > 0 + try: + return self._cmp(other) > 0 + except TypeError: + return NotImplemented + + def __le__(self): + try: + return self._cmp(other) <= 0 + except TypeError: + return NotImplemented def __lt__(self, other): - return apt_pkg.VersionCompare(self.version, other.version) < 0 + try: + return self._cmp(other) < 0 + except TypeError: + return NotImplemented def __ne__(self, other): - return not self.__eq__(other) + try: + return self._cmp(other) != 0 + except TypeError: + return NotImplemented def __hash__(self): return self._cand.Hash @@ -488,7 +518,7 @@ class Package(object): def candidate(self): """Return the candidate version of the package. - + This property is writeable to allow you to set the candidate version of the package. Just assign a Version() object, and it will be set as the candidate version. diff --git a/debian/changelog b/debian/changelog index 2c27b23d..122ebe92 100644 --- a/debian/changelog +++ b/debian/changelog @@ -18,6 +18,7 @@ python-apt (0.7.12.0) unstable; urgency=low - Allow to set the candidate of a package (Closes: #523997) + Support assignments to the 'candidate' property of Package objects. + Initial patch by Sebastian Heinlein + - Make comparisons of Version object more robust. * Use debhelper 7 instead of CDBS [ Stefano Zacchiroli ] @@ -26,7 +27,7 @@ python-apt (0.7.12.0) unstable; urgency=low [ Sebastian Heinlein ] * apt/progress.py: Extract the package name from the status message - (Closes: #532660) + (Closes: #532660) -- Julian Andres Klode Thu, 30 Jul 2009 14:08:30 +0200 @@ -35,7 +36,7 @@ python-apt (0.7.11.1) unstable; urgency=low [ Stephan Peijnik ] * apt/progress/__init__.py: - Exception handling fixes in InstallProgress class. - + [ Michael Vogt ] * python/tag.cc: - merge patch from John Wright that adds FindRaw method @@ -50,15 +51,15 @@ python-apt (0.7.11.0) unstable; urgency=low [ Stephan Peijnik ] * apt/progress/__init__.py: - - add update_status_full() that takes file_size/partial_size as + - add update_status_full() that takes file_size/partial_size as additional callback arguments - - add pulse_items() that takes a addtional "items" tuple that + - add pulse_items() that takes a addtional "items" tuple that gives the user full access to the individual items that are fetched * python/progress.cc: - low level code for update_status_full and pulse_items() - better threading support - + [ Michael Vogt ] * aptsources/distro.py: - fix indent error that causes incorrect sources.list additons @@ -70,7 +71,7 @@ python-apt (0.7.11.0) unstable; urgency=low required dirs/files automatically -- Michael Vogt Mon, 20 Jul 2009 15:35:27 +0200 - + python-apt (0.7.10.4) unstable; urgency=low [ Michael Vogt ] -- cgit v1.2.3 From e1fe8a0fdc56452f820c837a323729d93f57f581 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Thu, 30 Jul 2009 17:49:35 +0200 Subject: Return VersionList objects in Package.versions, which are sequences and also provide features of mappings. (partial API BREAK) --- apt/package.py | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- debian/changelog | 2 ++ 2 files changed, 85 insertions(+), 3 deletions(-) (limited to 'apt/package.py') diff --git a/apt/package.py b/apt/package.py index 0b8d4bf8..73ca5da0 100644 --- a/apt/package.py +++ b/apt/package.py @@ -29,11 +29,16 @@ import subprocess import urllib2 import warnings +try: + from collections import Sequence +except ImportError: + Sequence = object + import apt_pkg import apt.progress __all__ = ('BaseDependency', 'Dependency', 'Origin', 'Package', 'Record', - 'Version') + 'Version', 'VersionList') def _(string): @@ -499,6 +504,81 @@ class Version(object): return os.path.abspath(dsc) +class VersionList(Sequence): + """Provide a mapping & sequence interface to all versions of a package. + + This class can be used like a dictionary, where version strings are the + keys. It can also be used as a sequence, where integers are the keys. + + You can also convert this to a dictionary or a list, using the usual way + of dict(version_list) or list(version_list). This is useful if you need + to access the version objects multiple times, because they do not have to + be recreated this way. + + Examples ('package.versions' being a version list): + '0.7.92' in package.versions # Check whether 0.7.92 is a valid version. + package.versions[0] # Return first version or raise IndexError + package.versions[0:2] # Return a new VersionList for objects 0-2 + package.versions['0.7.92'] # Return version 0.7.92 or raise KeyError + package.versions.keys() # All keys, as strings. + max(package.versions) + """ + + def __init__(self, package, slice=None): + self._package = package # apt.package.Package() + self._versions = package._pkg.VersionList # [apt_pkg.Version(), ...] + if slice: + self._versions = self._versions[slice] + + def __getitem__(self, item): + if isinstance(item, slice): + return self.__class__(self._package, item) + try: + # Sequence interface, item is an integer + return Version(self._package, self._versions[item]) + except TypeError: + # Dictionary interface item is a string. + for ver in self._versions: + if ver.ver_str == item: + return Version(self._package, ver) + raise KeyError("Version: %r not found." % (item)) + + def __repr__(self): + return '' % self.keys() + + def __iter__(self): + """Return an iterator over all value objects.""" + return (Version(self._package, ver) for ver in self._versions) + + def __contains__(self, item): + if isinstance(item, Version): # Sequence interface + item = item.version + # Dictionary interface. + for ver in self._versions: + if ver.ver_str == item: + return True + return False + + def __eq__(self, other): + return list(self) == list(other) + + def __len__(self): + return len(self._versions) + + # Mapping interface + + def keys(self): + """Return a list of all versions, as strings.""" + return [ver.VerStr for ver in self._versions] + + def get(self, key, default=None): + """Return the key or the default.""" + try: + return self[key] + except LookupError: + return default + + class Package(object): """Representation of a package in a cache. @@ -911,11 +991,11 @@ class Package(object): @property def versions(self): - """Return a list of versions. + """Return a VersionList() object for all available versions. :since: 0.7.9 """ - return [Version(self, ver) for ver in self._pkg.VersionList] + return VersionList(self) # depcache actions diff --git a/debian/changelog b/debian/changelog index 122ebe92..c4ba63fc 100644 --- a/debian/changelog +++ b/debian/changelog @@ -19,6 +19,8 @@ python-apt (0.7.12.0) unstable; urgency=low + Support assignments to the 'candidate' property of Package objects. + Initial patch by Sebastian Heinlein - Make comparisons of Version object more robust. + - Return VersionList objects in Package.versions, which are sequences + and also provide features of mappings. (partial API BREAK) * Use debhelper 7 instead of CDBS [ Stefano Zacchiroli ] -- cgit v1.2.3