ci_fetch_submodule.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/env python
  2. # SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. # SPDX-License-Identifier: Apache-2.0
  4. # internal use only for CI
  5. # download archive of one commit instead of cloning entire submodule repo
  6. import argparse
  7. import os
  8. import re
  9. import shutil
  10. import subprocess
  11. import time
  12. import gitlab_api
  13. SUBMODULE_PATTERN = re.compile(r"\[submodule \"([^\"]+)\"]")
  14. PATH_PATTERN = re.compile(r'path\s+=\s+(\S+)')
  15. URL_PATTERN = re.compile(r'url\s+=\s+(\S+)')
  16. SUBMODULE_ARCHIVE_TEMP_FOLDER = 'submodule_archive'
  17. # need to match the one defined in CI yaml files for caching purpose
  18. SUBMODULE_ARCHIVE_CACHE_DIR = '.cache/submodule_archives'
  19. class SubModule(object):
  20. # We don't need to support recursive submodule clone now
  21. GIT_LS_TREE_OUTPUT_PATTERN = re.compile(r'\d+\s+commit\s+([0-9a-f]+)\s+')
  22. def __init__(self, gitlab_inst, path, url):
  23. self.path = path
  24. self.url = url
  25. self.gitlab_inst = gitlab_inst
  26. self.project_id = self._get_project_id(url)
  27. self.commit_id = self._get_commit_id(path)
  28. def _get_commit_id(self, path):
  29. output = subprocess.check_output(['git', 'ls-tree', 'HEAD', path])
  30. output = output.decode()
  31. # example output: 160000 commit d88a262fbdf35e5abb372280eb08008749c3faa0 components/esp_wifi/lib
  32. match = self.GIT_LS_TREE_OUTPUT_PATTERN.search(output)
  33. return match.group(1)
  34. def _get_project_id(self, url):
  35. base_name = os.path.basename(url)
  36. project_id = self.gitlab_inst.get_project_id(os.path.splitext(base_name)[0], # remove .git
  37. namespace='espressif')
  38. return project_id
  39. def download_archive(self):
  40. print('Update submodule: {}: {}'.format(self.path, self.commit_id))
  41. path_name = self.gitlab_inst.download_archive(self.commit_id, SUBMODULE_ARCHIVE_TEMP_FOLDER,
  42. self.project_id, SUBMODULE_ARCHIVE_CACHE_DIR)
  43. renamed_path = os.path.join(os.path.dirname(path_name), os.path.basename(self.path))
  44. os.rename(path_name, renamed_path)
  45. shutil.rmtree(self.path, ignore_errors=True)
  46. shutil.move(renamed_path, os.path.dirname(self.path))
  47. def update_submodule(git_module_file, submodules_to_update):
  48. gitlab_inst = gitlab_api.Gitlab()
  49. submodules = []
  50. with open(git_module_file, 'r') as f:
  51. data = f.read()
  52. match = SUBMODULE_PATTERN.search(data)
  53. while True:
  54. next_match = SUBMODULE_PATTERN.search(data, pos=match.end())
  55. if next_match:
  56. end_pos = next_match.start()
  57. else:
  58. end_pos = len(data)
  59. path_match = PATH_PATTERN.search(data, pos=match.end(), endpos=end_pos)
  60. url_match = URL_PATTERN.search(data, pos=match.end(), endpos=end_pos)
  61. path = path_match.group(1)
  62. url = url_match.group(1)
  63. filter_result = True
  64. if submodules_to_update:
  65. if path not in submodules_to_update:
  66. filter_result = False
  67. if filter_result:
  68. submodules.append(SubModule(gitlab_inst, path, url))
  69. match = next_match
  70. if not match:
  71. break
  72. shutil.rmtree(SUBMODULE_ARCHIVE_TEMP_FOLDER, ignore_errors=True)
  73. for submodule in submodules:
  74. submodule.download_archive()
  75. if __name__ == '__main__':
  76. start_time = time.time()
  77. parser = argparse.ArgumentParser()
  78. parser.add_argument('--repo_path', '-p', default='.', help='repo path')
  79. parser.add_argument('--submodule', '-s', default='all',
  80. help='Submodules to update. By default update all submodules. '
  81. 'For multiple submodules, separate them with `;`. '
  82. '`all` and `none` are special values that indicates we fetch all / none submodules')
  83. args = parser.parse_args()
  84. if args.submodule == 'none':
  85. print("don't need to update submodules")
  86. exit(0)
  87. if args.submodule == 'all':
  88. _submodules = []
  89. else:
  90. _submodules = args.submodule.split(';')
  91. update_submodule(os.path.join(args.repo_path, '.gitmodules'), _submodules)
  92. print('total time spent on update submodule: {:.02f}s'.format(time.time() - start_time))