File: parse_cts_report.py

package info (click to toggle)
android-platform-tools 35.0.2-1~exp6
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 211,716 kB
  • sloc: cpp: 995,749; java: 290,495; ansic: 145,647; xml: 58,531; python: 39,608; sh: 14,500; javascript: 5,198; asm: 4,866; makefile: 3,115; yacc: 769; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (416 lines) | stat: -rwxr-xr-x 11,689 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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#!/usr/bin/python3
#
# Copyright (C) 2023 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
#
"""Convert single cts report into information files.

Given a cts report, which could be a zip file or test_result.xml, this script
turns them into three files: info.json, result.csv, and summary.csv.
"""

import argparse
import csv
import json
import os
import shutil
import tempfile
import xml.etree.ElementTree as ET
import zipfile
import constant


# TODO(b/293809772): Logging.


class CtsReport:
  """Class to record the test result of a cts report."""

  STATUS_ORDER = [
      'pass',
      'IGNORED',
      'ASSUMPTION_FAILURE',
      'fail',
      'TEST_ERROR',
      'TEST_STATUS_UNSPECIFIED',
  ]

  FAIL_INDEX = STATUS_ORDER.index('fail')

  def __init__(self, info, selected_abis=constant.ALL_TEST_ABIS):
    self.info = info
    self.selected_abis = selected_abis
    self.result_tree = {}
    self.module_summaries = {}

  @staticmethod
  def is_fail(status):
    if status == constant.NO_DATA:
      return False
    else:
      return CtsReport.STATUS_ORDER.index(status) >= CtsReport.FAIL_INDEX

  def gen_keys_list(self):
    """Generate a 2D-list of keys."""

    keys_list = []

    modules = self.result_tree

    for module_name, abis in modules.items():
      for abi, test_classes in abis.items():
        for class_name, tests in test_classes.items():
          for test_name in tests.keys():
            keys_list.append([module_name, abi, class_name, test_name])

    return keys_list

  def is_compatible(self, info):
    return self.info['build_fingerprint'] == info['build_fingerprint']

  def get_test_status(self, module_name, abi, class_name, test_name):
    """Get test status from the CtsReport object."""

    if module_name not in self.result_tree:
      return constant.NO_DATA
    abis = self.result_tree[module_name]

    if abi not in abis:
      return constant.NO_DATA
    test_classes = abis[abi]

    if class_name not in test_classes:
      return constant.NO_DATA

    tests = test_classes[class_name]

    if test_name not in tests:
      return constant.NO_DATA

    return tests[test_name]

  def set_test_status(
      self, module_name, abi, class_name, test_name, test_status
  ):
    """Set test status to the CtsReport object."""

    previous = self.get_test_status(module_name, abi, class_name, test_name)

    abis = self.result_tree.setdefault(module_name, {})
    test_classes = abis.setdefault(abi, {})
    tests = test_classes.setdefault(class_name, {})

    if previous == constant.NO_DATA:
      tests[test_name] = test_status

      module_summary = self.module_summaries.setdefault(module_name, {})
      summary = module_summary.setdefault(abi, self.ModuleSummary())
      summary.counter[test_status] += 1

    elif (CtsReport.STATUS_ORDER.index(test_status)
          < CtsReport.STATUS_ORDER.index(previous)):
      summary = self.module_summaries[module_name][abi]

      tests[test_name] = test_status

      summary.counter[previous] -= 1
      summary.counter[test_status] += 1

  def read_test_result_xml(self, test_result_path, ignore_abi=False):
    """Read the result from test_result.xml into a CtsReport object."""

    tree = ET.parse(test_result_path)
    root = tree.getroot()

    for module in root.iter('Module'):
      module_name = module.attrib['name']
      abi = module.attrib['abi']
      if abi not in self.selected_abis:
        continue
      if ignore_abi:
        abi = constant.ABI_IGNORED

      for testcase in module.iter('TestCase'):
        class_name = testcase.attrib['name']

        for test in testcase.iter('Test'):
          test_name = test.attrib['name']
          result = test.attrib['result']
          self.set_test_status(module_name, abi, class_name, test_name, result)

  def load_from_csv(self, result_csvfile, ignore_abi=False):
    """Read the information of the report from the csv files.

    Args:
      result_csvfile: path to result.csv
      ignore_abi: if specified, load the test ABI name as constant.ABI_IGNORED
    """

    result_reader = csv.reader(result_csvfile)

    try:
      next(result_reader)  # skip the header of csv file
    except StopIteration:
      print(f'Empty file: {result_csvfile.name}')
      return

    for row in result_reader:
      module_name, abi, class_name, test_name, result = row
      if abi not in self.selected_abis:
        continue
      if ignore_abi:
        abi = constant.ABI_IGNORED
      self.set_test_status(module_name, abi, class_name, test_name, result)

  def write_to_csv(self, result_csvfile, summary_csvfile):
    """Write the information of the report to the csv files.

    Args:
      result_csvfile: path to result.csv
      summary_csvfile: path to summary.csv
    """

    summary_writer = csv.writer(summary_csvfile)
    summary_writer.writerow(['module_name', 'abi'] + CtsReport.STATUS_ORDER)

    result_writer = csv.writer(result_csvfile)
    result_writer.writerow(
        ['module_name', 'abi', 'class_name', 'test_name', 'result']
    )

    modules = self.result_tree

    for module_name, abis in modules.items():
      for abi, test_classes in abis.items():
        module_summary = self.module_summaries[module_name][abi]

        summary = module_summary.summary_list()

        row = [module_name, abi] + summary
        summary_writer.writerow(row)

        for class_name, tests in test_classes.items():
          for test_name, result in tests.items():
            result_writer.writerow(
                [module_name, abi, class_name, test_name, result]
            )

  def output_files(self, output_dir):
    """Produce output files into the directory."""

    parsed_info_path = os.path.join(output_dir, 'info.json')
    parsed_result_path = os.path.join(output_dir, 'result.csv')
    parsed_summary_path = os.path.join(output_dir, 'summary.csv')

    files = [parsed_info_path, parsed_result_path, parsed_summary_path]

    for f in files:
      if os.path.exists(f):
        raise FileExistsError(f'Output file {f} already exists.')

    with open(parsed_info_path, 'w') as info_file:
      info_file.write(json.dumps(self.info, indent=2))

    with (
        open(parsed_result_path, 'w') as result_csvfile,
        open(parsed_summary_path, 'w') as summary_csvfile,
    ):
      self.write_to_csv(result_csvfile, summary_csvfile)

    for f in files:
      print(f'Parsed output {f}')

    return files

  class ModuleSummary:
    """Record the result summary of each (module, abi) pair."""

    def __init__(self):
      self.counter = dict.fromkeys(CtsReport.STATUS_ORDER, 0)

    @property
    def tested_items(self):
      """All tested items."""
      items = 0
      for status in CtsReport.STATUS_ORDER:
        items += self.counter[status]
      return items

    @property
    def pass_rate(self):
      """Pass rate of the module."""
      if self.tested_items == 0:
        return 0.0
      else:
        pass_category = 0
        for status in CtsReport.STATUS_ORDER:
          if not CtsReport.is_fail(status):
            pass_category += self.counter[status]
        return pass_category / self.tested_items

    def print_summary(self):
      for key in CtsReport.STATUS_ORDER:
        print(f'{key}: {self.counter[key]}')
        print()

    def summary_list(self):
      return [self.counter[key] for key in CtsReport.STATUS_ORDER]


ATTRS_TO_SHOW = [
    'Result::Build.build_model',
    'Result::Build.build_id',
    'Result::Build.build_fingerprint',
    'Result::Build.build_device',
    'Result::Build.build_version_sdk',
    'Result::Build.build_version_security_patch',
    'Result::Build.build_board',
    'Result::Build.build_type',
    'Result::Build.build_version_release',
    'Result.suite_name',
    'Result.suite_version',
    'Result.suite_plan',
    'Result.suite_build_number',
]


def parse_attrib_path(attrib_path):
  """Parse the path into xml tag and attribute name."""
  first_dot = attrib_path.index('.')
  tags = attrib_path[:first_dot].split('::')
  attr_name = attrib_path[first_dot + 1 :]
  return tags, attr_name


def get_test_info_xml(test_result_path):
  """Get test info from xml file."""

  tree = ET.parse(test_result_path)
  root = tree.getroot()

  test_info = {
      'tool_version': constant.VERSION,
      'source_path': test_result_path,
  }

  for attrib_path in ATTRS_TO_SHOW:
    tags, attr_name = parse_attrib_path(attrib_path)
    node = root

    while True:
      tags = tags[1:]
      if tags:
        node = node.find(tags[0])
      else:
        break

    test_info[attr_name] = node.attrib[attr_name]

  return test_info


def print_test_info(info):
  """Print test information of the result in table format."""

  max_key_len = max([len(k) for k in info])
  max_value_len = max([len(info[k]) for k in info])
  table_len = max_key_len + 2 + max_value_len

  print('=' * table_len)

  for key in info:
    print(f'{key:<{max_key_len}}  {info[key]}')

  print('=' * table_len)
  print()


def extract_test_result_from_zip(zip_file_path, dest_dir):
  """Extract test_result.xml from the zip file."""

  result_name = 'test_result.xml'
  extracted = os.path.join(dest_dir, result_name)
  with zipfile.ZipFile(zip_file_path) as myzip:
    result_list = [f for f in myzip.namelist() if result_name in f]
    if len(result_list) != 1:
      raise RuntimeError(f'Cannot extract {result_name} from {zip_file_path}, '
                         f'matched files: {" ".join(result_list)}')
    with myzip.open(result_list[0]) as source, open(extracted, 'wb') as target:
      shutil.copyfileobj(source, target)
  return extracted


def parse_report_file(report_file,
                      selected_abis=constant.ALL_TEST_ABIS,
                      ignore_abi=False):
  """Turn one cts report into a CtsReport object."""

  with tempfile.TemporaryDirectory() as temp_dir:
    xml_path = (
        extract_test_result_from_zip(report_file, temp_dir)
        if zipfile.is_zipfile(report_file)
        else report_file
    )

    test_info = get_test_info_xml(xml_path)
    print(f'Parsing {selected_abis} test results from: ')
    print_test_info(test_info)

    report = CtsReport(test_info, selected_abis)
    report.read_test_result_xml(xml_path, ignore_abi)

  return report


def main():
  parser = argparse.ArgumentParser()

  parser.add_argument(
      '-r',
      '--report',
      required=True,
      help=(
          'Path to a cts report, where a cts report could '
          'be a zip archive or a xml file.'
      ),
  )
  parser.add_argument(
      '-d',
      '--output-dir',
      required=True,
      help='Path to the directory to store output files.',
  )
  parser.add_argument(
      '--abi',
      choices=constant.ALL_TEST_ABIS,
      nargs='*',
      default=constant.ALL_TEST_ABIS,
      help='Selected test ABIs to be parsed.',
  )

  args = parser.parse_args()

  report_file = args.report
  output_dir = args.output_dir

  if not os.path.exists(output_dir):
    raise FileNotFoundError(f'Output directory {output_dir} does not exist.')

  report = parse_report_file(report_file, args.abi)

  report.output_files(output_dir)


if __name__ == '__main__':
  main()