Advertisement
eudemonics

PYADB - library to port ADB/FASTBOOT (req for opo toolkit)

Aug 16th, 2014
693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.65 KB | None | 0 0
  1. #!/usr/bin/python
  2. ##### PYADB v1.0 STABLE!
  3. ##### lazy, unfinished library to port ADB and FASTBOOT functions to PYTHON
  4. ##### author: vvn
  5. ##### release date: august 18, 2014
  6. ##### for now it's a required companion to the half-assed one plus one toolkit.
  7. ##### here is the half-assed one plus one toolkit: http://pastebin.com/ciAj8NJy
  8. ##### you need python 2.7 and the android SDK installed to run it. go to toolkit link above for download links.
  9. ##### this project is vaguely based on something i found on github but had way too many errors.
  10. ##### feel free to share, modify, whatever.
  11. ##### i don't care if you credit me, it'd be nice. though i prefer you buy my EP instead to thank me. :)
  12. ##### you can stream and buy it here: dreamcorp.bandcamp.com
  13. ##### questions, comments, feedback, bugs?
  14. ##### contact me at:
  15. ##### vvn (at) eudemonics (dot) org
  16.  
  17. import os, readline
  18. from subprocess import call
  19.  
  20. class pyADB(object):
  21.  
  22.    def call_adb(self, command):
  23.       results = ''
  24.       command_text = 'adb %s' % command
  25.       results = call(command_text, shell=True)
  26.       #while 1:
  27.       #  line = results.readline()
  28.       #  if not line: break
  29.       #  command_result += line
  30.       return results
  31.      
  32.    def call_fastboot(self, command):
  33.       results = ''
  34.       command_text = 'fastboot %s' % command
  35.       results = call(command_text, shell=True)
  36.       return results
  37.  
  38.    # check for any fastboot device
  39.    def fastboot(self, device_id):
  40.       command = 'devices -s %s' % device_id
  41.       result = self.call_fastboot(command)
  42.       pass
  43.  
  44.    # return list of attached devices
  45.    def attached_devices(self):
  46.       command = "devices -l"
  47.       result = self.call_adb(command)
  48.       # devices = result.partition('\n')[2].replace('\n', '').split('\tdevice')
  49.       # return [device for device in devices if len(device) > 2]
  50.       return result
  51.  
  52.    # fastboot return list of devices
  53.    def fastboot_devices(self):
  54.       command = "devices -l"
  55.       result = self.call_adb(command)  
  56.       return result  
  57.  
  58.    # get device state
  59.    def get_state(self):
  60.       result = self.call_adb("get-state")
  61.       return result or None
  62.  
  63.    # install APK
  64.    def install(self, path_to_app):
  65.       command = "install %s" % path_to_app
  66.       result = self.call_adb(command)
  67.       return result
  68.  
  69.    # uninstall APK
  70.    def uninstall(self, path_to_app, args):
  71.       command = "uninstall %s" % path_to_app
  72.       if 'keep' in args:
  73.          command = "uninstall -k %s" % path_to_app
  74.       result = self.call_adb(command)
  75.       return result
  76.  
  77.    # reboot
  78.    def reboot(self, rb_type):
  79.       command = "reboot"
  80.       if 'recovery' in rb_type:
  81.          command += " recovery"
  82.       elif 'bootloader' in rb_type:
  83.          command += " bootloader"
  84.       result = self.call_adb(command)
  85.       return result
  86.      
  87.    # fastboot reboot
  88.    def fastreboot(self, rb_type):
  89.       command = "reboot"
  90.       if 'bootloader' in rb_type:
  91.          command = "reboot-bootloader"
  92.       result = self.call_fastboot(command)
  93.       return result
  94.  
  95.    # push files
  96.    def push(self, local, remote):
  97.       result = self.call_adb("push -p %s %s" % (local, remote))
  98.       return result
  99.  
  100.    # pull files
  101.    def pull(self, remote, local):
  102.       result = self.call_adb("pull -p %s %s" % (remote, local))
  103.       return result
  104.  
  105.    # sync
  106.    def sync(self, **directory):
  107.       command = "sync"
  108.       if directory:
  109.          command = "sync " + directory
  110.       result = self.call_adb(command)
  111.       return result
  112.      
  113.    # shell command
  114.    def shell(self, shellcmd):
  115.       command = "shell " + shellcmd
  116.       result = self.call_adb(command)
  117.       return result
  118.    
  119.    # backup device  
  120.    def backup(self, backupfile):
  121.       command = "backup -f %s -all" % backupfile
  122.       result = self.call_adb(command)
  123.       return result
  124.    
  125.    # restore device  
  126.    def restore(self, restorefile):
  127.       command = "restore %s" % restorefile
  128.       result = self.call_adb(command)
  129.       return result
  130.    
  131.    # boot from image file  
  132.    def bootimg(self, boot_file):      
  133.       command = "boot %s" % boot_file
  134.       result = self.call_fastboot(command)
  135.       return result
  136.    
  137.    # install update ZIP        
  138.    def update(self, update_file):      
  139.       command = "update %s" % update_file
  140.       result = self.call_fastboot(command)
  141.       return result
  142.    
  143.    # fastboot flash image  
  144.    def flashf(self, type, file):
  145.       command = "flash %s %s" % (type, file)
  146.       result = self.call_fastboot(command)
  147.       return result
  148.    
  149.    # unlock bootloader
  150.    def unlockboot(self):
  151.       command = "oem unlock"
  152.       result = self.call_fastboot(command)
  153.       return result
  154.      
  155.    # lock bootloader
  156.    def lockboot(self):
  157.       command = "oem lock"
  158.       result = self.call_fastboot(command)
  159.       return result
  160.      
  161.    # sideload installation
  162.    def sideload(self, file):
  163.       command = "sideload %s" % file
  164.       result = self.call_adb(command)
  165.       return result
  166.      
  167.    # bugreport
  168.    def bugreport(self):
  169.       command = "bugreport"
  170.       result = self.call_adb(command)
  171.       return result
  172.      
  173.    # wipe/erase partitions
  174.    def wipe(self, parts):
  175.       command = "-w erase"
  176.       if 'system' in parts:
  177.          command = "erase system"
  178.       elif 'all' in parts:
  179.          command = "flashall"
  180.       elif 'data' in parts:
  181.          command = "erase data"
  182.       elif 'cache' in parts:
  183.          command = "erase cache"
  184.       elif 'boot' in parts:
  185.          command = "erase boot"
  186.       elif 'recovery' in parts:
  187.          command = "erase recovery"
  188.       elif 'flashall' in parts:
  189.          command = "flashall"
  190.       else:
  191.          command = "-w erase system"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement