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.

277 lines
8.9 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
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. import shlex
  9. from android_db import AndroidSQLConn
  10. from load_things import loaded
  11. from decode_parcel import decode_parcel
  12. debug = True
  13. config = loaded.config
  14. keycodes = loaded.keycodes
  15. exe = config.defaults.exe
  16. exists = '''if [ -e "{file}" ]; then
  17. if [ -d "{file}" ]; then
  18. echo "directory"
  19. elif [ -f "{file}" ]; then
  20. echo "file"
  21. else
  22. echo "error"
  23. fi
  24. else
  25. echo "na"
  26. fi'''
  27. def merge(src, dst,log = False):
  28. if not os.path.exists(dst):
  29. return False
  30. ok = True
  31. for path, dirs, files in os.walk(src):
  32. relPath = os.path.relpath(path, src)
  33. destPath = os.path.join(dst, relPath)
  34. if not os.path.exists(destPath):
  35. os.makedirs(destPath)
  36. for file in files:
  37. destFile = os.path.join(destPath, file)
  38. if os.path.isfile(destFile):
  39. if log:
  40. print("Skipping existing file: " + os.path.join(relPath, file))
  41. ok = False
  42. continue
  43. srcFile = os.path.join(path, file)
  44. shutil.move(srcFile, destFile)
  45. for path, dirs, files in os.walk(src, False):
  46. if len(files) == 0 and len(dirs) == 0:
  47. os.rmdir(path)
  48. return ok
  49. def _adb(*args,output = "shell"):
  50. '''Output modes:
  51. "out": return output
  52. "shell": print to shell
  53. "buffered": read line by line'''
  54. args = [exe] + list(args)
  55. if output == "out":
  56. return subprocess.check_output(args,shell = False).decode().rstrip('\r\n')
  57. elif output == "shell":
  58. ret = subprocess.call(args,shell = False)
  59. if ret:
  60. raise subprocess.CalledProcessError(ret,args)
  61. elif output == "buffered":
  62. p = subprocess.Popen(args,stdout = subprocess.PIPE)
  63. return p.stdout
  64. def kill_server():
  65. _adb('kill-server')
  66. def start_server():
  67. _adb('start-server')
  68. def get_info():
  69. start_server()
  70. thing = _adb("devices","-l",output="out")
  71. formed = list(filter(bool,thing.split("\r\n")))[1:]
  72. main = {}
  73. for device in formed:
  74. categories = re.split(" +",device)
  75. device_dict = {
  76. "serial":categories[0],
  77. "mode":categories[1]
  78. }
  79. device_dict.update(dict(category.split(":") for category in categories[2:]))
  80. main[categories[0]] = device_dict
  81. return main
  82. class Device:
  83. root_mode = False
  84. def connect(ip,port=5555):
  85. if not re.match(r'(\d{1,3}\.){3}\d{1,3}',ip):
  86. raise TypeError("Invalid ip")
  87. if not all(int(n) <= 255 and int(n) >= 0 for n in ip.split('.')):
  88. raise TypeError("Invalid ip")
  89. if not (port >= 0 and port <= 2**16-1):
  90. raise TyperError("Port must be in the range 0-65536")
  91. id = '{}:{}'.format(ip,port)
  92. _adb('connect','{}:{}'.format(ip,port))
  93. dev = Device(id)
  94. dev.tcip = True
  95. return dev
  96. def disconnect(self):
  97. if self.tcip:
  98. _adb('disconnect',self.serial)
  99. def db_connect(self,filepath):
  100. return AndroidSQLConn(self,filepath)
  101. def prim_device():
  102. cont = True
  103. while cont:
  104. try:
  105. d = Device()
  106. cont = False
  107. except IndexError:
  108. time.sleep(1)
  109. return d
  110. def __init__(self,serial=None):
  111. self.tcip = False
  112. if serial:
  113. self.serial = serial
  114. info = get_info()[serial]
  115. else:
  116. serial,info = list(get_info().items())[0]
  117. self.__dict__.update(info)
  118. def adb(self,*args,output="shell"):
  119. args = ['-s',self.serial]+ list(args)
  120. return _adb(*args,output = output)
  121. def shell(self,*args,output="shell"):
  122. args = ('shell',)+args
  123. return self.adb(*args,output=output)
  124. def sudo(self,*args,output="shell"):
  125. if self.mode == 'recovery' or self.root_mode:
  126. return self.shell(*args,output=output)
  127. else:
  128. return self.shell('su','--','--',*args,output=output)
  129. @classmethod
  130. def root(cls):
  131. cls.root_mode = True
  132. _adb('root')
  133. @classmethod
  134. def unroot(cls):
  135. cls.root_mode = False
  136. _adb('unroot')
  137. def type(self,file):
  138. e = exists.format(file = file)
  139. res = self.sudo(e,output="out")
  140. return res
  141. def exists(self,file):
  142. return self.type(file) != "na"
  143. def isfile(self,file):
  144. return self.type(file) == 'file'
  145. def isdir(self,file):
  146. return self.type(file) == 'directory'
  147. def delete(self,path):
  148. return self.sudo("rm","-rf",path,output="out")
  149. def copy(self,remote,local,del_duplicates = True,ignore_error=True):
  150. remote_type = self.type(remote)
  151. if remote_type != "na":
  152. if remote_type == "directory" and not remote.endswith('/'):
  153. remote += '/'
  154. merge_flag = False
  155. if os.path.exists(local):
  156. last = os.path.split(local)[-1]
  157. real_dir = local
  158. local = os.path.join(config['local']['temp'],last)
  159. merge_flag = True
  160. try:
  161. self.adb("pull","-a",remote,local)
  162. except subprocess.CalledProcessError as e:
  163. if ignore_error:
  164. pass
  165. else:
  166. raise e
  167. if merge_flag:
  168. merge(local,real_dir)
  169. if os.path.exists(local) and del_duplicates:
  170. shutil.rmtree(local)
  171. else:
  172. print("File not found: {}".format(remote))
  173. def move(self,remote,local,del_duplicates = True,ignore_error=False):
  174. if self.exists(remote):
  175. self.copy(remote,local,del_duplicates = del_duplicates,ignore_error=ignore_error)
  176. self.delete(remote)
  177. else:
  178. print("File not found: {}".format(remote))
  179. def push(self,local,remote):
  180. self.adb('push',local,remote)
  181. def reboot(self,mode = None):
  182. if mode:
  183. if mode == "soft":
  184. if self.mode != 'recovery':
  185. pid = self.shell("pidof","zygote",output="out")
  186. return self.sudo("kill",pid,output="shell")
  187. else:
  188. return self.reboot()
  189. else:
  190. self.adb("reboot",mode)
  191. else:
  192. self.adb("reboot")
  193. while True:
  194. infos = get_info()
  195. if len(infos) > 0:
  196. self.__dict__.update(infos[self.serial])
  197. break
  198. time.sleep(1)
  199. def send_keycode(self,code):
  200. try:
  201. keycode = keycodes[code]
  202. except KeyError:
  203. keycode = str(code)
  204. self.shell("input","keyevent",keycode)
  205. def unlock_phone(self,password):
  206. if self.mode != 'recovery':
  207. if not decode_parcel(self.shell('service','call', 'power','12',output="out"),'int'):
  208. self.send_keycode('power')
  209. if decode_parcel(self.shell('service','call','trust','7',output="out"),'int'):
  210. self.send_keycode('space')
  211. self.shell("input","text",str(password))
  212. self.send_keycode('enter')
  213. def backup(self,*partitions,name = None,backupdir = None):
  214. if self.mode != 'recovery':
  215. self.reboot('recovery')
  216. if backupdir is None:
  217. backupdir = config['local']['twrp']
  218. else:
  219. backupdir = backupdir
  220. options_dict = {
  221. "system": "S",
  222. "data": "D",
  223. "cache": "C",
  224. "recovery": "R",
  225. "spec_part_1": "1",
  226. "spec_part_2": "2",
  227. "spec_part_3": "3",
  228. "boot": "B",
  229. "as": "A"
  230. }
  231. options = "".join(options_dict[option] for option in partitions)
  232. if not name:
  233. name = "backup_"+datetime.datetime.today().strftime(defaults['date_format'])
  234. filename = os.path.join(backupdir,name)
  235. self.shell("twrp","backup",options,name)
  236. phone_dir = "/data/media/0/TWRP/BACKUPS/{serial}/{name}".format(serial = self.serial,name = name)
  237. self.move(phone_dir,filename)
  238. def wipe(self,partition):
  239. if self.mode != 'recovery':
  240. self.reboot('recovery')
  241. self.shell("twrp","wipe",partition)
  242. def install(self,name):
  243. if self.mode != 'recovery':
  244. self.reboot('recovery')
  245. if os.path.exists(name):
  246. local_name = name
  247. name = os.path.split(name)[-1]
  248. update_path = '{}/{}'.format(config['remote']['updates'],name)
  249. if not self.exists(config['remote']['updates']):
  250. self.sudo('mkdir',config['remote']['updates'])
  251. if not self.exists(update_path):
  252. self.push(local_name,config['remote']['updates'])
  253. else:
  254. update_path = '{}/{}'.format(config['remote']['updates'],name)
  255. self.shell("twrp","install",update_path)
  256. if __name__ == "__main__" and debug:
  257. d = Device.prim_device()