summaryrefslogtreecommitdiff
path: root/portato/backend
diff options
context:
space:
mode:
Diffstat (limited to 'portato/backend')
-rw-r--r--portato/backend/__init__.py59
-rw-r--r--portato/backend/package.py10
-rw-r--r--portato/backend/portage/package.py14
-rw-r--r--portato/backend/portage/system.py21
-rw-r--r--portato/backend/system_interface.py22
5 files changed, 70 insertions, 56 deletions
diff --git a/portato/backend/__init__.py b/portato/backend/__init__.py
index 8eae806..4f20a18 100644
--- a/portato/backend/__init__.py
+++ b/portato/backend/__init__.py
@@ -16,9 +16,6 @@ from ..helper import debug
from .system_interface import SystemInterface
from .exceptions import BlockedException, PackageNotFoundException, DependencyCalcError, InvalidSystemError
-SYSTEM = "portage" # the name of the current system
-_sys = None # the SystemInterface-instance
-
class _Package (object):
"""Wrapping class from which L{portato.backend.Package} inherits. This is used by the flags module to check
whether an object is a package. It cannot use the normal Package class as this results in cyclic dependencies."""
@@ -26,42 +23,48 @@ class _Package (object):
def __init__ (self):
raise TypeError, "Calling __init__ on portato.backend._Package objects is not allowed."
+def is_package(what):
+ return isinstance(what, _Package)
+
class SystemWrapper (SystemInterface):
"""This is a wrapper to the different system interfaces, allowing the direct import via C{from portato.backend import system}.
With this wrapper a change of the system is propagated to all imports."""
+ __system = 'portage'
+ __wrapped_sys = None
+ __slots__ = ('__system', '__wrapped_sys', 'set_system', '__load')
+
def __getattribute__ (self, name):
"""Just pass all attribute accesses directly to _sys."""
- return getattr(_sys, name)
-def set_system (new_sys):
- """Sets the current system to a new one.
+ if name in SystemWrapper.__slots__:
+ return object.__getattribute__(self, name)
+
+ if SystemWrapper.__wrapped_sys is None:
+ SystemWrapper.__load()
- @param new_sys: the name of the system to take
- @type new_sys: string"""
+ return getattr(SystemWrapper.__wrapped_sys, name)
- global SYSTEM
- if new_sys != SYSTEM:
- SYSTEM = new_sys
- load_system()
+ @classmethod
+ def set_system (cls, system):
+ """Sets the current system to a new one.
-def load_system ():
- """Loads the current chosen system.
+ @param system: the name of the system to take
+ @type system: string"""
- @raises InvalidSystemError: if an inappropriate system is set"""
-
- global _sys
+ cls.__system = system
+ cls.__wrapped_sys = None
- if SYSTEM == "portage":
- debug("Setting Portage System")
- from .portage import PortageSystem
- _sys = PortageSystem ()
- else:
- raise InvalidSystemError, SYSTEM
+ @classmethod
+ def __load (cls):
+ """Loads the current chosen system.
-system = SystemWrapper()
+ @raises InvalidSystemError: if an inappropriate system is set"""
+ if cls.__system == "portage":
+ debug("Setting Portage System")
+ from .portage import PortageSystem
+ cls.__wrapped_sys = PortageSystem ()
+ else:
+ raise InvalidSystemError, cls.__system
-def is_package(what):
- return isinstance(what, _Package)
-
-load_system()
+system = SystemWrapper()
diff --git a/portato/backend/package.py b/portato/backend/package.py
index 8a80fd5..c085816 100644
--- a/portato/backend/package.py
+++ b/portato/backend/package.py
@@ -371,16 +371,6 @@ class Package (_Package):
raise NotImplementedError
- def compare_version(self, other):
- """Compares this package's version to another's CPV; returns -1, 0, 1.
-
- @param other: the other package
- @type other: Package
- @returns: -1, 0 or 1
- @rtype: int"""
-
- raise NotImplementedError
-
def matches (self, criterion):
"""This checks, whether this package matches a specific versioning criterion - e.g.: "<=net-im/foobar-1.2".
diff --git a/portato/backend/portage/package.py b/portato/backend/portage/package.py
index 2b40e41..79af79b 100644
--- a/portato/backend/portage/package.py
+++ b/portato/backend/portage/package.py
@@ -294,18 +294,8 @@ class PortagePackage (Package):
return self.get_package_settings("USE", installed = True).split()
else: return []
- def compare_version(self,other):
- v1 = self._scpv
- v2 = portage.catpkgsplit(other.get_cpv())
- # if category is different
- if v1[0] != v2[0]:
- return cmp(v1[0],v2[0])
- # if name is different
- elif v1[1] != v2[1]:
- return cmp(v1[1],v2[1])
- # Compare versions
- else:
- return portage.pkgcmp(v1[1:],v2[1:])
+ def __cmp__ (self, other):
+ return system.compare_versions(self.get_cpv(), other.get_cpv())
def matches (self, criterion):
# cpv_matches needs explicit slot info
diff --git a/portato/backend/portage/system.py b/portato/backend/portage/system.py
index 94553c6..620ab73 100644
--- a/portato/backend/portage/system.py
+++ b/portato/backend/portage/system.py
@@ -125,6 +125,20 @@ class PortageSystem (SystemInterface):
else:
return True
+ def compare_versions(self, v1, v2):
+ v1 = self.split_cpv(v1)
+ v2 = self.split_cpv(v2)
+
+ # if category is different
+ if v1[0] != v2[0]:
+ return cmp(v1[0],v2[0])
+ # if name is different
+ elif v1[1] != v2[1]:
+ return cmp(v1[1],v2[1])
+ # Compare versions
+ else:
+ return portage.pkgcmp(v1[1:],v2[1:])
+
def with_bdeps(self):
"""Returns whether the "--with-bdeps" option is set to true.
@@ -223,8 +237,11 @@ class PortageSystem (SystemInterface):
cpv = portage.dep_getcpv(cpv)
return portage.catpkgsplit(cpv)
- def sort_package_list(self, pkglist):
- pkglist.sort(PortagePackage.compare_version) # XXX: waaah ... direct package naming... =/
+ def sort_package_list(self, pkglist, only_cpv = False):
+ if only_cpv:
+ pkglist.sort(self.compare_versions)
+ else:
+ pkglist.sort()
return pkglist
def reload_settings (self):
diff --git a/portato/backend/system_interface.py b/portato/backend/system_interface.py
index 43de1ec..f0bfdf6 100644
--- a/portato/backend/system_interface.py
+++ b/portato/backend/system_interface.py
@@ -68,6 +68,18 @@ class SystemInterface (object):
"""
raise NotImplementedError
+
+ def compare_versions(self, v1, v2):
+ """Compares two CPVs; returns -1, 0, 1.
+
+ @param v1: one CPV
+ @type v1: cpv
+ @param v2: the second CPV
+ @type v2: cpv
+ @returns: -1, 0 or 1
+ @rtype: int"""
+
+ raise NotImplementedError
def find_best(self, list, only_cpv = False):
"""Returns the best package out of a list of packages.
@@ -108,8 +120,8 @@ class SystemInterface (object):
@param key: the key to look for
@type key: string
- @param all: the package set to use
- @type all: string
+ @param pkgSet: the package set to use
+ @type pkgSet: string
@param masked: if True, also look for masked packages
@type masked: boolean
@param with_version: if True, return CPVs - else CP
@@ -134,11 +146,13 @@ class SystemInterface (object):
raise NotImplementedError
- def sort_package_list(self, pkglist):
+ def sort_package_list(self, pkglist, only_cpv = False):
"""Sorts a package list in the same manner portage does.
@param pkglist: list to sort
- @type pkglist: Packages[]
+ @type pkglist: Packages[] or cpv[]
+ @param only_cpv: flags whether the passed list consists of Packages or of CPVs
+ @type: bool
"""
raise NotImplementedError