# -*- coding: utf-8 -*- # # File: portato/backend/portage/system.py # This file is part of the Portato-Project, a graphical portage-frontend. # # Copyright (C) 2006-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 from __future__ import absolute_import, with_statement import re, os, os.path import portage from collections import defaultdict from .package import PortagePackage from .settings import PortageSettings from ..system_interface import SystemInterface from ...helper import debug, info, warning, unique_array class PortageSystem (SystemInterface): """This class provides access to the portage-system.""" # pre-compile the RE removing the ".svn" and "CVS" entries unwantedPkgsRE = re.compile(r".*(\.svn|CVS)$") withBdepsRE = re.compile(r"--with-bdeps\s*( |=)\s*y") def __init__ (self): """Constructor.""" self.settings = PortageSettings() portage.WORLD_FILE = os.path.join(self.settings.settings["ROOT"],portage.WORLD_FILE) self.use_descs = {} self.local_use_descs = defaultdict(dict) self._version = tuple([x.split("_")[0] for x in portage.VERSION.split(".")]) def get_version (self): return "Portage %s" % portage.VERSION def new_package (self, cpv): return PortagePackage(cpv) def get_config_path (self): return portage.USER_CONFIG_PATH def get_merge_command (self): return ["/usr/bin/python", "/usr/bin/emerge"] def get_sync_command (self): return self.get_merge_command()+["--sync"] def get_oneshot_option (self): return ["--oneshot"] def get_newuse_option (self): return ["--newuse"] def get_deep_option (self): return ["--deep"] def get_update_option (self): return ["--update"] def get_pretend_option (self): return ["--pretend", "--verbose"] def get_unmerge_option (self): return ["--unmerge"] def get_environment (self): default_opts = self.get_global_settings("EMERGE_DEFAULT_OPTS") opts = dict(os.environ) opts.update(TERM = "xterm") # emulate terminal :) opts.update(PAGER = "less") # force less if default_opts: opt_list = default_opts.split() changed = False for option in ["--ask", "-a", "--pretend", "-p"]: if option in opt_list: opt_list.remove(option) changed = True if changed: opts.update(EMERGE_DEFAULT_OPTS = " ".join(opt_list)) return opts def cpv_matches (self, cpv, criterion): if portage.match_from_list(criterion, [cpv]) == []: return False else: return True def with_bdeps(self): """Returns whether the "--with-bdeps" option is set to true. @returns: the value of --with-bdeps @rtype: boolean """ settings = self.get_global_settings("EMERGE_DEFAULT_OPTS").split() for s in settings: if self.withBdepsRE.match(s): return True return False def find_lambda (self, name): """Returns the function needed by all the find_all_*-functions. Returns None if no name is given. @param name: name to build the function of @type name: string or RE @returns: 1. None if no name is given 2. a lambda function @rtype: function """ if name != None: if isinstance(name, str): return lambda x: re.match(".*"+name+".*",x, re.I) else: # assume regular expression return lambda x: name.match(x) else: return lambda x: True def geneticize_list (self, list_of_packages, only_cpv = False): """Convertes a list of cpv's into L{backend.Package}s. @param list_of_packages: the list of packages @type list_of_packages: string[] @param only_cpv: do nothing - return the passed list @type only_cpv: boolean @returns: converted list @rtype: PortagePackage[] """ if not only_cpv: return [PortagePackage(x) for x in list_of_packages] else: return list_of_packages def get_global_settings (self, key): self.settings.settings.reset() return self.settings.settings[key] def find_best (self, list, only_cpv = False): if only_cpv: return portage.best(list) else: return PortagePackage(portage.best(list)) def find_best_match (self, search_key, masked = False, only_installed = False, only_cpv = False): t = [] if not only_installed: pkgSet = "tree" else: pkgSet = "installed" t = self.find_packages(search_key, pkgSet = pkgSet, masked = masked, with_version = True, only_cpv = True) if self._version >= (2,1,5): t += [pkg.get_cpv() for pkg in self.find_installed_packages(search_key) if not (pkg.is_testing(True) or pkg.is_masked())] if t: t = unique_array(t) return self.find_best(t, only_cpv) return None def find_packages (self, key = "", pkgSet = "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 _ws (key, crit, pkglist): pkgs = self.__find_resolved_unresolved(pkglist, crit, only_cpv = with_version)[0] if not with_version: pkgs = [x.get_cp(x) for x in list] if is_regexp: return filter(lambda x: re.match(key, x, re.I), pkgs) return pkgs def world (key): with open(portage.WORLD_FILE) as f: pkglist = f.readlines() return _ws(key, lambda cpv: cpv[0] != "#", pkglist) def system (key): return _ws(key, lambda cpv: cpv[0] == "*", self.settings.settings.packages) funcmap = { "all" : all, "installed" : installed, "uninstalled" : uninstalled, "world" : world, "system" : system, "tree" : tree } 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 # Make the list of packages unique t = unique_array(t) t.sort() return self.geneticize_list(t, only_cpv or not with_version) def __find_resolved_unresolved (self, list, check, only_cpv = False): """Checks a given list and divides it into a "resolved" and an "unresolved" part. @param list: list of cpv's @type list: string[] @param check: function called to check whether an entry is ok @type check: function(cpv) @param only_cpv: do not return packages but cpv-strings @type only_cpv: boolean @returns: the divided list: (resolved, unresolved) @rtype: (Package[], Package[]) or (string[], string[])""" resolved = [] unresolved = [] for x in list: cpv = x.strip() if len(cpv) and check(cpv): pkg = self.find_best_match(cpv, only_cpv = only_cpv) if pkg: resolved.append(pkg) else: unresolved.append(self.find_best_match(cpv, True, only_cpv = only_cpv)) return (resolved, unresolved) def list_categories (self, name = None): categories = self.settings.settings.categories return filter(self.find_lambda(name), categories) def split_cpv (self, cpv): cpv = portage.dep_getcpv(cpv) return portage.catpkgsplit(cpv) def sort_package_list(self, pkglist): pkglist.sort(PortagePackage.compare_version) return pkglist def reload_settings (self): self.settings.load() def get_new_packages (self, packages): """Gets a list of packages and returns the best choice for each in the portage tree. @param packages: the list of packages @type packages: string[] @returns: the list of packages @rtype: backend.Package[] """ new_packages = [] for p in packages: inst = self.find_packages(p, "installed") best_p = self.find_best_match(p) if best_p is None: best_p = self.find_best_match(p, masked = True) if best_p is None: warning(_("No best match for %s. It seems not to be in the tree anymore.") % p) continue else: debug("Best match for %s is masked" % p) if len(inst) > 1: myslots = set() for i in inst: # get the slots of the installed packages myslots.add(i.get_package_settings("SLOT")) myslots.add(best_p.get_package_settings("SLOT")) # add the slot of the best package in portage for slot in myslots: new_packages.append(\ self.find_best(self.find_packages("%s:%s" % (i.get_cp(), slot), only_cpv = True))) else: new_packages.append(best_p) return new_packages def get_updated_packages (self): packages = self.get_new_packages(self.find_packages(pkgSet = "installed", with_version = False)) packages = [x for x in packages if x is not None and not x.is_installed()] return packages def update_world (self, newuse = False, deep = False): # read world file world = open(portage.WORLD_FILE) packages = [] for line in world: line = line.strip() if len(line) == 0: continue # empty line if line[0] == "#": continue # comment packages.append(line) world.close() # append system packages packages.extend(unique_array([p.get_cp() for p in self.find_packages(pkgSet = "system")])) states = [(["RDEPEND", "PDEPEND"], True)] if self.with_bdeps(): states.append((["DEPEND"], True)) checked = [] updating = [] raw_checked = {} def check (p, add_not_installed = True, prev_appended = False): """Checks whether a package is updated or not.""" if p.get_slot_cp() in checked: return else: if (not p.is_installed()) and (not add_not_installed): # don't add these packages to checked as we may see them again # - and then we might have add_not_installed being True return else: checked.append(p.get_slot_cp()) appended = False tempDeep = False if not p.is_installed(): oldList = self.find_packages(p.get_slot_cp(), "installed") if oldList: old = oldList[0] # we should only have one package here - else it is a bug else: oldList = self.sort_package_list(self.find_packages(p.get_cp(), "installed")) if not oldList: info(_("Found a not installed dependency: %s.") % p.get_cpv()) oldList = [p] old = oldList[-1] updating.append((p, old)) appended = True p = old if newuse and p.is_installed() and p.is_in_system(): # there is no use to check newuse for a package which is not existing in portage anymore :) new_iuse = set(p.get_iuse_flags(installed = False)) # IUSE in the ebuild old_iuse = set(p.get_iuse_flags(installed = True)) # IUSE in the vardb # add forced flags, as they might trigger a rebuild new_iuse_f = set(p.get_iuse_flags(installed = False, removeForced = False)) old_iuse_f = set(p.get_iuse_flags(installed = True, removeForced = False)) if new_iuse.symmetric_difference(old_iuse): # difference between IUSE (w/o forced) tempDeep = True if not appended: updating.append((p,p)) appended = True else: # check for difference between the _set_ useflags (w/ forced) if new_iuse_f.intersection(p.get_actual_use_flags()).symmetric_difference(old_iuse_f.intersection(p.get_installed_use_flags())): tempDeep = True if not appended: updating.append((p,p)) appended = True if deep or tempDeep: if (appended or prev_appended) and len(states) < 2: real_states = states + [("PDEPEND", True), ("DEPEND", False)] else: real_states = states for state in real_states: for i in p.get_matched_dep_packages(state[0]): if i not in raw_checked or raw_checked[i] == False: raw_checked.update({i : state[1]}) bm = self.get_new_packages([i]) if not bm: warning(_("Bug? No best match could be found for '%(package)s'. Needed by: '%(cpv)s'."), {"package" : i, "cpv": p.get_cpv()}) else: for pkg in bm: if not pkg: continue if not pkg.is_installed() and (pkg.is_masked() or pkg.is_testing(True)): # check to not update unnecessairily cont = False for inst in self.find_packages(pkg.get_cp(), "installed", only_cpv = True): if self.cpv_matches(inst, i): debug("The installed %s matches %s. Discarding upgrade to masked version.", inst, i) cont = True break if cont: continue check(pkg, state[1], appended) # XXX: should be 'or'ed with prev_appended? for p in self.get_new_packages(packages): if not p: continue # if a masked package is installed we have "None" here check(p, True) return updating def get_use_desc (self, flag, package = None): # In the first run the dictionaries 'use_descs' and 'local_use_descs' are filled. # fill cache if needed if not self.use_descs and not self.local_use_descs: for dir in [self.settings.settings["PORTDIR"]] + self.settings.settings["PORTDIR_OVERLAY"].split(): # read use.desc try: f = open(os.path.join(dir, "profiles/use.desc")) for line in f: line = line.strip() if line and line[0] != '#': fields = [x.strip() for x in line.split(" - ",1)] if len(fields) == 2: self.use_descs[fields[0]] = fields[1] except IOError: pass finally: f.close() # read use.local.desc try: f = open(os.path.join(dir, "profiles/use.local.desc")) for line in f: line = line.strip() if line and line[0] != '#': fields = [x.strip() for x in line.split(":",1)] if len(fields) == 2: subfields = [x.strip() for x in fields[1].split(" - ",1)] if len(subfields) == 2: self.local_use_descs[fields[0]].update([subfields]) except IOError: pass finally: f.close() # start desc = self.use_descs.get(flag, "") if package is not None: if package in self.local_use_descs: desc = self.local_use_descs[package].get(flag, desc) return desc