blob: 49816357cdaced2ce12a74a7a8995e2fb3dff3cf [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001# resulttool - Show logs
2#
3# Copyright (c) 2019 Garmin International
4#
5# This program is free software; you can redistribute it and/or modify it
6# under the terms and conditions of the GNU General Public License,
7# version 2, as published by the Free Software Foundation.
8#
9# This program is distributed in the hope it will be useful, but WITHOUT
10# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12# more details.
13#
14import os
15import resulttool.resultutils as resultutils
16
17def show_ptest(result, ptest, logger):
18 if 'ptestresult.sections' in result:
19 if ptest in result['ptestresult.sections'] and 'log' in result['ptestresult.sections'][ptest]:
20 print(result['ptestresult.sections'][ptest]['log'])
21 return 0
22
23 print("ptest '%s' not found" % ptest)
24 return 1
25
26def log(args, logger):
27 results = resultutils.load_resultsdata(args.source)
28
29 ptest_count = sum(1 for _, _, _, r in resultutils.test_run_results(results) if 'ptestresult.sections' in r)
30 if ptest_count > 1 and not args.prepend_run:
31 print("%i ptest sections found. '--prepend-run' is required" % ptest_count)
32 return 1
33
34 for _, run_name, _, r in resultutils.test_run_results(results):
35 if args.dump_ptest:
36 if 'ptestresult.sections' in r:
37 for name, ptest in r['ptestresult.sections'].items():
38 if 'log' in ptest:
39 dest_dir = args.dump_ptest
40 if args.prepend_run:
41 dest_dir = os.path.join(dest_dir, run_name)
42
43 os.makedirs(dest_dir, exist_ok=True)
44
45 dest = os.path.join(dest_dir, '%s.log' % name)
46 print(dest)
47 with open(dest, 'w') as f:
48 f.write(ptest['log'])
49
50 if args.raw:
51 if 'ptestresult.rawlogs' in r:
52 print(r['ptestresult.rawlogs']['log'])
53 else:
54 print('Raw logs not found')
55 return 1
56
57 for ptest in args.ptest:
58 if not show_ptest(r, ptest, logger):
59 return 1
60
61def register_commands(subparsers):
62 """Register subcommands from this plugin"""
63 parser = subparsers.add_parser('log', help='show logs',
64 description='show the logs from test results',
65 group='analysis')
66 parser.set_defaults(func=log)
67 parser.add_argument('source',
68 help='the results file/directory/URL to import')
69 parser.add_argument('--ptest', action='append', default=[],
70 help='show logs for a ptest')
71 parser.add_argument('--dump-ptest', metavar='DIR',
72 help='Dump all ptest log files to the specified directory.')
73 parser.add_argument('--prepend-run', action='store_true',
74 help='''Dump ptest results to a subdirectory named after the test run when using --dump-ptest.
75 Required if more than one test run is present in the result file''')
76 parser.add_argument('--raw', action='store_true',
77 help='show raw logs')
78