log.py 4.1 KB

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