store.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # resulttool - store test results
  2. #
  3. # Copyright (c) 2019, Intel Corporation.
  4. # Copyright (c) 2019, Linux Foundation
  5. #
  6. # This program is free software; you can redistribute it and/or modify it
  7. # under the terms and conditions of the GNU General Public License,
  8. # version 2, as published by the Free Software Foundation.
  9. #
  10. # This program is distributed in the hope it will be useful, but WITHOUT
  11. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. # more details.
  14. #
  15. import tempfile
  16. import os
  17. import subprocess
  18. import json
  19. import shutil
  20. import scriptpath
  21. scriptpath.add_bitbake_lib_path()
  22. scriptpath.add_oe_lib_path()
  23. import resulttool.resultutils as resultutils
  24. import oeqa.utils.gitarchive as gitarchive
  25. def store(args, logger):
  26. tempdir = tempfile.mkdtemp(prefix='testresults.')
  27. try:
  28. results = {}
  29. logger.info('Reading files from %s' % args.source)
  30. if resultutils.is_url(args.source) or os.path.isfile(args.source):
  31. resultutils.append_resultsdata(results, args.source)
  32. else:
  33. for root, dirs, files in os.walk(args.source):
  34. for name in files:
  35. f = os.path.join(root, name)
  36. if name == "testresults.json":
  37. resultutils.append_resultsdata(results, f)
  38. elif args.all:
  39. dst = f.replace(args.source, tempdir + "/")
  40. os.makedirs(os.path.dirname(dst), exist_ok=True)
  41. shutil.copyfile(f, dst)
  42. revisions = {}
  43. if not results and not args.all:
  44. if args.allow_empty:
  45. logger.info("No results found to store")
  46. return 0
  47. logger.error("No results found to store")
  48. return 1
  49. # Find the branch/commit/commit_count and ensure they all match
  50. for suite in results:
  51. for result in results[suite]:
  52. config = results[suite][result]['configuration']['LAYERS']['meta']
  53. revision = (config['commit'], config['branch'], str(config['commit_count']))
  54. if revision not in revisions:
  55. revisions[revision] = {}
  56. if suite not in revisions[revision]:
  57. revisions[revision][suite] = {}
  58. revisions[revision][suite][result] = results[suite][result]
  59. logger.info("Found %d revisions to store" % len(revisions))
  60. for r in revisions:
  61. results = revisions[r]
  62. keywords = {'commit': r[0], 'branch': r[1], "commit_count": r[2]}
  63. subprocess.check_call(["find", tempdir, "!", "-path", "./.git/*", "-delete"])
  64. resultutils.save_resultsdata(results, tempdir, ptestlogs=True)
  65. logger.info('Storing test result into git repository %s' % args.git_dir)
  66. gitarchive.gitarchive(tempdir, args.git_dir, False, False,
  67. "Results of {branch}:{commit}", "branch: {branch}\ncommit: {commit}", "{branch}",
  68. False, "{branch}/{commit_count}-g{commit}/{tag_number}",
  69. 'Test run #{tag_number} of {branch}:{commit}', '',
  70. [], [], False, keywords, logger)
  71. finally:
  72. subprocess.check_call(["rm", "-rf", tempdir])
  73. return 0
  74. def register_commands(subparsers):
  75. """Register subcommands from this plugin"""
  76. parser_build = subparsers.add_parser('store', help='store test results into a git repository',
  77. description='takes a results file or directory of results files and stores '
  78. 'them into the destination git repository, splitting out the results '
  79. 'files as configured',
  80. group='setup')
  81. parser_build.set_defaults(func=store)
  82. parser_build.add_argument('source',
  83. help='source file/directory/URL that contain the test result files to be stored')
  84. parser_build.add_argument('git_dir',
  85. help='the location of the git repository to store the results in')
  86. parser_build.add_argument('-a', '--all', action='store_true',
  87. help='include all files, not just testresults.json files')
  88. parser_build.add_argument('-e', '--allow-empty', action='store_true',
  89. help='don\'t error if no results to store are found')