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
import os
import sys
from pylib import android_commands
from pylib import constants
from pylib.device import adb_wrapper
from pylib.device import device_filter
from pylib.device import device_utils
......@@ -71,10 +72,11 @@ def main(argv):
constants.SetBuildType(options.build_type)
ValidateInstallAPKOption(parser, options, args)
devices = android_commands.GetAttachedDevices()
devices = adb_wrapper.AdbWrapper.Devices(
filters=device_filter.DefaultFilters())
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,
','.join(devices)))
devices = [options.device]
......
......@@ -11,12 +11,15 @@ i.e., "adb forward" in reverse. Requires |host_forwarder| and |device_forwarder|
to be built.
"""
import logging
import optparse
import sys
import time
from pylib import android_commands
from pylib import constants, forwarder
from pylib import constants
from pylib import forwarder
from pylib.device import adb_wrapper
from pylib.device import device_filter
from pylib.device import device_utils
from pylib.utils import run_tests_helper
......@@ -51,17 +54,18 @@ def main(argv):
parser.error('Bad port number')
sys.exit(1)
devices = android_commands.GetAttachedDevices()
devices = adb_wrapper.AdbWrapper.Devices(
filters=device_filter.DefaultFilters())
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,
','.join(devices)))
devices = [options.device]
else:
if not 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])
constants.SetBuildType(options.build_type)
......
......@@ -6,33 +6,37 @@
"""Enables dalvik vm asserts in the android device."""
from pylib import android_commands
from pylib.device import device_utils
import optparse
import argparse
import sys
from pylib.device import device_utils
def main():
parser = argparse.ArgumentParser()
def main(argv):
option_parser = optparse.OptionParser()
option_parser.add_option('--enable_asserts', dest='set_asserts',
action='store_true', default=None,
set_asserts_group = parser.add_mutually_exclusive_group(required=True)
set_asserts_group.add_argument(
'--enable_asserts', dest='set_asserts', action='store_true',
help='Sets the dalvik.vm.enableassertions property to "all"')
option_parser.add_option('--disable_asserts', dest='set_asserts',
action='store_false', default=None,
set_asserts_group.add_argument(
'--disable_asserts', dest='set_asserts', action='store_false',
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
# specified device when present.
devices = android_commands.GetAttachedDevices()
for device in [device_utils.DeviceUtils(serial) for serial in devices]:
if options.set_asserts != None:
if device.SetJavaAsserts(options.set_asserts):
# TODO(jbudorick) How to best do shell restarts after the
# android_commands refactor?
device.RunShellCommand('stop')
device.RunShellCommand('start')
devices = device_utils.DeviceUtils.parallel()
def set_java_asserts_and_restart(device):
if device.SetJavaAsserts(args.set_asserts):
device.RunShellCommand('stop')
device.RunShellCommand('start')
devices.pMap(set_java_asserts_and_restart)
return 0
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.
import sys
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
PULSE_PERIOD = 20
......@@ -20,10 +21,11 @@ PULSE_PERIOD = 20
def main():
while True:
try:
devices = android_commands.GetAttachedDevices()
for device_serial in devices:
device_utils.DeviceUtils(device_serial).RunShellCommand(
'touch /sdcard/host_heartbeat')
devices = adb_wrapper.AdbWrapper.Devices(
filters=device_filter.DefaultFilters())
for d in devices:
device_utils.DeviceUtils(d).RunShellCommand(
['touch', '/sdcard/host_heartbeat'], check_return=True)
except:
# Keep the heatbeat running bypassing all errors.
pass
......
......@@ -19,12 +19,13 @@ import subprocess
import sys
import time
from pylib import android_commands
from pylib import constants
from pylib import device_settings
from pylib.device import adb_wrapper
from pylib.device import battery_utils
from pylib.device import device_blacklist
from pylib.device import device_errors
from pylib.device import device_filter
from pylib.device import device_utils
from pylib.utils import run_tests_helper
from pylib.utils import timeout_retry
......@@ -54,7 +55,8 @@ def ProvisionDevices(options):
if options.device is not None:
devices = [options.device]
else:
devices = android_commands.GetAttachedDevices()
devices = adb_wrapper.AdbWrapper.Devices(
filters=device_filter.DefaultFilters())
parallel_devices = device_utils.DeviceUtils.parallel(devices)
parallel_devices.pMap(ProvisionDevice, options)
......@@ -196,7 +198,8 @@ def _ConfigureLocalProperties(device, java_debug=True):
'ro.setupwizard.mode=DISABLED',
]
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')
try:
device.WriteFile(
......
......@@ -17,6 +17,7 @@ from pylib import cmd_helper
from pylib import constants
from pylib.device import decorators
from pylib.device import device_errors
from pylib.device import device_filter
from pylib.utils import timeout_retry
......@@ -158,12 +159,23 @@ class AdbWrapper(object):
cls._RunAdbCmd(['start-server'], timeout=timeout, retries=retries,
cpu_affinity=0)
# TODO(craigdh): Determine the filter criteria that should be supported.
@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.
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.
retries: (optional) Number of retries to attempt.
......@@ -171,9 +183,18 @@ class AdbWrapper(object):
AdbWrapper instances.
"""
output = cls._RunAdbCmd(['devices'], timeout=timeout, retries=retries)
lines = [line.split() for line in output.splitlines()]
return [AdbWrapper(line[0]) for line in lines
if len(line) == 2 and line[1] == 'device']
lines = (line.split() for line in output.splitlines())
devices = (AdbWrapper(line[0]) for line in lines if len(line) == 2)
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):
"""Gets the device serial number associated with this object.
......
......@@ -16,7 +16,7 @@ from pylib.device import device_errors
class TestAdbWrapper(unittest.TestCase):
def setUp(self):
devices = adb_wrapper.AdbWrapper.GetDevices()
devices = adb_wrapper.AdbWrapper.Devices()
assert devices, 'A device must be attached'
self._adb = devices[0]
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
from pylib.device import decorators
from pylib.device import device_blacklist
from pylib.device import device_errors
from pylib.device import device_filter
from pylib.device import intent
from pylib.device import logcat_monitor
from pylib.device.commands import install_commands
......@@ -1559,9 +1560,8 @@ class DeviceUtils(object):
A Parallelizer operating over |devices|.
"""
if not devices:
blacklist = device_blacklist.ReadBlacklist()
devices = [d for d in adb_wrapper.AdbWrapper.GetDevices()
if d.GetDeviceSerial() not in blacklist]
devices = adb_wrapper.AdbWrapper.Devices(
filters=device_filter.DefaultFilters())
if not devices:
raise device_errors.NoDevicesError()
......
......@@ -1551,9 +1551,10 @@ class DeviceUtilsParallelTest(mock_calls.TestCase):
def testParallel_default(self):
test_serials = ['0123456789abcdef', 'fedcba9876543210']
with self.assertCall(
mock.call.pylib.device.adb_wrapper.AdbWrapper.GetDevices(),
[_AdbWrapperMock(serial) for serial in test_serials]):
with self.assertCalls(
(mock.call.pylib.device.device_filter.DefaultFilters(), None),
(mock.call.pylib.device.adb_wrapper.AdbWrapper.Devices(filters=None),
[_AdbWrapperMock(serial) for serial in test_serials])):
parallel_devices = device_utils.DeviceUtils.parallel()
for serial, device in zip(test_serials, parallel_devices.pGet(None)):
self.assertTrue(
......@@ -1562,8 +1563,10 @@ class DeviceUtilsParallelTest(mock_calls.TestCase):
'Expected a DeviceUtils object with serial %s' % serial)
def testParallel_noDevices(self):
with self.assertCall(
mock.call.pylib.device.adb_wrapper.AdbWrapper.GetDevices(), []):
with self.assertCalls(
(mock.call.pylib.device.device_filter.DefaultFilters(), None),
(mock.call.pylib.device.adb_wrapper.AdbWrapper.Devices(filters=None),
[])):
with self.assertRaises(device_errors.NoDevicesError):
device_utils.DeviceUtils.parallel()
......
......@@ -5,6 +5,7 @@
from pylib.base import environment
from pylib.device import adb_wrapper
from pylib.device import device_errors
from pylib.device import device_filter
from pylib.device import device_utils
from pylib.utils import parallelizer
......@@ -20,8 +21,8 @@ class LocalDeviceEnvironment(environment.Environment):
#override
def SetUp(self):
# TODO(jbudorick): This can be refined to support filters etc.
available_devices = adb_wrapper.AdbWrapper.GetDevices()
available_devices = adb_wrapper.AdbWrapper.Devices(
filters=device_filter.DefaultFilters())
if not available_devices:
raise device_errors.NoDevicesError
if self._device:
......
......@@ -11,8 +11,9 @@ import optparse
import os
import sys
from pylib import android_commands
from pylib import screenshot
from pylib.device import adb_wrapper
from pylib.device import device_filter
from pylib.device import device_utils
def _PrintMessage(heading, eol='\n'):
......@@ -69,7 +70,8 @@ def main():
if options.verbose:
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:
parser.error('Multiple devices are attached. '
......
......@@ -19,8 +19,9 @@ import subprocess
import sys
import optparse
from pylib import android_commands
from pylib.device import adb_wrapper
from pylib.device import device_errors
from pylib.device import device_filter
from pylib.device import device_utils
from pylib.utils import run_tests_helper
......@@ -235,14 +236,15 @@ def main():
if options.device:
devices = [options.device]
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
# used for the first time in a multithreaded environment.
# http://bugs.python.org/issue7980
tombstones = []
for device_serial in devices:
device = device_utils.DeviceUtils(device_serial)
for adb in devices:
device = device_utils.DeviceUtils(adb)
tombstones += _GetTombstonesForDevice(device, options)
_ResolveTombstones(options.jobs, tombstones)
......
......@@ -12,7 +12,8 @@ import shutil
import sys
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
def _SaveAppData(device, package_name, from_apk=None, data_dir=None):
......@@ -108,7 +109,8 @@ def main():
parser.print_help(sys.stderr)
parser.error('Unknown arguments: %s.' % args)
devices = android_commands.GetAttachedDevices()
devices = adb_wrapper.AdbWrapper.Devices(
filters=device_filter.DefaultFilters())
if len(devices) != 1:
parser.error('Exactly 1 device must be attached.')
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