You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.6 KiB

  1. import shlex
  2. import subprocess
  3. class AdbWrapper:
  4. def __init__(self, executable_path, serial=None):
  5. self.executable_path = executable_path
  6. self.serial = serial
  7. self.rooted = False
  8. def execute(self, comspec, output_streams=False):
  9. """Execute ADB command. If output streams is true, return stdout and stderr streams"""
  10. if isinstance(comspec, str):
  11. comspec = shlex.split(comspec)
  12. comspec.insert(0, self.executable_path)
  13. if self.serial:
  14. comspec = comspec[:1] + ["-s", self.serial] + comspec[1:]
  15. if output_streams:
  16. res = subprocess.run(comspec, capture_output=True, check=True)
  17. return res.stdout, res.stderr
  18. else:
  19. ret = subprocess.check_call(comspec, shell=False)
  20. def shell(self, comspec, output_streams=False):
  21. if isinstance(comspec, str):
  22. comspec = shlex.split(comspec)
  23. comspec.insert(0, "exec-out")
  24. return self.exececute(comspec, output_streams)
  25. def sudo(self, comspec, output_streams=False):
  26. if isinstance(comspec, str):
  27. comspec = shlex.split(comspec)
  28. if not self.rooted:
  29. comspec = ["su", "--", "--"] + comspec
  30. return self.shell(comspec, output_streams)
  31. def root(self):
  32. self.exececute("root")
  33. self.rooted = True
  34. def unroot(self):
  35. self.exececute("unroot")
  36. self.rooted = False
  37. if __name__ == "__main__":
  38. exec_path = r"C:\Program Files\platform-tools\adb.exe"
  39. wrapper = AdbWrapper(exec_path, "LGD415d60b8c9b")
  40. wrapper.root()
  41. wrapper.sudo(["ls", "/"])