|
| 1 | + |
| 2 | +#!/usr/bin/env python3 |
| 3 | + |
| 4 | +# Arduino IDE provides by default a package file for the ESP32. This causes version conflicts |
| 5 | +# when the user tries to use the JSON file with the Chinese mirrors. |
| 6 | +# |
| 7 | +# The downside is that the Arduino IDE will always warn the user that updates are available as it |
| 8 | +# will consider the version from the Chinese mirrors as a pre-release version. |
| 9 | +# |
| 10 | +# This script is used to append "-cn" to all versions in the package_esp32_index_cn.json file so that |
| 11 | +# the user can select the Chinese mirrors without conflicts. |
| 12 | +# |
| 13 | +# If Arduino ever stops providing the package_esp32_index.json file by default, |
| 14 | +# this script can be removed and the tags reverted. |
| 15 | + |
| 16 | +import json |
| 17 | + |
| 18 | +def append_cn_to_versions(obj): |
| 19 | + if isinstance(obj, dict): |
| 20 | + # dfu-util comes from arduino.cc and not from the Chinese mirrors, so we skip it |
| 21 | + if obj.get("name") == "dfu-util": |
| 22 | + return |
| 23 | + |
| 24 | + for key, value in obj.items(): |
| 25 | + if key == "version" and isinstance(value, str): |
| 26 | + if not value.endswith("-cn"): |
| 27 | + obj[key] = value + "-cn" |
| 28 | + else: |
| 29 | + append_cn_to_versions(value) |
| 30 | + |
| 31 | + elif isinstance(obj, list): |
| 32 | + for item in obj: |
| 33 | + append_cn_to_versions(item) |
| 34 | + |
| 35 | +def process_json_file(input_path, output_path=None): |
| 36 | + with open(input_path, "r", encoding="utf-8") as f: |
| 37 | + data = json.load(f) |
| 38 | + |
| 39 | + append_cn_to_versions(data) |
| 40 | + |
| 41 | + if output_path is None: |
| 42 | + output_path = input_path |
| 43 | + |
| 44 | + with open(output_path, "w", encoding="utf-8") as f: |
| 45 | + json.dump(data, f, indent=2) |
| 46 | + |
| 47 | + print(f"Updated JSON written to {output_path}") |
| 48 | + |
| 49 | +if __name__ == "__main__": |
| 50 | + import sys |
| 51 | + if len(sys.argv) < 2: |
| 52 | + print("Usage: python release_append_cn.py input.json [output.json]") |
| 53 | + else: |
| 54 | + input_file = sys.argv[1] |
| 55 | + output_file = sys.argv[2] if len(sys.argv) > 2 else None |
| 56 | + process_json_file(input_file, output_file) |
0 commit comments