zoukankan      html  css  js  c++  java
  • 使用bat脚本调用py文件直接获取应用的包名和targetversion

    背景:

    在上一篇已经介绍过如何利用python调用aapt获取包名

    https://www.cnblogs.com/reseelei-despair/p/11078750.html

    但是因为每次都要修改py文件内的安装包所在路径,感觉不是很方便,所以想办法用bat脚本实现直接将文件拖入bat文件直接运行获取到相关信息

    思路:

    批处理脚本直接运行py文件,修改py文件,导入sys模块,直接在cmd中将安装包路径传入py后运行

    代码:

    bat脚本代码如下

    @echo off
    COLOR 2
    set "getname=%1"
    echo %getname%
    python D:Pyexericeaapt_badging.py %getname%
    pause

    使用方法:创建一个txt文件,代码复制进去,保存后退出,将文件后缀名修改成bat即可

    python代码修改部分

    # -*- coding: utf-8 -*-
    import re
    import subprocess
    import os
    import sys#导入sys模块
    class ApkInfo:
        def __init__(self, apk_path):
            self.apkPath = apk_path
            self.aapt_path = self.get_aapt()
    
        @staticmethod
        def get_aapt():
            if "ANDROID_HOME" in os.environ:
                root_dir = os.path.join(os.environ["ANDROID_HOME"], "build-tools")
                for path, subdir, files in os.walk(root_dir):
                    if "aapt.exe" in files:
                        return os.path.join(path, "aapt.exe")
            else:
                return "ANDROID_HOME not exist"
    
        def get_apk_base_info(self):
            p = subprocess.Popen(self.aapt_path + " dump badging %s" % self.apkPath, stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 stdin=subprocess.PIPE, shell=True)
            (output, err) = p.communicate()
            match = re.compile("package: name='(S+)'").match(output.decode())
            if not match:
                raise Exception("can't get packageinfo")
            package_name = match.group(1)
            return package_name
    
        def get_apk_activity(self):
            p = subprocess.Popen(self.aapt_path + " dump badging %s" % self.apkPath, stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 stdin=subprocess.PIPE, shell=True)
            (output, err) = p.communicate()
            match = re.compile("launchable-activity: name='(S+)'").search(output.decode())
            if match is not None:
                return match.group(1)
    
        def get_apk_sdkVersion(self):
            p = subprocess.Popen(self.aapt_path + " dump badging %s" % self.apkPath, stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 stdin=subprocess.PIPE, shell=True)
            (output, err) = p.communicate()
            match = re.compile("sdkVersion:'(S+)'").search(output.decode())
            return match.group(1)
    
        def get_apk_targetSdkVersion(self):
            p = subprocess.Popen(self.aapt_path + " dump badging %s" % self.apkPath, stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 stdin=subprocess.PIPE, shell=True)
            (output, err) = p.communicate()
            match = re.compile("targetSdkVersion:'(S+)'").search(output.decode())
            return match.group(1)
    
    if __name__ == '__main__':
        apk_info = ApkInfo(sys.argv[1])#直接将cmd中给的参数引用至此
        print("Activity:%s"%apk_info.get_apk_activity())
        print("apkName:%s"%apk_info.get_apk_base_info())
        print("sdkVersion:%s"%apk_info.get_apk_sdkVersion())
        print("targetSdkVersion:%s"%apk_info.get_apk_targetSdkVersion())

    效果:

    将文件拖入

    运行结果如下

  • 相关阅读:
    Atom+latex+中文环境
    pytorch中,不同的kernel对不同的feature map进行卷积之后输出某一个channel对应的多个feature map如何得到一个channel的feature map
    Ubuntu16.04上添加用户以及修改用户所属的组
    shell批处理文件,并将运算结果返回
    pytorch如何能够保证模型的可重复性
    Linux用管道命令对文件的移动
    python中调用多线程加速处理文件
    Python中random模块在主函数中设置随机种子是否对于调用的函数中的随机值产生影响?
    pytorch统计模型参数量
    pytorch使用tensorboardX进行网络可视化
  • 原文地址:https://www.cnblogs.com/reseelei-despair/p/11082060.html
Copyright © 2011-2022 走看看