1# 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):
11    logdata = resultutils.ptestresult_get_log(result, ptest)
12    if logdata is not None:
13        print(logdata)
14        return 0
15
16    print("ptest '%s' log not found" % ptest)
17    return 1
18
19def 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
28def log(args, logger):
29    results = resultutils.load_resultsdata(args.source)
30
31    for _, run_name, _, r in resultutils.test_run_results(results):
32        if args.list_ptest:
33            print('\n'.join(sorted(r['ptestresult.sections'].keys())))
34
35        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)
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)
44                    if not sectname.startswith("ptest"):
45                        dest_dir = os.path.join(dest_dir, sectname.split(".")[0])
46
47                    os.makedirs(dest_dir, exist_ok=True)
48                    dest = os.path.join(dest_dir, '%s.log' % name)
49                    if os.path.exists(dest):
50                        print("Overlapping ptest logs found, skipping %s. The '--prepend-run' option would avoid this" % name)
51                        continue
52                    print(dest)
53                    with open(dest, 'w') as f:
54                        f.write(logdata)
55
56        if args.raw_ptest:
57            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:
64                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')
72                return 1
73
74        for ptest in args.ptest:
75            if not show_ptest(r, ptest, logger):
76                return 1
77
78        for reproducible in args.reproducible:
79            if not show_reproducible(r, reproducible, logger):
80                return 1
81
82def 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')
90    parser.add_argument('--list-ptest', action='store_true',
91            help='list the ptest test names')
92    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.')
96    parser.add_argument('--reproducible', action='append', default=[],
97            help='show logs for a reproducible test')
98    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',
102            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')
107
108