Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- This script facilitates flashing a custom ROM onto an Android phone.
- """
- import os
- import subprocess
- import sys
- import logging
- # Constants
- ANDROID_DIR = os.path.join(os.path.expanduser("~"), "Android")
- FIRMWARE_DIR = os.path.join(ANDROID_DIR, "Firmware")
- ROMS_DIR = os.path.join(ANDROID_DIR, "ROMs")
- GAPPS_DIR = os.path.join(ANDROID_DIR, "GApps")
- ADDON_GAPPS_DIR = os.path.join(ANDROID_DIR, "Addon-GApps")
- RECOVERY_DIR = os.path.join(ANDROID_DIR, "Recovery")
- ROOTING_DIR = os.path.join(ANDROID_DIR, "Rooting")
- ADB_TOOL = "adb"
- FASTBOOT_TOOL = "fastboot"
- # Configure logging
- logging.basicConfig(filename="flash.log", level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
- # Custom exceptions
- class FlashingError(Exception):
- pass
- class InvalidSelectionError(Exception):
- pass
- # Function to check if a file exists
- def file_exists(file_path):
- return os.path.isfile(file_path)
- # Function to check if a directory exists
- def dir_exists(dir_path):
- return os.path.isdir(dir_path)
- # Function to check if a tool is available in PATH
- def tool_exists(tool_name):
- for path in os.environ["PATH"].split(os.pathsep):
- if tool_name.lower() in os.listdir(path):
- return True
- return False
- # Class to handle flashing process
- class FlashHandler:
- def __init__(self):
- self.roms = self.list_available_roms()
- self.gapps = self.list_available_gapps()
- self.addon_gapps = self.list_available_addon_gapps()
- self.recovery = self.list_available_recovery()
- self.rooting = self.list_available_rooting()
- # Function to list available choices for ROMs
- def list_available_roms(self):
- roms = os.listdir(ROMS_DIR)
- return roms if roms else None
- # Function to list available choices for GApps
- def list_available_gapps(self):
- gapps = os.listdir(GAPPS_DIR)
- return gapps if gapps else None
- # Function to list available choices for Addon-GApps
- def list_available_addon_gapps(self):
- addon_gapps = os.listdir(ADDON_GAPPS_DIR)
- return addon_gapps if addon_gapps else None
- # Function to list available choices for Recovery
- def list_available_recovery(self):
- recovery = os.listdir(RECOVERY_DIR)
- return recovery if recovery else None
- # Function to list available choices for Rooting
- def list_available_rooting(self):
- rooting = os.listdir(ROOTING_DIR)
- return rooting if rooting else None
- # Function to display available choices
- def display_choices(self, choices):
- for i, choice in enumerate(choices):
- print(f"{i+1}. {choice}")
- print()
- # Function to prompt for user selection
- def prompt_selection(self, prompt, choices):
- while True:
- self.display_choices(choices)
- user_input = input(prompt)
- try:
- selection = int(user_input)
- if 1 <= selection <= len(choices):
- return selection - 1
- except ValueError:
- pass
- print("Invalid selection. Please try again.")
- # Function to prompt for multiple selections
- def prompt_multi_selection(self, prompt, choices):
- selections = []
- while True:
- self.display_choices(choices)
- user_input = input(prompt)
- try:
- user_input = user_input.strip()
- if user_input == "":
- return selections
- selections_input = user_input.split(",")
- selections = [int(s.strip()) - 1 for s in selections_input]
- if all(0 <= s < len(choices) for s in selections):
- return selections
- except (ValueError, IndexError):
- pass
- print("Invalid selection. Please try again.")
- # Function to prompt for yes/no confirmation
- def prompt_confirmation(self, prompt):
- while True:
- user_input = input(prompt).lower()
- if user_input in ["y", "yes"]:
- return True
- elif user_input in ["n", "no"]:
- return False
- print("Invalid input. Please try again.")
- # Function to handle flashing process
- def flash(self):
- # Check if essential tools are available
- if not tool_exists(ADB_TOOL):
- self.install_tool(ADB_TOOL)
- self.add_tool_to_path(ADB_TOOL)
- if not tool_exists(FASTBOOT_TOOL):
- self.install_tool(FASTBOOT_TOOL)
- self.add_tool_to_path(FASTBOOT_TOOL)
- # Determine the operating system
- os_name = sys.platform.lower()
- # Prompt for user selections
- rom_selection = self.prompt_selection("Select a ROM: ", self.roms)
- gapps_selection = None
- if self.gapps:
- gapps_selection = self.prompt_confirmation("Do you want to include GApps? (Y/N): ")
- if gapps_selection:
- gapps_parts_selection = self.prompt_multi_selection("Select GApps parts (comma-separated): ", self.gapps)
- addon_gapps_selection = self.prompt_confirmation("Do you want to flash any Addon-GApps? (Y/N): ")
- if addon_gapps_selection and self.addon_gapps:
- addon_gapps_parts_selection = self.prompt_multi_selection("Select Addon-GApps parts (comma-separated): ", self.addon_gapps)
- recovery_selection = self.prompt_selection("Select a recovery: ", self.recovery)
- rooting_selection = self.prompt_selection("Select a rooting file: ", self.rooting)
- # Confirmation before initiating the flashing process
- print()
- print("Flashing process summary:")
- print(f"ROM: {self.roms[rom_selection]}")
- if self.gapps:
- if gapps_selection:
- print("GApps parts:")
- for part in gapps_parts_selection:
- print(f"- {self.gapps[part]}")
- else:
- print("GApps: Skipped")
- if self.addon_gapps and addon_gapps_selection:
- print("Addon-GApps parts:")
- for part in addon_gapps_parts_selection:
- print(f"- {self.addon_gapps[part]}")
- else:
- print("Addon-GApps: Skipped")
- print(f"Recovery: {self.recovery[recovery_selection]}")
- print(f"Rooting: {self.rooting[rooting_selection]}")
- print()
- if not self.prompt_confirmation("Do you want to proceed with flashing? (Y/N): "):
- print("Flashing process cancelled.")
- return
- # Flashing process logic
- try:
- # TODO: Implement the flashing process
- print("Flashing process completed successfully.")
- logging.info("Flashing process completed successfully.")
- except FlashingError as e:
- print(f"Flashing failed: {str(e)}")
- logging.error(f"Flashing failed: {str(e)}")
- # Function to install a tool
- def install_tool(self, tool_name):
- # Placeholder for actual tool installation code
- print(f"Installing {tool_name}...")
- logging.info(f"Installing {tool_name}...")
- # Function to add a tool to PATH
- def add_tool_to_path(self, tool_name):
- # Placeholder for actual PATH addition code
- print(f"Adding {tool_name} to PATH...")
- logging.info(f"Adding {tool_name} to PATH...")
- # Main method
- if __name__ == "__main__":
- try:
- flash_handler = FlashHandler()
- flash_handler.flash()
- except Exception as e:
- print(f"An error occurred: {str(e)}")
- logging.exception(str(e))
Advertisement
Add Comment
Please, Sign In to add comment