blob: f1bfd99500b4f03741655fbb5d351ec68d495713 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001# resulttool - Show logs
2#
3# Copyright (c) 2019 Garmin International
4#
5# SPDX-License-Identifier: GPL-2.0-only
6#
7import os
8import resulttool.resultutils as resultutils
9
10def show_ptest(result, ptest, logger):
Brad Bishopa34c0302019-09-23 22:34:48 -040011 logdata = resultutils.ptestresult_get_log(result, ptest)
12 if logdata is not None:
13 print(logdata)
14 return 0
Brad Bishopc342db32019-05-15 21:57:59 -040015
Brad Bishopa34c0302019-09-23 22:34:48 -040016 print("ptest '%s' log not found" % ptest)
Brad Bishopc342db32019-05-15 21:57:59 -040017 return 1
18
Brad Bishop79641f22019-09-10 07:20:22 -040019def show_reproducible(result, reproducible, logger):
20 try:
21 print(result['reproducible'][reproducible]['diffoscope.text'])
22 return 0
23
24 except KeyError:
25 print("reproducible '%s' not found" % reproducible)
26 return 1
27
Brad Bishopc342db32019-05-15 21:57:59 -040028def log(args, logger):
29 results = resultutils.load_resultsdata(args.source)
30
31 ptest_count = sum(1 for _, _, _, r in resultutils.test_run_results(results) if 'ptestresult.sections' in r)
32 if ptest_count > 1 and not args.prepend_run:
33 print("%i ptest sections found. '--prepend-run' is required" % ptest_count)
34 return 1
35
36 for _, run_name, _, r in resultutils.test_run_results(results):
Brad Bishopa34c0302019-09-23 22:34:48 -040037 if args.dump_ptest and 'ptestresult.sections' in r:
38 for name, ptest in r['ptestresult.sections'].items():
39 logdata = resultutils.ptestresult_get_log(r, name)
40 if logdata is not None:
41 dest_dir = args.dump_ptest
42 if args.prepend_run:
43 dest_dir = os.path.join(dest_dir, run_name)
Brad Bishopc342db32019-05-15 21:57:59 -040044
Brad Bishopa34c0302019-09-23 22:34:48 -040045 os.makedirs(dest_dir, exist_ok=True)
46 dest = os.path.join(dest_dir, '%s.log' % name)
47 print(dest)
48 with open(dest, 'w') as f:
49 f.write(logdata)
Brad Bishopc342db32019-05-15 21:57:59 -040050
Brad Bishop79641f22019-09-10 07:20:22 -040051 if args.raw_ptest:
Brad Bishopa34c0302019-09-23 22:34:48 -040052 rawlog = resultutils.ptestresult_get_rawlogs(r)
53 if rawlog is not None:
54 print(rawlog)
Brad Bishopc342db32019-05-15 21:57:59 -040055 else:
Brad Bishop79641f22019-09-10 07:20:22 -040056 print('Raw ptest logs not found')
57 return 1
58
59 if args.raw_reproducible:
60 if 'reproducible.rawlogs' in r:
61 print(r['reproducible.rawlogs']['log'])
62 else:
63 print('Raw reproducible logs not found')
Brad Bishopc342db32019-05-15 21:57:59 -040064 return 1
65
66 for ptest in args.ptest:
67 if not show_ptest(r, ptest, logger):
68 return 1
69
Brad Bishop79641f22019-09-10 07:20:22 -040070 for reproducible in args.reproducible:
71 if not show_reproducible(r, reproducible, logger):
72 return 1
73
Brad Bishopc342db32019-05-15 21:57:59 -040074def register_commands(subparsers):
75 """Register subcommands from this plugin"""
76 parser = subparsers.add_parser('log', help='show logs',
77 description='show the logs from test results',
78 group='analysis')
79 parser.set_defaults(func=log)
80 parser.add_argument('source',
81 help='the results file/directory/URL to import')
82 parser.add_argument('--ptest', action='append', default=[],
83 help='show logs for a ptest')
84 parser.add_argument('--dump-ptest', metavar='DIR',
85 help='Dump all ptest log files to the specified directory.')
Brad Bishop79641f22019-09-10 07:20:22 -040086 parser.add_argument('--reproducible', action='append', default=[],
87 help='show logs for a reproducible test')
Brad Bishopc342db32019-05-15 21:57:59 -040088 parser.add_argument('--prepend-run', action='store_true',
89 help='''Dump ptest results to a subdirectory named after the test run when using --dump-ptest.
90 Required if more than one test run is present in the result file''')
91 parser.add_argument('--raw', action='store_true',
Brad Bishop79641f22019-09-10 07:20:22 -040092 help='show raw (ptest) logs. Deprecated. Alias for "--raw-ptest"', dest='raw_ptest')
93 parser.add_argument('--raw-ptest', action='store_true',
94 help='show raw ptest log')
95 parser.add_argument('--raw-reproducible', action='store_true',
96 help='show raw reproducible build logs')
Brad Bishopc342db32019-05-15 21:57:59 -040097