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.

266 lines
8.0 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
  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. path = os.path.dirname(__file__)
  10. with open(os.path.join(path,'defaults.json')) as d,open(os.path.join(path,'keycodes.json')) as k:
  11. defaults = json.load(d)
  12. keycodes = json.load(k)
  13. exe = defaults['exe']
  14. for key,value in defaults['local'].items():
  15. defaults['local'][key] = os.path.expandvars(value)
  16. def decode_parcel(s):
  17. bytes_pat = re.compile(r'(?<!0x)[a-f0-9]{8}')
  18. res = ''
  19. bytes_str = ''.join(match.group(0) for match in bytes_pat.finditer(s))
  20. for p in range(0,len(bytes_str),2):
  21. c= chr(int(bytes_str[p:p+2],16))
  22. res += c
  23. if '$' in res:
  24. res = re.search(r'(?<=\$).+',res).group(0)
  25. fres = ''
  26. for c in range(0,len(res),4):
  27. a = res[c:c+2]
  28. b = res[c+2:c+4]
  29. fres += b+a
  30. return fres
  31. else:
  32. try:
  33. return int(bytes_str,16)
  34. except:
  35. print(bytes_str,s)
  36. def merge(src, dst,log = False):
  37. if not os.path.exists(dst):
  38. return False
  39. ok = True
  40. for path, dirs, files in os.walk(src):
  41. relPath = os.path.relpath(path, src)
  42. destPath = os.path.join(dst, relPath)
  43. if not os.path.exists(destPath):
  44. os.makedirs(destPath)
  45. for file in files:
  46. destFile = os.path.join(destPath, file)
  47. if os.path.isfile(destFile):
  48. if log:
  49. print("Skipping existing file: " + os.path.join(relPath, file))
  50. ok = False
  51. continue
  52. srcFile = os.path.join(path, file)
  53. shutil.move(srcFile, destFile)
  54. for path, dirs, files in os.walk(src, False):
  55. if len(files) == 0 and len(dirs) == 0:
  56. os.rmdir(path)
  57. return ok
  58. def _adb(*args,out = False):
  59. args = [exe] + list(args)
  60. if out:
  61. return subprocess.check_output(args,shell = False).decode().rstrip('\r\n')
  62. else:
  63. subprocess.call(args,shell = False)
  64. def get_info():
  65. thing = _adb("devices","-l",out = True)
  66. formed = list(filter(bool,thing.split("\r\n")))[1:]
  67. main = {}
  68. for device in formed:
  69. categories = re.split(" +",device)
  70. device_dict = {
  71. "serial":categories[0],
  72. "mode":categories[1]
  73. }
  74. device_dict.update(dict(category.split(":") for category in categories[2:]))
  75. main[categories[0]] = device_dict
  76. return main
  77. class device:
  78. #init operations
  79. def prim_device():
  80. cont = True
  81. while cont:
  82. try:
  83. d = device()
  84. cont = False
  85. except IndexError:
  86. time.sleep(1)
  87. return d
  88. #todo connect over tcip
  89. def __init__(self,serial=None):
  90. if serial:
  91. self.serial = serial
  92. info = get_info()[serial]
  93. else:
  94. serial,info = list(get_info().items())[0]
  95. self.__dict__.update(info)
  96. #end of init operations
  97. #command interface
  98. def adb(self,*args,out = False):
  99. args = ['-s',self.serial]+ list(args)
  100. return _adb(*args,out = out)
  101. def shell(self,*args,out=False):
  102. args = ('shell',)+args
  103. return self.adb(*args,out=out)
  104. def sudo(self,*args,out = False):
  105. if self.mode == 'recovery':
  106. return self.shell(*args,out=out)
  107. else:
  108. args = '"{}"'.format(' '.join(args).replace('"','\\"'))
  109. return self.shell('su','-c',args,out = out)
  110. #end of cammand interface
  111. #file operations
  112. def type(self,file):
  113. exists = '''if [ -e "{file}" ]; then
  114. if [ -d "{file}" ]; then
  115. echo "directory"
  116. elif [ -f "{file}" ]; then
  117. echo "file"
  118. else
  119. echo "error"
  120. fi
  121. else
  122. echo "na"
  123. fi'''
  124. e = exists.format(file = file)
  125. res = self.sudo(e,out=True)
  126. return res
  127. def exists(self,file):
  128. return self.type(file) != "na"
  129. def isfile(self,file):
  130. return self.type(file) == 'file'
  131. def isdir(self,file):
  132. return self.type(file) == 'directory'
  133. def delete(self,path):
  134. return self.sudo("rm","-rf",path,out=True)
  135. def copy(self,remote,local,del_duplicates = True,ignore_error=True):
  136. remote_type = self.type(remote)
  137. if remote_type != "na":
  138. if remote_type == "directory" and not remote.endswith('/'):
  139. remote += '/'
  140. flag = False
  141. if os.path.exists(local):
  142. last = os.path.split(local)[-1]
  143. real_dir = local
  144. local = os.path.join(defaults['local']['temp'],last)
  145. flag = True
  146. try:
  147. self.adb("pull","-a",remote,local,out=True)
  148. except subprocess.CalledProcessError as e:
  149. if ignore_error:
  150. pass
  151. else:
  152. raise e
  153. if flag:
  154. merge(local,real_dir)
  155. if os.path.exists(local) and del_duplicates:
  156. shutil.rmtree(local)
  157. else:
  158. print("File not found: {}".format(remote))
  159. def move(self,remote,local,del_duplicates = True,ignore_error=False):
  160. if self.exists(remote):
  161. self.copy(remote,local,del_duplicates = del_duplicates,ignore_error=ignore_error)
  162. self.delete(remote)
  163. else:
  164. print("File not found: {}".format(remote))
  165. def push(self,local,remote):
  166. self.adb('push',local,remote)
  167. #end of file operations
  168. #convenience
  169. def reboot(self,mode = None):
  170. if mode:
  171. if mode == "soft":
  172. if self.mode != 'recovery':
  173. pid = self.shell("pidof","zygote",out = True)
  174. return self.sudo("kill",pid,out=False)
  175. else:
  176. return self.reboot()
  177. else:
  178. self.adb("reboot",mode)
  179. else:
  180. self.adb("reboot")
  181. while True:
  182. infos = get_info()
  183. if len(infos) > 0:
  184. self.__dict__.update(get_info()[self.serial])
  185. break
  186. time.sleep(1)
  187. def send_keycode(self,code):
  188. try:
  189. keycode = keycodes[code]
  190. except KeyError:
  191. keycode = str(code)
  192. self.shell("input","keyevent",keycode)
  193. def unlock_phone(self,pin):
  194. if self.mode != 'recovery':
  195. if not decode_parcel(self.shell('service','call', 'power','12',out=True)):
  196. self.send_keycode('power')
  197. if decode_parcel(self.shell('service','call','trust','7',out=True)):
  198. self.send_keycode('space')
  199. self.shell("input","text",str(pin))
  200. self.send_keycode('enter')
  201. #end of convenience
  202. #twrp
  203. def backup(self,*partitions,name = None):
  204. backupdir = defaults['local']['TWRP']
  205. options_dict = {
  206. "system": "S",
  207. "data": "D",
  208. "cache": "C",
  209. "recovery": "R",
  210. "spec_part_1": "1",
  211. "spec_part_2": "2",
  212. "spec_part_3": "3",
  213. "boot": "B",
  214. "as": "A"
  215. }
  216. options = "".join(options_dict[option] for option in partitions)
  217. if not name:
  218. name = "backup_"+datetime.datetime.today().strftime(defaults['date_format'])
  219. filename = os.path.join(backupdir,name)
  220. self.shell("twrp","backup",options,name)
  221. phone_dir = "/data/media/0/TWRP/BACKUPS/{serial}/{name}".format(serial = self.serial,name = name)
  222. self.move(phone_dir,filename)
  223. def wipe(self,partition):
  224. self.shell("twrp","wipe",partition)
  225. def install(self,name):
  226. if os.path.exists(name):
  227. local_name = name
  228. name = os.path.split(name)[-1]
  229. update_path = '{}/{}'.format(defaults['remote']['updates'],name)
  230. if not self.exists(update_path):
  231. self.push(local_name,defaults['remote']['updates'])
  232. else:
  233. update_path = '{}/{}'.format(defaults['remote']['updates'],name)
  234. self.shell("twrp","install",update_path)
  235. #end of twrp
  236. if __name__ == "__main__" and debug:
  237. d = device.prim_device()