|
1 | 1 | import platform
|
2 | 2 | import sys
|
3 | 3 | import os
|
| 4 | +import json |
4 | 5 |
|
5 |
| -# TODO: In next version, it will be a JSON file listing all the patches, and then it will iterate through to apply them. |
6 | 6 | def patch_android():
|
7 | 7 | print("- Patches List -")
|
8 |
| - print("[1] [deps/v8/src/trap-handler/trap-handler.h] related to https://github.com/nodejs/node/issues/36287") |
9 |
| - if platform.system() == "Linux": |
10 |
| - os.system('patch -f ./deps/v8/src/trap-handler/trap-handler.h < ./android-patches/trap-handler.h.patch') |
| 8 | + |
| 9 | + try: |
| 10 | + with open('android-patches.json', 'r') as f: |
| 11 | + config = json.load(f) |
| 12 | + except FileNotFoundError: |
| 13 | + print("\033[91mError: \033[0m" + "android-patches.json not found. This file is required for Android patch management.") |
| 14 | + return |
| 15 | + except json.JSONDecodeError as e: |
| 16 | + print("\033[91mError: \033[0m" + f"Invalid JSON in android-patches.json: {e}") |
| 17 | + return |
| 18 | + |
| 19 | + patches = config.get('patches', []) |
| 20 | + if not patches: |
| 21 | + print("\033[93mWarning: \033[0m" + "No patches found in configuration.") |
| 22 | + print("\033[92mInfo: \033[0m" + "Tried to patch.") |
| 23 | + return |
| 24 | + |
| 25 | + current_platform = platform.system() |
| 26 | + patch_number = 1 |
| 27 | + patches_applied = 0 |
| 28 | + |
| 29 | + for patch in patches: |
| 30 | + try: |
| 31 | + name = patch.get('name', 'Unknown Patch') |
| 32 | + target_file = patch.get('target_file', '') |
| 33 | + patch_file = patch.get('patch_file', '') |
| 34 | + platforms = patch.get('platforms', []) |
| 35 | + description = patch.get('description', '') |
| 36 | + required = patch.get('required', True) |
| 37 | + |
| 38 | + # Display patch information |
| 39 | + if description: |
| 40 | + print(f"[{patch_number}] [{target_file}] {description}") |
| 41 | + else: |
| 42 | + print(f"[{patch_number}] [{target_file}] {name}") |
| 43 | + |
| 44 | + # Check if patch applies to current platform |
| 45 | + if platforms and current_platform not in platforms: |
| 46 | + print(f" Skipping: Not applicable to {current_platform}") |
| 47 | + patch_number += 1 |
| 48 | + continue |
| 49 | + |
| 50 | + # Check if patch file exists |
| 51 | + if not os.path.exists(patch_file): |
| 52 | + error_msg = f"Patch file not found: {patch_file}" |
| 53 | + if required: |
| 54 | + print(f"\033[91mError: \033[0m{error_msg}") |
| 55 | + return |
| 56 | + else: |
| 57 | + print(f"\033[93mWarning: \033[0m{error_msg}") |
| 58 | + patch_number += 1 |
| 59 | + continue |
| 60 | + |
| 61 | + # Check if target file exists |
| 62 | + if not os.path.exists(target_file): |
| 63 | + error_msg = f"Target file not found: {target_file}" |
| 64 | + if required: |
| 65 | + print(f"\033[91mError: \033[0m{error_msg}") |
| 66 | + return |
| 67 | + else: |
| 68 | + print(f"\033[93mWarning: \033[0m{error_msg}") |
| 69 | + patch_number += 1 |
| 70 | + continue |
| 71 | + |
| 72 | + # Apply the patch |
| 73 | + result = os.system(f'patch -f {target_file} < {patch_file}') |
| 74 | + if result == 0: |
| 75 | + patches_applied += 1 |
| 76 | + elif required: |
| 77 | + print(f"\033[91mError: \033[0mFailed to apply required patch: {name}") |
| 78 | + return |
| 79 | + |
| 80 | + patch_number += 1 |
| 81 | + |
| 82 | + except KeyError as e: |
| 83 | + print(f"\033[91mError: \033[0mMissing required field in patch configuration: {e}") |
| 84 | + if patch.get('required', True): |
| 85 | + return |
| 86 | + patch_number += 1 |
| 87 | + continue |
| 88 | + |
11 | 89 | print("\033[92mInfo: \033[0m" + "Tried to patch.")
|
12 | 90 |
|
13 | 91 | if platform.system() != "Linux" and platform.system() != "Darwin":
|
|
0 commit comments