ci_fetch_submodule.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. class SubModule(object):
  18. # We don't need to support recursive submodule clone now
  19. GIT_LS_TREE_OUTPUT_PATTERN = re.compile(r'\d+\s+commit\s+([0-9a-f]+)\s+')
  20. def __init__(self, gitlab_inst, path, url):
  21. self.path = path
  22. self.gitlab_inst = gitlab_inst
  23. self.project_id = self._get_project_id(url)
  24. self.commit_id = self._get_commit_id(path)
  25. def _get_commit_id(self, path):
  26. output = subprocess.check_output(['git', 'ls-tree', 'HEAD', path])
  27. output = output.decode()
  28. # example output: 160000 commit d88a262fbdf35e5abb372280eb08008749c3faa0 components/esp_wifi/lib
  29. match = self.GIT_LS_TREE_OUTPUT_PATTERN.search(output)
  30. return match.group(1)
  31. def _get_project_id(self, url):
  32. base_name = os.path.basename(url)
  33. project_id = self.gitlab_inst.get_project_id(os.path.splitext(base_name)[0], # remove .git
  34. namespace='espressif')
  35. return project_id
  36. def download_archive(self):
  37. print('Update submodule: {}: {}'.format(self.path, self.commit_id))
  38. path_name = self.gitlab_inst.download_archive(self.commit_id, SUBMODULE_ARCHIVE_TEMP_FOLDER,
  39. self.project_id)
  40. renamed_path = os.path.join(os.path.dirname(path_name), os.path.basename(self.path))
  41. os.rename(path_name, renamed_path)
  42. shutil.rmtree(self.path, ignore_errors=True)
  43. shutil.move(renamed_path, os.path.dirname(self.path))
  44. def update_submodule(git_module_file, submodules_to_update):
  45. gitlab_inst = gitlab_api.Gitlab()
  46. submodules = []
  47. with open(git_module_file, 'r') as f:
  48. data = f.read()
  49. match = SUBMODULE_PATTERN.search(data)
  50. while True:
  51. next_match = SUBMODULE_PATTERN.search(data, pos=match.end())
  52. if next_match:
  53. end_pos = next_match.start()
  54. else:
  55. end_pos = len(data)
  56. path_match = PATH_PATTERN.search(data, pos=match.end(), endpos=end_pos)
  57. url_match = URL_PATTERN.search(data, pos=match.end(), endpos=end_pos)
  58. path = path_match.group(1)
  59. url = url_match.group(1)
  60. filter_result = True
  61. if submodules_to_update:
  62. if path not in submodules_to_update:
  63. filter_result = False
  64. if filter_result:
  65. submodules.append(SubModule(gitlab_inst, path, url))
  66. match = next_match
  67. if not match:
  68. break
  69. shutil.rmtree(SUBMODULE_ARCHIVE_TEMP_FOLDER, ignore_errors=True)
  70. for submodule in submodules:
  71. submodule.download_archive()
  72. if __name__ == '__main__':
  73. start_time = time.time()
  74. parser = argparse.ArgumentParser()
  75. parser.add_argument('--repo_path', '-p', default='.', help='repo path')
  76. parser.add_argument('--submodule', '-s', default='all',
  77. help='Submodules to update. By default update all submodules. '
  78. 'For multiple submodules, separate them with `;`. '
  79. '`all` and `none` are special values that indicates we fetch all / none submodules')
  80. args = parser.parse_args()
  81. if args.submodule == 'none':
  82. print("don't need to update submodules")
  83. exit(0)
  84. if args.submodule == 'all':
  85. _submodules = []
  86. else:
  87. _submodules = args.submodule.split(';')
  88. update_submodule(os.path.join(args.repo_path, '.gitmodules'), _submodules)
  89. print('total time spent on update submodule: {:.02f}s'.format(time.time() - start_time))