1*4882a593Smuzhiyun# resulttool - Show logs 2*4882a593Smuzhiyun# 3*4882a593Smuzhiyun# Copyright (c) 2019 Garmin International 4*4882a593Smuzhiyun# 5*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0-only 6*4882a593Smuzhiyun# 7*4882a593Smuzhiyunimport os 8*4882a593Smuzhiyunimport resulttool.resultutils as resultutils 9*4882a593Smuzhiyun 10*4882a593Smuzhiyundef show_ptest(result, ptest, logger): 11*4882a593Smuzhiyun logdata = resultutils.ptestresult_get_log(result, ptest) 12*4882a593Smuzhiyun if logdata is not None: 13*4882a593Smuzhiyun print(logdata) 14*4882a593Smuzhiyun return 0 15*4882a593Smuzhiyun 16*4882a593Smuzhiyun print("ptest '%s' log not found" % ptest) 17*4882a593Smuzhiyun return 1 18*4882a593Smuzhiyun 19*4882a593Smuzhiyundef show_reproducible(result, reproducible, logger): 20*4882a593Smuzhiyun try: 21*4882a593Smuzhiyun print(result['reproducible'][reproducible]['diffoscope.text']) 22*4882a593Smuzhiyun return 0 23*4882a593Smuzhiyun 24*4882a593Smuzhiyun except KeyError: 25*4882a593Smuzhiyun print("reproducible '%s' not found" % reproducible) 26*4882a593Smuzhiyun return 1 27*4882a593Smuzhiyun 28*4882a593Smuzhiyundef log(args, logger): 29*4882a593Smuzhiyun results = resultutils.load_resultsdata(args.source) 30*4882a593Smuzhiyun 31*4882a593Smuzhiyun ptest_count = sum(1 for _, _, _, r in resultutils.test_run_results(results) if 'ptestresult.sections' in r) 32*4882a593Smuzhiyun if ptest_count > 1 and not args.prepend_run: 33*4882a593Smuzhiyun print("%i ptest sections found. '--prepend-run' is required" % ptest_count) 34*4882a593Smuzhiyun return 1 35*4882a593Smuzhiyun 36*4882a593Smuzhiyun for _, run_name, _, r in resultutils.test_run_results(results): 37*4882a593Smuzhiyun if args.dump_ptest: 38*4882a593Smuzhiyun for sectname in ['ptestresult.sections', 'ltpposixresult.sections', 'ltpresult.sections']: 39*4882a593Smuzhiyun if sectname in r: 40*4882a593Smuzhiyun for name, ptest in r[sectname].items(): 41*4882a593Smuzhiyun logdata = resultutils.generic_get_log(sectname, r, name) 42*4882a593Smuzhiyun if logdata is not None: 43*4882a593Smuzhiyun dest_dir = args.dump_ptest 44*4882a593Smuzhiyun if args.prepend_run: 45*4882a593Smuzhiyun dest_dir = os.path.join(dest_dir, run_name) 46*4882a593Smuzhiyun if not sectname.startswith("ptest"): 47*4882a593Smuzhiyun dest_dir = os.path.join(dest_dir, sectname.split(".")[0]) 48*4882a593Smuzhiyun 49*4882a593Smuzhiyun os.makedirs(dest_dir, exist_ok=True) 50*4882a593Smuzhiyun dest = os.path.join(dest_dir, '%s.log' % name) 51*4882a593Smuzhiyun print(dest) 52*4882a593Smuzhiyun with open(dest, 'w') as f: 53*4882a593Smuzhiyun f.write(logdata) 54*4882a593Smuzhiyun 55*4882a593Smuzhiyun if args.raw_ptest: 56*4882a593Smuzhiyun found = False 57*4882a593Smuzhiyun for sectname in ['ptestresult.rawlogs', 'ltpposixresult.rawlogs', 'ltpresult.rawlogs']: 58*4882a593Smuzhiyun rawlog = resultutils.generic_get_rawlogs(sectname, r) 59*4882a593Smuzhiyun if rawlog is not None: 60*4882a593Smuzhiyun print(rawlog) 61*4882a593Smuzhiyun found = True 62*4882a593Smuzhiyun if not found: 63*4882a593Smuzhiyun print('Raw ptest logs not found') 64*4882a593Smuzhiyun return 1 65*4882a593Smuzhiyun 66*4882a593Smuzhiyun if args.raw_reproducible: 67*4882a593Smuzhiyun if 'reproducible.rawlogs' in r: 68*4882a593Smuzhiyun print(r['reproducible.rawlogs']['log']) 69*4882a593Smuzhiyun else: 70*4882a593Smuzhiyun print('Raw reproducible logs not found') 71*4882a593Smuzhiyun return 1 72*4882a593Smuzhiyun 73*4882a593Smuzhiyun for ptest in args.ptest: 74*4882a593Smuzhiyun if not show_ptest(r, ptest, logger): 75*4882a593Smuzhiyun return 1 76*4882a593Smuzhiyun 77*4882a593Smuzhiyun for reproducible in args.reproducible: 78*4882a593Smuzhiyun if not show_reproducible(r, reproducible, logger): 79*4882a593Smuzhiyun return 1 80*4882a593Smuzhiyun 81*4882a593Smuzhiyundef register_commands(subparsers): 82*4882a593Smuzhiyun """Register subcommands from this plugin""" 83*4882a593Smuzhiyun parser = subparsers.add_parser('log', help='show logs', 84*4882a593Smuzhiyun description='show the logs from test results', 85*4882a593Smuzhiyun group='analysis') 86*4882a593Smuzhiyun parser.set_defaults(func=log) 87*4882a593Smuzhiyun parser.add_argument('source', 88*4882a593Smuzhiyun help='the results file/directory/URL to import') 89*4882a593Smuzhiyun parser.add_argument('--ptest', action='append', default=[], 90*4882a593Smuzhiyun help='show logs for a ptest') 91*4882a593Smuzhiyun parser.add_argument('--dump-ptest', metavar='DIR', 92*4882a593Smuzhiyun help='Dump all ptest log files to the specified directory.') 93*4882a593Smuzhiyun parser.add_argument('--reproducible', action='append', default=[], 94*4882a593Smuzhiyun help='show logs for a reproducible test') 95*4882a593Smuzhiyun parser.add_argument('--prepend-run', action='store_true', 96*4882a593Smuzhiyun help='''Dump ptest results to a subdirectory named after the test run when using --dump-ptest. 97*4882a593Smuzhiyun Required if more than one test run is present in the result file''') 98*4882a593Smuzhiyun parser.add_argument('--raw', action='store_true', 99*4882a593Smuzhiyun help='show raw (ptest) logs. Deprecated. Alias for "--raw-ptest"', dest='raw_ptest') 100*4882a593Smuzhiyun parser.add_argument('--raw-ptest', action='store_true', 101*4882a593Smuzhiyun help='show raw ptest log') 102*4882a593Smuzhiyun parser.add_argument('--raw-reproducible', action='store_true', 103*4882a593Smuzhiyun help='show raw reproducible build logs') 104*4882a593Smuzhiyun 105