Advertisement
eudemonics

PYADB.PY v1.3.3.7 - android library for python

Feb 9th, 2015
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.10 KB | None | 0 0
  1. #!/usr/bin/env python
  2. ### PYADB (Python 2.7 Library)
  3. ##### VERSION: 1.3.3.7
  4. ##### RELEASE DATE: OCTOBER 13, 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@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. ##### 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.txt (remove *.txt extension, obviously.)
  41. ##### MD5 checksum file will accompany it:
  42. ##### http://notworth.it/opo/pyadb.py.txt.md5
  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.       # result = self.call_adb(command)
  93.       # devices = result.partition('\n')[2].replace('\n', '').split('\tdevice')
  94.       # return [device for device in devices if len(device) > 2]
  95.       # return result
  96.  
  97.    # fastboot return list of devices
  98.    def fastboot_devices(self):
  99.       command_text = 'fastboot devices'
  100.       output = Popen(command_text, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
  101.       response = output.communicate()
  102.       return response or None
  103.       # command = "devices -l"
  104.       # result = self.call_adb(command)  
  105.       # return result  
  106.  
  107.    # get device state
  108.    def get_state(self):
  109.       results = ''
  110.       command_text = 'adb get-state'
  111.       output = Popen(command_text, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
  112.       response = output.communicate()
  113.       return response or None
  114.       #result = self.call_adb("get-state")
  115.       #return result or None
  116.  
  117.    # install APK
  118.    def install(self, path_to_app):
  119.       command = "install %s" % path_to_app
  120.       result = self.call_adb(command)
  121.       return result
  122.  
  123.    # uninstall APK
  124.    def uninstall(self, path_to_app, args):
  125.       command = "uninstall %s" % path_to_app
  126.       if 'keep' in args:
  127.          command = "uninstall -k %s" % path_to_app
  128.       result = self.call_adb(command)
  129.       return result
  130.  
  131.    # reboot
  132.    def reboot(self, rb_type):
  133.       command = "reboot"
  134.       if 'recovery' in rb_type:
  135.          command += " recovery"
  136.       elif 'bootloader' in rb_type:
  137.          command += " bootloader"
  138.       result = self.call_adb(command)
  139.       return result
  140.      
  141.    # fastboot reboot
  142.    def fastreboot(self, rb_type):
  143.       command = "reboot"
  144.       if 'bootloader' in rb_type:
  145.          command = "reboot-bootloader"
  146.       result = self.call_fastboot(command)
  147.       return result
  148.  
  149.    # push files
  150.    def push(self, local, remote):
  151.       command = "push -p " + r'"%s"' % local
  152.       command += " " + r'"%s"' % remote
  153.       result = self.call_adb(command)
  154.       return result
  155.  
  156.    # pull files
  157.    def pull(self, remote, local):
  158.       command = "pull -p " + r'"%s"' % remote
  159.       command += " " + r'"%s"' % local
  160.       result = self.call_adb(command)
  161.       return result
  162.  
  163.    # sync
  164.    def sync(self, local, directory):
  165.       command = "sync"
  166.       if "none" in local:
  167.          if "none" not in directory:
  168.             command = "sync " + directory
  169.       elif "none" not in local:
  170.          command = "-p " + local + " sync"
  171.          if "none" not in directory:
  172.             command = "-p " + local + " sync " + directory
  173.       else:
  174.          command = "sync"
  175.       result = self.call_adb(command)
  176.       return result
  177.      
  178.    # shell command
  179.    def shell(self, shellcmd):
  180.       command = "shell " + shellcmd
  181.       result = self.call_adb(command)
  182.       return result
  183.      
  184.    # list packages
  185.    def listpkg(self):
  186.       command = "shell pm list packages"
  187.       result = self.call_adb(command)
  188.       return result
  189.      
  190.    # search packages
  191.    def searchpkg(self, query):
  192.       command = "shell pm list packages | grep %s" % query
  193.       result = self.call_adb(command)
  194.       return result
  195.      
  196.    # find path for package
  197.    def pathpkg(self, package):
  198.       command = "shell pm path %s" % package
  199.       result = self.call_adb(command)
  200.       return result
  201.    
  202.    # pull APK from default package location
  203.    def apkpull(self, pkgname):
  204.       command = "pull /data/app/%s" % pkgname
  205.       result = self.call_adb(command)
  206.       return result
  207.      
  208.    # backup device  
  209.    def backup(self, backupfile, backapk, backobb, backshared, backall, backsys):
  210.       command = "adb backup -f %s" % backupfile
  211.       cmdapk = " -noapk"
  212.       cmdshared = " -noshared"
  213.       cmdall = ""
  214.       cmdsys = " -system"
  215.       if "apk" in backapk:
  216.          cmdapk = " -apk"
  217.       else:
  218.          cmdapk = " -noapk"
  219.       if "obb" in backobb:
  220.          cmdobb = " -obb"
  221.       else:
  222.          cmdobb = ""
  223.       if "shared" in backshared:
  224.          cmdshared = " -shared"
  225.       else:
  226.          cmdshared = " -noshared"
  227.       if "all" in backall:
  228.          cmdall = " -all"
  229.       else:
  230.          cmdall = ""
  231.       if "sys" not in backsys:
  232.          cmdsys = " -nosystem"
  233.       else:
  234.          cmdsys = " -system"
  235.       command = 'adb backup -f ' + backupfile + cmdapk + cmdobb + cmdshared + cmdall + cmdsys
  236.       if "full" in backall:
  237.          command = 'adb backup -apk -shared -all -system -f ' + backupfile
  238.       print(command)
  239.       output = subprocess.check_call(command, shell=True, stdout=PIPE)
  240.       return output
  241.    
  242.    # restore device  
  243.    def restore(self, restorefile):
  244.       command = "restore %s" % restorefile
  245.       result = self.call_adb(command)
  246.       return result
  247.    
  248.    # boot from image file  
  249.    def bootimg(self, boot_file):      
  250.       command = "boot " + boot_file
  251.       result = self.call_fastboot(command)
  252.       return result
  253.    
  254.    # install update ZIP        
  255.    def update(self, update_file):      
  256.       command = "update " + update_file
  257.       result = self.call_fastboot(command)
  258.       return result
  259.    
  260.    # fastboot flash image  
  261.    def flashf(self, type, file):
  262.       command = "flash %s %s" % (type, file)
  263.       result = self.call_fastboot(command)
  264.       return result
  265.    
  266.    # unlock bootloader
  267.    def unlockboot(self):
  268.       command = "oem unlock"
  269.       result = self.call_fastboot(command)
  270.       return result
  271.      
  272.    # lock bootloader
  273.    def lockboot(self):
  274.       command = "oem lock"
  275.       result = self.call_fastboot(command)
  276.       return result
  277.    
  278.    # sideload installation
  279.    def sideload(self, file):
  280.       command = "sideload %s" % file
  281.       result = self.call_adb(command)
  282.       return result
  283.      
  284.    # bugreport
  285.    def bugreport(self):
  286.       filename = "bugreport-" + str(datetime.date.today()) + ".txt"
  287.       command = "adb bugreport > " + filename
  288.       output = Popen(command, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
  289.       response = output.communicate()
  290.       print("bug report saved as: " + filename)
  291.       return response
  292.      
  293.    # logcat
  294.    def logcat(self):
  295.       command = "adb logcat"
  296.       output = Popen(command, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
  297.       response = output.communicate()
  298.       return response
  299.      
  300.    # wipe/erase partitions
  301.    def wipe(self, parts):
  302.       command = "-w erase"
  303.       if 'system' in parts:
  304.          command = "erase system"
  305.       elif 'all' in parts:
  306.          command = "flashall"
  307.       elif 'data' in parts:
  308.          command = "erase data"
  309.       elif 'cache' in parts:
  310.          command = "erase cache"
  311.       elif 'boot' in parts:
  312.          command = "erase boot"
  313.       elif 'recovery' in parts:
  314.          command = "erase recovery"
  315.       elif 'flashall' in parts:
  316.          command = "flashall"
  317.       else:
  318.          command = "-w erase system"
  319.       result = self.call_fastboot(command)
  320.       return result
  321.      
  322.    # services list
  323.    def listsvc(self):
  324.       command = "shell su -c service list"
  325.       result = self.call_adb(command)
  326.       return result
  327.      
  328.    # services search
  329.    def searchsvc(self, query):
  330.       command = "shell su -c service list | grep %s" % query
  331.       result = self.call_adb(command)
  332.       return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement