diff --git a/magisk_boot_flasher.py b/magisk_boot_flasher.py index 344c475..8c5a596 100644 --- a/magisk_boot_flasher.py +++ b/magisk_boot_flasher.py @@ -1,3 +1,4 @@ +from pathlib import Path import requests import hashlib import sys @@ -40,6 +41,15 @@ def yes_or_no(question, default=None): print("Please respond with 'yes' or 'no' " "(or 'y' or 'n').\n") +def isAdbConnected(): + cmdlist = ['adb', 'devices'] + sp = subprocess.run(cmdlist, capture_output=True) + if len(sp.stdout.split(b'\n')) < 4: + return False + else: + return True + + def isMagiskInstalled(): cmdlist = ['adb', 'shell', 'command', '-v', 'su'] sp = subprocess.run(cmdlist, stdout=subprocess.DEVNULL, @@ -59,14 +69,20 @@ def checkInstalledVersion(): def downloadUpdate(device, version): filename = f"lineage-{version}.zip" - url = f'https://download.lineage.microg.org/{device}/{filename}' - r = requests.get(url, stream=True) - with tqdm.wrapattr(open(filename, "wb"), "write", - miniters=1, desc=filename, - total=int(r.headers.get('Content-Length'))) as fout: - for chunk in r.iter_content(chunk_size=4096): - fout.write(chunk) - fout.close() + + if Path(filename).exists(): + print(f'{filename} already found locally.') + else: + url = f'https://download.lineage.microg.org/{device}/{filename}' + r = requests.get(url, stream=True) + with tqdm.wrapattr(open(filename, "wb"), + "write", + miniters=1, + desc=filename, + total=int(r.headers.get('Content-Length'))) as fout: + for chunk in r.iter_content(chunk_size=4096): + fout.write(chunk) + fout.close() print('Comparing hashes...', end='', flush=True) checkHash(filename) @@ -150,6 +166,13 @@ def cleanUp(filename): if __name__ == "__main__": + print("Checking if an adb device is connected...", end='', flush=True) + if not isAdbConnected(): + print("\nNo adb device is connected.") + print("Aborting.") + sys.exit() + print(' [OK]') + print("Checking if Magisk already installed...", end='', flush=True) if isMagiskInstalled(): print("\nIt seems Magisk is already installed (su in PATH).")