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.

181 lines
5.4 KiB

8 years ago
  1. import datetime
  2. import json
  3. import os
  4. import re
  5. import shutil
  6. import subprocess
  7. import time
  8. debug = True
  9. with open('defaults.json') as d,open('keycodes.json') as k:
  10. defaults = json.load(d)
  11. keycodes = json.load(k)
  12. exe = defaults['exe']
  13. for key,value in defaults['local'].items():
  14. defaults['local'][key] = os.path.expandvars(value)
  15. #I can't lmao
  16. exists = '''if [ -e {} ]
  17. then
  18. echo "True"
  19. else
  20. echo "False"
  21. fi'''
  22. def merge(src, dst,log = False):
  23. if not os.path.exists(dst):
  24. return False
  25. ok = True
  26. for path, dirs, files in os.walk(src):
  27. relPath = os.path.relpath(path, src)
  28. destPath = os.path.join(dst, relPath)
  29. if not os.path.exists(destPath):
  30. os.makedirs(destPath)
  31. for file in files:
  32. destFile = os.path.join(destPath, file)
  33. if os.path.isfile(destFile):
  34. if log:
  35. print("Skipping existing file: " + os.path.join(relPath, file))
  36. ok = False
  37. continue
  38. srcFile = os.path.join(path, file)
  39. shutil.move(srcFile, destFile)
  40. for path, dirs, files in os.walk(src, False):
  41. if len(files) == 0 and len(dirs) == 0:
  42. os.rmdir(path)
  43. return ok
  44. def _adb(*args,out = False):
  45. args = [exe] + list(args)
  46. if out:
  47. return subprocess.check_output(args,shell = False).decode().rstrip('\r\n')
  48. else:
  49. subprocess.call(args,shell = False)
  50. def get_info():
  51. thing = _adb("devices","-l",out = True)
  52. formed = list(filter(bool,thing.split("\r\n")))[1:]
  53. main = {}
  54. for device in formed:
  55. categories = re.split(" +",device)
  56. device_dict = {
  57. "serial":categories[0],
  58. "mode":categories[1]
  59. }
  60. device_dict.update(dict(category.split(":") for category in categories[2:]))
  61. main[categories[0]] = device_dict
  62. return main
  63. class device:
  64. def prim_device():
  65. while True:
  66. prim_device_serial = get_info()
  67. if len(prim_device_serial.keys()) > 0:
  68. return device(list(prim_device_serial.keys())[0])
  69. def __init__(self,serial=None):
  70. if serial:
  71. self.serial = serial
  72. self.info = get_info()[serial]
  73. else:
  74. self.serial,self.info = get_info().items()[0]
  75. def adb(self,*args,out = False):
  76. args = ['-s',self.serial]+ list(args)
  77. return _adb(*args,out = out)
  78. def sudo(self,*args,out = False):
  79. args = '"{}"'.format(" ".join(args))
  80. return self.adb("shell","su","-c",args,out = out)
  81. def exists(self,file):
  82. e = exists.format(file)
  83. res = self.sudo(e,out=True)
  84. return res == "True"
  85. def reboot(self,mode = None):
  86. if mode:
  87. if mode == "soft":
  88. pid = self.adb("shell","pidof","zygote",out = True)
  89. return self.sudo("kill",pid,out=True)
  90. else:
  91. self.adb("reboot",mode)
  92. else:
  93. self.adb("reboot")
  94. def delete(self,path):
  95. self.sudo("rm","-rf",path)
  96. def copy(self,remote,local,del_duplicate = True):
  97. if os.path.exists(computer):
  98. last = os.path.split(computer)[-1]
  99. real_dir = computer
  100. computer = os.path.join(defaults['temp'],last)
  101. flag = True
  102. self.adb("pull","-a",phone,computer)
  103. if flag:
  104. shutil.merge(computer,real_dir)
  105. if os.path.exists(computer) and delete_dups:
  106. shutil.rmtree(computer)
  107. def send_keycode(self,code):
  108. try:
  109. keycode = keycodes[code]
  110. except KeyError:
  111. keycode = str(code)
  112. self.adb("shell","input","keyevent",keycode)
  113. def move(self,remote,local,del_duplicate = True):
  114. self.copy(remote,local,del_duplicate = del_duplicate)
  115. self.delete(remote)
  116. def push(self,local,remote):
  117. self.adb('push',local,remote)
  118. def backup(*partitions,name = None,backupdir):
  119. options_dict = {
  120. "system": "S",
  121. "data": "D",
  122. "cache": "C",
  123. "recovery": "R",
  124. "spec_part_1": "1",
  125. "spec_part_2": "2",
  126. "spec_part_3": "3",
  127. "boot": "B",
  128. "as": "A"
  129. }
  130. options = "".join(options_dict[option] for option in partitions)
  131. if not name:
  132. name = "backup_"+datetime.datetime.today().strftime(defaults['date_format'])
  133. filename = os.path.join(backupdir,name)
  134. self.adb("shell","twrp","backup",options,name)
  135. phone_dir = "/data/media/0/TWRP/BACKUPS/{serial}/{name}".format(serial = self.serial,name = name)
  136. self.move(phone_dir,filename)
  137. def unlock_phone(self,pin):
  138. self.send_keycode('power')
  139. self.send_keycode('space')
  140. self.adb("shell","input","text",str(pin))
  141. self.send_keycode('enter')
  142. def install(self,name):
  143. if os.path.exists(name):
  144. local_name = name
  145. name = os.path.split(name)[-1]
  146. update_path = '{}/{}'.format(defaults['remote']['updates'],name)
  147. if not self.exists(update_path):
  148. self.push(local_name,defaults['remote']['updates'])
  149. else:
  150. update_path = '{}/{}'.format(defaults['remote']['updates'],name)
  151. self.adb("shell","twrp","install",update_path)
  152. if __name__ == "__main__" and debug:
  153. d = device.prim_device()