4
# Copyright 2014 Canonical Ltd.
5
# This program is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU Affero General Public License version 3, as
7
# published by the Free Software Foundation.
9
# This program is distributed in the hope that it will be useful, but
10
# WITHOUT ANY WARRANTY; without even the implied warranties of
11
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
12
# PURPOSE. See the GNU Affero General Public License for more details.
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17
from functools import wraps
22
class CoverageRetrieverException(Exception):
23
"""Bad things happen to good retrievers."""
26
def snip_coverage_xml(log_text):
27
"""Extract coverage.xml from a specially-marked log.
29
NOTE that log_text must be an ascii string.
32
"""===== BEGIN coverage.xml =====
34
===== END coverage.xml =====""",
36
match = regex.search(log_text)
39
except AttributeError:
40
raise CoverageRetrieverException(
41
"Failed to parse build log: no coverage.xml.")
44
def retry(ExceptionToCheck, tries=4, delay=3, backoff=2, logger=None):
45
"""Retry calling the decorated function using an exponential backoff.
47
http://www.saltycrane.com/blog/2009/11/trying-out-retry-decorator-python/
48
Original from: http://wiki.python.org/moin/PythonDecoratorLibrary#Retry
50
:param ExceptionToCheck: the exception to check. may be a tuple of
52
:type ExceptionToCheck: Exception or tuple
53
:param tries: number of times to try (not retry) before giving up
55
:param delay: initial delay between retries in seconds
57
:param backoff: backoff multiplier e.g. value of 2 will double the delay
60
:param logger: logger to use.
61
:type logger: logging.Logger instance
63
TODO: also present in subunitresults, consider consolidating.
67
def f_retry(*args, **kwargs):
68
mtries, mdelay = tries, delay
71
return f(*args, **kwargs)
72
except ExceptionToCheck as e:
73
msg = "%s, Retrying in %d seconds..." % (str(e), mdelay)
79
return f(*args, **kwargs)
80
return f_retry # true decorator