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