diff -r 89116bd505cb Lib/platform.py --- a/Lib/platform.py Wed May 04 12:00:32 2016 -0400 +++ b/Lib/platform.py Thu May 05 03:03:06 2016 +0800 @@ -690,6 +690,52 @@ return release, vendor, vminfo, osinfo +def _android_getprop(prop, default): + # On Android 4.1, `getprop` relies on an inherited file descriptor to + # gain access to the system property area. The id of this file descriptor + # is stored in the environment variable ANDROID_PROPERTY_WORKSPACE + pass_fds = () + android_property_workspace = os.getenv('ANDROID_PROPERTY_WORKSPACE') + if android_property_workspace and ',' in android_property_workspace: + property_fd_str = android_property_workspace.split(',')[0] + if property_fd_str.isdigit(): + pass_fds = (int(property_fd_str),) + + try: + result = subprocess.check_output(['getprop', prop], pass_fds=pass_fds) + return result.decode('utf-8').strip() + except (OSError, subprocess.CalledProcessError, UnicodeDecodeError): + return default + +def android_ver(version='', sdk='', versioninfo=('', '', ''), hardwareinfo=('', '')): + """ Attempt to get the Android version information. + + The function checks Android system properties to retrieve info. + + Returns a (version, sdk, versioninfo, hardwareinfo) tuple with + versioninfo being a (buildstr, codename, incremental) tuple and + hardwareinfo being a (boardname, abi) tuple. + + Values which cannot be determined are set to the defaults + given as parameters (which all default to ''). + """ + + version = _android_getprop('ro.build.version.release', version) + sdk = _android_getprop('ro.build.version.sdk', sdk) + + buildstr, codename, incremental = versioninfo + buildstr = _android_getprop('ro.build.version.full', buildstr) + codename = _android_getprop('ro.build.version.codename', codename) + incremental = _android_getprop('ro.build.version.incremental', incremental) + versioninfo = buildstr, codename, incremental + + boardname, abi = hardwareinfo + boardname = _android_getprop('ro.product.board', boardname) + abi = _android_getprop('ro.product.cpu.abi', abi) + hardwareinfo = boardname, abi + + return version, sdk, versioninfo, hardwareinfo + ### System name aliasing def system_alias(system, release, version):