This file is indexed.

/usr/lib/python2.7/dist-packages/gnatpython/reports.py is in python-gnatpython 54-3+b1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
 ############################################################################
 #                                                                          #
 #                            REPORTS.PY                                    #
 #                                                                          #
 #           Copyright (C) 2008 - 2010 Ada Core Technologies, Inc.          #
 #                                                                          #
 # 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 3 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, see <http://www.gnu.org/licenses/>     #
 #                                                                          #
 ############################################################################

"""This package contains various classes to manipulate data generated by the
testsuites. We assume a testsuite result is contained in a directory. The
directory should have the following structure::

    root_dir/
        results (contains the test results)
        <test_name>.note (a note associated with test <test_name> - optional)
        <test_name>.out  (actual test output - optional)
        <test_name>.expected (expected test output - optional)

The format of the results file is the following::

    TEST1_NAME:STATUS[:MESSAGE]
    TEST2_NAME:STATUS[:MESSAGE]
    ...

STATUS is one of following string::

    DIFF, PROBLEM, FAILED,
    CRASH,
    INVALID_TEST,
    XFAIL, SKIP,
    UOK,
    DEAD,
    PASSED, OK, NOT-APPLICABLE, TENTATIVELY_PASSED

MESSAGE is an optional message
"""

from gnatpython.main import Main
from gnatpython.fileutils import split_file, FileUtilsError
import os.path

# This following lists regroup the status in different categories. A given
# status might appear in several categories. For convenience, the following
# code is using only categories, so that adding new test status result only
# in adding it in the following lists.
FAIL = ['DIFF', 'PROBLEM', 'FAILED']
CRASH = ['CRASH']
INVALID = ['INVALID_TEST']
XFAIL = ['XFAIL', 'SKIP']
UPASS = ['UOK']
DEAD = ['DEAD']
PASS = ['PASSED', 'OK', 'NOT-APPLICABLE', 'TENTATIVELY_PASSED']
FAIL_OR_CRASH = CRASH + FAIL
NON_DEAD = PASS + UPASS + XFAIL + INVALID + FAIL + CRASH


class TestResult(object):
    """Class that holds test information

    ATTRIBUTES
      dir: result directory. We expect to find all test related file in it
      name: test name
      status: test status
      msg: associated message
    """

    def __init__(self, dir, name, status, msg):
        """TestResult constructor

        PARAMETERS
          dir: result directory
          name: the test name
          status: test status
          msg: test message

        RETURN VALUE
          a TestResult object
        """
        self.dir = dir
        self.name = name
        self.status = status
        self.msg = msg

    def __get_file(self, ext):
        """Internal function that retrieves a file associated with a test

        PARAMETERS
          ext: extension of the file to look at

        RETURN VALUE
          the content of the file or None
        """
        filename = self.dir + '/' + self.name + ext
        if os.path.isfile(filename):
            fd = open(filename, 'rb')
            result = fd.read()
            fd.close()
            return result
        else:
            return None

    def get_note(self):
        """Retrieve the note associated with the test

        PARAMETERS
          None

        RETURN VALUE
          A string. If there is no note associated with the test, the null
          string is returned
        """
        note = self.__get_file('.note')
        if note is None:
            return ''
        else:
            return note.strip()

    def get_expected_output(self):
        """Retrieve test actual output

        PARAMETERS
          None

        RETURN VALUE
          test actual output or None
        """
        return self.__get_file('.expected')

    def get_actual_output(self):
        """Retrieve test expected output

        PARAMETERS
          None

        RETURN VALUE
          test expected output or None
        """
        return self.__get_file('.out')

    def __str__(self):
        return '%s:%s:%s' % (self.name, self.status, self.msg)


class Report(object):
    """Class that holds a complete testsuite result"""

    def __init__(self, dir):
        """Report constructor

        PARAMETERS
          dir: the directory that contains the testsuite results

        RETURN VALUE
          a Report Object
        """
        self.dir = dir
        self.result_db = {}

        if self.dir is not None:
            assert os.path.isdir(self.dir), "invalid result directory"

            result_list = split_file(dir + '/results')
            result_list = (k.split(':', 2) for k in result_list)

            for item in result_list:
                msg = ''
                if len(item) > 2:
                    msg = item[2]

                self.result_db[item[0]] = \
                  TestResult(self.dir, item[0], item[1], msg)

    def select(self, kind=None, return_set=False):
        """Retrieve the list/set of tests that match a given list of status

        PARAMETERS
          kind: None or a list of status. If None the complete list of test
                names will be returned
          return_set: if True then a set object is returned. Otherwise a
                sorted list

        RETURN VALUE
          a list or a set of test names
        """

        result = None
        if kind is None:
            result = set([k for k in self.result_db])
        else:
            result = set([k for k in self.result_db \
                           if self.result_db[k].status in kind])

        if not return_set:
            result = list(result)
            result.sort()

        return result

    def test(self, name):
        """Get a test object from the testsuite

        PARAMETERS
          name: name of the test we want to retrieve

        RETURN VALUE
          a TestResult object
        """
        return self.result_db[name]


# A few constant use by ReportDiff.select method
IN_ONE = 0
IN_BOTH = 1
IN_NEW_ONLY = 2
IN_OLD_ONLY = 3


class ReportDiff(object):
    """Class that allows comparison between two testsuite reports

    ATTRIBUTES
      new: a Report object that contains the new results
      old: a Report object that contains the old results
    """

    def __init__(self, dir, old_dir=None):
        """ReportDiff constructor

        PARAMETERS
          dir: the directory of the new testsuite result
          old_dir: the directory of the old testsuite result

        RETURN VALUE
          a ReportDiff object
        """
        self.new = Report(dir)

        if old_dir is not None and not (
            os.path.isdir(old_dir) and os.path.isfile(old_dir + '/results')):
            # There is no old report. Skip it.
            old_dir = None
        try:
            self.old = Report(old_dir)
        except FileUtilsError:
            self.old = Report(None)

    def select(self, kind=None, status=IN_BOTH, old_kind=None):
        """Do a query

        PARAMETERS
          kind: a list of status or None used to filter the tests in the new
            report
          status: a selector IN_ONE, IN_BOTH, IN_NEW_ONLY or IN_OLD_ONLY.
          old_kind: a list of status used to filter in the old report. If None
            the parameter kind is reused

        RETURN VALUE
          a sorted list of test names
        """

        if old_kind is None:
            old_kind = kind
        new = self.new.select(kind, return_set=True)
        old = self.old.select(old_kind, return_set=True)
        result = None
        if status == IN_BOTH:
            result = new & old
        elif status == IN_NEW_ONLY:
            result = new - old
        elif status == IN_OLD_ONLY:
            result = old - new
        else:
            result = old | new

        return result

    def txt_image(self, filename):
        if isinstance(filename, str):
            fd = open(filename, 'wb+')
        else:
            fd = filename

        def output_list(tests, tmpl_msg, output_if_null=False, from_old=False):
            if len(tests) > 0 or output_if_null:
                tmpl_msg = '---------------- %d %s\n' % (len(tests), tmpl_msg)
                fd.write('\n')
                fd.write(tmpl_msg)
                for test in tests:
                    if from_old:
                        fd.write('%s\n' % self.old.test(test))
                    else:
                        fd.write('%s\n' % self.new.test(test))

        def test_diff(test_name):
            test_obj = self.new.test(test_name)
            test_note = test_obj.get_note()
            test_out = test_obj.get_actual_output()
            test_exp = test_obj.get_expected_output()

            fd.write("================ Bug %s %s\n" % (test_name, test_note))
            if test_exp is None:
                fd.write('---------------- unexpected output\n')
                if test_out is not None:
                    fd.write(test_out)
            else:
                fd.write('---------------- expected output\n')
                if test_exp is not None:
                    fd.write(test_exp)
                fd.write('---------------- actual output\n')
                if test_out is not None:
                    fd.write(test_out)

        constant = self.select(FAIL, IN_BOTH)
        xfail_tests = self.new.select(XFAIL)
        uok_tests = self.new.select(UPASS)

        fixed_tests = self.select(PASS, IN_BOTH, FAIL_OR_CRASH)
        invalid_tests = self.new.select(INVALID)
        new_dead_tests = self.select(DEAD, IN_BOTH, NON_DEAD)
        removed_tests = self.select(status=IN_OLD_ONLY)
        crash_tests = self.new.select(CRASH)
        dead_tests = self.new.select(DEAD)
        complete_tests = self.new.select()
        diff_tests = self.new.select(FAIL)
        new_regressions = self.select(FAIL_OR_CRASH, IN_NEW_ONLY)
        non_dead_tests = self.new.select(NON_DEAD)

        fd.write('Out of %d tests:\n' % len(complete_tests))
        fd.write('%5d executed test(s) (non dead)\n' % len(non_dead_tests))
        fd.write('%5d crash(es) detected\n' % len(crash_tests))
        fd.write('%5d other potential regression(s)\n' % len(diff_tests))
        fd.write('%5d expected regression(s)\n' % len(xfail_tests))
        fd.write('%5d unexpected passed test(s)\n' % len(uok_tests))
        fd.write('%5d invalid test(s)\n' % len(invalid_tests))
        fd.write('%5d new dead test(s)\n' % len(new_dead_tests))
        fd.write('%5d test(s) removed\n' % len(removed_tests))

        output_list(new_regressions, 'new regression(s)', True)
        output_list(constant, 'already detected regression(s)', True)
        output_list(xfail_tests, 'expected regression(s)')
        output_list(uok_tests, 'unexpected passed test(s)')
        output_list(fixed_tests, 'fixed regression(s)')
        output_list(invalid_tests, 'invalid test(s)')
        output_list(new_dead_tests, 'new dead test(s)')
        output_list(removed_tests, 'test(s) removed', from_old=True)

        fd.write('\n---------------- differences in output\n')
        for test in self.new.select(FAIL):
            test_diff(test)

        if len(xfail_tests) > 0:
            fd.write('\n---------------- XFAIL differences in output\n')
            for test in xfail_tests:
                test_diff(test)

        if isinstance(filename, str):
            fd.close()