Advertisement
eudemonics

PYADB v1.3.3.7 - NOVEMBER (required for OPOTOOLKIT.PY)

Nov 27th, 2014
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. ### PYADB (Python 2.7 Library)
  3. ##### VERSION: 1.3.3.7
  4. ##### RELEASE DATE: NOVEMBER 26, 2014
  5. ##### AUTHOR: vvn
  6. ##### DESCRIPTION: simple library to port ADB and FASTBOOT functions to PYTHON
  7. #####
  8. ##### for now it's a required companion to the 'half-assed one plus one toolkit'.
  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. ##################################################
  13. ##################################################
  14. ##### USER LICENSE AGREEMENT & DISCLAIMER
  15. ##### copyright, copyleft (C) 2014  vvn <vvn@notworth.it>
  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. ##### 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 it)
  36. ##### questions, comments, feedback, bugs, complaints, death threats, marriage proposals?
  37. ##### contact me at:
  38. ##### vvn (at) notworth (dot) it
  39. ##### latest version will always be available HERE:
  40. ##### http://notworth.it/opo/pyadb.py
  41. ##### if unable to download above file, use this link:
  42. ##### http://notworth.it/opo/pyadb.py.txt (remove *.txt extension, obviously.)
  43. ##### there be only code after this -->
  44.  
  45. import os, subprocess, sys, datetime
  46. from subprocess import call, Popen, PIPE
  47.  
  48. class pyADB(object):
  49.  
  50.    # adb commands
  51.    def call_adb(self, command):
  52.       response = ''
  53.       command_text = 'adb %s' % command
  54.       # command_text = r'"%s"' % command_text
  55.       command_text = command_text + '; exit 0'
  56.       # output = Popen(command_text, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
  57.       output = subprocess.check_call(command_text, shell=True, stderr=subprocess.STDOUT)
  58.       return output
  59.       # response = output.communicate()
  60.       # return response
  61.  
  62.    # fastboot commands
  63.    def call_fastboot(self, command):
  64.       response = ''
  65.       command_text = 'fastboot %s' % command
  66.       output = Popen(command_text, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
  67.       response, errors = output.communicate()
  68.       # response = output.stdout()
  69.       return response
  70.      
  71.    # check for any ADB device
  72.    def adbcallany(self, device_id):
  73.       command = 'devices -s %s' % device_id
  74.       result = self.call_adb(command)
  75.       return result or None
  76.       pass
  77.  
  78.    # check for any fastboot device
  79.    def fastbootany(self, device_id):
  80.       command = 'devices -s %s' % device_id
  81.       result = self.call_fastboot(command)
  82.       return result or None
  83.       pass
  84.  
  85.    # return list of attached devices
  86.    def attached_devices(self):
  87.       command_text = "adb devices -l"
  88.       output = Popen(command_text, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
  89.       response = output.communicate()
  90.       return response or None
  91.  
  92.    # fastboot return list of devices
  93.    def fastboot_devices(self):
  94.       command_text = 'fastboot devices'
  95.       output = Popen(command_text, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
  96.       response = output.communicate()
  97.       return response or None
  98.  
  99.    # get device state
  100.    def get_state(self):
  101.       results = ''
  102.       command_text = 'adb get-state'
  103.       output = Popen(command_text, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
  104.       response = output.communicate()
  105.       return response or None
  106.       #result = self.call_adb("get-state")
  107.       #return result or None
  108.  
  109.    # install APK
  110.    def install(self, path_to_app):
  111.       command = "install %s" % path_to_app
  112.       result = self.call_adb(command)
  113.       return result
  114.  
  115.    # uninstall APK
  116.    def uninstall(self, path_to_app, args):
  117.       command = "uninstall %s" % path_to_app
  118.       if 'keep' in args:
  119.          command = "uninstall -k %s" % path_to_app
  120.       result = self.call_adb(command)
  121.       return result
  122.  
  123.    # reboot
  124.    def reboot(self, rb_type):
  125.       command = "reboot"
  126.       if 'recovery' in rb_type:
  127.          command += " recovery"
  128.       elif 'bootloader' in rb_type:
  129.          command += " bootloader"
  130.       result = self.call_adb(command)
  131.       return result
  132.      
  133.    # fastboot reboot
  134.    def fastreboot(self, rb_type):
  135.       command = "reboot"
  136.       if 'bootloader' in rb_type:
  137.          command = "reboot-bootloader"
  138.       result = self.call_fastboot(command)
  139.       return result
  140.  
  141.    # push files
  142.    def push(self, local, remote):
  143.       command = "push -p " + r'"%s"' % local
  144.       command += " " + r'"%s"' % remote
  145.       result = self.call_adb(command)
  146.       return result
  147.  
  148.    # pull files
  149.    def pull(self, remote, local):
  150.       command = "pull -p " + r'"%s"' % remote
  151.       command += " " + r'"%s"' % local
  152.       result = self.call_adb(command)
  153.       return result
  154.  
  155.    # sync
  156.    def sync(self, local, directory):
  157.       command = "sync"
  158.       if "none" in local:
  159.          if "none" not in directory:
  160.             command = "sync " + directory
  161.       elif "none" not in local:
  162.          command = "-p " + local + " sync"
  163.          if "none" not in directory:
  164.             command = "-p " + local + " sync " + directory
  165.       else:
  166.          command = "sync"
  167.       result = self.call_adb(command)
  168.       return result
  169.      
  170.    # shell command
  171.    def shell(self, shellcmd):
  172.       command = "shell " + shellcmd
  173.       result = self.call_adb(command)
  174.       return result
  175.      
  176.    # list packages
  177.    def listpkg(self):
  178.       command = "shell pm list packages"
  179.       result = self.call_adb(command)
  180.       return result
  181.      
  182.    # find path for package
  183.    def pathpkg(self, package):
  184.       command = "shell pm path %s" % package
  185.       result = self.call_adb(command)
  186.       return result
  187.    
  188.    # pull APK from default package location
  189.    def apkpull(self, pkgname):
  190.       command = "pull /data/app/%s" % pkgname
  191.       result = self.call_adb(command)
  192.       return result
  193.      
  194.    # backup device  
  195.    def backup(self, backupfile, backapk, backobb, backshared, backall, backsys):
  196.       command = "adb backup -f %s" % backupfile
  197.       cmdapk = " -noapk"
  198.       cmdshared = " -noshared"
  199.       cmdall = ""
  200.       cmdsys = " -system"
  201.       if "apk" in backapk:
  202.          cmdapk = " -apk"
  203.       else:
  204.          cmdapk = " -noapk"
  205.       if "obb" in backobb:
  206.          cmdobb = " -obb"
  207.       else:
  208.          cmdobb = ""
  209.       if "shared" in backshared:
  210.          cmdshared = " -shared"
  211.       else:
  212.          cmdshared = " -noshared"
  213.       if "all" in backall:
  214.          cmdall = " -all"
  215.       else:
  216.          cmdall = ""
  217.       if "sys" not in backsys:
  218.          cmdsys = " -nosystem"
  219.       else:
  220.          cmdsys = " -system"
  221.       command = 'adb backup -f ' + backupfile + cmdapk + cmdobb + cmdshared + cmdall + cmdsys
  222.       if "full" in backall:
  223.          command = 'adb backup -apk -shared -all -system -f ' + backupfile
  224.       print(command)
  225.       output = subprocess.check_call(command, shell=True, stdout=PIPE)
  226.       return output
  227.    
  228.    # restore device  
  229.    def restore(self, restorefile):
  230.       command = "restore %s" % restorefile
  231.       result = self.call_adb(command)
  232.       return result
  233.    
  234.    # boot from image file  
  235.    def bootimg(self, boot_file):      
  236.       command = "boot " + boot_file
  237.       result = self.call_fastboot(command)
  238.       return result
  239.    
  240.    # install update ZIP        
  241.    def update(self, update_file):      
  242.       command = "update " + update_file
  243.       result = self.call_fastboot(command)
  244.       return result
  245.    
  246.    # fastboot flash image  
  247.    def flashf(self, type, file):
  248.       command = "flash %s %s" % (type, file)
  249.       result = self.call_fastboot(command)
  250.       return result
  251.    
  252.    # unlock bootloader
  253.    def unlockboot(self):
  254.       command = "oem unlock"
  255.       result = self.call_fastboot(command)
  256.       return result
  257.      
  258.    # lock bootloader
  259.    def lockboot(self):
  260.       command = "oem lock"
  261.       result = self.call_fastboot(command)
  262.       return result
  263.    
  264.    # sideload installation
  265.    def sideload(self, file):
  266.       command = "sideload %s" % file
  267.       result = self.call_adb(command)
  268.       return result
  269.      
  270.    # bugreport
  271.    def bugreport(self):
  272.       filename = "bugreport-" + str(datetime.date.today()) + ".txt"
  273.       command = "adb bugreport > " + filename
  274.       output = Popen(command, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
  275.       response = output.communicate()
  276.       print("bug report saved as: " + filename)
  277.       return response
  278.      
  279.    # logcat
  280.    def logcat(self):
  281.       command = "adb logcat"
  282.       output = Popen(command, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
  283.       response = output.communicate()
  284.       return response
  285.      
  286.    # wipe/erase partitions
  287.    def wipe(self, parts):
  288.       command = "-w erase"
  289.       if 'system' in parts:
  290.          command = "erase system"
  291.       elif 'all' in parts:
  292.          command = "flashall"
  293.       elif 'data' in parts:
  294.          command = "erase data"
  295.       elif 'cache' in parts:
  296.          command = "erase cache"
  297.       elif 'boot' in parts:
  298.          command = "erase boot"
  299.       elif 'recovery' in parts:
  300.          command = "erase recovery"
  301.       elif 'flashall' in parts:
  302.          command = "flashall"
  303.       else:
  304.          command = "-w erase system"
  305.       result = self.call_fastboot(command)
  306.       return result
  307.      
  308.    # services list
  309.    def listsvc(self):
  310.       command = "shell su -c service list"
  311.       result = self.call_adb(command)
  312.       return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement