eudemonics

PYADB v1.3 (required by opotoolkit.py) [LATEST!]

Aug 24th, 2014
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. ##### PYADB v1.3
  3. ##### RELEASE DATE: AUGUST 26, 2014
  4. ##### AUTHOR: vvn
  5. ##### unfinished library to port ADB and FASTBOOT functions to PYTHON
  6. ##### for now it's a required companion to the half-assed one plus one toolkit.
  7. ##### that's also here on pastebin: http://pastebin.com/y511TjV1
  8. ##### check out the README file too! http://notworth.it/opo/README
  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.
  13. ##### i don't care if you credit me, though that'd be nice.
  14. ##### to really show your appreciation, you can buy my EP instead!
  15. ##### you can stream and purchase it at: dreamcorp.bandcamp.com
  16. ##### questions, comments, feedback, bugs, complaints, death threats, marriage proposals?
  17. ##### contact me at:
  18. ##### vvn (at) eudemonics (dot) org
  19.  
  20. import os, subprocess, sys
  21. from subprocess import call, Popen, PIPE
  22.  
  23. class pyADB(object):
  24.  
  25.    # adb commands
  26.    def call_adb(self, command):
  27.       results = ''
  28.       command_text = 'adb %s' % command
  29.       output = Popen(command_text, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
  30.       response = output.communicate()
  31.       # response = output.stdout()
  32.       return response
  33.  
  34.    # fastboot commands
  35.    def call_fastboot(self, command):
  36.       results = ''
  37.       command_text = 'fastboot %s' % command
  38.       output = Popen(command_text, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
  39.       response, errors = output.communicate()[0][1]
  40.       # response = output.stdout()
  41.       return response
  42.      
  43.    # check for any ADB device
  44.    def adbcallany(self, device_id):
  45.       command = 'devices -s %s' % device_id
  46.       result = self.call_adb(command)
  47.       return result
  48.       pass
  49.  
  50.    # check for any fastboot device
  51.    def fastbootany(self, device_id):
  52.       command = 'devices -s %s' % device_id
  53.       result = self.call_fastboot(command)
  54.       return result
  55.       pass
  56.  
  57.    # return list of attached devices
  58.    def attached_devices(self):
  59.       command_text = "adb devices -l"
  60.       output = Popen(command_text, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
  61.       response = output.communicate()
  62.       return response or None
  63.    
  64.       # result = self.call_adb(command)
  65.       # devices = result.partition('\n')[2].replace('\n', '').split('\tdevice')
  66.       # return [device for device in devices if len(device) > 2]
  67.       # return result
  68.  
  69.    # fastboot return list of devices
  70.    def fastboot_devices(self):
  71.       command_text = 'fastboot devices'
  72.       output = Popen(command_text, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
  73.       response = output.communicate()
  74.       return response or None
  75.       # command = "devices -l"
  76.       # result = self.call_adb(command)  
  77.       # return result  
  78.  
  79.    # get device state
  80.    def get_state(self):
  81.       results = ''
  82.       command_text = 'adb get-state'
  83.       output = Popen(command_text, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
  84.       response = output.communicate()
  85.       return response or None
  86.       #result = self.call_adb("get-state")
  87.       #return result or None
  88.  
  89.    # install APK
  90.    def install(self, path_to_app):
  91.       command = "install %s" % path_to_app
  92.       result = self.call_adb(command)
  93.       return result
  94.  
  95.    # uninstall APK
  96.    def uninstall(self, path_to_app, args):
  97.       command = "uninstall %s" % path_to_app
  98.       if 'keep' in args:
  99.          command = "uninstall -k %s" % path_to_app
  100.       result = self.call_adb(command)
  101.       return result
  102.  
  103.    # reboot
  104.    def reboot(self, rb_type):
  105.       command = "reboot"
  106.       if 'recovery' in rb_type:
  107.          command += " recovery"
  108.       elif 'bootloader' in rb_type:
  109.          command += " bootloader"
  110.       result = self.call_adb(command)
  111.       return result
  112.      
  113.    # fastboot reboot
  114.    def fastreboot(self, rb_type):
  115.       command = "reboot"
  116.       if 'bootloader' in rb_type:
  117.          command = "reboot-bootloader"
  118.       result = self.call_fastboot(command)
  119.       return result
  120.  
  121.    # push files
  122.    def push(self, local, remote):
  123.       command = "push -p %s %s" % (local, remote)
  124.       result = self.call_adb(command)
  125.       return result
  126.  
  127.    # pull files
  128.    def pull(self, remote, local):
  129.       command = "pull -p %s %s" % (remote, local)
  130.       result = self.call_adb(command)
  131.       return result
  132.  
  133.    # sync
  134.    def sync(self, **directory):
  135.       command = "sync"
  136.       if directory is not None:
  137.          command = "sync %s" % directory
  138.       result = self.call_adb(command)
  139.       return result
  140.      
  141.    # shell command
  142.    def shell(self, shellcmd):
  143.       command = "shell " + shellcmd
  144.       result = self.call_adb(command)
  145.       return result
  146.    
  147.    # backup device  
  148.    def backup(self, backupfile):
  149.       command = "backup -f %s -all" % backupfile
  150.       result = self.call_adb(command)
  151.       return result
  152.    
  153.    # restore device  
  154.    def restore(self, restorefile):
  155.       command = "restore %s" % restorefile
  156.       result = self.call_adb(command)
  157.       return result
  158.    
  159.    # boot from image file  
  160.    def bootimg(self, boot_file):      
  161.       command = "boot " + boot_file
  162.       result = self.call_fastboot(command)
  163.       return result
  164.    
  165.    # install update ZIP        
  166.    def update(self, update_file):      
  167.       command = "update " + update_file
  168.       result = self.call_fastboot(command)
  169.       return result
  170.    
  171.    # fastboot flash image  
  172.    def flashf(self, type, file):
  173.       command = "flash %s %s" % (type, file)
  174.       result = self.call_fastboot(command)
  175.       return result
  176.    
  177.    # unlock bootloader
  178.    def unlockboot(self):
  179.       command = "oem unlock"
  180.       result = self.call_fastboot(command)
  181.       return result
  182.      
  183.    # lock bootloader
  184.    def lockboot(self):
  185.       command = "oem lock"
  186.       result = self.call_fastboot(command)
  187.       return result
  188.      
  189.    # sideload installation
  190.    def sideload(self, file):
  191.       command = "sideload %s" % file
  192.       result = self.call_adb(command)
  193.       return result
  194.      
  195.    # bugreport
  196.    def bugreport(self):
  197.       command = "bugreport"
  198.       result = self.call_adb(command)
  199.       return result
  200.      
  201.    # wipe/erase partitions
  202.    def wipe(self, parts):
  203.       command = "-w erase"
  204.       if 'system' in parts:
  205.          command = "erase system"
  206.       elif 'all' in parts:
  207.          command = "flashall"
  208.       elif 'data' in parts:
  209.          command = "erase data"
  210.       elif 'cache' in parts:
  211.          command = "erase cache"
  212.       elif 'boot' in parts:
  213.          command = "erase boot"
  214.       elif 'recovery' in parts:
  215.          command = "erase recovery"
  216.       elif 'flashall' in parts:
  217.          command = "flashall"
  218.       else:
  219.          command = "-w erase system"
  220.       result = self.call_fastboot(command)
  221.       return result
Advertisement
Add Comment
Please, Sign In to add comment