Home | History | Annotate | Download | only in metrics
      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 versions properly'
     38 
     39         """
     40         result = self._shell.run(self.FASTBOOT_COMMAND, ignore_status=True)
     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         adb_revision = ""
     71         # Old versions of ADB do not have revision numbers.
     72         if len(stdout) > 1:
     73             adb_revision = stdout[1].split()[1]
     74 
     75         response = {
     76             self.ADB_VERSION: adb_version,
     77             self.ADB_REVISION: adb_revision
     78         }
     79         return response
     80 
     81 
     82 class PythonVersionMetric(Metric):
     83 
     84     PYTHON_COMMAND = 'python -V 2>&1'
     85     PYTHON_VERSION = 'python_version'
     86 
     87     def gather_metric(self):
     88         """Tells which versions of python
     89 
     90         Returns:
     91             A dict with the following fields:
     92               python_version: string representing version of python
     93 
     94         """
     95         result = self._shell.run(self.PYTHON_COMMAND)
     96         # Python prints this to stderr
     97         version = result.stdout.split()[-1]
     98 
     99         response = {self.PYTHON_VERSION: version}
    100         return response
    101 
    102 
    103 class KernelVersionMetric(Metric):
    104 
    105     KERNEL_COMMAND = 'uname -r'
    106     KERNEL_RELEASE = 'kernel_release'
    107 
    108     def gather_metric(self):
    109         """Tells which versions of kernel
    110 
    111         Returns:
    112             A dict with the following fields:
    113               kernel_release: string representing kernel release
    114 
    115         """
    116         result = self._shell.run(self.KERNEL_COMMAND).stdout
    117         response = {self.KERNEL_RELEASE: result}
    118         return response
    119