python deletes Android applications and folders

Some P words written in the front:

The tiring work hard, the lazy use tools. It is obvious to all that programmers promote social progress as lazy people.

adb has provided all the tools that developers can use, but it is also annoying to repeatedly execute a series of adb commands, so if the business requirements are fixed, execute the adb commands directly in the python script.
Please add image description
The core code is very simple:

python学习交流群:660193417###
cmd = 'adb shell'
 os.system(cmd)

1. Business requirements: Execute application uninstallation and delete the specified directory

Python学习交流Q群:660193417###
#!/usr/bin/python
import subprocess
import os, sys
import getopt

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

if __name__ == '__main__':

  """ change commands and add shell"""
  
  tag = ''
  
  try:   
opt, args = getopt.getopt(sys.argv[1:], "ht:", ['pkg', 'help'])    
for op, value in opt:      
if op in ("-t", "--pkg"):        
tag = value      
if op in ("-h", "--help"):        
print "Usage: main_app_clean.py -t APP_PKG_NAME"        
print "Options:"        
print "  -t  APP_PKG_NAME should be a bundle id !"        
print ""        
print "Sample : ./main_app_clean.py -t <bundle id>"        
print ""        
sys.exit()  except getopt.GetoptError:              
print "Error: Could not find the args."            
print "Usage: main_app_clean.py -t APP_PKG_NAME"          
print "Options:"         
 print "  -t  APP_PKG_NAME should be a bundle id !"          
 print ""          
 print "Sample : ./main_app_clean.py -t <bundle id>"          
 print ""          sys.exit()
  if tag == '':    
  print "you should input a bundle id  !"    
  exit()  pkg = tag
  print ''  print '1) uninstalling ' + pkg +' ...'  unInstallCmd = 'adb uninstall  ' + pkg   os.system(unInstallCmd)
  print ''  print '2) cleaning the cached file...'  cleanCmd1 = 'adb shell rm -fR /sdcard/.DataBackupTest'  
  os.system(cleanCmd1)  cleanCmd2 = 'adb shell rm -fR /sdcard/.DataBackup'  os.system(cleanCmd2)  
  print ''  
  print '  All done !^_^!'  
  print ''
  exit()

Instructions:

  • open terminal
cd <uninstall_clean_app.py dir path>
  • execute python command
python ./uninstall_clean_app.py -t com.xxx.app

2. Business requirements: push the obb file to the android device and associate it with the corresponding application

Python学习交流Q群:Python学习交流Q群:906715085##3
#!/usr/bin/python
import subprocess
import os, sys
import getopt
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


if __name__ == '__main__':


  """ change commands and add shell"""
  
  path = ''  
  package = '' 
obbVersion = ''
#see: https://blog.csdn.net/chengxuyuanyonghu/article/details/54972854  
try:    
opt, args = getopt.getopt(sys.argv[1:], "hf:p:v:", ['file=', 'package=','obbVersion=','help'])    
for op, value in opt:      
if op in ("-f", "--file"):        
path = value     
 if op in ("-p", "--package"):        
package = value      
if op in ("-v", "--obbVersion"):        
obbVersion = value      
if op in ("-h", "--help"):        
print "Usage: obb_push.py -f obb/file/full/path -p com.example.app.bundleid -v app_vesion"        
print "Options:"        
print "  <-f> <-p> <-v> should not be null !"        
print ""        
print "OR: <--file=> <--package=> <-obbVersion=> should not be null "        
print ""        
print "Sample : ./obb_push.py  -f obb/file/full/path.obb -p <com.example.app.bundleid> -v 15"        
print ""        
print "OR : ./obb_push.py --file=obb/file/full/path.obb --package=com.example.app.bundleid --obbVersion=15"        
print ""        
sys.exit()    
  
except getopt.GetoptError:            
print "Usage: obb_push.py -f obb/file/full/path -p com.example.app.bundleid -v app_vesion"           
print "Options:"           
print "  <-f> <-p> <-v> should not be null !"           
print "OR:"           
print " <--file=> <--package=> <-obbVersion=> should not be null "           
print ""           
print "Sample : ./obb_push.py  -f obb/file/full/path.obb -p <com.example.app.bundleid> -v 15"           
print ""           
print "OR : ./obb_push.py --file=obb/file/full/path.obb --package=com.example.app.bundleid --obbVersion=15"           
print ""           
sys.exit()
if path == '':    
print "you should input a obb file\'s path !"    
exit()
print '\n'  
print '||--------------------------------------------------------------||'  
print '\n'  
print 'NOTICE:'  
print 'obb file name rule: [main.bundleVersionCode.bundleID.obb]'  
print '\n'  
print '||--------------------------------------------------------------||'  
print '\n'
  print 'Start to copy obb file >>>>>>>>> '  
  print '  (1)===============> parsing obb file name:'  obbFilePath = path  
  if obbFilePath == '':    
  print 'you should input a obb file\'s path !'    
  exit()  
  obbSubDirs = obbFilePath.split('/')  # index  = len(obbSubDirs) - 1  obbFileName = obbSubDirs[-1]  print obbFileName  if obbFileName == '' or obbFileName.find('.obb') == -1:    
  print 'can not find a obb file in the path !'    
  exit()  
  print '\n'  
  print '    
  get package name = ' + package  
  print '\n'  
  print '  (3)===============> adb shell mkdir :'  obbDestPath = 'sdcard/Android/obb/' + package  subDir = ''  subDirs = obbDestPath.split('/')  for dir in subDirs:     
  subDir += '/' + dir     
  # print subDir      
  os.system('adb shell mkdir ' + subDir)
  print '\n'  print '  (4)===============> adb push obb file to device :'  pushCmd = 'adb push ' + obbFilePath.replace(' ','\\ ')+ ' /' + obbDestPath + '/'   
  # print pushCmd  os.system(pushCmd)
  print '\n'  
  print '  (5)===============> adb push rename obb file:'  
  newObbFileName = "main."+ obbVersion+"." + package + ".obb"  oldFileFullPath = '/' + obbDestPath + '/' + obbFileName   newFileFullPath = '/' + obbDestPath + '/' + newObbFileName 
   print '  old:' + oldFileFullPath  reameCmd = 'adb shell mv ' + oldFileFullPath + " " + newFileFullPath  os.system(reameCmd)  
  print '  new:' + newFileFullPath  print '\n'  print '  (6)===============> Completed!!!'  print '\n'  exit()
##3
#!/usr/bin/python
import subprocess
import os, sys
import getopt
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


if __name__ == '__main__':


  """ change commands and add shell"""
  
  path = ''  
  package = '' 
obbVersion = ''
#see: https://blog.csdn.net/chengxuyuanyonghu/article/details/54972854  
try:    
opt, args = getopt.getopt(sys.argv[1:], "hf:p:v:", ['file=', 'package=','obbVersion=','help'])    
for op, value in opt:      
if op in ("-f", "--file"):        
path = value     
 if op in ("-p", "--package"):        
package = value      
if op in ("-v", "--obbVersion"):        
obbVersion = value      
if op in ("-h", "--help"):        
print "Usage: obb_push.py -f obb/file/full/path -p com.example.app.bundleid -v app_vesion"        
print "Options:"        
print "  <-f> <-p> <-v> should not be null !"        
print ""        
print "OR: <--file=> <--package=> <-obbVersion=> should not be null "        
print ""        
print "Sample : ./obb_push.py  -f obb/file/full/path.obb -p <com.example.app.bundleid> -v 15"        
print ""        
print "OR : ./obb_push.py --file=obb/file/full/path.obb --package=com.example.app.bundleid --obbVersion=15"        
print ""        
sys.exit()    
  
except getopt.GetoptError:            
print "Usage: obb_push.py -f obb/file/full/path -p com.example.app.bundleid -v app_vesion"           
print "Options:"           
print "  <-f> <-p> <-v> should not be null !"           
print "OR:"           
print " <--file=> <--package=> <-obbVersion=> should not be null "           
print ""           
print "Sample : ./obb_push.py  -f obb/file/full/path.obb -p <com.example.app.bundleid> -v 15"           
print ""           
print "OR : ./obb_push.py --file=obb/file/full/path.obb --package=com.example.app.bundleid --obbVersion=15"           
print ""           
sys.exit()
if path == '':    
print "you should input a obb file\'s path !"    
exit()
print '\n'  
print '||--------------------------------------------------------------||'  
print '\n'  
print 'NOTICE:'  
print 'obb file name rule: [main.bundleVersionCode.bundleID.obb]'  
print '\n'  
print '||--------------------------------------------------------------||'  
print '\n'
  print 'Start to copy obb file >>>>>>>>> '  
  print '  (1)===============> parsing obb file name:'  obbFilePath = path  
  if obbFilePath == '':    
  print 'you should input a obb file\'s path !'    
  exit()  
  obbSubDirs = obbFilePath.split('/')  # index  = len(obbSubDirs) - 1  obbFileName = obbSubDirs[-1]  print obbFileName  if obbFileName == '' or obbFileName.find('.obb') == -1:    
  print 'can not find a obb file in the path !'    
  exit()  
  print '\n'  
  print '    
  get package name = ' + package  
  print '\n'  
  print '  (3)===============> adb shell mkdir :'  obbDestPath = 'sdcard/Android/obb/' + package  subDir = ''  subDirs = obbDestPath.split('/')  for dir in subDirs:     
  subDir += '/' + dir     
  # print subDir      
  os.system('adb shell mkdir ' + subDir)
  print '\n'  print '  (4)===============> adb push obb file to device :'  pushCmd = 'adb push ' + obbFilePath.replace(' ','\\ ')+ ' /' + obbDestPath + '/'   
  # print pushCmd  os.system(pushCmd)
  print '\n'  
  print '  (5)===============> adb push rename obb file:'  
  newObbFileName = "main."+ obbVersion+"." + package + ".obb"  oldFileFullPath = '/' + obbDestPath + '/' + obbFileName   newFileFullPath = '/' + obbDestPath + '/' + newObbFileName 
   print '  old:' + oldFileFullPath  reameCmd = 'adb shell mv ' + oldFileFullPath + " " + newFileFullPath  os.system(reameCmd)  
  print '  new:' + newFileFullPath  print '\n'  print '  (6)===============> Completed!!!'  print '\n'  exit()

Instructions:

python </path/obb_push.py> -p <app package name > -f </path/of/obbfile> -v <app version code>

At last

The deletion of the Android application set folder that I shared with you today is over here. I don’t want to give me a like.
Of course, remember to collect it and learn it slowly. If you have any questions, remember to comment and leave a message, and I will reply when I see it.
Please add image description

Guess you like

Origin blog.csdn.net/m0_67575344/article/details/124430769