blob: 15148ca2881fc197cb6bcd0509cba92a713b4946 [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
Brad Bishopc342db32019-05-15 21:57:59 -040031 for _, run_name, _, r in resultutils.test_run_results(results):
Andrew Geisslerfc113ea2023-03-31 09:59:46 -050032 if args.list_ptest:
33 print('\n'.join(sorted(r['ptestresult.sections'].keys())))
34
Andrew Geissler1e34c2d2020-05-29 16:02:59 -050035 if args.dump_ptest:
36 for sectname in ['ptestresult.sections', 'ltpposixresult.sections', 'ltpresult.sections']:
37 if sectname in r:
38 for name, ptest in r[sectname].items():
39 logdata = resultutils.generic_get_log(sectname, r, name)
Brad Bishopa34c0302019-09-23 22:34:48 -040040 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)
Andrew Geissler1e34c2d2020-05-29 16:02:59 -050044 if not sectname.startswith("ptest"):
45 dest_dir = os.path.join(dest_dir, sectname.split(".")[0])
Brad Bishopc342db32019-05-15 21:57:59 -040046
Brad Bishopa34c0302019-09-23 22:34:48 -040047 os.makedirs(dest_dir, exist_ok=True)
48 dest = os.path.join(dest_dir, '%s.log' % name)
Andrew Geisslerfc113ea2023-03-31 09:59:46 -050049 if os.path.exists(dest):
50 print("Overlapping ptest logs found, skipping %s. The '--prepend-run' option would avoid this" % name)
51 continue
Brad Bishopa34c0302019-09-23 22:34:48 -040052 print(dest)
53 with open(dest, 'w') as f:
54 f.write(logdata)
Brad Bishopc342db32019-05-15 21:57:59 -040055
Brad Bishop79641f22019-09-10 07:20:22 -040056 if args.raw_ptest:
Andrew Geissler1e34c2d2020-05-29 16:02:59 -050057 found = False
58 for sectname in ['ptestresult.rawlogs', 'ltpposixresult.rawlogs', 'ltpresult.rawlogs']:
59 rawlog = resultutils.generic_get_rawlogs(sectname, r)
60 if rawlog is not None:
61 print(rawlog)
62 found = True
63 if not found:
Brad Bishop79641f22019-09-10 07:20:22 -040064 print('Raw ptest logs not found')
65 return 1
66
67 if args.raw_reproducible:
68 if 'reproducible.rawlogs' in r:
69 print(r['reproducible.rawlogs']['log'])
70 else:
71 print('Raw reproducible logs not found')
Brad Bishopc342db32019-05-15 21:57:59 -040072 return 1
73
74 for ptest in args.ptest:
75 if not show_ptest(r, ptest, logger):
76 return 1
77
Brad Bishop79641f22019-09-10 07:20:22 -040078 for reproducible in args.reproducible:
79 if not show_reproducible(r, reproducible, logger):
80 return 1
81
Brad Bishopc342db32019-05-15 21:57:59 -040082def register_commands(subparsers):
83 """Register subcommands from this plugin"""
84 parser = subparsers.add_parser('log', help='show logs',
85 description='show the logs from test results',
86 group='analysis')
87 parser.set_defaults(func=log)
88 parser.add_argument('source',
89 help='the results file/directory/URL to import')
Andrew Geisslerfc113ea2023-03-31 09:59:46 -050090 parser.add_argument('--list-ptest', action='store_true',
91 help='list the ptest test names')
Brad Bishopc342db32019-05-15 21:57:59 -040092 parser.add_argument('--ptest', action='append', default=[],
93 help='show logs for a ptest')
94 parser.add_argument('--dump-ptest', metavar='DIR',
95 help='Dump all ptest log files to the specified directory.')
Brad Bishop79641f22019-09-10 07:20:22 -040096 parser.add_argument('--reproducible', action='append', default=[],
97 help='show logs for a reproducible test')
Brad Bishopc342db32019-05-15 21:57:59 -040098 parser.add_argument('--prepend-run', action='store_true',
99 help='''Dump ptest results to a subdirectory named after the test run when using --dump-ptest.
100 Required if more than one test run is present in the result file''')
101 parser.add_argument('--raw', action='store_true',
Brad Bishop79641f22019-09-10 07:20:22 -0400102 help='show raw (ptest) logs. Deprecated. Alias for "--raw-ptest"', dest='raw_ptest')
103 parser.add_argument('--raw-ptest', action='store_true',
104 help='show raw ptest log')
105 parser.add_argument('--raw-reproducible', action='store_true',
106 help='show raw reproducible build logs')
Brad Bishopc342db32019-05-15 21:57:59 -0400107