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.

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