blob: a09f73c596bd4fa34ec607b9c2db386b0c82eafe [file] [log] [blame]
# Copyright 1999-2014 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
from __future__ import unicode_literals
import sys
from itertools import chain
import warnings
import portage
from portage import _encodings, _unicode_decode, _unicode_encode
from portage.cache.mappings import slot_dict_class
from portage.const import EBUILD_PHASES
from portage.dep import Atom, check_required_use, use_reduce, \
paren_enclose, _slot_separator, _repo_separator
from portage.versions import _pkg_str, _unknown_repo
from portage.eapi import _get_eapi_attrs, eapi_has_use_aliases
from portage.exception import InvalidDependString
from portage.localization import _
from _emerge.Task import Task
if sys.hexversion >= 0x3000000:
basestring = str
long = int
_unicode = str
_unicode = unicode
class Package(Task):
__hash__ = Task.__hash__
__slots__ = ("built", "cpv", "depth",
"installed", "onlydeps", "operation",
"root_config", "type_name",
"category", "counter", "cp", "cpv_split",
"inherited", "iuse", "mtime",
"pf", "root", "slot", "sub_slot", "slot_atom", "version") + \
("_invalid", "_masks", "_metadata", "_raw_metadata", "_use",
"_validated_atoms", "_visible")
metadata_keys = [
"repository", "PROPERTIES", "RESTRICT", "SLOT", "USE",
_dep_keys = ('DEPEND', 'HDEPEND', 'PDEPEND', 'RDEPEND')
_buildtime_keys = ('DEPEND', 'HDEPEND')
_runtime_keys = ('PDEPEND', 'RDEPEND')
_use_conditional_misc_keys = ('LICENSE', 'PROPERTIES', 'RESTRICT')
UNKNOWN_REPO = _unknown_repo
def __init__(self, **kwargs):
metadata = _PackageMetadataWrapperBase(kwargs.pop('metadata'))
Task.__init__(self, **kwargs)
# the SlotObject constructor assigns self.root_config from keyword args
# and is an instance of a '_emerge.RootConfig.RootConfig class
self.root = self.root_config.root
self._raw_metadata = metadata
self._metadata = _PackageMetadataWrapper(self, metadata)
if not self.built:
self._metadata['CHOST'] = self.root_config.settings.get('CHOST', '')
eapi_attrs = _get_eapi_attrs(self.eapi)
self.cpv = _pkg_str(self.cpv, metadata=self._metadata,
if hasattr(self.cpv, 'slot_invalid'):
"SLOT: invalid value: '%s'" % self._metadata["SLOT"])
self.cpv_split = self.cpv.cpv_split
self.category, = portage.catsplit(self.cpv)
self.cp = self.cpv.cp
self.version = self.cpv.version
self.slot = self.cpv.slot
self.sub_slot = self.cpv.sub_slot
self.slot_atom = Atom("%s%s%s" % (self.cp, _slot_separator, self.slot))
# sync metadata with validated repo (may be UNKNOWN_REPO)
self._metadata['repository'] = self.cpv.repo
if eapi_attrs.iuse_effective:
implicit_match = self.root_config.settings._iuse_effective_match
implicit_match = self.root_config.settings._iuse_implicit_match
usealiases = self.root_config.settings._use_manager.getUseAliases(self)
self.iuse = self._iuse(self, self._metadata["IUSE"].split(), implicit_match,
usealiases, self.eapi)
if (self.iuse.enabled or self.iuse.disabled) and \
not eapi_attrs.iuse_defaults:
if not self.installed:
"IUSE contains defaults, but EAPI doesn't allow them")
if self.inherited is None:
self.inherited = frozenset()
if self.operation is None:
if self.onlydeps or self.installed:
self.operation = "nomerge"
self.operation = "merge"
self._hash_key = Package._gen_hash_key(cpv=self.cpv,
installed=self.installed, onlydeps=self.onlydeps,
operation=self.operation, repo_name=self.cpv.repo,
self._hash_value = hash(self._hash_key)
def eapi(self):
return self._metadata["EAPI"]
def build_time(self):
if not self.built:
raise AttributeError('build_time')
return long(self._metadata['BUILD_TIME'])
except (KeyError, ValueError):
return 0
def defined_phases(self):
return self._metadata.defined_phases
def properties(self):
def restrict(self):
return self._metadata.restrict
def metadata(self):
warnings.warn("_emerge.Package.Package.metadata is deprecated",
DeprecationWarning, stacklevel=3)
return self._metadata
# These are calculated on-demand, so that they are calculated
# after FakeVartree applies its metadata tweaks.
def invalid(self):
if self._invalid is None:
if self._invalid is None:
self._invalid = False
return self._invalid
def masks(self):
if self._masks is None:
self._masks = self._eval_masks()
return self._masks
def visible(self):
if self._visible is None:
self._visible = self._eval_visiblity(self.masks)
return self._visible
def validated_atoms(self):
Returns *all* validated atoms from the deps, regardless
of USE conditionals, with USE conditionals inside
atoms left unevaluated.
if self._validated_atoms is None:
return self._validated_atoms
def stable(self):
return self.cpv.stable
def _gen_hash_key(cls, cpv=None, installed=None, onlydeps=None,
operation=None, repo_name=None, root_config=None,
type_name=None, **kwargs):
if operation is None:
if installed or onlydeps:
operation = "nomerge"
operation = "merge"
root = None
if root_config is not None:
root = root_config.root
raise TypeError("root_config argument is required")
# For installed (and binary) packages we don't care for the repo
# when it comes to hashing, because there can only be one cpv.
# So overwrite the repo_key with type_name.
if type_name is None:
raise TypeError("type_name argument is required")
elif type_name == "ebuild":
if repo_name is None:
raise AssertionError(
"Package._gen_hash_key() " + \
"called without 'repo_name' argument")
repo_key = repo_name
# For installed (and binary) packages we don't care for the repo
# when it comes to hashing, because there can only be one cpv.
# So overwrite the repo_key with type_name.
repo_key = type_name
return (type_name, root, _unicode(cpv), operation, repo_key)
def _validate_deps(self):
Validate deps. This does not trigger USE calculation since that
is expensive for ebuilds and therefore we want to avoid doing
it unnecessarily (like for masked packages).
eapi = self.eapi
dep_eapi = eapi
dep_valid_flag = self.iuse.is_valid_flag
if self.installed:
# Ignore EAPI.incompatible and conditionals missing
# from IUSE for installed packages since these issues
# aren't relevant now (re-evaluate when new EAPIs are
# deployed).
dep_eapi = None
dep_valid_flag = None
validated_atoms = []
for k in self._dep_keys:
v = self._metadata.get(k)
if not v:
atoms = use_reduce(v, eapi=dep_eapi,
matchall=True, is_valid_flag=dep_valid_flag,
token_class=Atom, flat=True)
except InvalidDependString as e:
self._metadata_exception(k, e)
if not self.built:
for atom in atoms:
if not isinstance(atom, Atom):
if atom.slot_operator_built:
e = InvalidDependString(
_("Improper context for slot-operator "
"\"built\" atom syntax: %s") %
self._metadata_exception(k, e)
self._validated_atoms = tuple(set(atom for atom in
validated_atoms if isinstance(atom, Atom)))
v = self._metadata.get(k)
if v:
use_reduce(v, eapi=dep_eapi, matchall=True,
is_valid_flag=dep_valid_flag, token_class=Atom)
except InvalidDependString as e:
self._invalid_metadata("PROVIDE.syntax", "%s: %s" % (k, e))
for k in self._use_conditional_misc_keys:
v = self._metadata.get(k)
if not v:
use_reduce(v, eapi=dep_eapi, matchall=True,
except InvalidDependString as e:
self._metadata_exception(k, e)
v = self._metadata.get(k)
if v and not self.built:
if not _get_eapi_attrs(eapi).required_use:
"REQUIRED_USE set, but EAPI='%s' doesn't allow it" % eapi)
check_required_use(v, (),
self.iuse.is_valid_flag, eapi=eapi)
except InvalidDependString as e:
self._invalid_metadata(k + ".syntax", "%s: %s" % (k, e))
k = 'SRC_URI'
v = self._metadata.get(k)
if v:
use_reduce(v, is_src_uri=True, eapi=eapi, matchall=True,
except InvalidDependString as e:
if not self.installed:
self._metadata_exception(k, e)
def copy(self):
return Package(built=self.built, cpv=self.cpv, depth=self.depth,
installed=self.installed, metadata=self._raw_metadata,
onlydeps=self.onlydeps, operation=self.operation,
root_config=self.root_config, type_name=self.type_name)
def _eval_masks(self):
masks = {}
settings = self.root_config.settings
if self.invalid is not False:
masks['invalid'] = self.invalid
if not settings._accept_chost(self.cpv, self._metadata):
masks['CHOST'] = self._metadata['CHOST']
eapi = self.eapi
if not portage.eapi_is_supported(eapi):
masks['EAPI.unsupported'] = eapi
if portage._eapi_is_deprecated(eapi):
masks['EAPI.deprecated'] = eapi
missing_keywords = settings._getMissingKeywords(
self.cpv, self._metadata)
if missing_keywords:
masks['KEYWORDS'] = missing_keywords
missing_properties = settings._getMissingProperties(
self.cpv, self._metadata)
if missing_properties:
masks['PROPERTIES'] = missing_properties
except InvalidDependString:
# already recorded as 'invalid'
missing_restricts = settings._getMissingRestrict(
self.cpv, self._metadata)
if missing_restricts:
masks['RESTRICT'] = missing_restricts
except InvalidDependString:
# already recorded as 'invalid'
mask_atom = settings._getMaskAtom(self.cpv, self._metadata)
if mask_atom is not None:
masks['package.mask'] = mask_atom
missing_licenses = settings._getMissingLicenses(
self.cpv, self._metadata)
if missing_licenses:
masks['LICENSE'] = missing_licenses
except InvalidDependString:
# already recorded as 'invalid'
if not masks:
masks = False
return masks
def _eval_visiblity(self, masks):
if masks is not False:
if 'EAPI.unsupported' in masks:
return False
if 'invalid' in masks:
return False
if not self.installed and ( \
'CHOST' in masks or \
'EAPI.deprecated' in masks or \
'KEYWORDS' in masks or \
'PROPERTIES' in masks or \
'RESTRICT' in masks):
return False
if 'package.mask' in masks or \
'LICENSE' in masks:
return False
return True
def get_keyword_mask(self):
"""returns None, 'missing', or 'unstable'."""
missing = self.root_config.settings._getRawMissingKeywords(
self.cpv, self._metadata)
if not missing:
return None
if '**' in missing:
return 'missing'
global_accept_keywords = frozenset(
self.root_config.settings.get("ACCEPT_KEYWORDS", "").split())
for keyword in missing:
if keyword.lstrip("~") in global_accept_keywords:
return 'unstable'
return 'missing'
def isHardMasked(self):
"""returns a bool if the cpv is in the list of
expanded pmaskdict[cp] available ebuilds"""
pmask = self.root_config.settings._getRawMaskAtom(
self.cpv, self._metadata)
return pmask is not None
def _metadata_exception(self, k, e):
if k.endswith('DEPEND'):
qacat = 'dependency.syntax'
qacat = k + ".syntax"
# For unicode safety with python-2.x we need to avoid
# using the string format operator with a non-unicode
# format string, since that will result in the
# PortageException.__str__() method being invoked,
# followed by unsafe decoding that may result in a
# UnicodeDecodeError. Therefore, use unicode_literals
# to ensure that format strings are unicode, so that
# PortageException.__unicode__() is used when necessary
# in python-2.x.
if not self.installed:
categorized_error = False
if e.errors:
for error in e.errors:
if getattr(error, 'category', None) is None:
categorized_error = True
"%s: %s" % (k, error))
if not categorized_error:
self._invalid_metadata(qacat,"%s: %s" % (k, e))
# For installed packages, show the path of the file
# containing the invalid metadata, since the user may
# want to fix the deps by hand.
vardb = self.root_config.trees['vartree'].dbapi
path = vardb.getpath(self.cpv, filename=k)
self._invalid_metadata(qacat, "%s: %s in '%s'" % (k, e, path))
def _invalid_metadata(self, msg_type, msg):
if self._invalid is None:
self._invalid = {}
msgs = self._invalid.get(msg_type)
if msgs is None:
msgs = []
self._invalid[msg_type] = msgs
def __str__(self):
if self.operation == "merge":
if self.type_name == "binary":
cpv_color = "PKG_BINARY_MERGE"
cpv_color = "PKG_MERGE"
elif self.operation == "uninstall":
cpv_color = "PKG_UNINSTALL"
cpv_color = "PKG_NOMERGE"
s = "(%s, %s" \
% (portage.output.colorize(cpv_color, self.cpv + _slot_separator + \
self.slot + "/" + self.sub_slot + _repo_separator + self.repo) , self.type_name)
if self.type_name == "installed":
if self.root_config.settings['ROOT'] != "/":
s += " in '%s'" % self.root_config.settings['ROOT']
if self.operation == "uninstall":
s += " scheduled for uninstall"
if self.operation == "merge":
s += " scheduled for merge"
if self.root_config.settings['ROOT'] != "/":
s += " to '%s'" % self.root_config.settings['ROOT']
s += ")"
return s
if sys.hexversion < 0x3000000:
__unicode__ = __str__
def __str__(self):
return _unicode_encode(self.__unicode__(),
class _use_class(object):
__slots__ = ("enabled", "_expand", "_expand_hidden",
"_force", "_pkg", "_mask")
# Share identical frozenset instances when available.
_frozensets = {}
def __init__(self, pkg, enabled_flags):
self._pkg = pkg
self._expand = None
self._expand_hidden = None
self._force = None
self._mask = None
if eapi_has_use_aliases(pkg.eapi):
for enabled_flag in enabled_flags:
enabled_flags.extend(pkg.iuse.alias_mapping.get(enabled_flag, []))
self.enabled = frozenset(enabled_flags)
if pkg.built:
# Use IUSE to validate USE settings for built packages,
# in case the package manager that built this package
# failed to do that for some reason (or in case of
# data corruption).
missing_iuse = pkg.iuse.get_missing_iuse(self.enabled)
if missing_iuse:
self.enabled = self.enabled.difference(missing_iuse)
def _init_force_mask(self):
pkgsettings = self._pkg._get_pkgsettings()
frozensets = self._frozensets
s = frozenset(
pkgsettings.get("USE_EXPAND", "").lower().split())
self._expand = frozensets.setdefault(s, s)
s = frozenset(
pkgsettings.get("USE_EXPAND_HIDDEN", "").lower().split())
self._expand_hidden = frozensets.setdefault(s, s)
s = pkgsettings.useforce
self._force = frozensets.setdefault(s, s)
s = pkgsettings.usemask
self._mask = frozensets.setdefault(s, s)
def expand(self):
if self._expand is None:
return self._expand
def expand_hidden(self):
if self._expand_hidden is None:
return self._expand_hidden
def force(self):
if self._force is None:
return self._force
def mask(self):
if self._mask is None:
return self._mask
def repo(self):
return self._metadata['repository']
def repo_priority(self):
repo_info = self.root_config.settings.repositories.prepos.get(self.repo)
if repo_info is None:
return None
return repo_info.priority
def use(self):
if self._use is None:
return self._use
def _get_pkgsettings(self):
pkgsettings = self.root_config.trees[
return pkgsettings
def _init_use(self):
if self.built:
# Use IUSE to validate USE settings for built packages,
# in case the package manager that built this package
# failed to do that for some reason (or in case of
# data corruption). The enabled flags must be consistent
# with implicit IUSE, in order to avoid potential
# inconsistencies in USE dep matching (see bug #453400).
use_str = self._metadata['USE']
is_valid_flag = self.iuse.is_valid_flag
enabled_flags = [x for x in use_str.split() if is_valid_flag(x)]
use_str = " ".join(enabled_flags)
self._use = self._use_class(
self, enabled_flags)
use_str = _PackageMetadataWrapperBase.__getitem__(
self._metadata, 'USE')
except KeyError:
use_str = None
calculated_use = False
if not use_str:
use_str = self._get_pkgsettings()["PORTAGE_USE"]
calculated_use = True
self._use = self._use_class(
self, use_str.split())
# Initialize these now, since USE access has just triggered
# setcpv, and we want to cache the result of the force/mask
# calculations that were done.
if calculated_use:
self._metadata, 'USE', use_str)
return use_str
class _iuse(object):
__slots__ = ("__weakref__", "_iuse_implicit_match", "_pkg", "alias_mapping",
"all", "all_aliases", "enabled", "disabled", "tokens")
def __init__(self, pkg, tokens, iuse_implicit_match, aliases, eapi):
self._pkg = pkg
self.tokens = tuple(tokens)
self._iuse_implicit_match = iuse_implicit_match
enabled = []
disabled = []
other = []
enabled_aliases = []
disabled_aliases = []
other_aliases = []
aliases_supported = eapi_has_use_aliases(eapi)
self.alias_mapping = {}
for x in tokens:
prefix = x[:1]
if prefix == "+":
if aliases_supported:
self.alias_mapping[x[1:]] = aliases.get(x[1:], [])
elif prefix == "-":
if aliases_supported:
self.alias_mapping[x[1:]] = aliases.get(x[1:], [])
if aliases_supported:
self.alias_mapping[x] = aliases.get(x, [])
self.enabled = frozenset(chain(enabled, enabled_aliases))
self.disabled = frozenset(chain(disabled, disabled_aliases))
self.all = frozenset(chain(enabled, disabled, other))
self.all_aliases = frozenset(chain(enabled_aliases, disabled_aliases, other_aliases))
def is_valid_flag(self, flags):
@return: True if all flags are valid USE values which may
be specified in USE dependencies, False otherwise.
if isinstance(flags, basestring):
flags = [flags]
for flag in flags:
if not flag in self.all and not flag in self.all_aliases and \
not self._iuse_implicit_match(flag):
return False
return True
def get_missing_iuse(self, flags):
@return: A list of flags missing from IUSE.
if isinstance(flags, basestring):
flags = [flags]
missing_iuse = []
for flag in flags:
if not flag in self.all and not flag in self.all_aliases and \
not self._iuse_implicit_match(flag):
return missing_iuse
def get_real_flag(self, flag):
Returns the flag's name within the scope of this package
(accounting for aliases), or None if the flag is unknown.
if flag in self.all:
return flag
elif flag in self.all_aliases:
for k, v in self.alias_mapping.items():
if flag in v:
return k
if self._iuse_implicit_match(flag):
return flag
return None
def __len__(self):
return 4
def __iter__(self):
This is used to generate mtimedb resume mergelist entries, so we
limit it to 4 items for backward compatibility.
return iter(self._hash_key[:4])
def __lt__(self, other):
if other.cp != self.cp:
return False
if portage.vercmp(self.version, other.version) < 0:
return True
return False
def __le__(self, other):
if other.cp != self.cp:
return False
if portage.vercmp(self.version, other.version) <= 0:
return True
return False
def __gt__(self, other):
if other.cp != self.cp:
return False
if portage.vercmp(self.version, other.version) > 0:
return True
return False
def __ge__(self, other):
if other.cp != self.cp:
return False
if portage.vercmp(self.version, other.version) >= 0:
return True
return False
_all_metadata_keys = set(x for x in portage.auxdbkeys \
if not x.startswith("UNUSED_"))
_all_metadata_keys = frozenset(_all_metadata_keys)
_PackageMetadataWrapperBase = slot_dict_class(_all_metadata_keys)
class _PackageMetadataWrapper(_PackageMetadataWrapperBase):
Detect metadata updates and synchronize Package attributes.
__slots__ = ("_pkg",)
_wrapped_keys = frozenset(
["COUNTER", "INHERITED", "USE", "_mtime_"])
_use_conditional_keys = frozenset(
def __init__(self, pkg, metadata):
self._pkg = pkg
if not pkg.built:
# USE is lazy, but we want it to show up in self.keys().
_PackageMetadataWrapperBase.__setitem__(self, 'USE', '')
def __getitem__(self, k):
v = _PackageMetadataWrapperBase.__getitem__(self, k)
if k in self._use_conditional_keys:
if self._pkg.root_config.settings.local_config and '?' in v:
v = paren_enclose(use_reduce(v, uselist=self._pkg.use.enabled, \
except InvalidDependString:
# This error should already have been registered via
# self._pkg._invalid_metadata().
self[k] = v
elif k == 'USE' and not self._pkg.built:
if not v:
# This is lazy because it's expensive.
v = self._pkg._init_use()
return v
def __setitem__(self, k, v):
_PackageMetadataWrapperBase.__setitem__(self, k, v)
if k in self._wrapped_keys:
getattr(self, "_set_" + k.lower())(k, v)
def _set_inherited(self, k, v):
if isinstance(v, basestring):
v = frozenset(v.split())
self._pkg.inherited = v
def _set_counter(self, k, v):
if isinstance(v, basestring):
v = long(v.strip())
except ValueError:
v = 0
self._pkg.counter = v
def _set_use(self, k, v):
# Force regeneration of _use attribute
self._pkg._use = None
# Use raw metadata to restore USE conditional values
# to unevaluated state
raw_metadata = self._pkg._raw_metadata
for x in self._use_conditional_keys:
self[x] = raw_metadata[x]
except KeyError:
def _set__mtime_(self, k, v):
if isinstance(v, basestring):
v = long(v.strip())
except ValueError:
v = 0
self._pkg.mtime = v
def properties(self):
return self['PROPERTIES'].split()
def restrict(self):
return self['RESTRICT'].split()
def defined_phases(self):
Returns tokens from DEFINED_PHASES metadata if it is defined,
otherwise returns a tuple containing all possible phases. This
makes it easy to do containment checks to see if it's safe to
skip execution of a given phase.
s = self['DEFINED_PHASES']
if s:
return s.split()