Alastor-Ripley

android flasher

Sep 6th, 2023
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | Source Code | 0 0
  1. """
  2. This script facilitates flashing a custom ROM onto an Android phone.
  3.  
  4. """
  5.  
  6. import os
  7. import subprocess
  8. import sys
  9. import logging
  10.  
  11. # Constants
  12. ANDROID_DIR = os.path.join(os.path.expanduser("~"), "Android")
  13. FIRMWARE_DIR = os.path.join(ANDROID_DIR, "Firmware")
  14. ROMS_DIR = os.path.join(ANDROID_DIR, "ROMs")
  15. GAPPS_DIR = os.path.join(ANDROID_DIR, "GApps")
  16. ADDON_GAPPS_DIR = os.path.join(ANDROID_DIR, "Addon-GApps")
  17. RECOVERY_DIR = os.path.join(ANDROID_DIR, "Recovery")
  18. ROOTING_DIR = os.path.join(ANDROID_DIR, "Rooting")
  19.  
  20. ADB_TOOL = "adb"
  21. FASTBOOT_TOOL = "fastboot"
  22.  
  23. # Configure logging
  24. logging.basicConfig(filename="flash.log", level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
  25.  
  26. # Custom exceptions
  27. class FlashingError(Exception):
  28. pass
  29.  
  30. class InvalidSelectionError(Exception):
  31. pass
  32.  
  33. # Function to check if a file exists
  34. def file_exists(file_path):
  35. return os.path.isfile(file_path)
  36.  
  37. # Function to check if a directory exists
  38. def dir_exists(dir_path):
  39. return os.path.isdir(dir_path)
  40.  
  41. # Function to check if a tool is available in PATH
  42. def tool_exists(tool_name):
  43. for path in os.environ["PATH"].split(os.pathsep):
  44. if tool_name.lower() in os.listdir(path):
  45. return True
  46. return False
  47.  
  48. # Class to handle flashing process
  49. class FlashHandler:
  50. def __init__(self):
  51. self.roms = self.list_available_roms()
  52. self.gapps = self.list_available_gapps()
  53. self.addon_gapps = self.list_available_addon_gapps()
  54. self.recovery = self.list_available_recovery()
  55. self.rooting = self.list_available_rooting()
  56.  
  57. # Function to list available choices for ROMs
  58. def list_available_roms(self):
  59. roms = os.listdir(ROMS_DIR)
  60. return roms if roms else None
  61.  
  62. # Function to list available choices for GApps
  63. def list_available_gapps(self):
  64. gapps = os.listdir(GAPPS_DIR)
  65. return gapps if gapps else None
  66.  
  67. # Function to list available choices for Addon-GApps
  68. def list_available_addon_gapps(self):
  69. addon_gapps = os.listdir(ADDON_GAPPS_DIR)
  70. return addon_gapps if addon_gapps else None
  71.  
  72. # Function to list available choices for Recovery
  73. def list_available_recovery(self):
  74. recovery = os.listdir(RECOVERY_DIR)
  75. return recovery if recovery else None
  76.  
  77. # Function to list available choices for Rooting
  78. def list_available_rooting(self):
  79. rooting = os.listdir(ROOTING_DIR)
  80. return rooting if rooting else None
  81.  
  82. # Function to display available choices
  83. def display_choices(self, choices):
  84. for i, choice in enumerate(choices):
  85. print(f"{i+1}. {choice}")
  86. print()
  87.  
  88. # Function to prompt for user selection
  89. def prompt_selection(self, prompt, choices):
  90. while True:
  91. self.display_choices(choices)
  92. user_input = input(prompt)
  93.  
  94. try:
  95. selection = int(user_input)
  96. if 1 <= selection <= len(choices):
  97. return selection - 1
  98. except ValueError:
  99. pass
  100.  
  101. print("Invalid selection. Please try again.")
  102.  
  103. # Function to prompt for multiple selections
  104. def prompt_multi_selection(self, prompt, choices):
  105. selections = []
  106. while True:
  107. self.display_choices(choices)
  108. user_input = input(prompt)
  109.  
  110. try:
  111. user_input = user_input.strip()
  112. if user_input == "":
  113. return selections
  114.  
  115. selections_input = user_input.split(",")
  116. selections = [int(s.strip()) - 1 for s in selections_input]
  117.  
  118. if all(0 <= s < len(choices) for s in selections):
  119. return selections
  120. except (ValueError, IndexError):
  121. pass
  122.  
  123. print("Invalid selection. Please try again.")
  124.  
  125. # Function to prompt for yes/no confirmation
  126. def prompt_confirmation(self, prompt):
  127. while True:
  128. user_input = input(prompt).lower()
  129.  
  130. if user_input in ["y", "yes"]:
  131. return True
  132. elif user_input in ["n", "no"]:
  133. return False
  134.  
  135. print("Invalid input. Please try again.")
  136.  
  137. # Function to handle flashing process
  138. def flash(self):
  139. # Check if essential tools are available
  140. if not tool_exists(ADB_TOOL):
  141. self.install_tool(ADB_TOOL)
  142. self.add_tool_to_path(ADB_TOOL)
  143. if not tool_exists(FASTBOOT_TOOL):
  144. self.install_tool(FASTBOOT_TOOL)
  145. self.add_tool_to_path(FASTBOOT_TOOL)
  146.  
  147. # Determine the operating system
  148. os_name = sys.platform.lower()
  149.  
  150. # Prompt for user selections
  151. rom_selection = self.prompt_selection("Select a ROM: ", self.roms)
  152. gapps_selection = None
  153. if self.gapps:
  154. gapps_selection = self.prompt_confirmation("Do you want to include GApps? (Y/N): ")
  155. if gapps_selection:
  156. gapps_parts_selection = self.prompt_multi_selection("Select GApps parts (comma-separated): ", self.gapps)
  157. addon_gapps_selection = self.prompt_confirmation("Do you want to flash any Addon-GApps? (Y/N): ")
  158. if addon_gapps_selection and self.addon_gapps:
  159. addon_gapps_parts_selection = self.prompt_multi_selection("Select Addon-GApps parts (comma-separated): ", self.addon_gapps)
  160. recovery_selection = self.prompt_selection("Select a recovery: ", self.recovery)
  161. rooting_selection = self.prompt_selection("Select a rooting file: ", self.rooting)
  162.  
  163. # Confirmation before initiating the flashing process
  164. print()
  165. print("Flashing process summary:")
  166. print(f"ROM: {self.roms[rom_selection]}")
  167. if self.gapps:
  168. if gapps_selection:
  169. print("GApps parts:")
  170. for part in gapps_parts_selection:
  171. print(f"- {self.gapps[part]}")
  172. else:
  173. print("GApps: Skipped")
  174. if self.addon_gapps and addon_gapps_selection:
  175. print("Addon-GApps parts:")
  176. for part in addon_gapps_parts_selection:
  177. print(f"- {self.addon_gapps[part]}")
  178. else:
  179. print("Addon-GApps: Skipped")
  180. print(f"Recovery: {self.recovery[recovery_selection]}")
  181. print(f"Rooting: {self.rooting[rooting_selection]}")
  182. print()
  183.  
  184. if not self.prompt_confirmation("Do you want to proceed with flashing? (Y/N): "):
  185. print("Flashing process cancelled.")
  186. return
  187.  
  188. # Flashing process logic
  189.  
  190. try:
  191. # TODO: Implement the flashing process
  192.  
  193. print("Flashing process completed successfully.")
  194. logging.info("Flashing process completed successfully.")
  195.  
  196. except FlashingError as e:
  197. print(f"Flashing failed: {str(e)}")
  198. logging.error(f"Flashing failed: {str(e)}")
  199.  
  200. # Function to install a tool
  201. def install_tool(self, tool_name):
  202. # Placeholder for actual tool installation code
  203. print(f"Installing {tool_name}...")
  204. logging.info(f"Installing {tool_name}...")
  205.  
  206. # Function to add a tool to PATH
  207. def add_tool_to_path(self, tool_name):
  208. # Placeholder for actual PATH addition code
  209. print(f"Adding {tool_name} to PATH...")
  210. logging.info(f"Adding {tool_name} to PATH...")
  211.  
  212. # Main method
  213. if __name__ == "__main__":
  214. try:
  215. flash_handler = FlashHandler()
  216. flash_handler.flash()
  217. except Exception as e:
  218. print(f"An error occurred: {str(e)}")
  219. logging.exception(str(e))
  220.  
Advertisement
Add Comment
Please, Sign In to add comment