Commit bfffb22e authored by jbudorick's avatar jbudorick Committed by Commit bot

[Android] Remove android_commands uses from build/android/. (reland)

original CL:  https://codereview.chromium.org/1088793002/

BUG=267773

Review URL: https://codereview.chromium.org/1083403002

Cr-Commit-Position: refs/heads/master@{#325439}
parent 70057a8d
...@@ -10,8 +10,9 @@ import optparse ...@@ -10,8 +10,9 @@ import optparse
import os import os
import sys import sys
from pylib import android_commands
from pylib import constants from pylib import constants
from pylib.device import adb_wrapper
from pylib.device import device_filter
from pylib.device import device_utils from pylib.device import device_utils
...@@ -71,10 +72,11 @@ def main(argv): ...@@ -71,10 +72,11 @@ def main(argv):
constants.SetBuildType(options.build_type) constants.SetBuildType(options.build_type)
ValidateInstallAPKOption(parser, options, args) ValidateInstallAPKOption(parser, options, args)
devices = android_commands.GetAttachedDevices() devices = adb_wrapper.AdbWrapper.Devices(
filters=device_filter.DefaultFilters())
if options.device: if options.device:
if options.device not in devices: if options.device not in [d.GetDeviceSerial() for d in devices]:
raise Exception('Error: %s not in attached devices %s' % (options.device, raise Exception('Error: %s not in attached devices %s' % (options.device,
','.join(devices))) ','.join(devices)))
devices = [options.device] devices = [options.device]
......
...@@ -11,12 +11,15 @@ i.e., "adb forward" in reverse. Requires |host_forwarder| and |device_forwarder| ...@@ -11,12 +11,15 @@ i.e., "adb forward" in reverse. Requires |host_forwarder| and |device_forwarder|
to be built. to be built.
""" """
import logging
import optparse import optparse
import sys import sys
import time import time
from pylib import android_commands from pylib import constants
from pylib import constants, forwarder from pylib import forwarder
from pylib.device import adb_wrapper
from pylib.device import device_filter
from pylib.device import device_utils from pylib.device import device_utils
from pylib.utils import run_tests_helper from pylib.utils import run_tests_helper
...@@ -51,17 +54,18 @@ def main(argv): ...@@ -51,17 +54,18 @@ def main(argv):
parser.error('Bad port number') parser.error('Bad port number')
sys.exit(1) sys.exit(1)
devices = android_commands.GetAttachedDevices() devices = adb_wrapper.AdbWrapper.Devices(
filters=device_filter.DefaultFilters())
if options.device: if options.device:
if options.device not in devices: if options.device not in [d.GetDeviceSerial() for d in devices]:
raise Exception('Error: %s not in attached devices %s' % (options.device, raise Exception('Error: %s not in attached devices %s' % (options.device,
','.join(devices))) ','.join(devices)))
devices = [options.device] devices = [options.device]
else: else:
if not devices: if not devices:
raise Exception('Error: no connected devices') raise Exception('Error: no connected devices')
print "No device specified. Defaulting to " + devices[0] logging.info('No device specified. Defaulting to %s', devices[0])
device = device_utils.DeviceUtils(devices[0]) device = device_utils.DeviceUtils(devices[0])
constants.SetBuildType(options.build_type) constants.SetBuildType(options.build_type)
......
...@@ -6,33 +6,37 @@ ...@@ -6,33 +6,37 @@
"""Enables dalvik vm asserts in the android device.""" """Enables dalvik vm asserts in the android device."""
from pylib import android_commands import argparse
from pylib.device import device_utils
import optparse
import sys import sys
from pylib.device import device_utils
def main():
parser = argparse.ArgumentParser()
def main(argv): set_asserts_group = parser.add_mutually_exclusive_group(required=True)
option_parser = optparse.OptionParser() set_asserts_group.add_argument(
option_parser.add_option('--enable_asserts', dest='set_asserts', '--enable_asserts', dest='set_asserts', action='store_true',
action='store_true', default=None,
help='Sets the dalvik.vm.enableassertions property to "all"') help='Sets the dalvik.vm.enableassertions property to "all"')
option_parser.add_option('--disable_asserts', dest='set_asserts', set_asserts_group.add_argument(
action='store_false', default=None, '--disable_asserts', dest='set_asserts', action='store_false',
help='Removes the dalvik.vm.enableassertions property') help='Removes the dalvik.vm.enableassertions property')
options, _ = option_parser.parse_args(argv)
args = parser.parse_args()
# TODO(jbudorick): Accept optional serial number and run only for the # TODO(jbudorick): Accept optional serial number and run only for the
# specified device when present. # specified device when present.
devices = android_commands.GetAttachedDevices() devices = device_utils.DeviceUtils.parallel()
for device in [device_utils.DeviceUtils(serial) for serial in devices]:
if options.set_asserts != None: def set_java_asserts_and_restart(device):
if device.SetJavaAsserts(options.set_asserts): if device.SetJavaAsserts(args.set_asserts):
# TODO(jbudorick) How to best do shell restarts after the device.RunShellCommand('stop')
# android_commands refactor? device.RunShellCommand('start')
device.RunShellCommand('stop')
device.RunShellCommand('start') devices.pMap(set_java_asserts_and_restart)
return 0
if __name__ == '__main__': if __name__ == '__main__':
main(sys.argv) sys.exit(main())
...@@ -12,7 +12,8 @@ This heart beat lets the devices know that they are connected to a host. ...@@ -12,7 +12,8 @@ This heart beat lets the devices know that they are connected to a host.
import sys import sys
import time import time
from pylib import android_commands from pylib.device import adb_wrapper
from pylib.device import device_filter
from pylib.device import device_utils from pylib.device import device_utils
PULSE_PERIOD = 20 PULSE_PERIOD = 20
...@@ -20,10 +21,11 @@ PULSE_PERIOD = 20 ...@@ -20,10 +21,11 @@ PULSE_PERIOD = 20
def main(): def main():
while True: while True:
try: try:
devices = android_commands.GetAttachedDevices() devices = adb_wrapper.AdbWrapper.Devices(
for device_serial in devices: filters=device_filter.DefaultFilters())
device_utils.DeviceUtils(device_serial).RunShellCommand( for d in devices:
'touch /sdcard/host_heartbeat') device_utils.DeviceUtils(d).RunShellCommand(
['touch', '/sdcard/host_heartbeat'], check_return=True)
except: except:
# Keep the heatbeat running bypassing all errors. # Keep the heatbeat running bypassing all errors.
pass pass
......
...@@ -19,12 +19,13 @@ import subprocess ...@@ -19,12 +19,13 @@ import subprocess
import sys import sys
import time import time
from pylib import android_commands
from pylib import constants from pylib import constants
from pylib import device_settings from pylib import device_settings
from pylib.device import adb_wrapper
from pylib.device import battery_utils from pylib.device import battery_utils
from pylib.device import device_blacklist from pylib.device import device_blacklist
from pylib.device import device_errors from pylib.device import device_errors
from pylib.device import device_filter
from pylib.device import device_utils from pylib.device import device_utils
from pylib.utils import run_tests_helper from pylib.utils import run_tests_helper
from pylib.utils import timeout_retry from pylib.utils import timeout_retry
...@@ -54,7 +55,8 @@ def ProvisionDevices(options): ...@@ -54,7 +55,8 @@ def ProvisionDevices(options):
if options.device is not None: if options.device is not None:
devices = [options.device] devices = [options.device]
else: else:
devices = android_commands.GetAttachedDevices() devices = adb_wrapper.AdbWrapper.Devices(
filters=device_filter.DefaultFilters())
parallel_devices = device_utils.DeviceUtils.parallel(devices) parallel_devices = device_utils.DeviceUtils.parallel(devices)
parallel_devices.pMap(ProvisionDevice, options) parallel_devices.pMap(ProvisionDevice, options)
...@@ -196,7 +198,8 @@ def _ConfigureLocalProperties(device, java_debug=True): ...@@ -196,7 +198,8 @@ def _ConfigureLocalProperties(device, java_debug=True):
'ro.setupwizard.mode=DISABLED', 'ro.setupwizard.mode=DISABLED',
] ]
if java_debug: if java_debug:
local_props.append('%s=all' % android_commands.JAVA_ASSERT_PROPERTY) local_props.append(
'%s=all' % device_utils.DeviceUtils.JAVA_ASSERT_PROPERTY)
local_props.append('debug.checkjni=1') local_props.append('debug.checkjni=1')
try: try:
device.WriteFile( device.WriteFile(
......
...@@ -17,6 +17,7 @@ from pylib import cmd_helper ...@@ -17,6 +17,7 @@ from pylib import cmd_helper
from pylib import constants from pylib import constants
from pylib.device import decorators from pylib.device import decorators
from pylib.device import device_errors from pylib.device import device_errors
from pylib.device import device_filter
from pylib.utils import timeout_retry from pylib.utils import timeout_retry
...@@ -158,12 +159,23 @@ class AdbWrapper(object): ...@@ -158,12 +159,23 @@ class AdbWrapper(object):
cls._RunAdbCmd(['start-server'], timeout=timeout, retries=retries, cls._RunAdbCmd(['start-server'], timeout=timeout, retries=retries,
cpu_affinity=0) cpu_affinity=0)
# TODO(craigdh): Determine the filter criteria that should be supported.
@classmethod @classmethod
def GetDevices(cls, timeout=_DEFAULT_TIMEOUT, retries=_DEFAULT_RETRIES): def GetDevices(cls, filters=None, timeout=_DEFAULT_TIMEOUT,
retries=_DEFAULT_RETRIES):
"""DEPRECATED. Refer to Devices(...) below."""
# TODO(jbudorick): Remove this function once no more clients are using it.
return cls.Devices(filters=filters, timeout=timeout, retries=retries)
@classmethod
def Devices(cls, filters=None, timeout=_DEFAULT_TIMEOUT,
retries=_DEFAULT_RETRIES):
"""Get the list of active attached devices. """Get the list of active attached devices.
Args: Args:
filters: (optional) A list of binary functions that take an AdbWrapper
instance and a string description. Any device for which all provided
filter functions do not return True will not be included in the
returned list.
timeout: (optional) Timeout per try in seconds. timeout: (optional) Timeout per try in seconds.
retries: (optional) Number of retries to attempt. retries: (optional) Number of retries to attempt.
...@@ -171,9 +183,18 @@ class AdbWrapper(object): ...@@ -171,9 +183,18 @@ class AdbWrapper(object):
AdbWrapper instances. AdbWrapper instances.
""" """
output = cls._RunAdbCmd(['devices'], timeout=timeout, retries=retries) output = cls._RunAdbCmd(['devices'], timeout=timeout, retries=retries)
lines = [line.split() for line in output.splitlines()] lines = (line.split() for line in output.splitlines())
return [AdbWrapper(line[0]) for line in lines devices = (AdbWrapper(line[0]) for line in lines if len(line) == 2)
if len(line) == 2 and line[1] == 'device']
def matches_all_filters(device):
for f in filters or ():
if not f(device):
logging.info('Device %s failed filter %s', device.GetDeviceSerial(),
f.__name__)
return False
return True
return [d for d in devices if matches_all_filters(d)]
def GetDeviceSerial(self): def GetDeviceSerial(self):
"""Gets the device serial number associated with this object. """Gets the device serial number associated with this object.
......
...@@ -16,7 +16,7 @@ from pylib.device import device_errors ...@@ -16,7 +16,7 @@ from pylib.device import device_errors
class TestAdbWrapper(unittest.TestCase): class TestAdbWrapper(unittest.TestCase):
def setUp(self): def setUp(self):
devices = adb_wrapper.AdbWrapper.GetDevices() devices = adb_wrapper.AdbWrapper.Devices()
assert devices, 'A device must be attached' assert devices, 'A device must be attached'
self._adb = devices[0] self._adb = devices[0]
self._adb.WaitForDevice() self._adb.WaitForDevice()
......
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from pylib.device import device_blacklist
from pylib.device import device_errors
def DefaultFilters():
"""Returns a list of the most commonly-used device filters.
These filters match devices that:
- are in a "device" state (as opposed to, e.g., "unauthorized" or
"emulator")
- are not blacklisted.
Returns:
A list of the most commonly-used device filters.
"""
return [DeviceFilter, BlacklistFilter()]
def BlacklistFilter():
"""Returns a filter that matches devices that are not blacklisted.
Note that this function is not the filter. It creates one when called using
the blacklist at that time and returns that.
Returns:
A filter function that matches devices that are not blacklisted.
"""
blacklist = set(device_blacklist.ReadBlacklist())
def f(adb):
return adb.GetDeviceSerial() not in blacklist
return f
def DeviceFilter(adb):
"""A filter that matches devices in a "device" state.
(Basically, this is adb get-state == "device")
Args:
adb: An instance of AdbWrapper.
Returns:
True if the device is in a "device" state.
"""
try:
return adb.GetState() == 'device'
except device_errors.CommandFailedError:
return False
...@@ -30,6 +30,7 @@ from pylib.device import adb_wrapper ...@@ -30,6 +30,7 @@ from pylib.device import adb_wrapper
from pylib.device import decorators from pylib.device import decorators
from pylib.device import device_blacklist from pylib.device import device_blacklist
from pylib.device import device_errors from pylib.device import device_errors
from pylib.device import device_filter
from pylib.device import intent from pylib.device import intent
from pylib.device import logcat_monitor from pylib.device import logcat_monitor
from pylib.device.commands import install_commands from pylib.device.commands import install_commands
...@@ -1559,9 +1560,8 @@ class DeviceUtils(object): ...@@ -1559,9 +1560,8 @@ class DeviceUtils(object):
A Parallelizer operating over |devices|. A Parallelizer operating over |devices|.
""" """
if not devices: if not devices:
blacklist = device_blacklist.ReadBlacklist() devices = adb_wrapper.AdbWrapper.Devices(
devices = [d for d in adb_wrapper.AdbWrapper.GetDevices() filters=device_filter.DefaultFilters())
if d.GetDeviceSerial() not in blacklist]
if not devices: if not devices:
raise device_errors.NoDevicesError() raise device_errors.NoDevicesError()
......
...@@ -1551,9 +1551,10 @@ class DeviceUtilsParallelTest(mock_calls.TestCase): ...@@ -1551,9 +1551,10 @@ class DeviceUtilsParallelTest(mock_calls.TestCase):
def testParallel_default(self): def testParallel_default(self):
test_serials = ['0123456789abcdef', 'fedcba9876543210'] test_serials = ['0123456789abcdef', 'fedcba9876543210']
with self.assertCall( with self.assertCalls(
mock.call.pylib.device.adb_wrapper.AdbWrapper.GetDevices(), (mock.call.pylib.device.device_filter.DefaultFilters(), None),
[_AdbWrapperMock(serial) for serial in test_serials]): (mock.call.pylib.device.adb_wrapper.AdbWrapper.Devices(filters=None),
[_AdbWrapperMock(serial) for serial in test_serials])):
parallel_devices = device_utils.DeviceUtils.parallel() parallel_devices = device_utils.DeviceUtils.parallel()
for serial, device in zip(test_serials, parallel_devices.pGet(None)): for serial, device in zip(test_serials, parallel_devices.pGet(None)):
self.assertTrue( self.assertTrue(
...@@ -1562,8 +1563,10 @@ class DeviceUtilsParallelTest(mock_calls.TestCase): ...@@ -1562,8 +1563,10 @@ class DeviceUtilsParallelTest(mock_calls.TestCase):
'Expected a DeviceUtils object with serial %s' % serial) 'Expected a DeviceUtils object with serial %s' % serial)
def testParallel_noDevices(self): def testParallel_noDevices(self):
with self.assertCall( with self.assertCalls(
mock.call.pylib.device.adb_wrapper.AdbWrapper.GetDevices(), []): (mock.call.pylib.device.device_filter.DefaultFilters(), None),
(mock.call.pylib.device.adb_wrapper.AdbWrapper.Devices(filters=None),
[])):
with self.assertRaises(device_errors.NoDevicesError): with self.assertRaises(device_errors.NoDevicesError):
device_utils.DeviceUtils.parallel() device_utils.DeviceUtils.parallel()
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
from pylib.base import environment from pylib.base import environment
from pylib.device import adb_wrapper from pylib.device import adb_wrapper
from pylib.device import device_errors from pylib.device import device_errors
from pylib.device import device_filter
from pylib.device import device_utils from pylib.device import device_utils
from pylib.utils import parallelizer from pylib.utils import parallelizer
...@@ -20,8 +21,8 @@ class LocalDeviceEnvironment(environment.Environment): ...@@ -20,8 +21,8 @@ class LocalDeviceEnvironment(environment.Environment):
#override #override
def SetUp(self): def SetUp(self):
# TODO(jbudorick): This can be refined to support filters etc. available_devices = adb_wrapper.AdbWrapper.Devices(
available_devices = adb_wrapper.AdbWrapper.GetDevices() filters=device_filter.DefaultFilters())
if not available_devices: if not available_devices:
raise device_errors.NoDevicesError raise device_errors.NoDevicesError
if self._device: if self._device:
......
...@@ -11,8 +11,9 @@ import optparse ...@@ -11,8 +11,9 @@ import optparse
import os import os
import sys import sys
from pylib import android_commands
from pylib import screenshot from pylib import screenshot
from pylib.device import adb_wrapper
from pylib.device import device_filter
from pylib.device import device_utils from pylib.device import device_utils
def _PrintMessage(heading, eol='\n'): def _PrintMessage(heading, eol='\n'):
...@@ -69,7 +70,8 @@ def main(): ...@@ -69,7 +70,8 @@ def main():
if options.verbose: if options.verbose:
logging.getLogger().setLevel(logging.DEBUG) logging.getLogger().setLevel(logging.DEBUG)
devices = android_commands.GetAttachedDevices() devices = adb_wrapper.AdbWrapper.Devices(
filters=device_filter.DefaultFilters())
if not options.device and len(devices) > 1: if not options.device and len(devices) > 1:
parser.error('Multiple devices are attached. ' parser.error('Multiple devices are attached. '
......
...@@ -19,8 +19,9 @@ import subprocess ...@@ -19,8 +19,9 @@ import subprocess
import sys import sys
import optparse import optparse
from pylib import android_commands from pylib.device import adb_wrapper
from pylib.device import device_errors from pylib.device import device_errors
from pylib.device import device_filter
from pylib.device import device_utils from pylib.device import device_utils
from pylib.utils import run_tests_helper from pylib.utils import run_tests_helper
...@@ -235,14 +236,15 @@ def main(): ...@@ -235,14 +236,15 @@ def main():
if options.device: if options.device:
devices = [options.device] devices = [options.device]
else: else:
devices = android_commands.GetAttachedDevices() devices = adb_wrapper.AdbWrapper.Devices(
filters=device_filter.DefaultFilters())
# This must be done serially because strptime can hit a race condition if # This must be done serially because strptime can hit a race condition if
# used for the first time in a multithreaded environment. # used for the first time in a multithreaded environment.
# http://bugs.python.org/issue7980 # http://bugs.python.org/issue7980
tombstones = [] tombstones = []
for device_serial in devices: for adb in devices:
device = device_utils.DeviceUtils(device_serial) device = device_utils.DeviceUtils(adb)
tombstones += _GetTombstonesForDevice(device, options) tombstones += _GetTombstonesForDevice(device, options)
_ResolveTombstones(options.jobs, tombstones) _ResolveTombstones(options.jobs, tombstones)
......
...@@ -12,7 +12,8 @@ import shutil ...@@ -12,7 +12,8 @@ import shutil
import sys import sys
import time import time
from pylib import android_commands from pylib.device import adb_wrapper
from pylib.device import device_filter
from pylib.device import device_utils from pylib.device import device_utils
def _SaveAppData(device, package_name, from_apk=None, data_dir=None): def _SaveAppData(device, package_name, from_apk=None, data_dir=None):
...@@ -108,7 +109,8 @@ def main(): ...@@ -108,7 +109,8 @@ def main():
parser.print_help(sys.stderr) parser.print_help(sys.stderr)
parser.error('Unknown arguments: %s.' % args) parser.error('Unknown arguments: %s.' % args)
devices = android_commands.GetAttachedDevices() devices = adb_wrapper.AdbWrapper.Devices(
filters=device_filter.DefaultFilters())
if len(devices) != 1: if len(devices) != 1:
parser.error('Exactly 1 device must be attached.') parser.error('Exactly 1 device must be attached.')
device = device_utils.DeviceUtils(devices[0]) device = device_utils.DeviceUtils(devices[0])
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment