aboutsummaryrefslogtreecommitdiff
path: root/catapult/common/battor/battor/battor_wrapper.py
diff options
context:
space:
mode:
Diffstat (limited to 'catapult/common/battor/battor/battor_wrapper.py')
-rw-r--r--catapult/common/battor/battor/battor_wrapper.py44
1 files changed, 30 insertions, 14 deletions
diff --git a/catapult/common/battor/battor/battor_wrapper.py b/catapult/common/battor/battor/battor_wrapper.py
index 6947f837..93aed8b0 100644
--- a/catapult/common/battor/battor/battor_wrapper.py
+++ b/catapult/common/battor/battor/battor_wrapper.py
@@ -2,7 +2,6 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
-import atexit
import datetime
import os
import logging
@@ -15,6 +14,7 @@ import time
from battor import battor_error
import py_utils
+from py_utils import atexit_with_log
from py_utils import cloud_storage
import dependency_manager
from devil.utils import battor_device_mapping
@@ -27,7 +27,20 @@ from serial.tools import list_ports
DEFAULT_SHELL_CLOSE_TIMEOUT_S = 60
-def IsBattOrConnected(test_platform, android_device=None,
+def IsBattOrConnected(*args, **kwargs):
+ """Returns True if BattOr is detected.
+
+ See _IsBattOrConnected below for arguments.
+ """
+ is_connected = _IsBattOrConnected(*args, **kwargs)
+ if is_connected:
+ logging.info('BattOr power monitor is connected.')
+ else:
+ logging.info('BattOr power monitor is not connected.')
+ return is_connected
+
+
+def _IsBattOrConnected(test_platform, android_device=None,
android_device_map=None, android_device_file=None):
"""Returns True if BattOr is detected."""
if test_platform == 'android':
@@ -38,11 +51,8 @@ def IsBattOrConnected(test_platform, android_device=None,
if not android_device_map:
device_tree = find_usb_devices.GetBusNumberToDeviceTreeMap()
if device_tree:
- logging.warning('Device tree:')
for _, node in sorted(device_tree.iteritems()):
node.Display()
- else:
- logging.warning('Empty device tree.')
if len(battor_device_mapping.GetBattOrList(device_tree)) == 1:
return True
if android_device_file:
@@ -51,9 +61,7 @@ def IsBattOrConnected(test_platform, android_device=None,
else:
try:
android_device_map = battor_device_mapping.GenerateSerialMap()
- logging.warning('Android device map: %s', android_device_map)
except battor_error.BattOrError:
- logging.exception('Error generating serial map')
return False
# If neither if statement above is triggered, it means that an
@@ -129,7 +137,8 @@ class BattOrWrapper(object):
self._dm = dependency_manager.DependencyManager(
[dependency_manager.BaseConfig(config)])
self._battor_agent_binary = self._dm.FetchPath(
- 'battor_agent_binary', '%s_%s' % (sys.platform, platform.machine()))
+ 'battor_agent_binary',
+ '%s_%s' % (py_utils.GetHostOsName(), py_utils.GetHostArchName()))
self._autoflash = autoflash
self._serial_log_bucket = serial_log_bucket
@@ -143,7 +152,7 @@ class BattOrWrapper(object):
self._target_platform = target_platform
self._git_hash = None
- atexit.register(self.KillBattOrShell)
+ atexit_with_log.Register(self.KillBattOrShell)
def _FlashBattOr(self):
assert self._battor_shell, (
@@ -164,6 +173,7 @@ class BattOrWrapper(object):
except ValueError:
logging.exception('Git hash returned from BattOr was not as expected: %s'
% self._git_hash)
+ self.StopShell()
finally:
if not self._battor_shell:
@@ -208,10 +218,11 @@ class BattOrWrapper(object):
assert not self._tracing, 'Attempting to stop a BattOr shell while tracing.'
timeout = timeout if timeout else DEFAULT_SHELL_CLOSE_TIMEOUT_S
- self._SendBattOrCommand(self._EXIT_CMD, check_return=False)
try:
+ self._SendBattOrCommand(self._EXIT_CMD, check_return=False)
py_utils.WaitFor(lambda: self.GetShellReturnCode() != None, timeout)
- except py_utils.TimeoutException:
+ except:
+ # If graceful shutdown failed, resort to a simple kill command.
self.KillBattOrShell()
finally:
self._battor_shell = None
@@ -233,8 +244,7 @@ class BattOrWrapper(object):
self._trace_results_path = temp_file.name
temp_file.close()
self._SendBattOrCommand(
- '%s %s' % (self._STOP_TRACING_CMD, self._trace_results_path),
- check_return=False)
+ '%s %s' % (self._STOP_TRACING_CMD, self._trace_results_path))
self._tracing = False
self._stop_tracing_time = int(time.time())
@@ -245,6 +255,12 @@ class BattOrWrapper(object):
seconds.
Returns: Trace data in form of a list.
"""
+ if not self._stop_tracing_time or not self._start_tracing_time:
+ raise battor_error.BattOrError(
+ 'No start or stop time detected when collecting BattOr trace.\n'
+ 'Start: %s \n Stop: %s' % (self._start_tracing_time,
+ self._stop_tracing_time))
+
# The BattOr shell terminates after returning the results.
if timeout is None:
timeout = self._stop_tracing_time - self._start_tracing_time
@@ -295,7 +311,6 @@ class BattOrWrapper(object):
return port
if target_platform in ['android', 'linux']:
- device_tree = find_usb_devices.GetBusNumberToDeviceTreeMap(fast=True)
if battor_path:
if not isinstance(battor_path, basestring):
raise battor_error.BattOrError(
@@ -315,6 +330,7 @@ class BattOrWrapper(object):
serial_map=battor_map)
# Not Android and no explicitly passed BattOr.
+ device_tree = find_usb_devices.GetBusNumberToDeviceTreeMap(fast=True)
battors = battor_device_mapping.GetBattOrList(device_tree)
if len(battors) != 1:
raise battor_error.BattOrError(