Extend the _pkg_str class with build_id, build_time, file_size, and
mtime attributes. These will be used to distinguish binary package
instances that have the same cpv. Package sorting accounts for
build_time, which will be used to prefer newer builds over older builds
when their versions are identical.
---
[PATCH 1/7 v2] updates pkg_desc_index._pkg_node to have a build_time
attribute, which fixes an AttributeError raised from dbapi._cmp_cpv
for some emerge search actions.

 pym/_emerge/Package.py                    | 51 +++++++++++++++++++++----------
 pym/_emerge/resolver/output.py            | 21 ++++++++++---
 pym/portage/cache/index/pkg_desc_index.py |  1 +
 pym/portage/dbapi/__init__.py             | 10 ++++--
 pym/portage/dbapi/vartree.py              |  8 +++--
 pym/portage/versions.py                   | 28 +++++++++++++++--
 6 files changed, 93 insertions(+), 26 deletions(-)

diff --git a/pym/_emerge/Package.py b/pym/_emerge/Package.py
index e8a13cb..975335d 100644
--- a/pym/_emerge/Package.py
+++ b/pym/_emerge/Package.py
@@ -41,12 +41,12 @@ class Package(Task):
                "_validated_atoms", "_visible")
 
        metadata_keys = [
-               "BUILD_TIME", "CHOST", "COUNTER", "DEPEND", "EAPI",
-               "HDEPEND", "INHERITED", "IUSE", "KEYWORDS",
-               "LICENSE", "PDEPEND", "PROVIDE", "RDEPEND",
-               "repository", "PROPERTIES", "RESTRICT", "SLOT", "USE",
-               "_mtime_", "DEFINED_PHASES", "REQUIRED_USE", "PROVIDES",
-               "REQUIRES"]
+               "BUILD_ID", "BUILD_TIME", "CHOST", "COUNTER", "DEFINED_PHASES",
+               "DEPEND", "EAPI", "HDEPEND", "INHERITED", "IUSE", "KEYWORDS",
+               "LICENSE", "MD5", "PDEPEND", "PROVIDE", "PROVIDES",
+               "RDEPEND", "repository", "REQUIRED_USE",
+               "PROPERTIES", "REQUIRES", "RESTRICT", "SIZE",
+               "SLOT", "USE", "_mtime_"]
 
        _dep_keys = ('DEPEND', 'HDEPEND', 'PDEPEND', 'RDEPEND')
        _buildtime_keys = ('DEPEND', 'HDEPEND')
@@ -114,13 +114,14 @@ class Package(Task):
                return self._metadata["EAPI"]
 
        @property
+       def build_id(self):
+               return self.cpv.build_id
+
+       @property
        def build_time(self):
                if not self.built:
                        raise AttributeError('build_time')
-               try:
-                       return long(self._metadata['BUILD_TIME'])
-               except (KeyError, ValueError):
-                       return 0
+               return self.cpv.build_time
 
        @property
        def defined_phases(self):
@@ -509,9 +510,15 @@ class Package(Task):
                else:
                        cpv_color = "PKG_NOMERGE"
 
+               build_id_str = ""
+               if isinstance(self.cpv.build_id, long) and self.cpv.build_id > 
0:
+                       build_id_str = "-%s" % self.cpv.build_id
+
                s = "(%s, %s" \
-                       % (portage.output.colorize(cpv_color, self.cpv + 
_slot_separator + \
-                       self.slot + "/" + self.sub_slot + _repo_separator + 
self.repo) , self.type_name)
+                       % (portage.output.colorize(cpv_color, self.cpv +
+                       build_id_str + _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'] != "/":
@@ -755,29 +762,41 @@ class Package(Task):
        def __lt__(self, other):
                if other.cp != self.cp:
                        return self.cp < other.cp
-               if portage.vercmp(self.version, other.version) < 0:
+               result = portage.vercmp(self.version, other.version)
+               if result < 0:
                        return True
+               if result == 0 and self.built and other.built:
+                       return self.build_time < other.build_time
                return False
 
        def __le__(self, other):
                if other.cp != self.cp:
                        return self.cp <= other.cp
-               if portage.vercmp(self.version, other.version) <= 0:
+               result = portage.vercmp(self.version, other.version)
+               if result <= 0:
                        return True
+               if result == 0 and self.built and other.built:
+                       return self.build_time <= other.build_time
                return False
 
        def __gt__(self, other):
                if other.cp != self.cp:
                        return self.cp > other.cp
-               if portage.vercmp(self.version, other.version) > 0:
+               result = portage.vercmp(self.version, other.version)
+               if result > 0:
                        return True
+               if result == 0 and self.built and other.built:
+                       return self.build_time > other.build_time
                return False
 
        def __ge__(self, other):
                if other.cp != self.cp:
                        return self.cp >= other.cp
-               if portage.vercmp(self.version, other.version) >= 0:
+               result = portage.vercmp(self.version, other.version)
+               if result >= 0:
                        return True
+               if result == 0 and self.built and other.built:
+                       return self.build_time >= other.build_time
                return False
 
        def with_use(self, use):
diff --git a/pym/_emerge/resolver/output.py b/pym/_emerge/resolver/output.py
index 7df0302..400617d 100644
--- a/pym/_emerge/resolver/output.py
+++ b/pym/_emerge/resolver/output.py
@@ -424,6 +424,18 @@ class Display(object):
                        pkg_str += _repo_separator + pkg.repo
                return pkg_str
 
+       def _append_build_id(self, pkg_str, pkg, pkg_info):
+               """Potentially appends repository to package string.
+
+               @param pkg_str: string
+               @param pkg: _emerge.Package.Package instance
+               @param pkg_info: dictionary
+               @rtype string
+               """
+               if pkg.type_name == "binary" and pkg.cpv.build_id is not None:
+                       pkg_str += "-%s" % pkg.cpv.build_id
+               return pkg_str
+
        def _set_non_root_columns(self, pkg, pkg_info):
                """sets the indent level and formats the output
 
@@ -431,7 +443,7 @@ class Display(object):
                @param pkg_info: dictionary
                @rtype string
                """
-               ver_str = pkg_info.ver
+               ver_str = self._append_build_id(pkg_info.ver, pkg, pkg_info)
                if self.conf.verbosity == 3:
                        ver_str = self._append_slot(ver_str, pkg, pkg_info)
                        ver_str = self._append_repository(ver_str, pkg, 
pkg_info)
@@ -470,7 +482,7 @@ class Display(object):
                @rtype string
                Modifies self.verboseadd
                """
-               ver_str = pkg_info.ver
+               ver_str = self._append_build_id(pkg_info.ver, pkg, pkg_info)
                if self.conf.verbosity == 3:
                        ver_str = self._append_slot(ver_str, pkg, pkg_info)
                        ver_str = self._append_repository(ver_str, pkg, 
pkg_info)
@@ -507,7 +519,7 @@ class Display(object):
                @param pkg_info: dictionary
                @rtype the updated addl
                """
-               pkg_str = pkg.cpv
+               pkg_str = self._append_build_id(pkg.cpv, pkg, pkg_info)
                if self.conf.verbosity == 3:
                        pkg_str = self._append_slot(pkg_str, pkg, pkg_info)
                        pkg_str = self._append_repository(pkg_str, pkg, 
pkg_info)
@@ -868,7 +880,8 @@ class Display(object):
                                        if self.conf.columns:
                                                myprint = 
self._set_non_root_columns(pkg, pkg_info)
                                        else:
-                                               pkg_str = pkg.cpv
+                                               pkg_str = self._append_build_id(
+                                                       pkg.cpv, pkg, pkg_info)
                                                if self.conf.verbosity == 3:
                                                        pkg_str = 
self._append_slot(pkg_str, pkg, pkg_info)
                                                        pkg_str = 
self._append_repository(pkg_str, pkg, pkg_info)
diff --git a/pym/portage/cache/index/pkg_desc_index.py 
b/pym/portage/cache/index/pkg_desc_index.py
index a2e45da..dbcbb83 100644
--- a/pym/portage/cache/index/pkg_desc_index.py
+++ b/pym/portage/cache/index/pkg_desc_index.py
@@ -26,6 +26,7 @@ class pkg_node(_unicode):
                self.__dict__['cp'] = cp
                self.__dict__['repo'] = repo
                self.__dict__['version'] = version
+               self.__dict__['build_time'] = None
 
        def __new__(cls, cp, version, repo=None):
                return _unicode.__new__(cls, cp + "-" + version)
diff --git a/pym/portage/dbapi/__init__.py b/pym/portage/dbapi/__init__.py
index 34dfaa7..044faec 100644
--- a/pym/portage/dbapi/__init__.py
+++ b/pym/portage/dbapi/__init__.py
@@ -31,7 +31,8 @@ class dbapi(object):
        _use_mutable = False
        _known_keys = frozenset(x for x in auxdbkeys
                if not x.startswith("UNUSED_0"))
-       _pkg_str_aux_keys = ("EAPI", "KEYWORDS", "SLOT", "repository")
+       _pkg_str_aux_keys = ("BUILD_TIME", "EAPI", "BUILD_ID",
+               "KEYWORDS", "SLOT", "repository")
 
        def __init__(self):
                pass
@@ -57,7 +58,12 @@ class dbapi(object):
 
        @staticmethod
        def _cmp_cpv(cpv1, cpv2):
-               return vercmp(cpv1.version, cpv2.version)
+               result = vercmp(cpv1.version, cpv2.version)
+               if (result == 0 and cpv1.build_time is not None and
+                       cpv2.build_time is not None):
+                       result = ((cpv1.build_time > cpv2.build_time) -
+                               (cpv1.build_time < cpv2.build_time))
+               return result
 
        @staticmethod
        def _cpv_sort_ascending(cpv_list):
diff --git a/pym/portage/dbapi/vartree.py b/pym/portage/dbapi/vartree.py
index cf31c8e..277c2f1 100644
--- a/pym/portage/dbapi/vartree.py
+++ b/pym/portage/dbapi/vartree.py
@@ -173,7 +173,8 @@ class vardbapi(dbapi):
                self.vartree = vartree
                self._aux_cache_keys = set(
                        ["BUILD_TIME", "CHOST", "COUNTER", "DEPEND", 
"DESCRIPTION",
-                       "EAPI", "HDEPEND", "HOMEPAGE", "IUSE", "KEYWORDS",
+                       "EAPI", "HDEPEND", "HOMEPAGE",
+                       "BUILD_ID", "IUSE", "KEYWORDS",
                        "LICENSE", "PDEPEND", "PROPERTIES", "PROVIDE", 
"RDEPEND",
                        "repository", "RESTRICT" , "SLOT", "USE", 
"DEFINED_PHASES",
                        "PROVIDES", "REQUIRES"
@@ -425,7 +426,10 @@ class vardbapi(dbapi):
                                continue
                        if len(mysplit) > 1:
                                if ps[0] == mysplit[1]:
-                                       
returnme.append(_pkg_str(mysplit[0]+"/"+x))
+                                       cpv = "%s/%s" % (mysplit[0], x)
+                                       metadata = 
dict(zip(self._aux_cache_keys,
+                                               self.aux_get(cpv, 
self._aux_cache_keys)))
+                                       returnme.append(_pkg_str(cpv, 
metadata=metadata))
                self._cpv_sort_ascending(returnme)
                if use_cache:
                        self.cpcache[mycp] = [mystat, returnme[:]]
diff --git a/pym/portage/versions.py b/pym/portage/versions.py
index 2c9fe5b..1ca9a36 100644
--- a/pym/portage/versions.py
+++ b/pym/portage/versions.py
@@ -18,6 +18,7 @@ if sys.hexversion < 0x3000000:
        _unicode = unicode
 else:
        _unicode = str
+       long = int
 
 import portage
 portage.proxy.lazyimport.lazyimport(globals(),
@@ -361,11 +362,13 @@ class _pkg_str(_unicode):
        """
 
        def __new__(cls, cpv, metadata=None, settings=None, eapi=None,
-               repo=None, slot=None):
+               repo=None, slot=None, build_time=None, build_id=None,
+               file_size=None, mtime=None):
                return _unicode.__new__(cls, cpv)
 
        def __init__(self, cpv, metadata=None, settings=None, eapi=None,
-               repo=None, slot=None):
+               repo=None, slot=None, build_time=None, build_id=None,
+               file_size=None, mtime=None):
                if not isinstance(cpv, _unicode):
                        # Avoid TypeError from _unicode.__init__ with PyPy.
                        cpv = _unicode_decode(cpv)
@@ -375,10 +378,19 @@ class _pkg_str(_unicode):
                        slot = metadata.get('SLOT', slot)
                        repo = metadata.get('repository', repo)
                        eapi = metadata.get('EAPI', eapi)
+                       build_time = metadata.get('BUILD_TIME', build_time)
+                       file_size = metadata.get('SIZE', file_size)
+                       build_id = metadata.get('BUILD_ID', build_id)
+                       mtime = metadata.get('_mtime_', mtime)
                if settings is not None:
                        self.__dict__['_settings'] = settings
                if eapi is not None:
                        self.__dict__['eapi'] = eapi
+
+               self.__dict__['build_time'] = self._long(build_time, 0)
+               self.__dict__['file_size'] = self._long(file_size, None)
+               self.__dict__['build_id'] = self._long(build_id, None)
+               self.__dict__['mtime'] = self._long(mtime, None)
                self.__dict__['cpv_split'] = catpkgsplit(cpv, eapi=eapi)
                if self.cpv_split is None:
                        raise InvalidData(cpv)
@@ -419,6 +431,18 @@ class _pkg_str(_unicode):
                raise AttributeError("_pkg_str instances are immutable",
                        self.__class__, name, value)
 
+       @staticmethod
+       def _long(var, default):
+               if var is not None:
+                       try:
+                               var = long(var)
+                       except ValueError:
+                               if var:
+                                       var = -1
+                               else:
+                                       var = default
+               return var
+
        @property
        def stable(self):
                try:
-- 
2.0.5


Reply via email to