checkout_project_ref.py 4.2 KB

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