144 lines
4.6 KiB
Python
144 lines
4.6 KiB
Python
from pathlib import Path
|
|
path = Path('/home/eric/redroid-android13/bootstrap_redroid.py')
|
|
text = path.read_text()
|
|
old = ''' def connect(self):
|
|
if self.mode == "adb" and ":" in self.serial:
|
|
run(["adb", "connect", self.serial], check=False)
|
|
|
|
def has_root(self):
|
|
if self.mode == "container":
|
|
return True
|
|
if not self._root_checked or not self._root_available:
|
|
result = adb(
|
|
self.serial,
|
|
["shell", "su", "-mm", "-c", "id"],
|
|
check=False,
|
|
capture_output=True,
|
|
)
|
|
self._root_available = result.returncode == 0 and "uid=0(" in result.stdout
|
|
self._root_checked = self._root_available
|
|
return self._root_available
|
|
|
|
def wait_for_root(self, timeout):
|
|
if self.mode == "container":
|
|
return True
|
|
deadline = time.time() + timeout
|
|
while time.time() < deadline:
|
|
if self.has_root():
|
|
return True
|
|
time.sleep(2)
|
|
return False
|
|
|
|
def exec_shell(self, command, check=True, capture_output=False, root=False):
|
|
if self.mode == "container":
|
|
return run(
|
|
[
|
|
self.container_runtime,
|
|
"exec",
|
|
self.container_name,
|
|
"/system/bin/sh",
|
|
"-c",
|
|
command,
|
|
],
|
|
check=check,
|
|
capture_output=capture_output,
|
|
)
|
|
if root and self.has_root():
|
|
return adb(
|
|
self.serial,
|
|
["shell", "su", "-mm", "-c", command],
|
|
check=check,
|
|
capture_output=capture_output,
|
|
)
|
|
return adb(
|
|
self.serial,
|
|
["shell", command],
|
|
check=check,
|
|
capture_output=capture_output,
|
|
)
|
|
'''
|
|
new = ''' def connect(self):
|
|
if self.mode == "adb" and ":" in self.serial:
|
|
run(["adb", "connect", self.serial], check=False)
|
|
|
|
def _root_probe_args(self):
|
|
return [
|
|
["shell", "su", "root", "id"],
|
|
["shell", "su", "0", "id"],
|
|
]
|
|
|
|
def _root_shell_args(self, command):
|
|
return [
|
|
["shell", "su", "root", "sh", "-c", command],
|
|
["shell", "su", "0", "sh", "-c", command],
|
|
]
|
|
|
|
def has_root(self):
|
|
if self.mode == "container":
|
|
return True
|
|
if not self._root_checked or not self._root_available:
|
|
for args in self._root_probe_args():
|
|
result = adb(
|
|
self.serial,
|
|
args,
|
|
check=False,
|
|
capture_output=True,
|
|
)
|
|
if result.returncode == 0 and "uid=0(" in result.stdout:
|
|
self._root_available = True
|
|
self._root_checked = True
|
|
return True
|
|
self._root_available = False
|
|
self._root_checked = False
|
|
return self._root_available
|
|
|
|
def wait_for_root(self, timeout):
|
|
if self.mode == "container":
|
|
return True
|
|
deadline = time.time() + timeout
|
|
while time.time() < deadline:
|
|
if self.has_root():
|
|
return True
|
|
time.sleep(2)
|
|
return False
|
|
|
|
def exec_shell(self, command, check=True, capture_output=False, root=False):
|
|
if self.mode == "container":
|
|
return run(
|
|
[
|
|
self.container_runtime,
|
|
"exec",
|
|
self.container_name,
|
|
"/system/bin/sh",
|
|
"-c",
|
|
command,
|
|
],
|
|
check=check,
|
|
capture_output=capture_output,
|
|
)
|
|
if root and self.has_root():
|
|
last_result = None
|
|
for args in self._root_shell_args(command):
|
|
result = adb(
|
|
self.serial,
|
|
args,
|
|
check=False,
|
|
capture_output=capture_output,
|
|
)
|
|
last_result = result
|
|
if result.returncode == 0:
|
|
return result
|
|
if check and last_result is not None:
|
|
last_result.check_returncode()
|
|
return last_result
|
|
return adb(
|
|
self.serial,
|
|
["shell", command],
|
|
check=check,
|
|
capture_output=capture_output,
|
|
)
|
|
'''
|
|
if old not in text:
|
|
raise SystemExit('expected bootstrap_redroid.py block not found')
|
|
path.write_text(text.replace(old, new))
|