"Selfie Tutorial 51" Python_adb generated batch App version table

Case I : The version of the software development phase is very important, different versions
have been fixed Bug are not the same, not the same function implemented,
before the official release version of Android terminal products, in addition to the project manager ensure that the system determines the correct version of the outside,
are all versions of App will be individually equipped with the verification is correct,
prevent App do the system integration integrated wrong time, resulting in loss of function or abnormal App!
Case II : Test Manager asked me to do a table, the table contains the name of the system all App,
App version information for performance tests statistics (CPU & Memory).


That question is, how to batch inside of Android has an integrated version of App of all listed and generate tables?
To Note5 Meizu phones, for example, if it is to manually record registration,
normally set - "Application Management -" one by one see version:


preparation stage
  1. adb shell pm list package can list app package name in all systems,
    PM is the package manger short, is an important app for Android installation package management tool,
    it can be used to install the app, uninstall app, lists all app so on.
  2. adb shell dumpsys package + App package name can be parsed version information,
    dumpsys Android is an important analytical tool that can parse App package.
  3. Openpyxl modules could be considered to generate an excel format, of course, also conceivable to make csv text format,
    if excel operation, are recommended openpyxl, try not xlrd, xlwt like obsolete modules.

Python batch script form

The essence of the batch script is executed sequentially, can be batch processing.

# coding=utf-8

import os
import re
import csv

app_list = []  # 新建一个空的列表,用于存放所有app的package name用的。
app_version_dict = {}  # 新建一个空字典,用于存放app package name及version信息。

# 先读取系统内的所有App的package
app_str = os.popen("adb shell pm list package").read()

for line in app_str.splitlines():
    app_list.append(line.replace("package:", ""))
print(app_list)

# 获取各个App package的version信息
for app in app_list:
    version_str = os.popen("adb shell \"dumpsys package %s| grep versionName\"" % app).read()
    version_name = re.findall(r"versionName=(.*)", version_str)[0]
    print("App : %s, Version : %s" % (app, version_name))
    app_version_dict[app] = version_name

# 将app_version_dict字典写入CSV 表格中。
table_title = ["App_Package_Name", "Version"]  # 表格第一行

csvfile = "App_Version.csv"
with open(csvfile, "w", newline='') as hf:
    writer = csv.DictWriter(hf, fieldnames=table_title)  # 将字典填写进csv,建议用DictWriter类
    writer.writeheader()
    for key, value in app_version_dict.items():
        writer.writerow({'App_Package_Name': key, 'Version': value})

print("App的包名与其版本信息,已经存储到了%s" % os.path.abspath(csvfile))
os.system("pause")

Process for the functional form Python
# coding=utf-8

import os
import re
import csv


def get_package():
    app_list = []  # 新建一个空的列表,用于存放所有app的package name用的。
    app_str = os.popen("adb shell pm list package").read()
    for line in app_str.splitlines():
        app_list.append(line.replace("package:", ""))
    return app_list


def get_app_version(app_package):
    '''获取指定app的版本号'''
    version_str = os.popen("adb shell \"dumpsys package %s| grep versionName\"" % app_package).read()
    version_name = re.findall(r"versionName=(.*)", version_str)[0]
    print("App : %s,  Version : %s" % (app_package, version_name))
    return version_name


def get_all_apps_version():
    '''获取所有app的版本号'''
    app_version_dict = {}  # 新建一个空字典,用于存放app package name及version信息。
    for app in get_package():
        version_name = get_app_version(app)
        app_version_dict[app] = version_name
    return app_version_dict


def write_csv(input_dict, csvfile):
    '''将app_version_dict字典写入CSV 表格中'''
    table_title = ["App_Package_Name", "Version"]  # 表格第一行
    with open(csvfile, "w", newline='') as hf:
        writer = csv.DictWriter(hf, fieldnames=table_title)
        writer.writeheader()
        for key, value in input_dict.items():
            writer.writerow({'App_Package_Name': key, 'Version': value})
    print("App的包名与其版本信息,已经存储到了%s" % os.path.abspath(csvfile))


app_version_dict = get_all_apps_version()  # 获取所有的App及其版本组成的字典
csvfile = "App_Version.csv"  # 自定义指定保存到哪个csvfile
write_csv(app_version_dict, csvfile)  # 将字典写入csv
os.system("pause")

In the form of object-oriented classes Python
# coding=utf-8

import os
import re
import csv


class PackageVersionGetter():
    def __init__(self):
        self.app_list = []  # 新建一个空的列表,用于存放所有app的package name用的。
        self.app_version_dict = []  # 新建一个空字典,用于存放app package name及version信息。

    def get_package(self):
        app_str = os.popen("adb shell pm list package").read()
        for line in app_str.splitlines():
            self.app_list.append(line.replace("package:", ""))

    def get_app_version(self, app_package):
        '''获取指定app的版本号'''
        version_str = os.popen("adb shell \"dumpsys package %s| grep versionName\"" % app_package).read()
        version_name = re.findall(r"versionName=(.*)", version_str)[0]
        print("App : %s,  Version : %s" % (app_package, version_name))
        return version_name

    def get_all_apps_version(self):
        '''获取所有app的版本号'''
        self.get_package()  # 确保self.app_list里边有数据,不会是空列表
        for app in self.app_list:
            version_name = self.get_app_version(app)
            self.app_version_dict[app] = version_name
        return self.app_version_dict


class CsvWriter():
    def __init__(self, csvfile, input_dict):
        self.csvfile = csvfile
        self.input_dict = input_dict

    def write_csv(self):
        '''将app_version_dict字典写入CSV 表格中'''
        table_title = ["App_Package_Name", "Version"]  # 表格第一行
        with open(self.csvfile, "w", newline='') as hf:
            writer = csv.DictWriter(hf, fieldnames=table_title)
            writer.writeheader()
            for key, value in self.input_dict.items():
                writer.writerow({'App_Package_Name': key, 'Version': value})
        print("App的包名与其版本信息,已经存储到了%s" % os.path.abspath(csvfile))


if __name__ == '__main__':
    p_obj = PackageVersionGetter()
    app_version_dict = p_obj.get_all_apps_version()  # 获取所有的App及其版本组成的字典

    csvfile = "App_Version.csv"  # 自定义指定保存到哪个csvfile
    c_obj = CsvWriter(csvfile, app_version_dict)
    c_obj.write_csv()  # 将字典写入csv
    os.system("pause")

Operation mode and effect

Make sure your Android device via USB cable connected to the computer, adb devices effectively connected,
three kinds of forms to achieve the above code can be run directly, such as saving for the get_app_version.py and on the desktop,
recommendations python get_app_version.py run, of course, you can double-click run.
Results are as follows: Note that some version 7 is normal, because these are google comes Android7 version of App.


More and better original article, please visit the official website: www.zipython.com
selfie tutorial (automated testing Python tutorial, Wu scattered al., Eds)
Original link: https://www.zipython.com/#/detail?id=966384ef80e24721afbede1dfafb55e5
also may be concerned about "Wu scattered people" micro-channel subscription number, ready to accept the article push.

Guess you like

Origin www.cnblogs.com/zipython/p/12600130.html