~allanlesage/uci-engine/coverage-extractor

« back to all changes in this revision

Viewing changes to coverage-retriever/coverageretriever/__init__.py

  • Committer: Allan LeSage
  • Date: 2014-10-03 20:48:44 UTC
  • Revision ID: allan.lesage@canonical.com-20141003204844-r0aty01f0y2dm5ml
Initial coverage-retriever for review.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# Ubuntu CI Engine
 
3
#
 
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.
 
8
#
 
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.
 
13
#
 
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/>.
 
16
 
 
17
from functools import wraps
 
18
import re
 
19
import time
 
20
 
 
21
 
 
22
class CoverageRetrieverException(Exception):
 
23
    """Bad things happen to good retrievers."""
 
24
 
 
25
 
 
26
def snip_coverage_xml(log_text):
 
27
    """Extract coverage.xml from a specially-marked log.
 
28
 
 
29
    NOTE that log_text must be an ascii string.
 
30
    """
 
31
    regex = re.compile(
 
32
        """===== BEGIN coverage.xml =====
 
33
(.+)
 
34
===== END coverage.xml =====""",
 
35
        re.DOTALL)
 
36
    match = regex.search(log_text)
 
37
    try:
 
38
        return match.group(1)
 
39
    except AttributeError:
 
40
        raise CoverageRetrieverException(
 
41
            "Failed to parse build log: no coverage.xml.")
 
42
 
 
43
 
 
44
def retry(ExceptionToCheck, tries=4, delay=3, backoff=2, logger=None):
 
45
    """Retry calling the decorated function using an exponential backoff.
 
46
 
 
47
    http://www.saltycrane.com/blog/2009/11/trying-out-retry-decorator-python/
 
48
    Original from: http://wiki.python.org/moin/PythonDecoratorLibrary#Retry
 
49
 
 
50
    :param ExceptionToCheck: the exception to check. may be a tuple of
 
51
        exceptions to check
 
52
    :type ExceptionToCheck: Exception or tuple
 
53
    :param tries: number of times to try (not retry) before giving up
 
54
    :type tries: int
 
55
    :param delay: initial delay between retries in seconds
 
56
    :type delay: int
 
57
    :param backoff: backoff multiplier e.g. value of 2 will double the delay
 
58
        each retry
 
59
    :type backoff: int
 
60
    :param logger: logger to use.
 
61
    :type logger: logging.Logger instance
 
62
 
 
63
    TODO: also present in subunitresults, consider consolidating.
 
64
    """
 
65
    def deco_retry(f):
 
66
        @wraps(f)
 
67
        def f_retry(*args, **kwargs):
 
68
            mtries, mdelay = tries, delay
 
69
            while mtries > 1:
 
70
                try:
 
71
                    return f(*args, **kwargs)
 
72
                except ExceptionToCheck as e:
 
73
                    msg = "%s, Retrying in %d seconds..." % (str(e), mdelay)
 
74
                    if logger:
 
75
                        logger.warning(msg)
 
76
                    time.sleep(mdelay)
 
77
                    mtries -= 1
 
78
                    mdelay *= backoff
 
79
            return f(*args, **kwargs)
 
80
        return f_retry  # true decorator
 
81
    return deco_retry