File: printer.py

package info (click to toggle)
libreswan 5.2-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 81,632 kB
  • sloc: ansic: 129,988; sh: 32,018; xml: 20,646; python: 10,303; makefile: 3,022; javascript: 1,506; sed: 574; yacc: 511; perl: 264; awk: 52
file content (188 lines) | stat: -rw-r--r-- 6,681 bytes parent folder | download
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# Print a test result summary gathered by scanning the OUTPUT.
#
# Copyright (C) 2015-2017 Andrew Cagney <cagney@gnu.org>
#
# 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.  See <https://www.gnu.org/licenses/gpl2.txt>.
#
# 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.

import argparse
import sys
import os
import re
from enum import Enum

from fab import testsuite
from fab import logutil
from fab import post
from fab import skip
from fab import ignore
from fab import argutil
from fab import jsonutil

class Print(argutil.List):
    # tests
    TEST_DIRECTORY = "test-directory"
    TEST_GUEST_NAMES = "test-guest-names"
    TEST_HOST_NAMES = "test-host-names"
    TEST_GUEST_PLATFORMS = "test-guest-platforms"
    TEST_PLATFORMS = "test-platforms"
    TEST_KIND = "test-kind"
    TEST_NAME = "test-name"
    TEST_COMMANDS = "test-commands"
    TEST_STATUS = "test-status"
    # results
    START_TIME = "start-time"
    STOP_TIME = "stop-time"
    TOTAL_TIME = "total-time"
    BOOT_TIME = "boot-time"
    TEST_TIME = "test-time"
    DIFFS = "diffs"
    ISSUES = "issues"
    OUTPUT_DIRECTORY = "output-directory"
    PATH = "path"
    RESULT = "result"
    SAVED_OUTPUT_DIRECTORY = "saved-output-directory"
    TESTING_DIRECTORY = "testing-directory"


class JsonBuilder:
    def __init__(self, stream=None):
        self.stream = stream
        self.table = {}
    def prefix(self, key, value):
        self.add(str(key), value)
    def add(self, *keyval, string=None):
        keys = [key.replace("-","_") for key in keyval[0:-1]]
        value = keyval[-1]
        # Only suppress non-existent values.
        if value is None:
            return
        table = self.table
        for key in keys[0:-1]:
            key = key.replace("-","_")
            if not key in table:
                table[key] = {}
            table = table[key]
        table[keys[-1]] = value
    def json(self):
        return self.table
    def flush(self):
        if self.stream:
            jsonutil.dump(self.table, self.stream)
            self.stream.write("\n")
            self.stream.flush()


class TextBuilder:
    def __init__(self, stream=sys.stdout):
        self.eol = False
        self.sep = ""
        self.stream = stream
    def prefix(self, key, value):
        self.stream.write(value)
        self.eol = True
    def add(self, *keyval, string=lambda s: s and str(s) or False):
        # By default, when the VALUE is False-like, the output is
        # suppressed.  For instance, when ISSUES is False (there are
        # none) it is skipped.
        keys = keyval[0:-1]
        value = keyval[-1]
        s = string(value)
        if s:
            self.stream.write(self.sep)
            self.stream.write(s)
            self.sep = " "
            self.eol = True
    def flush(self):
        if self.eol:
            self.stream.write("\n")
        self.stream.flush()
        self.sep = ""
        self.eol = False


def build_result(logger, result, what_to_print, b):

    # Does multi-line output, namely Print.DIFFS, need a newline
    # before starting its output?
    newline= ""

    # Print the test's name/path

    for p in what_to_print:
        match p:
            case Print.PATH:
                # When the path given on the command line explicitly
                # specifies a test's output directory (found in
                # RESULT.TEST.SAVED_OUTPUT_DIRECTORY), print that; otherwise
                # print the path to the test's directory.
                b.add(p, (result.test.saved_output_directory
                          and result.test.saved_output_directory
                          or result.test.directory))
            case Print.TEST_DIRECTORY:
                b.add(p, result.test.directory)
            case Print.TEST_STATUS:
                b.add(p, result.test.status)
            case Print.TEST_GUEST_NAMES:
                b.add(p, [guest.name for guest in result.test.guests],
                      string=lambda names: ",".join(names))
            case Print.TEST_HOST_NAMES:
                b.add(p, [guest.host.name for guest in result.test.guests],
                      string=lambda names: ",".join(names))
            case Print.TEST_GUEST_PLATFORMS:
                b.add(p, [guest.platform for guest in result.test.guests],
                      string=lambda names: ",".join(names))
            case Print.TEST_PLATFORMS:
                b.add(p, result.test.platforms,
                      string=lambda platforms: ",".join(platforms))
            case Print.TEST_KIND:
                b.add(p, result.test.kind)
            case Print.TEST_NAME:
                b.add(p, result.test.name)
            case Print.OUTPUT_DIRECTORY:
                b.add(p, result.test.output_directory)
            case Print.RESULT:
                b.add(p, result, string=lambda result: str(result))
            case Print.ISSUES:
                b.add(p, result.issues)
            case Print.TESTING_DIRECTORY:
                b.add(p, result.test.testing_directory())
            case Print.SAVED_OUTPUT_DIRECTORY:
                b.add(p, result.test.saved_output_directory)
            case Print.TEST_COMMANDS:
                b.add(p, result.test.commands)
            case Print.START_TIME:
                b.add(p, result.start_time())
            case Print.STOP_TIME:
                b.add(p, result.stop_time())
            case Print.TOTAL_TIME:
                b.add(p, result.total_time())
            case Print.BOOT_TIME:
                b.add(p, result.boot_time())
            case Print.TEST_TIME:
                b.add(p, result.test_time())
            case Print.DIFFS:
                # see below; skips setting newline
                continue
            case _:
                raise Exception("unhandled print option %s" % p)

        # Print.DIFFS, below, needs to finish the current line.
        newline = "\n"

    if Print.DIFFS in what_to_print:
        for guest_name, diff_output in result.diff_output.items():
            if diff_output: # could be blank
                b.add(Print.DIFFS, guest_name, diff_output,
                      string=(lambda diff:
                              diff and newline + b"\n".join(diff).decode('utf-8') or ""))
                newline = "\n"

    b.flush()