store.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # test result tool - store test results
  2. #
  3. # Copyright (c) 2019, Intel Corporation.
  4. #
  5. # This program is free software; you can redistribute it and/or modify it
  6. # under the terms and conditions of the GNU General Public License,
  7. # version 2, as published by the Free Software Foundation.
  8. #
  9. # This program is distributed in the hope it will be useful, but WITHOUT
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. # more details.
  13. #
  14. import datetime
  15. import tempfile
  16. import os
  17. import subprocess
  18. import scriptpath
  19. scriptpath.add_bitbake_lib_path()
  20. scriptpath.add_oe_lib_path()
  21. from resulttool.resultsutils import checkout_git_dir
  22. try:
  23. import bb
  24. except ImportError:
  25. pass
  26. class ResultsGitStore(object):
  27. def _get_output_dir(self):
  28. basepath = os.environ['BUILDDIR']
  29. return basepath + '/testresults_%s/' % datetime.datetime.now().strftime("%Y%m%d%H%M%S")
  30. def _create_temporary_workspace_dir(self):
  31. return tempfile.mkdtemp(prefix='testresults.')
  32. def _remove_temporary_workspace_dir(self, workspace_dir):
  33. return subprocess.run(["rm", "-rf", workspace_dir])
  34. def _oe_copy_files(self, source_dir, destination_dir):
  35. from oe.path import copytree
  36. copytree(source_dir, destination_dir)
  37. def _copy_files(self, source_dir, destination_dir, copy_ignore=None):
  38. from shutil import copytree
  39. copytree(source_dir, destination_dir, ignore=copy_ignore)
  40. def _store_files_to_git(self, logger, file_dir, git_dir, git_branch, commit_msg_subject, commit_msg_body):
  41. logger.debug('Storing test result into git repository (%s) and branch (%s)'
  42. % (git_dir, git_branch))
  43. return subprocess.run(["oe-git-archive",
  44. file_dir,
  45. "-g", git_dir,
  46. "-b", git_branch,
  47. "--commit-msg-subject", commit_msg_subject,
  48. "--commit-msg-body", commit_msg_body])
  49. def store_to_existing(self, logger, source_dir, git_dir, git_branch):
  50. logger.debug('Storing files to existing git repository and branch')
  51. from shutil import ignore_patterns
  52. dest_dir = self._create_temporary_workspace_dir()
  53. dest_top_dir = os.path.join(dest_dir, 'top_dir')
  54. self._copy_files(git_dir, dest_top_dir, copy_ignore=ignore_patterns('.git'))
  55. self._oe_copy_files(source_dir, dest_top_dir)
  56. self._store_files_to_git(logger, dest_top_dir, git_dir, git_branch,
  57. 'Store as existing git and branch', 'Store as existing git repository and branch')
  58. self._remove_temporary_workspace_dir(dest_dir)
  59. return git_dir
  60. def store_to_existing_with_new_branch(self, logger, source_dir, git_dir, git_branch):
  61. logger.debug('Storing files to existing git repository with new branch')
  62. self._store_files_to_git(logger, source_dir, git_dir, git_branch,
  63. 'Store as existing git with new branch',
  64. 'Store as existing git repository with new branch')
  65. return git_dir
  66. def store_to_new(self, logger, source_dir, git_branch):
  67. logger.debug('Storing files to new git repository')
  68. output_dir = self._get_output_dir()
  69. self._store_files_to_git(logger, source_dir, output_dir, git_branch,
  70. 'Store as new', 'Store as new git repository')
  71. return output_dir
  72. def store(self, logger, source_dir, git_dir, git_branch):
  73. if git_dir:
  74. if checkout_git_dir(git_dir, git_branch):
  75. self.store_to_existing(logger, source_dir, git_dir, git_branch)
  76. else:
  77. self.store_to_existing_with_new_branch(logger, source_dir, git_dir, git_branch)
  78. else:
  79. self.store_to_new(logger, source_dir, git_branch)
  80. def store(args, logger):
  81. gitstore = ResultsGitStore()
  82. gitstore.store(logger, args.source_dir, args.git_dir, args.git_branch)
  83. return 0
  84. def register_commands(subparsers):
  85. """Register subcommands from this plugin"""
  86. parser_build = subparsers.add_parser('store', help='store test result files and directories into git repository',
  87. description='store the testresults.json files and related directories '
  88. 'from the source directory into the destination git repository '
  89. 'with the given git branch',
  90. group='setup')
  91. parser_build.set_defaults(func=store)
  92. parser_build.add_argument('source_dir',
  93. help='source directory that contain the test result files and directories to be stored')
  94. parser_build.add_argument('git_branch', help='git branch used for store')
  95. parser_build.add_argument('-d', '--git-dir', default='',
  96. help='(optional) default store to new <top_dir>/<build>/<testresults_datetime> '
  97. 'directory unless provided with existing git repository as destination')