diff options
Diffstat (limited to '')
-rw-r--r-- | portato/backend/portage/sets.py | 128 | ||||
-rw-r--r-- | portato/backend/portage/system.py | 112 |
2 files changed, 145 insertions, 95 deletions
diff --git a/portato/backend/portage/sets.py b/portato/backend/portage/sets.py new file mode 100644 index 0000000..2305c85 --- /dev/null +++ b/portato/backend/portage/sets.py @@ -0,0 +1,128 @@ +# -*- coding: utf-8 -*- +# +# File: portato/backend/portage/sets.py +# This file is part of the Portato-Project, a graphical portage-frontend. +# +# Copyright (C) 2008 René 'Necoro' Neumann +# This is free software. You may redistribute copies of it under the terms of +# the GNU General Public License version 2. +# There is NO WARRANTY, to the extent permitted by law. +# +# Written by René 'Necoro' Neumann <necoro@necoro.net> + +from __future__ import absolute_import, with_statement + +import re + +from .. import system + +class Set(object): + + def get_pkgs(self, key, is_regexp, masked, with_version, only_cpv): + raise NotImplementedError + + def find (self, key, masked = False, with_version = True, only_cpv = False): + if key is None: key = "" + + is_regexp = key == "" or ("*" in key and key[0] not in ("*","=","<",">","~","!")) + + try: + t = self.get_pkgs(key, is_regexp, masked, with_version, only_cpv) + # catch the "ambigous package" Exception + except ValueError, e: + if isinstance(e[0], list): + t = set() + for cp in e[0]: + t.update(self.get_pkgs(cp, is_regexp, masked, with_version, only_cpv)) + else: + raise + + return t + +class FilterSet (Set): + + def get_list(self): + raise NotImplementedError + + def get_pkgs (self, key, is_regexp, masked, with_version, only_cpv): + t = set() + for pkg in self.get_list(): + if is_regexp: + if not re.match(key, pkg, re.I): continue + + if not with_version: + t.add(portage_dep.dep_getkey(pkg)) + + t.add(system.find_best_match(pkg, only_cpv = True)) + + return t + +class SystemSet (FilterSet): + + def get_list(self): + for cp in system.settings.settings.packages: + if cp[0] == "*": yield cp[1:] + +class WorldSet (FilterSet): + + def get_list(self): + with open(portage.WORLD_FILE) as f: + for cp in f: + cp = cp.strip() + if cp and cp[0] != "#": + yield cp + +class InstalledSet (Set): + + def get_pkgs (self, key, is_regexp, masked, with_version, only_cpv): + if is_regexp: + if with_version: + t = system.settings.vartree.dbapi.cpv_all() + else: + t = system.settings.vartree.dbapi.cp_all() + + if key: + t = filter(lambda x: re.match(key, x, re.I), t) + + return set(t) + else: + return set(system.settings.vartree.dbapi.match(key)) + +class TreeSet (Set): + + def get_pkgs (self, key, is_regexp, masked, with_version, only_cpv): + if is_regexp: + if with_version: + t = system.settings.porttree.dbapi.cpv_all() + else: + t = system.settings.porttree.dbapi.cp_all() + + if key: + t = filter(lambda x: re.match(key, x, re.I), t) + + elif masked: + t = system.settings.porttree.dbapi.xmatch("match-all", key) + else: + t = system.settings.porttree.dbapi.match(key) + + return set(t) + +class AllSet (Set): + + def __init__ (self): + Set.__init__(self) + self.tree = TreeSet() + self.installed = InstalledSet() + + def find (self, *args, **kwargs): + return self.tree.find(*args, **kwargs) | self.installed.find(*args, **kwargs) + +class UninstalledSet (Set): + + def __init__ (self): + Set.__init__(self) + self.all = AllSet() + self.installed = InstalledSet() + + def find (self, *args, **kwargs): + return self.all.find(*args, **kwargs) - self.installed.find(*args, **kwargs) diff --git a/portato/backend/portage/system.py b/portato/backend/portage/system.py index edbb4f0..c241a4b 100644 --- a/portato/backend/portage/system.py +++ b/portato/backend/portage/system.py @@ -23,6 +23,7 @@ from collections import defaultdict import itertools as itt from . import VERSION +from . import sets as syssets from .package import PortagePackage from .settings import PortageSettings from ..system_interface import SystemInterface @@ -43,6 +44,15 @@ class PortageSystem (SystemInterface): self.use_descs = {} self.local_use_descs = defaultdict(dict) + self.setmap = { + self.SET_ALL : syssets.AllSet, + self.SET_INSTALLED : syssets.InstalledSet, + self.SET_UNINSTALLED : syssets.UninstalledSet, + self.SET_TREE : syssets.TreeSet, + "world" : syssets.WorldSet, + "system" : syssets.SystemSet + } + def has_set_support (self): return False @@ -157,6 +167,8 @@ class PortageSystem (SystemInterface): if not only_cpv: return [self.new_package(x) for x in list_of_packages] + elif not isinstance(list_of_packages, list): + return list(list_of_packages) else: return list_of_packages @@ -191,104 +203,14 @@ class PortageSystem (SystemInterface): return None - def find_packages (self, key = "", pkgSet = SystemInterface.SET_ALL, masked = False, with_version = True, only_cpv = False): - if key is None: key = "" - - is_regexp = key == "" or ("*" in key and key[0] not in ("*","=","<",">","~","!")) - - def installed(key): - if is_regexp: - if with_version: - t = self.settings.vartree.dbapi.cpv_all() - else: - t = self.settings.vartree.dbapi.cp_all() - - if key: - t = filter(lambda x: re.match(key, x, re.I), t) - - return t - else: - return self.settings.vartree.dbapi.match(key) - - def tree(key): - if is_regexp: - if with_version: - t = self.settings.porttree.dbapi.cpv_all() - else: - t = self.settings.porttree.dbapi.cp_all() - - if key: - t = filter(lambda x: re.match(key, x, re.I), t) - - elif masked: - t = self.settings.porttree.dbapi.xmatch("match-all", key) - else: - t = self.settings.porttree.dbapi.match(key) - - return t - - def all(key): - return unique_array(installed(key)+tree(key)) - - def uninstalled (key): - alist = set(all(key)) - inst = set(installed(key)) - return list(alist - inst) - - def world (key): - with open(portage.WORLD_FILE) as f: - for cp in f: - cp = cp.strip() - if cp and cp[0] != "#": - if is_regexp: - if not re.match(key, cp, re.I): continue - - if not with_version: - yield portage_dep.dep_getkey(cp) - - yield self.find_best_match(cp, only_cpv = True) - - def system (key): - for cp in self.settings.settings.packages: - if cp[0] != "*": continue - - if is_regexp: - if not re.match(key, cp, re.I): continue - - if not with_version: - yield portage_dep.dep_getkey(cp) - - yield self.find_best_match(cp, only_cpv = True) - - funcmap = { - self.SET_ALL : all, - self.SET_INSTALLED : installed, - self.SET_UNINSTALLED : uninstalled, - self.SET_TREE : tree, - "world" : world, - "system" : system - } - + def _get_set (self, pkgSet): pkgSet = pkgSet.lower() - if pkgSet == "": pkgSet = "all" - - func = funcmap[pkgSet] - - try: - t = func(key) - # catch the "ambigous package" Exception - except ValueError, e: - if isinstance(e[0], list): - t = [] - for cp in e[0]: - t += func(cp) - else: - raise + if pkgSet == "": pkgSet = self.SET_ALL - # Make the list of packages unique - t = unique_array(t) + return self.setmap[pkgSet]() - return self.geneticize_list(t, only_cpv or not with_version) + def find_packages (self, key = "", pkgSet = SystemInterface.SET_ALL, masked = False, with_version = True, only_cpv = False): + return self.geneticize_list(self._get_set(pkgSet).find(key, masked, with_version, only_cpv), only_cpv or not with_version) def list_categories (self, name = None): categories = self.settings.settings.categories |