fetch_releases.py 7.1 KB

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