adb command in selfie tutorial 42 read system version information with one key

Android system test,
Before starting the test, we need to confirm whether the tested system version is correct,
When reporting bug s, you need to provide specific system version information for development.
And the system printing time,
Different versions fix different bug s and incorporate different new functions,
If the system version tested by the tester is not correct, it will be directly linked to the development.
How to get Android version information with one click?
Generally, we get it by reading the configuration file / system/build.prop,
This scheme is basically suitable for all Android terminal devices to obtain system version information.


Preparation stage
  1. adb shell cat /system/build.prop can get system version information.
  2. os.popen() can easily get the output string of the above command.
  3. python's re regular expression can easily perform powerful string processing such as data matching, searching, etc

Introduction to build.prop file

build.prop is an important property file in Android system,
It is a property file that is automatically generated when the version is compiled,
It records the system version, system compilation time, Android version number, etc,
After the completion of the brush, it is generally stored in the / system/build.prop file of Android device.
We can check this file through cat /system/build.prop command
For example, note 5 of Meizu build.prop file (click to download)

Introduction to os.popen() function

In the previous lesson, we used the os.system() function to execute the command line,
This time, we increased the difficulty of executing the command line with the os.popen() function,
The advantages of the os.popen() function are:

  1. It is very convenient to get the output string after command execution
  2. Nonblocking command execution

Python batch script form
# coding=utf-8

import os
import re

#Execute and read command line output
info = os.popen("adb shell cat /system/build.prop").read()
# In general, it is recommended to remove the spaces and line breaks at the beginning and end of the obtained strings
info = info.strip()

# Get system version
try:
    build_version = re.findall(r"ro.build.display.id=(.*)", info)[0].strip()
except:
    build_version = "Unknown"
print("Build Version: %s"%build_version)

#Get system version printing (compiling) time
try:
    build_time = re.findall(r"ro.build.date=(.*)", info)[0].strip()
except:
    build_time = "Unknown"
print("Build Time: %s"%build_time)

#Get the version number of the system's native Android
try:
    android_version = re.findall(r"ro.build.version.release=(.*)", info)[0].strip()
except:
    android_version = "Unknown"
print("Android Version: %s"%android_version)

os.system("pause")

The re.findall() function can be used to match strings. If it is matched to this region (. *),
What is in parentheses is extracted and a list of strings matching successfully is returned.
We only extract the first string in the list,
As long as it's a string, strip() is recommended.
try...except is trying to match, because there are inevitably android devices,
If the fields are inconsistent, and any strings cannot be matched, an empty string list will be returned,
The dictionary cannot be accessed through the sequence number [0], resulting in an exception that directly reports an error,
In case of exception, the corresponding system version information will be assigned as "Unknown",
It helps to improve the robustness of the program!

Python procedure oriented function form
# coding=utf-8

import os
import re


def get_info():
    # Execute and read command line output
    info = os.popen("adb shell cat /system/build.prop").read()
    # In general, it is recommended to remove the spaces and line breaks at the beginning and end of the obtained strings
    info = info.strip()

    # Get system version
    try:
        build_version = re.findall(r"ro.build.display.id=(.*)", info)[0].strip()
    except:
        build_version = "Unknown"

    # Get system version printing (compiling) time
    try:
        build_time = re.findall(r"ro.build.date=(.*)", info)[0].strip()
    except:
        build_time = "Unknown"

    # Get the version number of the system's native Android
    try:
        android_version = re.findall(r"ro.build.version.release=(.*)", info)[0].strip()
    except:
        android_version = "Unknown"

    return build_version, build_time, android_version  # Return 1 List (3 variable values) at a time


build_version, build_time, android_version = get_info()

print("Build Version: %s" % build_version)
print("Build Time: %s" % build_time)
print("Android Version: %s" % android_version)
os.system("pause")


Python object oriented class form
  1. With the idea of "all things can be classified", we first abstract a class,
    Class name is generally recommended to use "noun", so we call it "InfoGetter",
    Represents the information collector, and generally hump type (initial capital) is used to standardize the class naming.
  2. Form a good habit of class init ialization,
    During the initialization process, three properties (variables) can be initialized and can be called at will within the scope of the class
  3. It's enough to have a get? Info function
  4. Class is an abstract thing, which must be instantiated into concrete objects,
    Can be called, so we instantiate and name it as I obj, indicating that it is an object.
  5. After an instance is converted into a concrete object, the object can call the get info function
# coding=utf-8

import os
import re


class InfoGetter():
    def __init__(self):
        self.build_version = None
        self.build_time = None
        self.android_version = None

    def get_info(self):
        # Execute and read command line output
        info = os.popen("adb shell cat /system/build.prop").read()
        # In general, it is recommended to remove the spaces and line breaks at the beginning and end of the obtained strings
        info = info.strip()

        # Get system version
        try:
            self.build_version = re.findall(r"ro.build.display.id=(.*)", info)[0].strip()
        except:
            self.build_version = "Unknown"

        # Get system version printing (compiling) time
        try:
            self.build_time = re.findall(r"ro.build.date=(.*)", info)[0].strip()
        except:
            self.build_time = "Unknown"

        # Get the version number of the system's native Android
        try:
            self.android_version = re.findall(r"ro.build.version.release=(.*)", info)[0].strip()
        except:
            self.android_version = "Unknown"
        return self.android_version, self.build_time, self.android_version


if __name__ == '__main__':
    i_obj = InfoGetter()
    build_version, build_time, android_version = i_obj.get_info()

    print("Build Version: %s" % build_version)
    print("Build Time: %s" % build_time)
    print("Android Version: %s" % android_version)
    os.system("pause")


Code operation mode

Make sure the Android device is connected to the computer through the USB cable, and the adb device is effectively connected,
The three implementation forms of the above code can be run directly, for example, save it as get_info.py and put it on the desktop,
Every time you need to get the system version information, double-click to run get_info.py,
The operation effect is as follows (take Meizu Note5 for example),

The comparison is as follows (the time of security patch and the time of system printing and compiling are two different things)


For more and better original articles, please visit the official website: www.zipython.com
Selfie course (Python course of automatic test, compiled by Wu Sanren)
Original link: https://www.zipython.com/#/detail?id=3ccb257563254802b640d624d3c6ca6f
You can also follow the wechat subscription number of "wusanren" and accept the article push at any time.

Keywords: Python Android shell

Added by Elarion on Thu, 12 Mar 2020 14:47:12 +0200