1 #!/usr/bin/env python 2 # 3 # Copyright 2017 - The Android Open Source Project 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 from metrics.metric import Metric 18 19 20 class FastbootVersionMetric(Metric): 21 22 FASTBOOT_COMMAND = 'fastboot --version' 23 FASTBOOT_VERSION = 'fastboot_version' 24 25 # String to return if Fastboot version is too old 26 FASTBOOT_ERROR_MESSAGE = ('this version is older than versions that' 27 'return versions properly') 28 29 def gather_metric(self): 30 """Tells which versions of fastboot 31 32 Returns: 33 A dict with the following fields: 34 fastboot_version: string representing version of fastboot 35 Older versions of fastboot do not have a version flag. On an 36 older version, this metric will print 'this version is older 37 than versions that return veresions properly' 38 39 """ 40 result = self._shell.run(self.FASTBOOT_COMMAND) 41 # If '--version' flag isn't recognized, will print to stderr 42 if result.stderr: 43 version = self.FASTBOOT_ERROR_MESSAGE 44 else: 45 # The version is the last token on the first line 46 version = result.stdout.splitlines()[0].split()[-1] 47 48 response = {self.FASTBOOT_VERSION: version} 49 return response 50 51 52 class AdbVersionMetric(Metric): 53 54 ADB_COMMAND = 'adb version' 55 ADB_VERSION = 'adb_version' 56 ADB_REVISION = 'adb_revision' 57 58 def gather_metric(self): 59 """Tells which versions of adb 60 61 Returns: 62 A dict with the following fields: 63 adb_version: string representing version of adb 64 adb_revision: string representing revision of adb 65 66 """ 67 result = self._shell.run(self.ADB_COMMAND) 68 stdout = result.stdout.splitlines() 69 adb_version = stdout[0].split()[-1] 70 # Revision information will always be in next line 71 adb_revision = stdout[1].split()[1] 72 73 response = { 74 self.ADB_VERSION: adb_version, 75 self.ADB_REVISION: adb_revision 76 } 77 return response 78 79 80 class PythonVersionMetric(Metric): 81 82 PYTHON_COMMAND = 'python -V 2>&1' 83 PYTHON_VERSION = 'python_version' 84 85 def gather_metric(self): 86 """Tells which versions of python 87 88 Returns: 89 A dict with the following fields: 90 python_version: string representing version of python 91 92 """ 93 result = self._shell.run(self.PYTHON_COMMAND) 94 # Python prints this to stderr 95 version = result.stdout.split()[-1] 96 97 response = {self.PYTHON_VERSION: version} 98 return response 99 100 101 class KernelVersionMetric(Metric): 102 103 KERNEL_COMMAND = 'uname -r' 104 KERNEL_RELEASE = 'kernel_release' 105 106 def gather_metric(self): 107 """Tells which versions of kernel 108 109 Returns: 110 A dict with the following fields: 111 kernel_release: string representing kernel release 112 113 """ 114 result = self._shell.run(self.KERNEL_COMMAND).stdout 115 response = {self.KERNEL_RELEASE: result} 116 return response 117