1# This file is part of Buildbot.  Buildbot is free software: you can
2# redistribute it and/or modify it under the terms of the GNU General Public
3# License as published by the Free Software Foundation, version 2.
4#
5# This program is distributed in the hope that it will be useful, but WITHOUT
6# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
7# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
8# details.
9#
10# You should have received a copy of the GNU General Public License along with
11# this program; if not, write to the Free Software Foundation, Inc., 51
12# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
13#
14# Copyright Buildbot Team Members
15
16ALL_RESULTS = list(range(7))
17SUCCESS, WARNINGS, FAILURE, SKIPPED, EXCEPTION, RETRY, CANCELLED = ALL_RESULTS
18Results = ["success", "warnings", "failure", "skipped", "exception", "retry", "cancelled"]
19MultipleResults = ["successes", "warnings", "failures", "skipped", "exceptions",
20                   "retries", "cancelled"]
21
22
23def statusToString(status, count=1):
24    if status is None:
25        return "not finished"
26    if status < 0 or status >= len(Results):
27        return "Invalid status"
28    if count > 1:
29        return MultipleResults[status]
30    return Results[status]
31
32
33def worst_status(a, b):
34    # SKIPPED > SUCCESS > WARNINGS > FAILURE > EXCEPTION > RETRY > CANCELLED
35    # CANCELLED needs to be considered the worst.
36    for s in (CANCELLED, RETRY, EXCEPTION, FAILURE, WARNINGS, SUCCESS, SKIPPED):
37        if s in (a, b):
38            return s
39    return None
40
41
42def computeResultAndTermination(obj, result, previousResult):
43    possible_overall_result = result
44    terminate = False
45    if result == FAILURE:
46        if not obj.flunkOnFailure:
47            possible_overall_result = SUCCESS
48        if obj.warnOnFailure:
49            possible_overall_result = WARNINGS
50        if obj.flunkOnFailure:
51            possible_overall_result = FAILURE
52        if obj.haltOnFailure:
53            terminate = True
54    elif result == WARNINGS:
55        if not obj.warnOnWarnings:
56            possible_overall_result = SUCCESS
57        else:
58            possible_overall_result = WARNINGS
59        if obj.flunkOnWarnings:
60            possible_overall_result = FAILURE
61    elif result in (EXCEPTION, RETRY, CANCELLED):
62        terminate = True
63
64    result = worst_status(previousResult, possible_overall_result)
65    return result, terminate
66
67
68class ResultComputingConfigMixin:
69
70    haltOnFailure = False
71    flunkOnWarnings = False
72    flunkOnFailure = True
73    warnOnWarnings = False
74    warnOnFailure = False
75
76    resultConfig = [
77        "haltOnFailure",
78        "flunkOnWarnings",
79        "flunkOnFailure",
80        "warnOnWarnings",
81        "warnOnFailure",
82    ]
83