checkout_project_ref.py 4.2 KB

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