blob: eb3927ec82a564c36b968bea3e7b869976d0ccf2 [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):
Andrew Geissler1e34c2d2020-05-29 16:02:59 -050037 if args.dump_ptest:
38 for sectname in ['ptestresult.sections', 'ltpposixresult.sections', 'ltpresult.sections']:
39 if sectname in r:
40 for name, ptest in r[sectname].items():
41 logdata = resultutils.generic_get_log(sectname, r, name)
Brad Bishopa34c0302019-09-23 22:34:48 -040042 if logdata is not None:
43 dest_dir = args.dump_ptest
44 if args.prepend_run:
45 dest_dir = os.path.join(dest_dir, run_name)
Andrew Geissler1e34c2d2020-05-29 16:02:59 -050046 if not sectname.startswith("ptest"):
47 dest_dir = os.path.join(dest_dir, sectname.split(".")[0])
Brad Bishopc342db32019-05-15 21:57:59 -040048
Brad Bishopa34c0302019-09-23 22:34:48 -040049 os.makedirs(dest_dir, exist_ok=True)
50 dest = os.path.join(dest_dir, '%s.log' % name)
51 print(dest)
52 with open(dest, 'w') as f:
53 f.write(logdata)
Brad Bishopc342db32019-05-15 21:57:59 -040054
Brad Bishop79641f22019-09-10 07:20:22 -040055 if args.raw_ptest:
Andrew Geissler1e34c2d2020-05-29 16:02:59 -050056 found = False
57 for sectname in ['ptestresult.rawlogs', 'ltpposixresult.rawlogs', 'ltpresult.rawlogs']:
58 rawlog = resultutils.generic_get_rawlogs(sectname, r)
59 if rawlog is not None:
60 print(rawlog)
61 found = True
62 if not found:
Brad Bishop79641f22019-09-10 07:20:22 -040063 print('Raw ptest logs not found')
64 return 1
65
66 if args.raw_reproducible:
67 if 'reproducible.rawlogs' in r:
68 print(r['reproducible.rawlogs']['log'])
69 else:
70 print('Raw reproducible logs not found')
Brad Bishopc342db32019-05-15 21:57:59 -040071 return 1
72
73 for ptest in args.ptest:
74 if not show_ptest(r, ptest, logger):
75 return 1
76
Brad Bishop79641f22019-09-10 07:20:22 -040077 for reproducible in args.reproducible:
78 if not show_reproducible(r, reproducible, logger):
79 return 1
80
Brad Bishopc342db32019-05-15 21:57:59 -040081def register_commands(subparsers):
82 """Register subcommands from this plugin"""
83 parser = subparsers.add_parser('log', help='show logs',
84 description='show the logs from test results',
85 group='analysis')
86 parser.set_defaults(func=log)
87 parser.add_argument('source',
88 help='the results file/directory/URL to import')
89 parser.add_argument('--ptest', action='append', default=[],
90 help='show logs for a ptest')
91 parser.add_argument('--dump-ptest', metavar='DIR',
92 help='Dump all ptest log files to the specified directory.')
Brad Bishop79641f22019-09-10 07:20:22 -040093 parser.add_argument('--reproducible', action='append', default=[],
94 help='show logs for a reproducible test')
Brad Bishopc342db32019-05-15 21:57:59 -040095 parser.add_argument('--prepend-run', action='store_true',
96 help='''Dump ptest results to a subdirectory named after the test run when using --dump-ptest.
97 Required if more than one test run is present in the result file''')
98 parser.add_argument('--raw', action='store_true',
Brad Bishop79641f22019-09-10 07:20:22 -040099 help='show raw (ptest) logs. Deprecated. Alias for "--raw-ptest"', dest='raw_ptest')
100 parser.add_argument('--raw-ptest', action='store_true',
101 help='show raw ptest log')
102 parser.add_argument('--raw-reproducible', action='store_true',
103 help='show raw reproducible build logs')
Brad Bishopc342db32019-05-15 21:57:59 -0400104