This repository has been archived on 2023-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
Android-Mobile-Toolkit/helpers/root.py

111 lines
3.4 KiB
Python
Raw Normal View History

2019-09-29 19:38:41 +00:00
import adbutils
import os
import sys
import time
import re
import requests
from lxml import html
def adb_start():
if "platform-tools" in os.environ['PATH']:
print("ADB found in PATH")
else:
os.environ['PATH'] += ';'+os.getcwd()+'\\platform-tools'
def adb_connect():
adb = adbutils.AdbClient(host="127.0.0.1", port=5037)
d = adb.device()
if d != None:
print("Device connected.")
return d
def twrp_download(d):
found = 0
files = os.listdir(os.curdir)
for file in files:
if "twrp" in file:
found = 1
if found != 1:
2019-10-03 17:15:57 +00:00
cpu = d.shell('getprop ro.product.board')
2019-09-29 19:38:41 +00:00
r = requests.get('https://dl.twrp.me/'+cpu)
tree = html.fromstring(r.text)
urls = tree.xpath('//a/@href')
downloads = []
for i in urls:
if "img" in i:
downloads.append(i)
2019-10-03 17:36:02 +00:00
url_to_download = "https://dl.twrp.me"+downloads[0]
url_to_download = url_to_download.replace('.html', '')
s = requests.Session()
s.headers.update({'referer':url_to_download})
img = s.get(url_to_download)
with open("twrp.img",'wb') as f:
f.write(img.content)
files = os.listdir(os.curdir)
for file in files:
if "twrp" in file:
found = 1
while found != 1:
print("File not found. Please confirm it has been moved to the correct directory")
input("Press Enter to continue...")
files = os.listdir(os.curdir)
for file in files:
if "twrp" in file:
found = 1
else:
print("Error. Returning to the main menu")
time.sleep(2)
2019-09-29 19:38:41 +00:00
else:
print("twrp already downloaded")
def push_files(d):
initcheck_magisk = d.shell("cd /sdcard && ls | grep Magisk")
if initcheck_magisk == None and initcheck_twrp == None:
d.sync.push("Magisk-v19.3.zip", "/sdcard/Magisk.zip")
check_magisk = d.shell("cd /sdcard && ls | grep Magisk")
if check != None:
print("File copied successfully.")
else:
print("Something went wrong. Please try again.")
else:
print("Magisk already copied")
def reboot_bootloader():
adb = "platform-tools\\adb.exe"
fastboot = "platform-tools\\fastboot.exe"
os.system(adb+" reboot bootloader")
input("Press Enter when the device has rebooted")
os.system(fastboot+" devices")
def root_device():
device = adb_connect()
twrp_download(device)
push_files(device)
reboot_bootloader()
adb = "platform-tools\\adb.exe"
fastboot = "platform-tools\\fastboot.exe"
files = os.listdir(os.curdir)
for file in files:
if "twrp" in file:
twrp = file
os.system(fastboot + " boot "+twrp)
input("Press Enter when TWRP has booted")
print("Follow the onscreen directions to install Magisk (Located at the bottom of the install window)")
print("After Magisk installs click [Reboot] then [Do Not Install]")
input("Press Enter when the device has rebooted")
2019-10-02 05:11:09 +00:00
def root_check():
device = adb_connect()
rootcheck = device.shell("ls /sbin | grep su")
if rootcheck != None:
print("Device is rooted")
else:
print("Device is not rooted")