Gentoo Archives: gentoo-portage-dev

From: Zac Medico <zmedico@g.o>
To: gentoo-portage-dev@l.g.o
Cc: Zac Medico <zmedico@g.o>
Subject: [gentoo-portage-dev] [PATCH 1/5] portdbapi: add async_fetch_map method (bug 653810)
Date: Sun, 22 Apr 2018 22:33:10
Message-Id: 20180422223014.24341-2-zmedico@gentoo.org
In Reply to: [gentoo-portage-dev] [PATCH 0/5] EbuildFetcher._get_uri_map(): fix event loop recursion (bug 653810) by Zac Medico
1 Add a portdbapi.async_fetch_map method which is identical to the
2 existing portdbapi.getFetchMap method, but returns a future. This
3 will be used by EbuildFetcher in order to avoid event loop recursion.
4
5 Bug: https://bugs.gentoo.org/653810
6 ---
7 pym/portage/dbapi/porttree.py | 75 +++++++++++++++++++++++++++++++++----------
8 1 file changed, 58 insertions(+), 17 deletions(-)
9
10 diff --git a/pym/portage/dbapi/porttree.py b/pym/portage/dbapi/porttree.py
11 index 951e5760a..3cd929963 100644
12 --- a/pym/portage/dbapi/porttree.py
13 +++ b/pym/portage/dbapi/porttree.py
14 @@ -728,25 +728,66 @@ class portdbapi(dbapi):
15 URIs.
16 @rtype: dict
17 """
18 + loop = self._event_loop
19 + return loop.run_until_complete(
20 + self.async_fetch_map(mypkg, useflags=useflags,
21 + mytree=mytree, loop=loop))
22
23 - try:
24 - eapi, myuris = self.aux_get(mypkg,
25 - ["EAPI", "SRC_URI"], mytree=mytree)
26 - except KeyError:
27 - # Convert this to an InvalidDependString exception since callers
28 - # already handle it.
29 - raise portage.exception.InvalidDependString(
30 - "getFetchMap(): aux_get() error reading "+mypkg+"; aborting.")
31 + def async_fetch_map(self, mypkg, useflags=None, mytree=None, loop=None):
32 + """
33 + Asynchronous form of getFetchMap.
34
35 - if not eapi_is_supported(eapi):
36 - # Convert this to an InvalidDependString exception
37 - # since callers already handle it.
38 - raise portage.exception.InvalidDependString(
39 - "getFetchMap(): '%s' has unsupported EAPI: '%s'" % \
40 - (mypkg, eapi))
41 -
42 - return _parse_uri_map(mypkg, {'EAPI':eapi,'SRC_URI':myuris},
43 - use=useflags)
44 + @param mypkg: cpv for an ebuild
45 + @type mypkg: String
46 + @param useflags: a collection of enabled USE flags, for evaluation of
47 + conditionals
48 + @type useflags: set, or None to enable all conditionals
49 + @param mytree: The canonical path of the tree in which the ebuild
50 + is located, or None for automatic lookup
51 + @type mypkg: String
52 + @param loop: event loop (defaults to global event loop)
53 + @type loop: EventLoop
54 + @return: A future that results in a dict which maps each file name to
55 + a set of alternative URIs.
56 + @rtype: asyncio.Future (or compatible)
57 + """
58 + loop = loop or global_event_loop()
59 + loop = getattr(loop, '_asyncio_wrapper', loop)
60 + result = loop.create_future()
61 +
62 + def aux_get_done(aux_get_future):
63 + if result.cancelled():
64 + return
65 + if aux_get_future.exception() is not None:
66 + if isinstance(future.exception(), PortageKeyError):
67 + # Convert this to an InvalidDependString exception since
68 + # callers already handle it.
69 + result.set_exception(portage.exception.InvalidDependString(
70 + "getFetchMap(): aux_get() error reading "
71 + + mypkg + "; aborting."))
72 + else:
73 + result.set_exception(future.exception())
74 + return
75 +
76 + eapi, myuris = aux_get_future.result()
77 +
78 + if not eapi_is_supported(eapi):
79 + # Convert this to an InvalidDependString exception
80 + # since callers already handle it.
81 + result.set_exception(portage.exception.InvalidDependString(
82 + "getFetchMap(): '%s' has unsupported EAPI: '%s'" % \
83 + (mypkg, eapi)))
84 + return
85 +
86 + result.set_result(_parse_uri_map(mypkg,
87 + {'EAPI':eapi,'SRC_URI':myuris}, use=useflags))
88 +
89 + aux_get_future = self.async_aux_get(
90 + mypkg, ["EAPI", "SRC_URI"], mytree=mytree)
91 + result.add_done_callback(lambda result:
92 + aux_get_future.cancel() if result.cancelled() else None)
93 + aux_get_future.add_done_callback(aux_get_done)
94 + return result
95
96 def getfetchsizes(self, mypkg, useflags=None, debug=0, myrepo=None):
97 # returns a filename:size dictionnary of remaining downloads
98 --
99 2.13.6