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.

54 lines
1.5 KiB

  1. import subprocess
  2. import shlex
  3. class AdbWrapper:
  4. def __init__(self,
  5. executable_path,
  6. serial=None
  7. ):
  8. self.executable_path = executable_path
  9. self.serial = serial
  10. self.selfrooted = False
  11. def exec(self, comspec, output_streams=False):
  12. if isinstance(comspec, str):
  13. comspec = shlex.split(comspec)
  14. comspec.insert(0, self.executable_path)
  15. if self.serial:
  16. comspec = comspec[:1] + ['-s', self.serial] + comspec[1:]
  17. if output_streams:
  18. res = subprocess.run(comspec, capture_output=True, check=True)
  19. return res.stdout
  20. else:
  21. ret = subprocess.check_call(comspec, shell=False)
  22. def shell(self, comspec, output_streams=False):
  23. if isinstance(comspec, str):
  24. comspec = shlex.split(comspec)
  25. comspec.insert(0, 'exec-out')
  26. return self.exec(comspec, output_streams)
  27. def sudo(self, comspec, output_streams=False):
  28. if not self.rooted:
  29. comspec = ['su', '--', '--'] + comspec
  30. return self.shell(comspec, output_streams)
  31. def root(self):
  32. self.exec('root')
  33. self.rooted = True
  34. def unroot(self):
  35. self.exec('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', '/'])