Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3944)

Unified Diff: build/android/provision_devices.py

Issue 1060943002: [Android] Add severable device provisioning. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: build/android/provision_devices.py
diff --git a/build/android/provision_devices.py b/build/android/provision_devices.py
index ad51f039e068af625cd8035e7369b56ed4ada743..049726426ab8e989a4d4fb14b3f7ff3802982d34 100755
--- a/build/android/provision_devices.py
+++ b/build/android/provision_devices.py
@@ -42,91 +42,60 @@ class _DEFAULT_TIMEOUTS(object):
HELP_TEXT = '{}s on L, {}s on pre-L'.format(LOLLIPOP, PRE_LOLLIPOP)
-def KillHostHeartbeat():
- ps = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
- stdout, _ = ps.communicate()
- matches = re.findall('\\n.*host_heartbeat.*', stdout)
- for match in matches:
- logging.info('An instance of host heart beart running... will kill')
- pid = re.findall(r'(\S+)', match)[1]
- subprocess.call(['kill', str(pid)])
+class _PHASES(object):
+ WIPE = 'wipe'
+ PROPERTIES = 'properties'
+ FINISH = 'finish'
+ ALL = [WIPE, PROPERTIES, FINISH]
-def LaunchHostHeartbeat():
- # Kill if existing host_heartbeat
- KillHostHeartbeat()
- # Launch a new host_heartbeat
- logging.info('Spawning host heartbeat...')
- subprocess.Popen([os.path.join(constants.DIR_SOURCE_ROOT,
- 'build/android/host_heartbeat.py')])
+def ProvisionDevices(options):
+ if options.device is not None:
+ devices = [options.device]
navabi 2015/04/07 20:52:37 This hasn't changed right? Only moved in the scrip
jbudorick 2015/04/08 01:32:56 Yep, moved from (1)
+ else:
+ devices = android_commands.GetAttachedDevices()
-def PushAndLaunchAdbReboot(device, target):
- """Pushes and launches the adb_reboot binary on the device.
+ parallel_devices = device_utils.DeviceUtils.parallel(devices)
+ parallel_devices.pMap(ProvisionDevice, options)
+ if options.auto_reconnect:
+ _LaunchHostHeartbeat()
+ blacklist = device_blacklist.ReadBlacklist()
+ if all(d in blacklist for d in devices):
+ raise device_errors.NoDevicesError
+ return 0
- Arguments:
- device: The DeviceUtils instance for the device to which the adb_reboot
- binary should be pushed.
- target: The build target (example, Debug or Release) which helps in
- locating the adb_reboot binary.
- """
- logging.info('Will push and launch adb_reboot on %s' % str(device))
- # Kill if adb_reboot is already running.
- try:
- # Don't try to kill adb_reboot more than once. We don't expect it to be
- # running at all.
- device.KillAll('adb_reboot', blocking=True, timeout=2, retries=0)
- except device_errors.CommandFailedError:
- # We can safely ignore the exception because we don't expect adb_reboot
- # to be running.
- pass
- # Push adb_reboot
- logging.info(' Pushing adb_reboot ...')
- adb_reboot = os.path.join(constants.DIR_SOURCE_ROOT,
- 'out/%s/adb_reboot' % target)
- device.PushChangedFiles([(adb_reboot, '/data/local/tmp/')])
- # Launch adb_reboot
- logging.info(' Launching adb_reboot ...')
- device.RunShellCommand([
- device.GetDevicePieWrapper(),
- '/data/local/tmp/adb_reboot'])
+def ProvisionDevice(device, options):
+ if options.reboot_timeout:
+ reboot_timeout = options.reboot_timeout
+ elif (device.build_version_sdk >=
+ constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP):
+ reboot_timeout = _DEFAULT_TIMEOUTS.LOLLIPOP
+ else:
+ reboot_timeout = _DEFAULT_TIMEOUTS.PRE_LOLLIPOP
-def _ConfigureLocalProperties(device, java_debug=True):
- """Set standard readonly testing device properties prior to reboot."""
- local_props = [
- 'persist.sys.usb.config=adb',
- 'ro.monkey=1',
- 'ro.test_harness=1',
- 'ro.audio.silent=1',
- 'ro.setupwizard.mode=DISABLED',
- ]
- if java_debug:
- local_props.append('%s=all' % android_commands.JAVA_ASSERT_PROPERTY)
- local_props.append('debug.checkjni=1')
- try:
- device.WriteFile(
- constants.DEVICE_LOCAL_PROPERTIES_PATH,
- '\n'.join(local_props), as_root=True)
- # Android will not respect the local props file if it is world writable.
- device.RunShellCommand(
- ['chmod', '644', constants.DEVICE_LOCAL_PROPERTIES_PATH],
- as_root=True)
- except device_errors.CommandFailedError as e:
- logging.warning(str(e))
+ def should_run_phase(phase_name):
+ return not options.phases or phase_name in options.phases
- # LOCAL_PROPERTIES_PATH = '/data/local.prop'
+ def run_phase(phase_func, reboot=True):
+ device.WaitUntilFullyBooted(timeout=reboot_timeout)
+ phase_func(device, options)
+ if reboot:
+ device.Reboot(False, retries=0)
jbudorick 2015/04/08 19:44:35 (3): both reboots are done here.
+ device.adb.WaitForDevice()
-def WriteAdbKeysFile(device, adb_keys_string):
- dir_path = posixpath.dirname(constants.ADB_KEYS_FILE)
- device.RunShellCommand('mkdir -p %s' % dir_path, as_root=True)
- device.RunShellCommand('restorecon %s' % dir_path, as_root=True)
- device.WriteFile(constants.ADB_KEYS_FILE, adb_keys_string, as_root=True)
- device.RunShellCommand('restorecon %s' % constants.ADB_KEYS_FILE,
- as_root=True)
+ if should_run_phase(_PHASES.WIPE):
+ run_phase(WipeDevice)
+
+ if should_run_phase(_PHASES.PROPERTIES):
+ run_phase(SetProperties)
jbudorick 2015/04/08 19:44:35 (2): The reboot that previously preceded setting t
+ if should_run_phase(_PHASES.FINISH):
+ run_phase(FinishProvisioning, reboot=False)
-def WipeDeviceData(device, options):
+
+def WipeDevice(device, options):
"""Wipes data from device, keeping only the adb_keys for authorization.
After wiping data on a device that has been authorized, adb can still
@@ -138,11 +107,22 @@ def WipeDeviceData(device, options):
Arguments:
device: the device to wipe
"""
+ if options.skip_wipe:
+ return
+
+ device.EnableRoot()
device_authorized = device.FileExists(constants.ADB_KEYS_FILE)
if device_authorized:
adb_keys = device.ReadFile(constants.ADB_KEYS_FILE,
as_root=True).splitlines()
- device.RunShellCommand('wipe data', as_root=True)
+ try:
+ device.RunShellCommand(['wipe', 'data'],
+ as_root=True, check_return=True, retries=0)
+ except device_errors.CommandFailedError:
+ logging.exception('Possible failure while wiping the device. '
+ 'Attempting to continue.')
+ device.adb.WaitForDevice()
+
if device_authorized:
adb_keys_set = set(adb_keys)
for adb_key_file in options.adb_key_files or []:
@@ -152,64 +132,82 @@ def WipeDeviceData(device, options):
adb_keys_set.update(adb_public_keys)
except IOError:
logging.warning('Unable to find adb keys file %s.' % adb_key_file)
- WriteAdbKeysFile(device, '\n'.join(adb_keys_set))
+ _WriteAdbKeysFile(device, '\n'.join(adb_keys_set))
+
+
+def _WriteAdbKeysFile(device, adb_keys_string):
+ dir_path = posixpath.dirname(constants.ADB_KEYS_FILE)
+ device.RunShellCommand(['mkdir', '-p', dir_path],
+ as_root=True, check_return=True)
+ device.RunShellCommand(['restorecon', dir_path],
+ as_root=True, check_return=True)
+ device.WriteFile(constants.ADB_KEYS_FILE, adb_keys_string, as_root=True)
+ device.RunShellCommand(['restorecon', constants.ADB_KEYS_FILE],
+ as_root=True, check_return=True)
-def WipeDeviceIfPossible(device, timeout, options):
+def SetProperties(device, options):
try:
device.EnableRoot()
- WipeDeviceData(device, options)
- device.Reboot(True, timeout=timeout, retries=0)
- except (errors.DeviceUnresponsiveError, device_errors.CommandFailedError):
- pass
-
+ except device_errors.CommandFailedError as e:
+ logging.warning(str(e))
-def ProvisionDevice(device, options):
- if options.reboot_timeout:
- reboot_timeout = options.reboot_timeout
- elif (device.build_version_sdk >=
- constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP):
- reboot_timeout = _DEFAULT_TIMEOUTS.LOLLIPOP
+ _ConfigureLocalProperties(device, options.enable_java_debug)
+ device_settings.ConfigureContentSettings(
+ device, device_settings.DETERMINISTIC_DEVICE_SETTINGS)
+ if options.disable_location:
+ device_settings.ConfigureContentSettings(
+ device, device_settings.DISABLE_LOCATION_SETTINGS)
else:
- reboot_timeout = _DEFAULT_TIMEOUTS.PRE_LOLLIPOP
+ device_settings.ConfigureContentSettings(
+ device, device_settings.ENABLE_LOCATION_SETTINGS)
+ device_settings.SetLockScreenSettings(device)
+ if options.disable_network:
+ device_settings.ConfigureContentSettings(
+ device, device_settings.NETWORK_DISABLED_SETTINGS)
- try:
- if not options.skip_wipe:
- WipeDeviceIfPossible(device, reboot_timeout, options)
+ if options.min_battery_level is not None:
try:
- device.EnableRoot()
+ battery = battery_utils.BatteryUtils(device)
+ battery.ChargeDeviceToLevel(options.min_battery_level)
except device_errors.CommandFailedError as e:
- logging.warning(str(e))
- _ConfigureLocalProperties(device, options.enable_java_debug)
- device_settings.ConfigureContentSettings(
- device, device_settings.DETERMINISTIC_DEVICE_SETTINGS)
- if options.disable_location:
- device_settings.ConfigureContentSettings(
- device, device_settings.DISABLE_LOCATION_SETTINGS)
- else:
- device_settings.ConfigureContentSettings(
- device, device_settings.ENABLE_LOCATION_SETTINGS)
- device_settings.SetLockScreenSettings(device)
- if options.disable_network:
- device_settings.ConfigureContentSettings(
- device, device_settings.NETWORK_DISABLED_SETTINGS)
- if options.min_battery_level is not None:
- try:
- battery = battery_utils.BatteryUtils(device)
- battery.ChargeDeviceToLevel(options.min_battery_level)
- except device_errors.CommandFailedError as e:
- logging.exception('Unable to charge device to specified level.')
-
- if not options.skip_wipe:
- device.Reboot(True, timeout=reboot_timeout, retries=0)
navabi 2015/04/07 20:52:37 we reboot after setting all the properties so they
jbudorick 2015/04/08 01:32:56 Yeah, this ordering was maintained in the new vers
navabi 2015/04/08 19:39:27 Hmm. Maybe I'm just not seeing something obvious,
jbudorick 2015/04/08 19:44:35 I broke this into phases around the reboots, i.e.,
navabi 2015/04/08 19:48:47 Ah ha!! Reboot at the end of every run_phase. I li
- device.RunShellCommand('date -s %s' % time.strftime('%Y%m%d.%H%M%S',
- time.gmtime()),
- as_root=True)
- props = device.RunShellCommand('getprop')
+ logging.exception('Unable to charge device to specified level.')
+
+
+def _ConfigureLocalProperties(device, java_debug=True):
+ """Set standard readonly testing device properties prior to reboot."""
+ local_props = [
+ 'persist.sys.usb.config=adb',
+ 'ro.monkey=1',
+ 'ro.test_harness=1',
+ 'ro.audio.silent=1',
+ 'ro.setupwizard.mode=DISABLED',
+ ]
+ if java_debug:
+ local_props.append('%s=all' % android_commands.JAVA_ASSERT_PROPERTY)
+ local_props.append('debug.checkjni=1')
+ try:
+ device.WriteFile(
+ constants.DEVICE_LOCAL_PROPERTIES_PATH,
+ '\n'.join(local_props), as_root=True)
+ # Android will not respect the local props file if it is world writable.
+ device.RunShellCommand(
+ ['chmod', '644', constants.DEVICE_LOCAL_PROPERTIES_PATH],
+ as_root=True, check_return=True)
+ except device_errors.CommandFailedError as e:
+ logging.warning(str(e))
+
+
+def FinishProvisioning(device, options):
+ try:
+ device.RunShellCommand(
navabi 2015/04/07 20:52:37 Do we reboot before setting the date? I think I re
jbudorick 2015/04/08 01:32:57 This does set the date after rebooting.
+ ['date', '-s', time.strftime('%Y%m%d.%H%M%S', time.gmtime())],
+ as_root=True, check_return=True)
+ props = device.RunShellCommand('getprop', check_return=True)
for prop in props:
logging.info(' %s' % prop)
if options.auto_reconnect:
- PushAndLaunchAdbReboot(device, options.target)
+ _PushAndLaunchAdbReboot(device, options.target)
except (errors.WaitForResponseTimedOutError,
device_errors.CommandTimeoutError):
logging.info('Timed out waiting for device %s. Adding to blacklist.',
@@ -222,28 +220,57 @@ def ProvisionDevice(device, options):
device_blacklist.ExtendBlacklist([str(device)])
-def ProvisionDevices(options):
- if options.device is not None:
- devices = [options.device]
jbudorick 2015/04/08 01:32:56 (1)
- else:
- devices = android_commands.GetAttachedDevices()
+def _PushAndLaunchAdbReboot(device, target):
+ """Pushes and launches the adb_reboot binary on the device.
- parallel_devices = device_utils.DeviceUtils.parallel(devices)
- parallel_devices.pMap(ProvisionDevice, options)
- if options.auto_reconnect:
- LaunchHostHeartbeat()
- blacklist = device_blacklist.ReadBlacklist()
- if all(d in blacklist for d in devices):
- raise device_errors.NoDevicesError
- return 0
+ Arguments:
+ device: The DeviceUtils instance for the device to which the adb_reboot
+ binary should be pushed.
+ target: The build target (example, Debug or Release) which helps in
+ locating the adb_reboot binary.
+ """
+ logging.info('Will push and launch adb_reboot on %s' % str(device))
+ # Kill if adb_reboot is already running.
+ try:
+ # Don't try to kill adb_reboot more than once. We don't expect it to be
+ # running at all.
+ device.KillAll('adb_reboot', blocking=True, timeout=2, retries=0)
+ except device_errors.CommandFailedError:
+ # We can safely ignore the exception because we don't expect adb_reboot
+ # to be running.
+ pass
+ # Push adb_reboot
+ logging.info(' Pushing adb_reboot ...')
+ adb_reboot = os.path.join(constants.DIR_SOURCE_ROOT,
+ 'out/%s/adb_reboot' % target)
+ device.PushChangedFiles([(adb_reboot, '/data/local/tmp/')])
+ # Launch adb_reboot
+ logging.info(' Launching adb_reboot ...')
+ device.RunShellCommand(
+ [device.GetDevicePieWrapper(), '/data/local/tmp/adb_reboot'],
+ check_return=True)
-def main():
- custom_handler = logging.StreamHandler(sys.stdout)
- custom_handler.setFormatter(run_tests_helper.CustomFormatter())
- logging.getLogger().addHandler(custom_handler)
- logging.getLogger().setLevel(logging.INFO)
+def _LaunchHostHeartbeat():
+ # Kill if existing host_heartbeat
+ _KillHostHeartbeat()
+ # Launch a new host_heartbeat
+ logging.info('Spawning host heartbeat...')
+ subprocess.Popen([os.path.join(constants.DIR_SOURCE_ROOT,
+ 'build/android/host_heartbeat.py')])
+
+def _KillHostHeartbeat():
+ ps = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
+ stdout, _ = ps.communicate()
+ matches = re.findall('\\n.*host_heartbeat.*', stdout)
+ for match in matches:
+ logging.info('An instance of host heart beart running... will kill')
+ pid = re.findall(r'(\S+)', match)[1]
+ subprocess.call(['kill', str(pid)])
+
+
+def main():
# Recommended options on perf bots:
# --disable-network
# TODO(tonyg): We eventually want network on. However, currently radios
@@ -258,6 +285,10 @@ def main():
parser.add_argument('-d', '--device', metavar='SERIAL',
help='the serial number of the device to be provisioned'
' (the default is to provision all devices attached)')
+ parser.add_argument('--phase', action='append', choices=_PHASES.ALL,
+ dest='phases',
+ help='Phases of provisioning to run. '
+ '(If omitted, all phases will be run.)')
parser.add_argument('--skip-wipe', action='store_true', default=False,
help="don't wipe device data during provisioning")
parser.add_argument('--reboot-timeout', metavar='SECS', type=int,
@@ -281,9 +312,13 @@ def main():
' disconnections')
parser.add_argument('--adb-key-files', type=str, nargs='+',
help='list of adb keys to push to device')
+ parser.add_argument('-v', '--verbose', action='count', default=1,
+ help='Log more information.')
args = parser.parse_args()
constants.SetBuildType(args.target)
+ run_tests_helper.SetLogLevel(args.verbose)
+
return ProvisionDevices(args)
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698