SHOW:
|
|
- or go back to the newest paste.
1 | - | #!/usr/bin/python |
1 | + | #!/usr/bin/env python |
2 | - | ##### PYADB v1.3 |
2 | + | ### PYADB (Python 2.7 Library) |
3 | - | ##### RELEASE DATE: AUGUST 26, 2014 |
3 | + | ##### VERSION: 1.3.1 |
4 | ##### RELEASE DATE: SEPTEMBER 13, 2014 | |
5 | - | ##### unfinished library to port ADB and FASTBOOT functions to PYTHON |
5 | + | |
6 | ##### DESCRIPTION: simple library to port ADB and FASTBOOT functions to PYTHON | |
7 | - | ##### that's also here on pastebin: http://pastebin.com/y511TjV1 |
7 | + | ##### |
8 | - | ##### check out the README file too! http://notworth.it/opo/README |
8 | + | |
9 | ##### this project is vaguely based on some github code i found that had too many errors. | |
10 | ##### i know it isn't anything new. i know it's probably been done better. | |
11 | ##### but i wanted one that worked for me. if it works for you, go ahead and use it too! | |
12 | - | ##### feel free to share, modify, whatever. |
12 | + | ################################################## |
13 | - | ##### i don't care if you credit me, though that'd be nice. |
13 | + | ################################################## |
14 | - | ##### to really show your appreciation, you can buy my EP instead! |
14 | + | ##### USER LICENSE AGREEMENT & DISCLAIMER |
15 | ##### copyright (C) 2014 vvn <vvn@eudemonics.org> | |
16 | ##### | |
17 | ##### This program is FREE software: you can use it, redistribute it and/or modify | |
18 | ##### it as you wish. Copying and distribution of this file, with or without modification, | |
19 | ##### are permitted in any medium without royalty provided the copyright | |
20 | - | import os, subprocess, sys |
20 | + | ##### notice and this notice are preserved. This program is offered AS-IS, |
21 | ##### WITHOUT ANY WARRANTY; without even the implied warranty of | |
22 | ##### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
23 | ##### GNU General Public License for more details. | |
24 | ##### | |
25 | ##### For more information, please refer to the "LICENSE AND NOTICE" file that should | |
26 | ##### accompany all official download releases of this program. | |
27 | ################################################## | |
28 | ################################################## | |
29 | ##### don't ask about the arbitrary versioning. i am totally making this shit up. | |
30 | ##### getting credited for my work is nice. so are donations. | |
31 | ##### BTC: 1M511j1CHR8x7RYgakNdw1iF3ike2KehXh | |
32 | ##### https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=26PWMPCNKN28L | |
33 | ##### but to really show your appreciation, you should buy my EP instead! | |
34 | ##### you can stream and purchase it at: dreamcorp.bandcamp.com | |
35 | ##### (you might even enjoy listening to it) | |
36 | ##### questions, comments, feedback, bugs, complaints, death threats, marriage proposals? | |
37 | ##### contact me at: | |
38 | ##### vvn (at) eudemonics (dot) org | |
39 | ##### latest version will always be available HERE: http://notworth.it/opo/pyadb.py | |
40 | ||
41 | import os, subprocess, sys, datetime | |
42 | from subprocess import call, Popen, PIPE | |
43 | ||
44 | class pyADB(object): | |
45 | ||
46 | # adb commands | |
47 | def call_adb(self, command): | |
48 | response = '' | |
49 | command_text = 'adb %s' % command | |
50 | # command_text = r'"%s"' % command_text | |
51 | command_text = command_text + '; exit 0' | |
52 | # output = Popen(command_text, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE) | |
53 | output = subprocess.check_call(command_text, shell=True, stderr=subprocess.STDOUT) | |
54 | return output | |
55 | # response = output.communicate() | |
56 | # return response | |
57 | ||
58 | # fastboot commands | |
59 | def call_fastboot(self, command): | |
60 | response = '' | |
61 | command_text = 'fastboot %s' % command | |
62 | output = Popen(command_text, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE) | |
63 | response, errors = output.communicate()[0][1] | |
64 | # response = output.stdout() | |
65 | return response | |
66 | ||
67 | # check for any ADB device | |
68 | def adbcallany(self, device_id): | |
69 | command = 'devices -s %s' % device_id | |
70 | result = self.call_adb(command) | |
71 | return result or None | |
72 | pass | |
73 | ||
74 | # check for any fastboot device | |
75 | def fastbootany(self, device_id): | |
76 | command = 'devices -s %s' % device_id | |
77 | result = self.call_fastboot(command) | |
78 | return result or None | |
79 | pass | |
80 | ||
81 | # return list of attached devices | |
82 | def attached_devices(self): | |
83 | command_text = "adb devices -l" | |
84 | output = Popen(command_text, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE) | |
85 | response = output.communicate() | |
86 | return response or None | |
87 | ||
88 | # result = self.call_adb(command) | |
89 | # devices = result.partition('\n')[2].replace('\n', '').split('\tdevice') | |
90 | # return [device for device in devices if len(device) > 2] | |
91 | # return result | |
92 | ||
93 | # fastboot return list of devices | |
94 | def fastboot_devices(self): | |
95 | command_text = 'fastboot devices' | |
96 | output = Popen(command_text, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE) | |
97 | response = output.communicate() | |
98 | return response or None | |
99 | # command = "devices -l" | |
100 | # result = self.call_adb(command) | |
101 | # return result | |
102 | ||
103 | # get device state | |
104 | def get_state(self): | |
105 | results = '' | |
106 | command_text = 'adb get-state' | |
107 | output = Popen(command_text, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE) | |
108 | response = output.communicate() | |
109 | return response or None | |
110 | #result = self.call_adb("get-state") | |
111 | #return result or None | |
112 | ||
113 | # install APK | |
114 | def install(self, path_to_app): | |
115 | command = "install %s" % path_to_app | |
116 | result = self.call_adb(command) | |
117 | return result | |
118 | ||
119 | # uninstall APK | |
120 | def uninstall(self, path_to_app, args): | |
121 | command = "uninstall %s" % path_to_app | |
122 | if 'keep' in args: | |
123 | - | command = "push -p %s %s" % (local, remote) |
123 | + | |
124 | result = self.call_adb(command) | |
125 | return result | |
126 | ||
127 | # reboot | |
128 | def reboot(self, rb_type): | |
129 | - | command = "pull -p %s %s" % (remote, local) |
129 | + | |
130 | if 'recovery' in rb_type: | |
131 | command += " recovery" | |
132 | elif 'bootloader' in rb_type: | |
133 | command += " bootloader" | |
134 | - | def sync(self, **directory): |
134 | + | |
135 | return result | |
136 | - | if directory is not None: |
136 | + | |
137 | - | command = "sync %s" % directory |
137 | + | |
138 | def fastreboot(self, rb_type): | |
139 | command = "reboot" | |
140 | if 'bootloader' in rb_type: | |
141 | command = "reboot-bootloader" | |
142 | result = self.call_fastboot(command) | |
143 | return result | |
144 | ||
145 | # push files | |
146 | def push(self, local, remote): | |
147 | command = "push -p " + r'"%s"' % local | |
148 | - | def backup(self, backupfile): |
148 | + | command += " " + r'"%s"' % remote |
149 | - | command = "backup -f %s -all" % backupfile |
149 | + | |
150 | return result | |
151 | ||
152 | # pull files | |
153 | def pull(self, remote, local): | |
154 | command = "pull -p " + r'"%s"' % remote | |
155 | command += " " + r'"%s"' % local | |
156 | result = self.call_adb(command) | |
157 | return result | |
158 | ||
159 | # sync | |
160 | def sync(self, local, directory): | |
161 | command = "sync" | |
162 | if "none" in local: | |
163 | if "none" not in directory: | |
164 | command = "sync " + directory | |
165 | elif "none" not in local: | |
166 | command = "-p " + local + " sync" | |
167 | if "none" not in directory: | |
168 | command = "-p " + local + " sync " + directory | |
169 | else: | |
170 | command = "sync" | |
171 | result = self.call_adb(command) | |
172 | return result | |
173 | ||
174 | # shell command | |
175 | def shell(self, shellcmd): | |
176 | command = "shell " + shellcmd | |
177 | result = self.call_adb(command) | |
178 | return result | |
179 | ||
180 | # backup device | |
181 | def backup(self, backupfile, backapk, backobb, backshared, backall, backsys): | |
182 | command = "adb backup -f %s" % backupfile | |
183 | cmdapk = " -noapk" | |
184 | cmdshared = " -noshared" | |
185 | cmdall = "" | |
186 | cmdsys = " -system" | |
187 | if "apk" in backapk: | |
188 | cmdapk = " -apk" | |
189 | else: | |
190 | cmdapk = " -noapk" | |
191 | if "obb" in backobb: | |
192 | cmdobb = " -obb" | |
193 | else: | |
194 | cmdobb = "" | |
195 | if "shared" in backshared: | |
196 | cmdshared = " -shared" | |
197 | - | command = "bugreport" |
197 | + | |
198 | cmdshared = " -noshared" | |
199 | if "all" in backall: | |
200 | cmdall = " -all" | |
201 | else: | |
202 | cmdall = "" | |
203 | if "sys" not in backsys: | |
204 | cmdsys = " -nosystem" | |
205 | else: | |
206 | cmdsys = " -system" | |
207 | command = 'adb backup -f ' + backupfile + cmdapk + cmdobb + cmdshared + cmdall + cmdsys | |
208 | if "full" in backall: | |
209 | command = 'adb backup -apk -shared -all -system -f ' + backupfile | |
210 | print(command) | |
211 | output = subprocess.check_call(command, shell=True, stdout=PIPE) | |
212 | return output | |
213 | ||
214 | # restore device | |
215 | def restore(self, restorefile): | |
216 | command = "restore %s" % restorefile | |
217 | result = self.call_adb(command) | |
218 | return result | |
219 | ||
220 | # boot from image file | |
221 | def bootimg(self, boot_file): | |
222 | command = "boot " + boot_file | |
223 | result = self.call_fastboot(command) | |
224 | return result | |
225 | ||
226 | # install update ZIP | |
227 | def update(self, update_file): | |
228 | command = "update " + update_file | |
229 | result = self.call_fastboot(command) | |
230 | return result | |
231 | ||
232 | # fastboot flash image | |
233 | def flashf(self, type, file): | |
234 | command = "flash %s %s" % (type, file) | |
235 | result = self.call_fastboot(command) | |
236 | return result | |
237 | ||
238 | # unlock bootloader | |
239 | def unlockboot(self): | |
240 | command = "oem unlock" | |
241 | result = self.call_fastboot(command) | |
242 | return result | |
243 | ||
244 | # lock bootloader | |
245 | def lockboot(self): | |
246 | command = "oem lock" | |
247 | result = self.call_fastboot(command) | |
248 | return result | |
249 | ||
250 | # sideload installation | |
251 | def sideload(self, file): | |
252 | command = "sideload %s" % file | |
253 | result = self.call_adb(command) | |
254 | return result | |
255 | ||
256 | # bugreport | |
257 | def bugreport(self): | |
258 | filename = "bugreport-" + str(datetime.date.today()) + ".txt" | |
259 | command = "adb bugreport > " + filename | |
260 | output = Popen(command, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE) | |
261 | response = output.communicate() | |
262 | print("bug report saved as: " + filename) | |
263 | return response | |
264 | ||
265 | # logcat | |
266 | def logcat(self): | |
267 | command = "adb logcat" | |
268 | output = Popen(command, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE) | |
269 | response = output.communicate() | |
270 | return response | |
271 | ||
272 | # wipe/erase partitions | |
273 | def wipe(self, parts): | |
274 | command = "-w erase" | |
275 | if 'system' in parts: | |
276 | command = "erase system" | |
277 | elif 'all' in parts: | |
278 | command = "flashall" | |
279 | elif 'data' in parts: | |
280 | command = "erase data" | |
281 | elif 'cache' in parts: | |
282 | command = "erase cache" | |
283 | elif 'boot' in parts: | |
284 | command = "erase boot" | |
285 | elif 'recovery' in parts: | |
286 | command = "erase recovery" | |
287 | elif 'flashall' in parts: | |
288 | command = "flashall" | |
289 | else: | |
290 | command = "-w erase system" | |
291 | result = self.call_fastboot(command) | |
292 | return result | |
293 | ||
294 | # services list | |
295 | def listsvc(self): | |
296 | command = "shell su -c service list" | |
297 | result = self.call_adb(command) | |
298 | return result |