checkout_project_ref.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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
  5. # called by CI jobs when it uses a project related to IDF
  6. import argparse
  7. import json
  8. import os
  9. import re
  10. import subprocess
  11. from typing import List
  12. IDF_GIT_DESCRIBE_PATTERN = re.compile(r'^v(\d)\.(\d)')
  13. RETRY_COUNT = 3
  14. def get_customized_project_revision(proj_name: str) -> str:
  15. """
  16. get customized project revision defined in bot message
  17. """
  18. revision = ''
  19. customized_project_revisions_file = os.getenv('BOT_CUSTOMIZED_REVISION')
  20. if customized_project_revisions_file:
  21. customized_project_revisions = json.loads(customized_project_revisions_file)
  22. try:
  23. revision = customized_project_revisions[proj_name.lower()]
  24. except (KeyError, TypeError):
  25. pass
  26. return revision
  27. def target_branch_candidates(proj_name: str) -> List:
  28. """
  29. :return: a list of target branch candidates, from highest priority to lowest priority.
  30. """
  31. candidates = [
  32. # branch name (or tag name) of current IDF
  33. os.getenv('CI_COMMIT_REF_NAME'),
  34. # CI_MERGE_REQUEST_TARGET_BRANCH_NAME
  35. os.getenv('CI_MERGE_REQUEST_TARGET_BRANCH_NAME'),
  36. ]
  37. customized_candidate = get_customized_project_revision(proj_name)
  38. if customized_candidate:
  39. # highest priority, insert to head of list
  40. candidates.insert(0, customized_candidate)
  41. # branch name read from IDF
  42. try:
  43. git_describe = subprocess.check_output(['git', 'describe', 'HEAD'])
  44. match = IDF_GIT_DESCRIBE_PATTERN.search(git_describe.decode())
  45. if match:
  46. major_revision = match.group(1)
  47. minor_revision = match.group(2)
  48. # release branch
  49. candidates.append('release/v{}.{}'.format(major_revision, minor_revision))
  50. # branch to match all major branches, like v3.x or v3
  51. candidates.append('release/v{}.x'.format(major_revision))
  52. candidates.append('release/v{}'.format(major_revision))
  53. except subprocess.CalledProcessError:
  54. # this should not happen as IDF should have describe message
  55. pass
  56. return [c for c in candidates if c] # filter out null value
  57. if __name__ == '__main__':
  58. parser = argparse.ArgumentParser()
  59. parser.add_argument('project',
  60. help='the name of project')
  61. parser.add_argument('project_relative_path',
  62. help='relative path of project to IDF repository directory')
  63. parser.add_argument('--customized_only', action='store_true',
  64. help='Only to find customized revision')
  65. args = parser.parse_args()
  66. if args.customized_only:
  67. customized_revision = get_customized_project_revision(args.project)
  68. candidate_branches = [customized_revision] if customized_revision else []
  69. else:
  70. candidate_branches = target_branch_candidates(args.project)
  71. # change to project dir for checkout
  72. os.chdir(args.project_relative_path)
  73. ref_to_use = ''
  74. for candidate in candidate_branches:
  75. # check if the branch, tag or commit exists
  76. try:
  77. subprocess.check_call(['git', 'cat-file', '-t', 'origin/{}'.format(candidate)], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  78. ref_to_use = candidate
  79. break
  80. except subprocess.CalledProcessError:
  81. try:
  82. # For customized commits
  83. subprocess.check_call(['git', 'cat-file', '-t', candidate], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  84. ref_to_use = candidate
  85. break
  86. except subprocess.CalledProcessError:
  87. pass
  88. continue
  89. if ref_to_use:
  90. for _ in range(RETRY_COUNT):
  91. # Add retry for projects with git-lfs
  92. try:
  93. subprocess.check_call(['git', 'checkout', '-f', ref_to_use], stdout=subprocess.PIPE) # not print the stdout
  94. print('CI using ref {} for project {}'.format(ref_to_use, args.project))
  95. break
  96. except subprocess.CalledProcessError:
  97. pass
  98. else:
  99. print('Failed to use ref {} for project {}'.format(ref_to_use, args.project))
  100. exit(1)
  101. else:
  102. print('using default branch')