1c342db35SBrad Bishop# resulttool - Show logs
2c342db35SBrad Bishop#
3c342db35SBrad Bishop# Copyright (c) 2019 Garmin International
4c342db35SBrad Bishop#
5c342db35SBrad Bishop# SPDX-License-Identifier: GPL-2.0-only
6c342db35SBrad Bishop#
7c342db35SBrad Bishopimport os
8c342db35SBrad Bishopimport resulttool.resultutils as resultutils
9c342db35SBrad Bishop
10c342db35SBrad Bishopdef show_ptest(result, ptest, logger):
11a34c030eSBrad Bishop    logdata = resultutils.ptestresult_get_log(result, ptest)
12a34c030eSBrad Bishop    if logdata is not None:
13a34c030eSBrad Bishop        print(logdata)
14c342db35SBrad Bishop        return 0
15c342db35SBrad Bishop
16a34c030eSBrad Bishop    print("ptest '%s' log not found" % ptest)
17c342db35SBrad Bishop    return 1
18c342db35SBrad Bishop
1979641f25SBrad Bishopdef show_reproducible(result, reproducible, logger):
2079641f25SBrad Bishop    try:
2179641f25SBrad Bishop        print(result['reproducible'][reproducible]['diffoscope.text'])
2279641f25SBrad Bishop        return 0
2379641f25SBrad Bishop
2479641f25SBrad Bishop    except KeyError:
2579641f25SBrad Bishop        print("reproducible '%s' not found" % reproducible)
2679641f25SBrad Bishop        return 1
2779641f25SBrad Bishop
28c342db35SBrad Bishopdef log(args, logger):
29c342db35SBrad Bishop    results = resultutils.load_resultsdata(args.source)
30c342db35SBrad Bishop
31c342db35SBrad Bishop    for _, run_name, _, r in resultutils.test_run_results(results):
32*fc113eadSAndrew Geissler        if args.list_ptest:
33*fc113eadSAndrew Geissler            print('\n'.join(sorted(r['ptestresult.sections'].keys())))
34*fc113eadSAndrew Geissler
351e34c2d0SAndrew Geissler        if args.dump_ptest:
361e34c2d0SAndrew Geissler            for sectname in ['ptestresult.sections', 'ltpposixresult.sections', 'ltpresult.sections']:
371e34c2d0SAndrew Geissler             if sectname in r:
381e34c2d0SAndrew Geissler              for name, ptest in r[sectname].items():
391e34c2d0SAndrew Geissler                logdata = resultutils.generic_get_log(sectname, r, name)
40a34c030eSBrad Bishop                if logdata is not None:
41c342db35SBrad Bishop                    dest_dir = args.dump_ptest
42c342db35SBrad Bishop                    if args.prepend_run:
43c342db35SBrad Bishop                        dest_dir = os.path.join(dest_dir, run_name)
441e34c2d0SAndrew Geissler                    if not sectname.startswith("ptest"):
451e34c2d0SAndrew Geissler                        dest_dir = os.path.join(dest_dir, sectname.split(".")[0])
46c342db35SBrad Bishop
47c342db35SBrad Bishop                    os.makedirs(dest_dir, exist_ok=True)
48c342db35SBrad Bishop                    dest = os.path.join(dest_dir, '%s.log' % name)
49*fc113eadSAndrew Geissler                    if os.path.exists(dest):
50*fc113eadSAndrew Geissler                        print("Overlapping ptest logs found, skipping %s. The '--prepend-run' option would avoid this" % name)
51*fc113eadSAndrew Geissler                        continue
52c342db35SBrad Bishop                    print(dest)
53c342db35SBrad Bishop                    with open(dest, 'w') as f:
54a34c030eSBrad Bishop                        f.write(logdata)
55c342db35SBrad Bishop
5679641f25SBrad Bishop        if args.raw_ptest:
571e34c2d0SAndrew Geissler            found = False
581e34c2d0SAndrew Geissler            for sectname in ['ptestresult.rawlogs', 'ltpposixresult.rawlogs', 'ltpresult.rawlogs']:
591e34c2d0SAndrew Geissler                rawlog = resultutils.generic_get_rawlogs(sectname, r)
60a34c030eSBrad Bishop                if rawlog is not None:
61a34c030eSBrad Bishop                    print(rawlog)
621e34c2d0SAndrew Geissler                    found = True
631e34c2d0SAndrew Geissler            if not found:
6479641f25SBrad Bishop                print('Raw ptest logs not found')
6579641f25SBrad Bishop                return 1
6679641f25SBrad Bishop
6779641f25SBrad Bishop        if args.raw_reproducible:
6879641f25SBrad Bishop            if 'reproducible.rawlogs' in r:
6979641f25SBrad Bishop                print(r['reproducible.rawlogs']['log'])
7079641f25SBrad Bishop            else:
7179641f25SBrad Bishop                print('Raw reproducible logs not found')
72c342db35SBrad Bishop                return 1
73c342db35SBrad Bishop
74c342db35SBrad Bishop        for ptest in args.ptest:
75c342db35SBrad Bishop            if not show_ptest(r, ptest, logger):
76c342db35SBrad Bishop                return 1
77c342db35SBrad Bishop
7879641f25SBrad Bishop        for reproducible in args.reproducible:
7979641f25SBrad Bishop            if not show_reproducible(r, reproducible, logger):
8079641f25SBrad Bishop                return 1
8179641f25SBrad Bishop
82c342db35SBrad Bishopdef register_commands(subparsers):
83c342db35SBrad Bishop    """Register subcommands from this plugin"""
84c342db35SBrad Bishop    parser = subparsers.add_parser('log', help='show logs',
85c342db35SBrad Bishop                                         description='show the logs from test results',
86c342db35SBrad Bishop                                         group='analysis')
87c342db35SBrad Bishop    parser.set_defaults(func=log)
88c342db35SBrad Bishop    parser.add_argument('source',
89c342db35SBrad Bishop            help='the results file/directory/URL to import')
90*fc113eadSAndrew Geissler    parser.add_argument('--list-ptest', action='store_true',
91*fc113eadSAndrew Geissler            help='list the ptest test names')
92c342db35SBrad Bishop    parser.add_argument('--ptest', action='append', default=[],
93c342db35SBrad Bishop            help='show logs for a ptest')
94c342db35SBrad Bishop    parser.add_argument('--dump-ptest', metavar='DIR',
95c342db35SBrad Bishop            help='Dump all ptest log files to the specified directory.')
9679641f25SBrad Bishop    parser.add_argument('--reproducible', action='append', default=[],
9779641f25SBrad Bishop            help='show logs for a reproducible test')
98c342db35SBrad Bishop    parser.add_argument('--prepend-run', action='store_true',
99c342db35SBrad Bishop            help='''Dump ptest results to a subdirectory named after the test run when using --dump-ptest.
100c342db35SBrad Bishop                    Required if more than one test run is present in the result file''')
101c342db35SBrad Bishop    parser.add_argument('--raw', action='store_true',
10279641f25SBrad Bishop            help='show raw (ptest) logs. Deprecated. Alias for "--raw-ptest"', dest='raw_ptest')
10379641f25SBrad Bishop    parser.add_argument('--raw-ptest', action='store_true',
10479641f25SBrad Bishop            help='show raw ptest log')
10579641f25SBrad Bishop    parser.add_argument('--raw-reproducible', action='store_true',
10679641f25SBrad Bishop            help='show raw reproducible build logs')
107c342db35SBrad Bishop
108