fetch_releases.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. def run(base_dir, remote_name):
  124. remotes_json_path = os.path.join(base_dir, 'configs', 'remotes.json')
  125. tags = fetch_tags_from_github()
  126. vehicles = []
  127. vehicles.append(construct_vehicle_versions_list(
  128. "Copter",
  129. "ArduCopter",
  130. "Copter",
  131. [
  132. "(ArduCopter-(beta-4.3|beta|stable))",
  133. "(Copter-(\d+\.\d+\.\d+))" # noqa
  134. ],
  135. tags
  136. ))
  137. vehicles.append(construct_vehicle_versions_list(
  138. "Plane",
  139. "ArduPlane",
  140. "Plane",
  141. [
  142. "(ArduPlane-(beta-4.3|beta|stable))",
  143. "(Plane-(\d+\.\d+\.\d+))" # noqa
  144. ],
  145. tags
  146. ))
  147. vehicles.append(construct_vehicle_versions_list(
  148. "Rover",
  149. "Rover",
  150. "Rover",
  151. [
  152. "(APMrover2-(beta-4.3|beta|stable))",
  153. "(Rover-(\d+\.\d+\.\d+))" # noqa
  154. ],
  155. tags
  156. ))
  157. vehicles.append(construct_vehicle_versions_list(
  158. "Sub",
  159. "ArduSub",
  160. "Sub",
  161. [
  162. "(ArduSub-(beta-4.3|beta|stable))",
  163. "(Sub-(\d+\.\d+\.\d+))" # noqa
  164. ],
  165. tags
  166. ))
  167. vehicles.append(construct_vehicle_versions_list(
  168. "Tracker",
  169. "AntennaTracker",
  170. "AntennaTracker",
  171. [
  172. "(AntennaTracker-(beta-4.3|beta|stable))",
  173. "(Tracker-(\d+\.\d+\.\d+))" # noqa
  174. ],
  175. tags
  176. ))
  177. vehicles.append(construct_vehicle_versions_list(
  178. "Blimp",
  179. "Blimp",
  180. "Blimp",
  181. [
  182. "(Blimp-(beta-4.3|beta|stable|\d+\.\d+\.\d+))" # noqa
  183. ],
  184. tags
  185. ))
  186. vehicles.append(construct_vehicle_versions_list(
  187. "Heli",
  188. "ArduCopter",
  189. "Copter",
  190. [
  191. "(ArduCopter-(beta-4.3|beta|stable)-heli)"
  192. ],
  193. tags
  194. ))
  195. remotes_json = {
  196. "name": remote_name,
  197. "url": "https://github.com/ardupilot/ardupilot.git",
  198. "vehicles": vehicles
  199. }
  200. try:
  201. with open(remotes_json_path, 'r') as f:
  202. remotes = json.loads(f.read())
  203. # remove existing remote entry from the list
  204. temp = []
  205. for remote in remotes:
  206. if remote['name'] != remote_name:
  207. temp.append(remote)
  208. remotes = temp
  209. except Exception as e:
  210. print(e)
  211. print("Writing to empty file")
  212. remotes = []
  213. with open(remotes_json_path, 'w') as f:
  214. remotes.append(remotes_json)
  215. f.write(json.dumps(remotes, indent=2))
  216. print(f"Wrote {remotes_json_path}")
  217. if __name__ == "__main__":
  218. parser = optparse.OptionParser("fetch_releases.py")
  219. parser.add_option(
  220. "", "--basedir", type="string",
  221. default=os.path.abspath(
  222. os.path.join(os.path.dirname(__file__), "..", "..", "base")
  223. ),
  224. help="base directory"
  225. )
  226. parser.add_option(
  227. "", "--remotename", type="string",
  228. default="ardupilot",
  229. help="Remote name to write in json file"
  230. )
  231. cmd_opts, cmd_args = parser.parse_args()
  232. basedir = os.path.abspath(cmd_opts.basedir)
  233. remotename = cmd_opts.remotename
  234. run(base_dir=basedir, remote_name=remotename)