versions.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #! /usr/bin/env python3
  2. from dotenv import load_dotenv
  3. load_dotenv()
  4. import os
  5. import struct
  6. import zipfile
  7. import oss2
  8. import json
  9. import requests
  10. from requests.exceptions import RequestException
  11. # 切换到项目根目录
  12. os.chdir(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  13. def get_chip_id_string(chip_id):
  14. return {
  15. 0x0000: "esp32",
  16. 0x0002: "esp32s2",
  17. 0x0005: "esp32c3",
  18. 0x0009: "esp32s3",
  19. 0x000C: "esp32c2",
  20. 0x000D: "esp32c6",
  21. 0x0010: "esp32h2",
  22. 0x0011: "esp32c5",
  23. 0x0012: "esp32p4",
  24. 0x0017: "esp32c5",
  25. }[chip_id]
  26. def get_flash_size(flash_size):
  27. MB = 1024 * 1024
  28. return {
  29. 0x00: 1 * MB,
  30. 0x01: 2 * MB,
  31. 0x02: 4 * MB,
  32. 0x03: 8 * MB,
  33. 0x04: 16 * MB,
  34. 0x05: 32 * MB,
  35. 0x06: 64 * MB,
  36. 0x07: 128 * MB,
  37. }[flash_size]
  38. def get_app_desc(data):
  39. magic = struct.unpack("<I", data[0x00:0x04])[0]
  40. if magic != 0xabcd5432:
  41. raise Exception("Invalid app desc magic")
  42. version = data[0x10:0x30].decode("utf-8").strip('\0')
  43. project_name = data[0x30:0x50].decode("utf-8").strip('\0')
  44. time = data[0x50:0x60].decode("utf-8").strip('\0')
  45. date = data[0x60:0x70].decode("utf-8").strip('\0')
  46. idf_ver = data[0x70:0x90].decode("utf-8").strip('\0')
  47. elf_sha256 = data[0x90:0xb0].hex()
  48. return {
  49. "name": project_name,
  50. "version": version,
  51. "compile_time": date + "T" + time,
  52. "idf_version": idf_ver,
  53. "elf_sha256": elf_sha256,
  54. }
  55. def get_board_name(folder):
  56. basename = os.path.basename(folder)
  57. if basename.startswith("v0.2"):
  58. return "bread-simple"
  59. if basename.startswith("v0.3") or basename.startswith("v0.4") or basename.startswith("v0.5") or basename.startswith("v0.6"):
  60. if "ML307" in basename:
  61. return "bread-compact-ml307"
  62. elif "WiFi" in basename:
  63. return "bread-compact-wifi"
  64. elif "KevinBox1" in basename:
  65. return "kevin-box-1"
  66. if basename.startswith("v0.7") or basename.startswith("v0.8") or basename.startswith("v0.9") or basename.startswith("v1."):
  67. return basename.split("_")[1]
  68. raise Exception(f"Unknown board name: {basename}")
  69. def read_binary(dir_path):
  70. merged_bin_path = os.path.join(dir_path, "merged-binary.bin")
  71. merged_bin_data = open(merged_bin_path, "rb").read()
  72. # find app partition
  73. if merged_bin_data[0x100000] == 0xE9:
  74. data = merged_bin_data[0x100000:]
  75. elif merged_bin_data[0x200000] == 0xE9:
  76. data = merged_bin_data[0x200000:]
  77. else:
  78. print(dir_path, "is not a valid image")
  79. return
  80. # get flash size
  81. flash_size = get_flash_size(data[0x3] >> 4)
  82. chip_id = get_chip_id_string(data[0xC])
  83. # get segments
  84. segment_count = data[0x1]
  85. segments = []
  86. offset = 0x18
  87. for i in range(segment_count):
  88. segment_size = struct.unpack("<I", data[offset + 4:offset + 8])[0]
  89. offset += 8
  90. segment_data = data[offset:offset + segment_size]
  91. offset += segment_size
  92. segments.append(segment_data)
  93. assert offset < len(data), "offset is out of bounds"
  94. # extract bin file
  95. bin_path = os.path.join(dir_path, "xiaozhi.bin")
  96. if not os.path.exists(bin_path):
  97. print("extract bin file to", bin_path)
  98. open(bin_path, "wb").write(data)
  99. # The app desc is in the first segment
  100. desc = get_app_desc(segments[0])
  101. return {
  102. "chip_id": chip_id,
  103. "flash_size": flash_size,
  104. "board": get_board_name(dir_path),
  105. "application": desc,
  106. "firmware_size": len(data),
  107. }
  108. def extract_zip(zip_path, extract_path):
  109. if not os.path.exists(extract_path):
  110. os.makedirs(extract_path)
  111. print(f"Extracting {zip_path} to {extract_path}")
  112. with zipfile.ZipFile(zip_path, 'r') as zip_ref:
  113. zip_ref.extractall(extract_path)
  114. def upload_dir_to_oss(source_dir, target_dir):
  115. auth = oss2.Auth(os.environ['OSS_ACCESS_KEY_ID'], os.environ['OSS_ACCESS_KEY_SECRET'])
  116. bucket = oss2.Bucket(auth, os.environ['OSS_ENDPOINT'], os.environ['OSS_BUCKET_NAME'])
  117. for filename in os.listdir(source_dir):
  118. oss_key = os.path.join(target_dir, filename)
  119. print('uploading', oss_key)
  120. bucket.put_object(oss_key, open(os.path.join(source_dir, filename), 'rb'))
  121. def post_info_to_server(info):
  122. """
  123. 将固件信息发送到服务器
  124. Args:
  125. info: 包含固件信息的字典
  126. """
  127. try:
  128. # 从环境变量获取服务器URL和token
  129. server_url = os.environ.get('VERSIONS_SERVER_URL')
  130. server_token = os.environ.get('VERSIONS_TOKEN')
  131. if not server_url or not server_token:
  132. raise Exception("Missing SERVER_URL or TOKEN in environment variables")
  133. # 准备请求头和数据
  134. headers = {
  135. 'Authorization': f'Bearer {server_token}',
  136. 'Content-Type': 'application/json'
  137. }
  138. # 发送POST请求
  139. response = requests.post(
  140. server_url,
  141. headers=headers,
  142. json={'jsonData': json.dumps(info)}
  143. )
  144. # 检查响应状态
  145. response.raise_for_status()
  146. print(f"Successfully uploaded version info for tag: {info['tag']}")
  147. except RequestException as e:
  148. if hasattr(e.response, 'json'):
  149. error_msg = e.response.json().get('error', str(e))
  150. else:
  151. error_msg = str(e)
  152. print(f"Failed to upload version info: {error_msg}")
  153. raise
  154. except Exception as e:
  155. print(f"Error uploading version info: {str(e)}")
  156. raise
  157. def main():
  158. release_dir = "releases"
  159. # look for zip files startswith "v"
  160. for name in os.listdir(release_dir):
  161. if name.startswith("v") and name.endswith(".zip"):
  162. tag = name[:-4]
  163. folder = os.path.join(release_dir, tag)
  164. info_path = os.path.join(folder, "info.json")
  165. if not os.path.exists(info_path):
  166. if not os.path.exists(folder):
  167. os.makedirs(folder)
  168. extract_zip(os.path.join(release_dir, name), folder)
  169. info = read_binary(folder)
  170. target_dir = os.path.join("firmwares", tag)
  171. info["tag"] = tag
  172. info["url"] = os.path.join(os.environ['OSS_BUCKET_URL'], target_dir, "xiaozhi.bin")
  173. open(info_path, "w").write(json.dumps(info, indent=4))
  174. # upload all file to oss
  175. upload_dir_to_oss(folder, target_dir)
  176. # read info.json
  177. info = json.load(open(info_path))
  178. # post info.json to server
  179. post_info_to_server(info)
  180. if __name__ == "__main__":
  181. main()