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/2] SyncBase: split out _refresh_keys method (bug 660732)
Date: Sun, 08 Jul 2018 22:28:54
Message-Id: 20180708222825.8634-1-zmedico@gentoo.org
1 Split out a _refresh_keys method from the RsyncSync class,
2 so GitSync can use it for retry support.
3
4 Bug: https://bugs.gentoo.org/660732
5 ---
6 pym/portage/sync/modules/rsync/rsync.py | 33 +-------------------------
7 pym/portage/sync/syncbase.py | 42 +++++++++++++++++++++++++++++++++
8 2 files changed, 43 insertions(+), 32 deletions(-)
9
10 diff --git a/pym/portage/sync/modules/rsync/rsync.py b/pym/portage/sync/modules/rsync/rsync.py
11 index 382a1eaaef..a715e2818b 100644
12 --- a/pym/portage/sync/modules/rsync/rsync.py
13 +++ b/pym/portage/sync/modules/rsync/rsync.py
14 @@ -7,7 +7,6 @@ import time
15 import signal
16 import socket
17 import datetime
18 -import functools
19 import io
20 import re
21 import random
22 @@ -23,10 +22,8 @@ good = create_color_func("GOOD")
23 bad = create_color_func("BAD")
24 warn = create_color_func("WARN")
25 from portage.const import VCS_DIRS, TIMESTAMP_FORMAT, RSYNC_PACKAGE_ATOM
26 -from portage.util._eventloop.global_event_loop import global_event_loop
27 from portage.util import writemsg, writemsg_stdout
28 from portage.util.futures import asyncio
29 -from portage.util.futures.executor.fork import ForkExecutor
30 from portage.sync.getaddrinfo_validate import getaddrinfo_validate
31 from _emerge.UserQuery import UserQuery
32 from portage.sync.syncbase import NewBase
33 @@ -149,39 +146,11 @@ class RsyncSync(NewBase):
34 # will not be performed and the user will have to fix it and try again,
35 # so we may as well bail out before actual rsync happens.
36 if openpgp_env is not None and self.repo.sync_openpgp_key_path is not None:
37 -
38 try:
39 out.einfo('Using keys from %s' % (self.repo.sync_openpgp_key_path,))
40 with io.open(self.repo.sync_openpgp_key_path, 'rb') as f:
41 openpgp_env.import_key(f)
42 - out.ebegin('Refreshing keys from keyserver')
43 - retry_decorator = self._key_refresh_retry_decorator()
44 - if retry_decorator is None:
45 - openpgp_env.refresh_keys()
46 - else:
47 - def noisy_refresh_keys():
48 - """
49 - Since retry does not help for some types of
50 - errors, display errors as soon as they occur.
51 - """
52 - try:
53 - openpgp_env.refresh_keys()
54 - except Exception as e:
55 - writemsg_level("%s\n" % (e,),
56 - level=logging.ERROR, noiselevel=-1)
57 - raise # retry
58 -
59 - # The ThreadPoolExecutor that asyncio uses by default
60 - # does not support cancellation of tasks, therefore
61 - # use ForkExecutor for task cancellation support, in
62 - # order to enforce timeouts.
63 - loop = global_event_loop()
64 - with ForkExecutor(loop=loop) as executor:
65 - func_coroutine = functools.partial(loop.run_in_executor,
66 - executor, noisy_refresh_keys)
67 - decorated_func = retry_decorator(func_coroutine, loop=loop)
68 - loop.run_until_complete(decorated_func())
69 - out.eend(0)
70 + self._refresh_keys(openpgp_env)
71 except (GematoException, asyncio.TimeoutError) as e:
72 writemsg_level("!!! Manifest verification impossible due to keyring problem:\n%s\n"
73 % (e,),
74 diff --git a/pym/portage/sync/syncbase.py b/pym/portage/sync/syncbase.py
75 index 7d4d33272e..fe13f16434 100644
76 --- a/pym/portage/sync/syncbase.py
77 +++ b/pym/portage/sync/syncbase.py
78 @@ -7,13 +7,17 @@ This class contains common initialization code and functions.
79 '''
80
81 from __future__ import unicode_literals
82 +import functools
83 +import io
84 import logging
85 import os
86
87 import portage
88 from portage.util import writemsg_level
89 +from portage.util._eventloop.global_event_loop import global_event_loop
90 from portage.util.backoff import RandomExponentialBackoff
91 from portage.util.futures.retry import retry
92 +from portage.util.futures.executor.fork import ForkExecutor
93 from . import _SUBMODULE_PATH_MAP
94
95 class SyncBase(object):
96 @@ -189,6 +193,44 @@ class SyncBase(object):
97 multiplier=(1 if retry_delay_mult is None else retry_delay_mult),
98 base=(2 if retry_delay_exp_base is None else retry_delay_exp_base)))
99
100 + def _refresh_keys(self, openpgp_env):
101 + """
102 + Refresh keys stored in openpgp_env. Raises gemato.exceptions.GematoException
103 + or asyncio.TimeoutError on failure.
104 +
105 + @param openpgp_env: openpgp environment
106 + @type openpgp_env: gemato.openpgp.OpenPGPEnvironment
107 + """
108 + out = portage.output.EOutput(quiet=('--quiet' in self.options['emerge_config'].opts))
109 + out.ebegin('Refreshing keys from keyserver')
110 + retry_decorator = self._key_refresh_retry_decorator()
111 + if retry_decorator is None:
112 + openpgp_env.refresh_keys()
113 + else:
114 + def noisy_refresh_keys():
115 + """
116 + Since retry does not help for some types of
117 + errors, display errors as soon as they occur.
118 + """
119 + try:
120 + openpgp_env.refresh_keys()
121 + except Exception as e:
122 + writemsg_level("%s\n" % (e,),
123 + level=logging.ERROR, noiselevel=-1)
124 + raise # retry
125 +
126 + # The ThreadPoolExecutor that asyncio uses by default
127 + # does not support cancellation of tasks, therefore
128 + # use ForkExecutor for task cancellation support, in
129 + # order to enforce timeouts.
130 + loop = global_event_loop()
131 + with ForkExecutor(loop=loop) as executor:
132 + func_coroutine = functools.partial(loop.run_in_executor,
133 + executor, noisy_refresh_keys)
134 + decorated_func = retry_decorator(func_coroutine, loop=loop)
135 + loop.run_until_complete(decorated_func())
136 + out.eend(0)
137 +
138
139 class NewBase(SyncBase):
140 '''Subclasses Syncbase adding a new() and runs it
141 --
142 2.13.6

Replies