|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import hashlib |
| 4 | +import json |
| 5 | +import subprocess |
| 6 | + |
| 7 | +def main(): |
| 8 | + codename = input("Enter device codename (e.g. PL2, miatoll): ").strip() |
| 9 | + ota_package_dir = f"out/target/product/{codename}" |
| 10 | + |
| 11 | + # Try to find the OTA package matching pattern AndroidOne-*{codename}-*.zip |
| 12 | + try: |
| 13 | + files = [f for f in os.listdir(ota_package_dir) if f.startswith("AndroidOne-") and codename in f and f.endswith(".zip")] |
| 14 | + except FileNotFoundError: |
| 15 | + print(f"OTA package directory not found: {ota_package_dir}") |
| 16 | + sys.exit(1) |
| 17 | + |
| 18 | + if not files: |
| 19 | + print(f"OTA package file not found in {ota_package_dir}.") |
| 20 | + print(f"Ensure the file follows the pattern: AndroidOne-*{codename}-*.zip") |
| 21 | + sys.exit(1) |
| 22 | + |
| 23 | + # If multiple files matched, take the first one |
| 24 | + filename = files[0] |
| 25 | + file_path = os.path.join(ota_package_dir, filename) |
| 26 | + print(f"Found OTA package: {file_path}") |
| 27 | + |
| 28 | + # Extract datetime from build.prop |
| 29 | + build_prop_path = os.path.join(ota_package_dir, "system", "build.prop") |
| 30 | + datetime = "UNKNOWN" |
| 31 | + if os.path.exists(build_prop_path): |
| 32 | + try: |
| 33 | + with open(build_prop_path, "r") as f: |
| 34 | + for line in f: |
| 35 | + if line.startswith("ro.build.date.utc="): |
| 36 | + datetime = line.strip().split("=", 1)[1] |
| 37 | + break |
| 38 | + except Exception: |
| 39 | + pass |
| 40 | + |
| 41 | + # Calculate sha256 checksum |
| 42 | + def sha256sum(filename): |
| 43 | + h = hashlib.sha256() |
| 44 | + with open(filename, "rb") as f: |
| 45 | + for chunk in iter(lambda: f.read(8192), b""): |
| 46 | + h.update(chunk) |
| 47 | + return h.hexdigest() |
| 48 | + id_hash = sha256sum(file_path) |
| 49 | + |
| 50 | + # Get size in bytes |
| 51 | + size = os.path.getsize(file_path) |
| 52 | + |
| 53 | + version = "15" # change if needed |
| 54 | + |
| 55 | + base_url = "https://storage.googleapis.com/rom" |
| 56 | + url = f"{base_url}/{filename}" |
| 57 | + |
| 58 | + output_dir = "./OTA/devices" |
| 59 | + os.makedirs(output_dir, exist_ok=True) |
| 60 | + output_file = os.path.join(output_dir, f"{codename}.json") |
| 61 | + |
| 62 | + # Create JSON structure |
| 63 | + data = { |
| 64 | + "response": [ |
| 65 | + { |
| 66 | + "datetime": datetime, |
| 67 | + "filename": filename, |
| 68 | + "id": id_hash, |
| 69 | + "size": size, |
| 70 | + "url": url, |
| 71 | + "version": version, |
| 72 | + } |
| 73 | + ] |
| 74 | + } |
| 75 | + |
| 76 | + # Write JSON, prettified if possible |
| 77 | + try: |
| 78 | + import json |
| 79 | + with open(output_file, "w") as f: |
| 80 | + json.dump(data, f, indent=2) |
| 81 | + print(f"Minimal OTA JSON saved to {output_file}") |
| 82 | + except Exception as e: |
| 83 | + print(f"Failed to write JSON: {e}") |
| 84 | + sys.exit(1) |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + main() |
0 commit comments