core.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import logging
  2. import time
  3. import os
  4. import fnmatch
  5. import ap_git
  6. from . import exceptions as ex
  7. logger = logging.getLogger(__name__)
  8. class APSourceMetadataFetcher:
  9. """
  10. Class to fetch metadata like available boards, features etc.
  11. from the AP source code
  12. """
  13. __singleton = None
  14. def __init__(self, ap_repo_path: str) -> None:
  15. """
  16. Initializes the APSourceMetadataFetcher instance
  17. with a given repository path.
  18. Parameters:
  19. ap_repo_path (str): Path to the repository where
  20. metadata scripts are located.
  21. Raises:
  22. TooManyInstancesError: If an instance of this class already exists,
  23. enforcing a singleton pattern.
  24. """
  25. # Enforce singleton pattern by raising an error if
  26. # an instance already exists.
  27. if APSourceMetadataFetcher.__singleton:
  28. raise ex.TooManyInstancesError()
  29. # Initialize the Git repository object pointing to the source repo.
  30. self.repo = ap_git.GitRepo(local_path=ap_repo_path)
  31. APSourceMetadataFetcher.__singleton = self
  32. def get_boards_at_commit(self, remote: str,
  33. commit_ref: str) -> tuple:
  34. """
  35. Retrieves a list of boards available for building at a
  36. specified commit and returns the list and the default board.
  37. Parameters:
  38. remote (str): The name of the remote repository.
  39. commit_ref (str): The commit reference to check out.
  40. Returns:
  41. tuple: A tuple containing:
  42. - boards (list): A list of boards available at the
  43. specified commit.
  44. - default_board (str): The first board in the sorted list,
  45. designated as the default.
  46. """
  47. tstart = time.time()
  48. import importlib.util
  49. with self.repo.get_checkout_lock():
  50. self.repo.checkout_remote_commit_ref(
  51. remote=remote,
  52. commit_ref=commit_ref,
  53. force=True,
  54. hard_reset=True,
  55. clean_working_tree=True
  56. )
  57. spec = importlib.util.spec_from_file_location(
  58. name="board_list.py",
  59. location=os.path.join(
  60. self.repo.get_local_path(),
  61. 'Tools', 'scripts',
  62. 'board_list.py')
  63. )
  64. mod = importlib.util.module_from_spec(spec)
  65. spec.loader.exec_module(mod)
  66. all_boards = mod.AUTOBUILD_BOARDS
  67. exclude_patterns = ['fmuv*', 'SITL*']
  68. boards = []
  69. for b in all_boards:
  70. excluded = False
  71. for p in exclude_patterns:
  72. if fnmatch.fnmatch(b.lower(), p.lower()):
  73. excluded = True
  74. break
  75. if not excluded:
  76. boards.append(b)
  77. logger.debug(
  78. f"Took {(time.time() - tstart)} seconds to get boards"
  79. )
  80. boards.sort()
  81. default_board = boards[0]
  82. return (boards, default_board)
  83. def get_build_options_at_commit(self, remote: str,
  84. commit_ref: str) -> list:
  85. """
  86. Retrieves a list of build options available at a specified commit.
  87. Parameters:
  88. remote (str): The name of the remote repository.
  89. commit_ref (str): The commit reference to check out.
  90. Returns:
  91. list: A list of build options available at the specified commit.
  92. """
  93. tstart = time.time()
  94. import importlib.util
  95. with self.repo.get_checkout_lock():
  96. self.repo.checkout_remote_commit_ref(
  97. remote=remote,
  98. commit_ref=commit_ref,
  99. force=True,
  100. hard_reset=True,
  101. clean_working_tree=True
  102. )
  103. spec = importlib.util.spec_from_file_location(
  104. name="build_options.py",
  105. location=os.path.join(
  106. self.repo.get_local_path(),
  107. 'Tools',
  108. 'scripts',
  109. 'build_options.py'
  110. )
  111. )
  112. mod = importlib.util.module_from_spec(spec)
  113. spec.loader.exec_module(mod)
  114. build_options = mod.BUILD_OPTIONS
  115. logger.debug(
  116. f"Took {(time.time() - tstart)} seconds to get build options"
  117. )
  118. return build_options
  119. @staticmethod
  120. def get_singleton():
  121. return APSourceMetadataFetcher.__singleton