1#!/usr/bin/python3
2#
3# Copyright (C) 2023 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may not
6# use this file except in compliance with the License. You may obtain a copy of
7# the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations under
15# the License.
16#
17"""Aggregate several cts reports into information files.
18
19Given several cts reports, where a cts report could be a zip file or
20test_result.xml, this script convert them into one set of information files.
21The reports must be based on the same build fingerprint.
22"""
23
24import argparse
25import os
26import tempfile
27import zipfile
28
29import constant
30import parse_cts_report
31
32
33def aggregate_cts_reports(report_files,
34                          selected_abis=constant.ALL_TEST_ABIS,
35                          ignore_abi=False):
36  """Aggregate all report files and produce information files to output_dir.
37
38  If the results of the same test are different in two reports, choose the one
39  with a higher priority, following the order: PASS > IGNORED
40  > ASSUMPTION_FAILURE > FAIL > TEST_ERROR > TEST_STATUS_UNSPECIFIED.
41
42  Args:
43    report_files: A list of paths to cts reports.
44    ignore_abi: Ignore the tests ABI when doing the aggregation, which means
45                that tests would be considered as the same one as long as they
46                have the same module_name#class_name.test_name.
47
48  Raises:
49    UserWarning: Report files not compatible.
50
51  Returns:
52    A dictionary that maps build_fingerprint to a CtsReport object.
53  """
54
55  first_report_file = report_files[0]
56
57  report = parse_cts_report.parse_report_file(
58      first_report_file, selected_abis, ignore_abi)
59
60  with tempfile.TemporaryDirectory() as temp_dir:
61
62    for report_file in report_files[1:]:
63      xml_path = (
64          parse_cts_report.extract_test_result_from_zip(report_file, temp_dir)
65          if zipfile.is_zipfile(report_file)
66          else report_file)
67
68      test_info = parse_cts_report.get_test_info_xml(xml_path)
69
70      if not report.is_compatible(test_info):
71        msg = (f'{report_file} is incompatible to {first_report_file}.')
72        raise UserWarning(msg)
73
74      report.read_test_result_xml(xml_path, ignore_abi)
75
76  return report
77
78
79def main():
80  parser = argparse.ArgumentParser()
81
82  parser.add_argument('-r', '--report', required=True, nargs='+',
83                      help=('Path to cts report(s), where a cts report could '
84                            'be a zip archive or a xml file.'))
85  parser.add_argument('-d', '--output-dir', required=True,
86                      help=('Path to the directory to store output files.'))
87  parser.add_argument('--ignore-abi', action='store_true',
88                      help='Ignore the tests ABI while aggregating reports.')
89  parser.add_argument('--abi', choices=constant.ALL_TEST_ABIS, nargs='*',
90                      default=constant.ALL_TEST_ABIS,
91                      help='Selected test ABIs to be aggregated.')
92
93  args = parser.parse_args()
94
95  report_files = args.report
96  output_dir = args.output_dir
97
98  if not os.path.exists(output_dir):
99    raise FileNotFoundError(f'Output directory {output_dir} does not exist.')
100
101  report = aggregate_cts_reports(report_files, args.abi, args.ignore_abi)
102  report.output_files(output_dir)
103
104
105if __name__ == '__main__':
106  main()
107