File: adtlog.py

package info (click to toggle)
autopkgtest 5.55
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,600 kB
  • sloc: python: 15,479; sh: 2,317; makefile: 116; perl: 19
file content (140 lines) | stat: -rw-r--r-- 3,202 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# adtlog is part of autopkgtest
# autopkgtest is a tool for testing Debian binary packages
#
# autopkgtest is Copyright (C) 2006-2014 Canonical Ltd.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# See the file CREDITS for a full list of credits information (often
# installed as /usr/share/doc/autopkgtest/CREDITS).

import sys
import time
import errno
import os

summary_stream = None
verbosity = 1  # 0: quiet (warning/error only), 1: info, 2: debug
enable_colors = None


def log(message, level, prefix="", timestamp=False, color=None):
    """Write a log message to stderr"""

    if level > verbosity:
        return

    # needs lazy initialization as it may be redirected to tee
    global enable_colors
    if enable_colors is None:
        enable_colors = os.isatty(sys.stderr.fileno())

    head = sys.argv[0].split("/")[-1]
    if timestamp:
        head += " [%s]: " % time.strftime("%H:%M:%S")
    else:
        head += ": "

    if prefix:
        head += prefix + ": "

    out = (head + message + "\n").encode("UTF-8")

    if color is not None and enable_colors:
        out = b"\033[3" + chr(47 + color).encode() + b"m" + out + b"\033[0m"

    # we sometimes hit EAGAIN here, try a few times
    retries = 10
    while retries >= 0:
        try:
            sys.stderr.buffer.write(out)
            break
        except IOError as e:
            if e.errno == errno.EAGAIN:
                retries -= 1
                time.sleep(0.05)
            else:
                raise
    sys.stderr.buffer.flush()


def error(message):
    log(message, 0, prefix="ERROR", timestamp=True, color=2)


def warning(message):
    log(message, 0, prefix="WARNING", color=5)


def info(message):
    log(message, 1, timestamp=True, color=4)


def debug(message):
    log(message, 2, prefix="DBG", timestamp=False, color=8)


def psummary(m):
    if summary_stream is not None:
        summary_stream.write(m.encode("UTF-8"))
        summary_stream.write(b"\n")


def preport(m):
    sys.stdout.buffer.write(m.encode("UTF-8"))
    sys.stdout.buffer.write(b"\n")
    sys.stdout.buffer.flush()
    psummary(m)


def report(tname, result):
    preport("%-20s %s" % (tname, result))


#
# Errors
#


class TestbedFailure(RuntimeError):
    pass


class BadPackageError(RuntimeError):
    pass


class AutopkgtestError(RuntimeError):
    pass


def bomb(m):
    raise AutopkgtestError(m)


def badpkg(m):
    raise BadPackageError(m)


class AptError(Exception):
    pass


class AptPermanentError(AptError):
    pass


class AptDownloadError(AptError):
    pass