fetch_releases.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import json
  2. import optparse
  3. import os
  4. import re
  5. import requests
  6. from packaging.version import Version
  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. ignore_versions_before=''):
  63. ret = []
  64. for tag_info in tags:
  65. tag = tag_info['ref'].replace('refs/tags/', '')
  66. matches = []
  67. for exp in tag_filter_exps:
  68. # the regexes capture two groups
  69. # first group is the matched substring itself
  70. # second group is the tag body (e.g., beta, stable, 4.5.1 etc)
  71. matches.extend(re.findall(re.compile(exp), tag))
  72. if matches:
  73. matched_string, tag_body = matches[0]
  74. if len(matched_string) < len(tag):
  75. print(
  76. f"Partial match. Ignoring. Matched '{matched_string}' "
  77. f"in '{tag}'."
  78. )
  79. continue
  80. try:
  81. v_num, v_type = version_number_and_type(
  82. tag_info['object']['sha'],
  83. ap_source_subdir
  84. )
  85. except Exception as e:
  86. print(f'Cannot determine version number for tag {tag}')
  87. print(e)
  88. continue
  89. if Version(v_num) < Version(ignore_versions_before):
  90. print(f"{v_num} Version too old. Ignoring.")
  91. continue
  92. if re.search(r'\d+\.\d+\.\d+', tag_body):
  93. # we do stable version tags in this format
  94. # e.g. Rover-4.5.1, Copter-4.5.1, where Rover and Copter
  95. # are prefixes and 4.5.1 is the tag body
  96. # artifacts for these versions are stored in firmware
  97. # server at stable-x.y.z subdirs
  98. afcts_url = (
  99. f'https://firmware.ardupilot.org/{fw_server_vehicle_sdir}'
  100. f'/stable-{tag_body}'
  101. )
  102. else:
  103. afcts_url = (
  104. f'https://firmware.ardupilot.org/{fw_server_vehicle_sdir}'
  105. f'/{tag_body}'
  106. )
  107. ret.append({
  108. 'release_type': v_type,
  109. 'version_number': v_num,
  110. 'ap_build_artifacts_url': afcts_url,
  111. 'commit_reference': tag_info['object']['sha']
  112. })
  113. ret = remove_duplicate_entries(ret)
  114. # entry for master
  115. ret.append({
  116. 'release_type': 'latest',
  117. 'version_number': 'NA',
  118. 'ap_build_artifacts_url': (
  119. f'https://firmware.ardupilot.org/{fw_server_vehicle_sdir}/latest'
  120. ),
  121. 'commit_reference': 'refs/heads/master'
  122. })
  123. return {
  124. 'name': vehicle,
  125. 'releases': ret
  126. }
  127. def run(base_dir, remote_name):
  128. remotes_json_path = os.path.join(base_dir, 'configs', 'remotes.json')
  129. tags = fetch_tags_from_github()
  130. vehicles = []
  131. vehicles.append(construct_vehicle_versions_list(
  132. "Copter",
  133. "ArduCopter",
  134. "Copter",
  135. [
  136. "(ArduCopter-(beta-4.3|beta|stable))",
  137. "(Copter-(\d+\.\d+\.\d+))" # noqa
  138. ],
  139. tags,
  140. '4.3',
  141. ))
  142. vehicles.append(construct_vehicle_versions_list(
  143. "Plane",
  144. "ArduPlane",
  145. "Plane",
  146. [
  147. "(ArduPlane-(beta-4.3|beta|stable))",
  148. "(Plane-(\d+\.\d+\.\d+))" # noqa
  149. ],
  150. tags,
  151. '4.3',
  152. ))
  153. vehicles.append(construct_vehicle_versions_list(
  154. "Rover",
  155. "Rover",
  156. "Rover",
  157. [
  158. "(APMrover2-(beta-4.3|beta|stable))",
  159. "(Rover-(\d+\.\d+\.\d+))" # noqa
  160. ],
  161. tags,
  162. '4.3',
  163. ))
  164. vehicles.append(construct_vehicle_versions_list(
  165. "Sub",
  166. "ArduSub",
  167. "Sub",
  168. [
  169. "(ArduSub-(beta-4.3|beta|stable))",
  170. "(Sub-(\d+\.\d+\.\d+))" # noqa
  171. ],
  172. tags,
  173. '4.3',
  174. ))
  175. vehicles.append(construct_vehicle_versions_list(
  176. "Tracker",
  177. "AntennaTracker",
  178. "AntennaTracker",
  179. [
  180. "(AntennaTracker-(beta-4.3|beta|stable))",
  181. "(Tracker-(\d+\.\d+\.\d+))" # noqa
  182. ],
  183. tags,
  184. '4.3',
  185. ))
  186. vehicles.append(construct_vehicle_versions_list(
  187. "Blimp",
  188. "Blimp",
  189. "Blimp",
  190. [
  191. "(Blimp-(beta-4.3|beta|stable|\d+\.\d+\.\d+))" # noqa
  192. ],
  193. tags,
  194. '4.3',
  195. ))
  196. vehicles.append(construct_vehicle_versions_list(
  197. "Heli",
  198. "ArduCopter",
  199. "Copter",
  200. [
  201. "(ArduCopter-(beta-4.3|beta|stable)-heli)"
  202. ],
  203. tags,
  204. '4.3',
  205. ))
  206. vehicles.append(construct_vehicle_versions_list(
  207. "AP_Periph",
  208. "Tools/AP_Periph",
  209. "AP_Periph",
  210. [
  211. "(AP_Periph-(beta|stable))"
  212. ],
  213. tags,
  214. '1.8.1',
  215. ))
  216. remotes_json = {
  217. "name": remote_name,
  218. "url": "https://github.com/ardupilot/ardupilot.git",
  219. "vehicles": vehicles
  220. }
  221. try:
  222. with open(remotes_json_path, 'r') as f:
  223. remotes = json.loads(f.read())
  224. # remove existing remote entry from the list
  225. temp = []
  226. for remote in remotes:
  227. if remote['name'] != remote_name:
  228. temp.append(remote)
  229. remotes = temp
  230. except Exception as e:
  231. print(e)
  232. print("Writing to empty file")
  233. remotes = []
  234. with open(remotes_json_path, 'w') as f:
  235. remotes.append(remotes_json)
  236. f.write(json.dumps(remotes, indent=2))
  237. print(f"Wrote {remotes_json_path}")
  238. if __name__ == "__main__":
  239. parser = optparse.OptionParser("fetch_releases.py")
  240. parser.add_option(
  241. "", "--basedir", type="string",
  242. default=os.path.abspath(
  243. os.path.join(os.path.dirname(__file__), "..", "base")
  244. ),
  245. help="base directory"
  246. )
  247. parser.add_option(
  248. "", "--remotename", type="string",
  249. default="ardupilot",
  250. help="Remote name to write in json file"
  251. )
  252. cmd_opts, cmd_args = parser.parse_args()
  253. basedir = os.path.abspath(cmd_opts.basedir)
  254. remotename = cmd_opts.remotename
  255. run(base_dir=basedir, remote_name=remotename)