Gentoo Archives: gentoo-portage-dev

From: Brian Dolbec <dolsen@g.o>
To: gentoo-portage-dev@l.g.o
Subject: Re: [gentoo-portage-dev] [PATCH] binarytree._read_metadata: return empty strings for undefine values (bug 603826)
Date: Tue, 27 Dec 2016 23:39:07
Message-Id: 20161227153906.3c1c2222.dolsen@gentoo.org
In Reply to: [gentoo-portage-dev] [PATCH] binarytree._read_metadata: return empty strings for undefine values (bug 603826) by Zac Medico
1 On Tue, 27 Dec 2016 13:33:55 -0800
2 Zac Medico <zmedico@g.o> wrote:
3
4 > Fix the binarytree._read_metadata method to return empty strings for
5 > undefined metadata values, in order to fulfill a contract with the
6 > _pkg_str class. This prevents an AttributeError triggered by old
7 > binary packages which have undefined repository metadata, as reported
8 > in bug 603826.
9 >
10 > X-Gentoo-bug: 603826
11 > X-Gentoo-bug-url: https://bugs.gentoo.org/603826
12 > ---
13 > pym/portage/dbapi/bintree.py | 31 +++++++++++++++++++++++++------
14 > pym/portage/versions.py | 8 +++++++-
15 > 2 files changed, 32 insertions(+), 7 deletions(-)
16 >
17 > diff --git a/pym/portage/dbapi/bintree.py
18 > b/pym/portage/dbapi/bintree.py index f483059..3341889 100644
19 > --- a/pym/portage/dbapi/bintree.py
20 > +++ b/pym/portage/dbapi/bintree.py
21 > @@ -1,4 +1,4 @@
22 > -# Copyright 1998-2014 Gentoo Foundation
23 > +# Copyright 1998-2016 Gentoo Foundation
24 > # Distributed under the terms of the GNU General Public License v2
25 >
26 > from __future__ import unicode_literals
27 > @@ -1041,12 +1041,12 @@ class binarytree(object):
28 > noiselevel=-1)
29 > return
30 > metadata = self._read_metadata(full_path, s)
31 > - slot = metadata.get("SLOT")
32 > + invalid_depend = False
33 > try:
34 > self._eval_use_flags(cpv, metadata)
35 > except portage.exception.InvalidDependString:
36 > - slot = None
37 > - if slot is None:
38 > + invalid_depend = True
39 > + if invalid_depend or not metadata.get("SLOT"):
40 > writemsg(_("!!! Invalid binary package:
41 > '%s'\n") % full_path, noiselevel=-1)
42 > return
43 > @@ -1126,6 +1126,21 @@ class binarytree(object):
44 > return cpv
45 >
46 > def _read_metadata(self, filename, st, keys=None):
47 > + """
48 > + Read metadata from a binary package. The returned
49 > metadata
50 > + dictionary will contain empty strings for any values
51 > that
52 > + are undefined (this is important because the
53 > _pkg_str class
54 > + distinguishes between missing and undefined values).
55 > +
56 > + @param filename: File path of the binary package
57 > + @type filename: string
58 > + @param st: stat result for the binary package
59 > + @type st: os.stat_result
60 > + @param keys: optional list of specific metadata keys
61 > to retrieve
62 > + @type keys: iterable
63 > + @rtype: dict
64 > + @return: package metadata
65 > + """
66 > if keys is None:
67 > keys = self.dbapi._aux_cache_keys
68 > metadata = self.dbapi._aux_cache_slot_dict()
69 > @@ -1139,10 +1154,14 @@ class binarytree(object):
70 > metadata[k] = _unicode(st.st_size)
71 > else:
72 > v =
73 > binary_metadata.get(_unicode_encode(k))
74 > - if v is not None:
75 > + if v is None:
76 > + if k == "EAPI":
77 > + metadata[k] = "0"
78 > + else:
79 > + metadata[k] = ""
80 > + else:
81 > v = _unicode_decode(v)
82 > metadata[k] = "
83 > ".join(v.split())
84 > - metadata.setdefault("EAPI", "0")
85 > return metadata
86 >
87 > def _inject_file(self, pkgindex, cpv, filename):
88 > diff --git a/pym/portage/versions.py b/pym/portage/versions.py
89 > index a028d93..adfb1c3 100644
90 > --- a/pym/portage/versions.py
91 > +++ b/pym/portage/versions.py
92 > @@ -1,5 +1,5 @@
93 > # versions.py -- core Portage functionality
94 > -# Copyright 1998-2014 Gentoo Foundation
95 > +# Copyright 1998-2016 Gentoo Foundation
96 > # Distributed under the terms of the GNU General Public License v2
97 >
98 > from __future__ import unicode_literals
99 > @@ -359,6 +359,12 @@ class _pkg_str(_unicode):
100 > Instances are typically created in dbapi.cp_list() or the
101 > Atom contructor, and propagate from there. Generally, code that
102 > pickles these objects will manually convert them to a plain unicode
103 > object first. +
104 > + Instances of this class will have missing attributes for
105 > metadata that
106 > + has not been passed into the constructor. The missing
107 > attributes are
108 > + used to distinguish missing metadata values from undefined
109 > metadata values.
110 > + For example, the repo attribute will be missing if the
111 > 'repository' key
112 > + is missing from the metadata dictionary.
113 > """
114 >
115 > def __new__(cls, cpv, metadata=None, settings=None,
116 > eapi=None,
117
118 LGTM
119
120 --
121 Brian Dolbec <dolsen>

Replies