• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
17from metrics.metric import Metric
18
19
20class VerifyMetric(Metric):
21    """Gathers the information of connected devices via ADB"""
22    COMMAND = r"adb devices | sed '1d;$d'"
23    UNAUTHORIZED = 'unauthorized'
24    OFFLINE = 'offline'
25    RECOVERY = 'recovery'
26    QUESTION = 'question'
27    DEVICE = 'device'
28    TOTAL_UNHEALTHY = 'total_unhealthy'
29
30    def gather_metric(self):
31        """ Gathers device info based on adb output.
32
33        Returns:
34            A dictionary with the fields:
35            unauthorized: list of phone sn's that are unauthorized
36            offline: list of phone sn's that are offline
37            recovery: list of phone sn's that are in recovery mode
38            question: list of phone sn's in ??? mode
39            device: list of phone sn's that are in device mode
40            total: total number of offline, recovery, question or unauthorized
41                devices
42        """
43        offline_list = list()
44        unauth_list = list()
45        recovery_list = list()
46        question_list = list()
47        device_list = list()
48
49        # Delete first and last line of output of adb.
50        output = self._shell.run(self.COMMAND).stdout
51
52        # Example Line, Device Serial Num TAB Phone Status
53        # 00bd977c7f504caf	offline
54        if output:
55            for line in output.split('\n'):
56                spl_line = line.split('\t')
57                # spl_line[0] is serial, [1] is status. See example line.
58                phone_sn = spl_line[0]
59                phone_state = spl_line[1]
60
61                if phone_state == 'device':
62                    device_list.append(phone_sn)
63                elif phone_state == 'unauthorized':
64                    unauth_list.append(phone_sn)
65                elif phone_state == 'recovery':
66                    recovery_list.append(phone_sn)
67                elif '?' in phone_state:
68                    question_list.append(phone_sn)
69                elif phone_state == 'offline':
70                    offline_list.append(phone_sn)
71
72        return {
73            self.UNAUTHORIZED:
74            unauth_list,
75            self.OFFLINE:
76            offline_list,
77            self.RECOVERY:
78            recovery_list,
79            self.QUESTION:
80            question_list,
81            self.DEVICE:
82            device_list,
83            self.TOTAL_UNHEALTHY:
84            len(unauth_list) + len(offline_list) + len(recovery_list) +
85            len(question_list)
86        }
87