fetch_releases.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import json
  2. import optparse
  3. import os
  4. import re
  5. import requests
  6. IGNORE_VERSIONS_BEFORE = '4.3'
  7. def version_number_and_type(git_hash, ap_source_subdir):
  8. url = (
  9. "https://raw.githubusercontent.com/ArduPilot/ardupilot/"
  10. f"{git_hash}/{ap_source_subdir}/version.h"
  11. )
  12. response = requests.get(url=url)
  13. if response.status_code != 200:
  14. print(response.text)
  15. print(url)
  16. raise Exception(
  17. "Couldn't fetch version.h from github server. "
  18. f"Got status code {response.status_code}"
  19. )
  20. exp = re.compile(
  21. r'#define FIRMWARE_VERSION (\d+),(\d+),(\d+),FIRMWARE_VERSION_TYPE_(\w+)' # noqa
  22. )
  23. matches = re.findall(exp, response.text)
  24. major, minor, patch, fw_type = matches[0]
  25. fw_type = fw_type.lower()
  26. if fw_type == 'official':
  27. # to avoid any confusion, beta is also 'official' ;-)
  28. fw_type = 'stable'
  29. return f'{major}.{minor}.{patch}', fw_type
  30. def fetch_tags_from_github():
  31. url = 'https://api.github.com/repos/ardupilot/ardupilot/git/refs/tags'
  32. headers = {
  33. 'X-GitHub-Api-Version': '2022-11-28',
  34. 'Accept': 'application/vnd.github+json'
  35. }
  36. token = os.getenv("CBS_GITHUB_ACCESS_TOKEN")
  37. if token:
  38. headers['Authorization'] = f"Bearer {token}"
  39. response = requests.get(url=url, headers=headers)
  40. if response.status_code != 200:
  41. print(response.text)
  42. print(url)
  43. raise Exception(
  44. "Couldn't fetch tags from github server. "
  45. f"Got status code {response.status_code}"
  46. )
  47. tags_objs = response.json()
  48. return tags_objs
  49. def remove_duplicate_entries(releases):
  50. temp = {}
  51. for release in releases:
  52. # if we have already seen a version with similar hash
  53. # and we now see a beta release with same hash, we skip it
  54. if temp.get(release['commit_reference']) and \
  55. release['release_type'] == 'beta':
  56. continue
  57. temp[release['commit_reference']] = release
  58. return list(temp.values())
  59. def construct_vehicle_versions_list(vehicle, ap_source_subdir,
  60. fw_server_vehicle_sdir,
  61. tag_filter_exps, tags):
  62. ret = []
  63. for tag_info in tags:
  64. tag = tag_info['ref'].replace('refs/tags/', '')
  65. matches = []
  66. for exp in tag_filter_exps:
  67. # the regexes capture two groups
  68. # first group is the matched substring itself
  69. # second group is the tag body (e.g., beta, stable, 4.5.1 etc)
  70. matches.extend(re.findall(re.compile(exp), tag))
  71. if matches:
  72. matched_string, tag_body = matches[0]
  73. if len(matched_string) < len(tag):
  74. print(
  75. f"Partial match. Ignoring. Matched '{matched_string}' "
  76. f"in '{tag}'."
  77. )
  78. continue
  79. try:
  80. v_num, v_type = version_number_and_type(
  81. tag_info['object']['sha'],
  82. ap_source_subdir
  83. )
  84. except Exception as e:
  85. print(f'Cannot determine version number for tag {tag}')
  86. print(e)
  87. continue
  88. if v_num < IGNORE_VERSIONS_BEFORE:
  89. print(f"{v_num} Version too old. Ignoring.")
  90. continue
  91. if re.search(r'\d+\.\d+\.\d+', tag_body):
  92. # we do stable version tags in this format
  93. # e.g. Rover-4.5.1, Copter-4.5.1, where Rover and Copter
  94. # are prefixes and 4.5.1 is the tag body
  95. # artifacts for these versions are stored in firmware
  96. # server at stable-x.y.z subdirs
  97. afcts_url = (
  98. f'https://firmware.ardupilot.org/{fw_server_vehicle_sdir}'
  99. f'/stable-{tag_body}'
  100. )
  101. else:
  102. afcts_url = (
  103. f'https://firmware.ardupilot.org/{fw_server_vehicle_sdir}'
  104. f'/{tag_body}'
  105. )
  106. ret.append({
  107. 'release_type': v_type,
  108. 'version_number': v_num,
  109. 'ap_build_artifacts_url': afcts_url,
  110. 'commit_reference': tag_info['object']['sha']
  111. })
  112. ret = remove_duplicate_entries(ret)
  113. # entry for master
  114. ret.append({
  115. 'release_type': 'latest',
  116. 'version_number': 'NA',
  117. 'ap_build_artifacts_url': (
  118. f'https://firmware.ardupilot.org/{fw_server_vehicle_sdir}/latest'
  119. ),
  120. 'commit_reference': 'refs/heads/master'
  121. })
  122. return {
  123. 'name': vehicle,
  124. 'releases': ret
  125. }
  126. def run(base_dir, remote_name):
  127. remotes_json_path = os.path.join(base_dir, 'configs', 'remotes.json')
  128. tags = fetch_tags_from_github()
  129. vehicles = []
  130. vehicles.append(construct_vehicle_versions_list(
  131. "Copter",
  132. "ArduCopter",
  133. "Copter",
  134. [
  135. "(ArduCopter-(beta-4.3|beta|stable))",
  136. "(Copter-(\d+\.\d+\.\d+))" # noqa
  137. ],
  138. tags
  139. ))
  140. vehicles.append(construct_vehicle_versions_list(
  141. "Plane",
  142. "ArduPlane",
  143. "Plane",
  144. [
  145. "(ArduPlane-(beta-4.3|beta|stable))",
  146. "(Plane-(\d+\.\d+\.\d+))" # noqa
  147. ],
  148. tags
  149. ))
  150. vehicles.append(construct_vehicle_versions_list(
  151. "Rover",
  152. "Rover",
  153. "Rover",
  154. [
  155. "(APMrover2-(beta-4.3|beta|stable))",
  156. "(Rover-(\d+\.\d+\.\d+))" # noqa
  157. ],
  158. tags
  159. ))
  160. vehicles.append(construct_vehicle_versions_list(
  161. "Sub",
  162. "ArduSub",
  163. "Sub",
  164. [
  165. "(ArduSub-(beta-4.3|beta|stable))",
  166. "(Sub-(\d+\.\d+\.\d+))" # noqa
  167. ],
  168. tags
  169. ))
  170. vehicles.append(construct_vehicle_versions_list(
  171. "Tracker",
  172. "AntennaTracker",
  173. "AntennaTracker",
  174. [
  175. "(AntennaTracker-(beta-4.3|beta|stable))",
  176. "(Tracker-(\d+\.\d+\.\d+))" # noqa
  177. ],
  178. tags
  179. ))
  180. vehicles.append(construct_vehicle_versions_list(
  181. "Blimp",
  182. "Blimp",
  183. "Blimp",
  184. [
  185. "(Blimp-(beta-4.3|beta|stable|\d+\.\d+\.\d+))" # noqa
  186. ],
  187. tags
  188. ))
  189. vehicles.append(construct_vehicle_versions_list(
  190. "Heli",
  191. "ArduCopter",
  192. "Copter",
  193. [
  194. "(ArduCopter-(beta-4.3|beta|stable)-heli)"
  195. ],
  196. tags
  197. ))
  198. remotes_json = {
  199. "name": remote_name,
  200. "url": "https://github.com/ardupilot/ardupilot.git",
  201. "vehicles": vehicles
  202. }
  203. try:
  204. with open(remotes_json_path, 'r') as f:
  205. remotes = json.loads(f.read())
  206. # remove existing remote entry from the list
  207. temp = []
  208. for remote in remotes:
  209. if remote['name'] != remote_name:
  210. temp.append(remote)
  211. remotes = temp
  212. except Exception as e:
  213. print(e)
  214. print("Writing to empty file")
  215. remotes = []
  216. with open(remotes_json_path, 'w') as f:
  217. remotes.append(remotes_json)
  218. f.write(json.dumps(remotes, indent=2))
  219. print(f"Wrote {remotes_json_path}")
  220. if __name__ == "__main__":
  221. parser = optparse.OptionParser("fetch_releases.py")
  222. parser.add_option(
  223. "", "--basedir", type="string",
  224. default=os.path.abspath(
  225. os.path.join(os.path.dirname(__file__), "..", "base")
  226. ),
  227. help="base directory"
  228. )
  229. parser.add_option(
  230. "", "--remotename", type="string",
  231. default="ardupilot",
  232. help="Remote name to write in json file"
  233. )
  234. cmd_opts, cmd_args = parser.parse_args()
  235. basedir = os.path.abspath(cmd_opts.basedir)
  236. remotename = cmd_opts.remotename
  237. run(base_dir=basedir, remote_name=remotename)