# Copyright (c) 2012 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. """Provides an interface to communicate with the device via the adb command. Assumes adb binary is currently on system path. """ import collections import datetime import logging import os import re import shlex import subprocess import sys import tempfile import time import cmd_helper import constants import io_stats_parser try: import pexpect except: pexpect = None sys.path.append(os.path.join( constants.DIR_SOURCE_ROOT, 'third_party', 'android_testrunner')) import adb_interface import am_instrument_parser import errors # Pattern to search for the next whole line of pexpect output and capture it # into a match group. We can't use ^ and $ for line start end with pexpect, # see http://www.noah.org/python/pexpect/#doc for explanation why. PEXPECT_LINE_RE = re.compile('\n([^\r]*)\r') # Set the adb shell prompt to be a unique marker that will [hopefully] not # appear at the start of any line of a command's output. SHELL_PROMPT = '~+~PQ\x17RS~+~' # Java properties file LOCAL_PROPERTIES_PATH = '/data/local.prop' # Property in /data/local.prop that controls Java assertions. JAVA_ASSERT_PROPERTY = 'dalvik.vm.enableassertions' MEMORY_INFO_RE = re.compile('^(?P\w+):\s+(?P\d+) kB$') NVIDIA_MEMORY_INFO_RE = re.compile('^\s*(?P\S+)\s*(?P\S+)\s*' '(?P\d+)\s*(?P\d+)$') # Keycode "enum" suitable for passing to AndroidCommands.SendKey(). KEYCODE_HOME = 3 KEYCODE_BACK = 4 KEYCODE_DPAD_UP = 19 KEYCODE_DPAD_DOWN = 20 KEYCODE_DPAD_RIGHT = 22 KEYCODE_ENTER = 66 KEYCODE_MENU = 82 MD5SUM_DEVICE_FOLDER = constants.TEST_EXECUTABLE_DIR + '/md5sum/' MD5SUM_DEVICE_PATH = MD5SUM_DEVICE_FOLDER + 'md5sum_bin' MD5SUM_LD_LIBRARY_PATH = 'LD_LIBRARY_PATH=%s' % MD5SUM_DEVICE_FOLDER def GetAVDs(): """Returns a list of AVDs.""" re_avd = re.compile('^[ ]+Name: ([a-zA-Z0-9_:.-]+)', re.MULTILINE) avds = re_avd.findall(cmd_helper.GetCmdOutput(['android', 'list', 'avd'])) return avds def GetAttachedDevices(hardware=True, emulator=True, offline=False): """Returns a list of attached, android devices and emulators. If a preferred device has been set with ANDROID_SERIAL, it will be first in the returned list. The arguments specify what devices to include in the list. Example output: * daemon not running. starting it now on port 5037 * * daemon started successfully * List of devices attached 027c10494100b4d7 device emulator-5554 offline Args: hardware: Include attached actual devices that are online. emulator: Include emulators (i.e. AVD's) currently on host. offline: Include devices and emulators that are offline. Returns: List of devices. """ adb_devices_output = cmd_helper.GetCmdOutput([constants.ADB_PATH, 'devices']) re_device = re.compile('^([a-zA-Z0-9_:.-]+)\tdevice$', re.MULTILINE) online_devices = re_device.findall(adb_devices_output) re_device = re.compile('^(emulator-[0-9]+)\tdevice', re.MULTILINE) emulator_devices = re_device.findall(adb_devices_output) re_device = re.compile('^([a-zA-Z0-9_:.-]+)\toffline$', re.MULTILINE) offline_devices = re_device.findall(adb_devices_output) devices = [] # First determine list of online devices (e.g. hardware and/or emulator). if hardware and emulator: devices = online_devices elif hardware: devices = [device for device in online_devices if device not in emulator_devices] elif emulator: devices = emulator_devices # Now add offline devices if offline is true if offline: devices = devices + offline_devices preferred_device = os.environ.get('ANDROID_SERIAL') if preferred_device in devices: devices.remove(preferred_device) devices.insert(0, preferred_device) return devices def IsDeviceAttached(device): """Return true if the device is attached and online.""" return device in GetAttachedDevices() def _GetFilesFromRecursiveLsOutput(path, ls_output, re_file, utc_offset=None): """Gets a list of files from `ls` command output. Python's os.walk isn't used because it doesn't work over adb shell. Args: path: The path to list. ls_output: A list of lines returned by an `ls -lR` command. re_file: A compiled regular expression which parses a line into named groups consisting of at minimum "filename", "date", "time", "size" and optionally "timezone". utc_offset: A 5-character string of the form +HHMM or -HHMM, where HH is a 2-digit string giving the number of UTC offset hours, and MM is a 2-digit string giving the number of UTC offset minutes. If the input utc_offset is None, will try to look for the value of "timezone" if it is specified in re_file. Returns: A dict of {"name": (size, lastmod), ...} where: name: The file name relative to |path|'s directory. size: The file size in bytes (0 for directories). lastmod: The file last modification date in UTC. """ re_directory = re.compile('^%s/(?P[^:]+):$' % re.escape(path)) path_dir = os.path.dirname(path) current_dir = '' files = {} for line in ls_output: directory_match = re_directory.match(line) if directory_match: current_dir = directory_match.group('dir') continue file_match = re_file.match(line) if file_match: filename = os.path.join(current_dir, file_match.group('filename')) if filename.startswith(path_dir): filename = filename[len(path_dir) + 1:] lastmod = datetime.datetime.strptime( file_match.group('date') + ' ' + file_match.group('time')[:5], '%Y-%m-%d %H:%M') if not utc_offset and 'timezone' in re_file.groupindex: utc_offset = file_match.group('timezone') if isinstance(utc_offset, str) and len(utc_offset) == 5: utc_delta = datetime.timedelta(hours=int(utc_offset[1:3]), minutes=int(utc_offset[3:5])) if utc_offset[0:1] == '-': utc_delta = -utc_delta lastmod -= utc_delta files[filename] = (int(file_match.group('size')), lastmod) return files def _ParseMd5SumOutput(md5sum_output): """Returns a list of tuples from the provided md5sum output. Args: md5sum_output: output directly from md5sum binary. Returns: List of namedtuples with attributes |hash| and |path|, where |path| is the absolute path to the file with an Md5Sum of |hash|. """ HashAndPath = collections.namedtuple('HashAndPath', ['hash', 'path']) split_lines = [line.split(' ') for line in md5sum_output] return [HashAndPath._make(s) for s in split_lines if len(s) == 2] def _HasAdbPushSucceeded(command_output): """Returns whether adb push has succeeded from the provided output.""" # TODO(frankf): We should look at the return code instead of the command # output for many of the commands in this file. if not command_output: return True # Success looks like this: "3035 KB/s (12512056 bytes in 4.025s)" # Errors look like this: "failed to copy ... " if not re.search('^[0-9]', command_output.splitlines()[-1]): logging.critical('PUSH FAILED: ' + command_output) return False return True def GetLogTimestamp(log_line, year): """Returns the timestamp of the given |log_line| in the given year.""" try: return datetime.datetime.strptime('%s-%s' % (year, log_line[:18]), '%Y-%m-%d %H:%M:%S.%f') except (ValueError, IndexError): logging.critical('Error reading timestamp from ' + log_line) return None class AndroidCommands(object): """Helper class for communicating with Android device via adb. Args: device: If given, adb commands are only send to the device of this ID. Otherwise commands are sent to all attached devices. """ def __init__(self, device=None): adb_dir = os.path.dirname(constants.ADB_PATH) if adb_dir and adb_dir not in os.environ['PATH'].split(os.pathsep): # Required by third_party/android_testrunner to call directly 'adb'. os.environ['PATH'] += os.pathsep + adb_dir self._adb = adb_interface.AdbInterface() if device: self._adb.SetTargetSerial(device) self._device = device self._logcat = None self.logcat_process = None self._logcat_tmpoutfile = None self._pushed_files = [] self._device_utc_offset = None self._potential_push_size = 0 self._actual_push_size = 0 self._md5sum_build_dir = '' self._external_storage = '' self._util_wrapper = '' def _LogShell(self, cmd): """Logs the adb shell command.""" if self._device: device_repr = self._device[-4:] else: device_repr = '????' logging.info('[%s]> %s', device_repr, cmd) def Adb(self): """Returns our AdbInterface to avoid us wrapping all its methods.""" return self._adb def GetDevice(self): """Returns the device serial.""" return self._device def IsOnline(self): """Checks whether the device is online. Returns: True if device is in 'device' mode, False otherwise. """ out = self._adb.SendCommand('get-state') return out.strip() == 'device' def IsRootEnabled(self): """Checks if root is enabled on the device.""" root_test_output = self.RunShellCommand('ls /root') or [''] return not 'Permission denied' in root_test_output[0] def EnableAdbRoot(self): """Enables adb root on the device. Returns: True: if output from executing adb root was as expected. False: otherwise. """ if self.GetBuildType() == 'user': logging.warning("Can't enable root in production builds with type user") return False else: return_value = self._adb.EnableAdbRoot() # EnableAdbRoot inserts a call for wait-for-device only when adb logcat # output matches what is expected. Just to be safe add a call to # wait-for-device. self._adb.SendCommand('wait-for-device') return return_value def GetDeviceYear(self): """Returns the year information of the date on device.""" return self.RunShellCommand('date +%Y')[0] def GetExternalStorage(self): if not self._external_storage: self._external_storage = self.RunShellCommand('echo $EXTERNAL_STORAGE')[0] assert self._external_storage, 'Unable to find $EXTERNAL_STORAGE' return self._external_storage def WaitForDevicePm(self): """Blocks until the device's package manager is available. To workaround http://b/5201039, we restart the shell and retry if the package manager isn't back after 120 seconds. Raises: errors.WaitForResponseTimedOutError after max retries reached. """ last_err = None retries = 3 while retries: try: self._adb.WaitForDevicePm() return # Success except errors.WaitForResponseTimedOutError as e: last_err = e logging.warning('Restarting and retrying after timeout: %s', e) retries -= 1 self.RestartShell() raise last_err # Only reached after max retries, re-raise the last error. def RestartShell(self): """Restarts the shell on the device. Does not block for it to return.""" self.RunShellCommand('stop') self.RunShellCommand('start') def Reboot(self, full_reboot=True): """Reboots the device and waits for the package manager to return. Args: full_reboot: Whether to fully reboot the device or just restart the shell. """ # TODO(torne): hive can't reboot the device either way without breaking the # connection; work out if we can handle this better if os.environ.get('USING_HIVE'): logging.warning('Ignoring reboot request as we are on hive') return if full_reboot or not self.IsRootEnabled(): self._adb.SendCommand('reboot') timeout = 300 retries = 1 # Wait for the device to disappear. while retries < 10 and self.IsOnline(): time.sleep(1) retries += 1 else: self.RestartShell() timeout = 120 # To run tests we need at least the package manager and the sd card (or # other external storage) to be ready. self.WaitForDevicePm() self.WaitForSdCardReady(timeout) def Shutdown(self): """Shuts down the device.""" self._adb.SendCommand('reboot -p') def Uninstall(self, package): """Uninstalls the specified package from the device. Args: package: Name of the package to remove. Returns: A status string returned by adb uninstall """ uninstall_command = 'uninstall %s' % package self._LogShell(uninstall_command) return self._adb.SendCommand(uninstall_command, timeout_time=60) def Install(self, package_file_path, reinstall=False): """Installs the specified package to the device. Args: package_file_path: Path to .apk file to install. reinstall: Reinstall an existing apk, keeping the data. Returns: A status string returned by adb install """ assert os.path.isfile(package_file_path), ('<%s> is not file' % package_file_path) install_cmd = ['install'] if reinstall: install_cmd.append('-r') install_cmd.append(package_file_path) install_cmd = ' '.join(install_cmd) self._LogShell(install_cmd) return self._adb.SendCommand(install_cmd, timeout_time=2 * 60, retry_count=0) def ManagedInstall(self, apk_path, keep_data=False, package_name=None, reboots_on_failure=2): """Installs specified package and reboots device on timeouts. If package_name is supplied, checks if the package is already installed and doesn't reinstall if the apk md5sums match. Args: apk_path: Path to .apk file to install. keep_data: Reinstalls instead of uninstalling first, preserving the application data. package_name: Package name (only needed if keep_data=False). reboots_on_failure: number of time to reboot if package manager is frozen. """ # Check if package is already installed and up to date. if package_name: installed_apk_path = self.GetApplicationPath(package_name) if (installed_apk_path and not self.GetFilesChanged(apk_path, installed_apk_path)): logging.info('Skipped install: identical %s APK already installed' % package_name) return # Install. reboots_left = reboots_on_failure while True: try: if not keep_data: assert package_name self.Uninstall(package_name) install_status = self.Install(apk_path, reinstall=keep_data) if 'Success' in install_status: return except errors.WaitForResponseTimedOutError: print '@@@STEP_WARNINGS@@@' logging.info('Timeout on installing %s on device %s', apk_path, self._device) if reboots_left <= 0: raise Exception('Install failure') # Force a hard reboot on last attempt self.Reboot(full_reboot=(reboots_left == 1)) reboots_left -= 1 def MakeSystemFolderWritable(self): """Remounts the /system folder rw.""" out = self._adb.SendCommand('remount') if out.strip() != 'remount succeeded': raise errors.MsgException('Remount failed: %s' % out) def RestartAdbServer(self): """Restart the adb server.""" self.KillAdbServer() self.StartAdbServer() def KillAdbServer(self): """Kill adb server.""" adb_cmd = [constants.ADB_PATH, 'kill-server'] return cmd_helper.RunCmd(adb_cmd) def StartAdbServer(self): """Start adb server.""" adb_cmd = [constants.ADB_PATH, 'start-server'] return cmd_helper.RunCmd(adb_cmd) def WaitForSystemBootCompleted(self, wait_time): """Waits for targeted system's boot_completed flag to be set. Args: wait_time: time in seconds to wait Raises: WaitForResponseTimedOutError if wait_time elapses and flag still not set. """ logging.info('Waiting for system boot completed...') self._adb.SendCommand('wait-for-device') # Now the device is there, but system not boot completed. # Query the sys.boot_completed flag with a basic command boot_completed = False attempts = 0 wait_period = 5 while not boot_completed and (attempts * wait_period) < wait_time: output = self._adb.SendShellCommand('getprop sys.boot_completed', retry_count=1) output = output.strip() if output == '1': boot_completed = True else: # If 'error: xxx' returned when querying the flag, it means # adb server lost the connection to the emulator, so restart the adb # server. if 'error:' in output: self.RestartAdbServer() time.sleep(wait_period) attempts += 1 if not boot_completed: raise errors.WaitForResponseTimedOutError( 'sys.boot_completed flag was not set after %s seconds' % wait_time) def WaitForSdCardReady(self, timeout_time): """Wait for the SD card ready before pushing data into it.""" logging.info('Waiting for SD card ready...') sdcard_ready = False attempts = 0 wait_period = 5 external_storage = self.GetExternalStorage() while not sdcard_ready and attempts * wait_period < timeout_time: output = self.RunShellCommand('ls ' + external_storage) if output: sdcard_ready = True else: time.sleep(wait_period) attempts += 1 if not sdcard_ready: raise errors.WaitForResponseTimedOutError( 'SD card not ready after %s seconds' % timeout_time) # It is tempting to turn this function into a generator, however this is not # possible without using a private (local) adb_shell instance (to ensure no # other command interleaves usage of it), which would defeat the main aim of # being able to reuse the adb shell instance across commands. def RunShellCommand(self, command, timeout_time=20, log_result=False): """Send a command to the adb shell and return the result. Args: command: String containing the shell command to send. Must not include the single quotes as we use them to escape the whole command. timeout_time: Number of seconds to wait for command to respond before retrying, used by AdbInterface.SendShellCommand. log_result: Boolean to indicate whether we should log the result of the shell command. Returns: list containing the lines of output received from running the command """ self._LogShell(command) if "'" in command: logging.warning(command + " contains ' quotes") result = self._adb.SendShellCommand( "'%s'" % command, timeout_time).splitlines() if ['error: device not found'] == result: raise errors.DeviceUnresponsiveError('device not found') if log_result: self._LogShell('\n'.join(result)) return result def GetShellCommandStatusAndOutput(self, command, timeout_time=20, log_result=False): """See RunShellCommand() above. Returns: The tuple (exit code, list of output lines). """ lines = self.RunShellCommand( command + '; echo %$?', timeout_time, log_result) last_line = lines[-1] status_pos = last_line.rfind('%') assert status_pos >= 0 status = int(last_line[status_pos + 1:]) if status_pos == 0: lines = lines[:-1] else: lines = lines[:-1] + [last_line[:status_pos]] return (status, lines) def KillAll(self, process): """Android version of killall, connected via adb. Args: process: name of the process to kill off Returns: the number of processes killed """ pids = self.ExtractPid(process) if pids: self.RunShellCommand('kill -9 ' + ' '.join(pids)) return len(pids) def KillAllBlocking(self, process, timeout_sec): """Blocking version of killall, connected via adb. This waits until no process matching the corresponding name appears in ps' output anymore. Args: process: name of the process to kill off timeout_sec: the timeout in seconds Returns: the number of processes killed """ processes_killed = self.KillAll(process) if processes_killed: elapsed = 0 wait_period = 0.1 # Note that this doesn't take into account the time spent in ExtractPid(). while self.ExtractPid(process) and elapsed < timeout_sec: time.sleep(wait_period) elapsed += wait_period if elapsed >= timeout_sec: return 0 return processes_killed def _GetActivityCommand(self, package, activity, wait_for_completion, action, category, data, extras, trace_file_name, force_stop): """Creates command to start |package|'s activity on the device. Args - as for StartActivity Returns: the command to run on the target to start the activity """ cmd = 'am start -a %s' % action if force_stop: cmd += ' -S' if wait_for_completion: cmd += ' -W' if category: cmd += ' -c %s' % category if package and activity: cmd += ' -n %s/%s' % (package, activity) if data: cmd += ' -d "%s"' % data if extras: for key in extras: value = extras[key] if isinstance(value, str): cmd += ' --es' elif isinstance(value, bool): cmd += ' --ez' elif isinstance(value, int): cmd += ' --ei' else: raise NotImplementedError( 'Need to teach StartActivity how to pass %s extras' % type(value)) cmd += ' %s %s' % (key, value) if trace_file_name: cmd += ' --start-profiler ' + trace_file_name return cmd def StartActivity(self, package, activity, wait_for_completion=False, action='android.intent.action.VIEW', category=None, data=None, extras=None, trace_file_name=None, force_stop=False): """Starts |package|'s activity on the device. Args: package: Name of package to start (e.g. 'com.google.android.apps.chrome'). activity: Name of activity (e.g. '.Main' or 'com.google.android.apps.chrome.Main'). wait_for_completion: wait for the activity to finish launching (-W flag). action: string (e.g. "android.intent.action.MAIN"). Default is VIEW. category: string (e.g. "android.intent.category.HOME") data: Data string to pass to activity (e.g. 'http://www.example.com/'). extras: Dict of extras to pass to activity. Values are significant. trace_file_name: If used, turns on and saves the trace to this file name. force_stop: force stop the target app before starting the activity (-S flag). """ cmd = self._GetActivityCommand(package, activity, wait_for_completion, action, category, data, extras, trace_file_name, force_stop) self.RunShellCommand(cmd) def StartActivityTimed(self, package, activity, wait_for_completion=False, action='android.intent.action.VIEW', category=None, data=None, extras=None, trace_file_name=None, force_stop=False): """Starts |package|'s activity on the device, returning the start time Args - as for StartActivity Returns: a timestamp string for the time at which the activity started """ cmd = self._GetActivityCommand(package, activity, wait_for_completion, action, category, data, extras, trace_file_name, force_stop) self.StartMonitoringLogcat() self.RunShellCommand('log starting activity; ' + cmd) activity_started_re = re.compile('.*starting activity.*') m = self.WaitForLogMatch(activity_started_re, None) assert m start_line = m.group(0) return GetLogTimestamp(start_line, self.GetDeviceYear()) def GoHome(self): """Tell the device to return to the home screen. Blocks until completion.""" self.RunShellCommand('am start -W ' '-a android.intent.action.MAIN -c android.intent.category.HOME') def CloseApplication(self, package): """Attempt to close down the application, using increasing violence. Args: package: Name of the process to kill off, e.g. com.google.android.apps.chrome """ self.RunShellCommand('am force-stop ' + package) def GetApplicationPath(self, package): """Get the installed apk path on the device for the given package. Args: package: Name of the package. Returns: Path to the apk on the device if it exists, None otherwise. """ pm_path_output = self.RunShellCommand('pm path ' + package) # The path output contains anything if and only if the package # exists. if pm_path_output: # pm_path_output is of the form: "package:/path/to/foo.apk" return pm_path_output[0].split(':')[1] else: return None def ClearApplicationState(self, package): """Closes and clears all state for the given |package|.""" # Check that the package exists before clearing it. Necessary because # calling pm clear on a package that doesn't exist may never return. pm_path_output = self.RunShellCommand('pm path ' + package) # The path output only contains anything if and only if the package exists. if pm_path_output: self.RunShellCommand('pm clear ' + package) def SendKeyEvent(self, keycode): """Sends keycode to the device. Args: keycode: Numeric keycode to send (see "enum" at top of file). """ self.RunShellCommand('input keyevent %d' % keycode) def _RunMd5Sum(self, host_path, device_path): """Gets the md5sum of a host path and device path. Args: host_path: Path (file or directory) on the host. device_path: Path on the device. Returns: A tuple containing lists of the host and device md5sum results as created by _ParseMd5SumOutput(). """ if not self._md5sum_build_dir: default_build_type = os.environ.get('BUILD_TYPE', 'Debug') build_dir = '%s/%s/' % ( cmd_helper.OutDirectory().get(), default_build_type) md5sum_dist_path = '%s/md5sum_dist' % build_dir if not os.path.exists(md5sum_dist_path): build_dir = '%s/Release/' % cmd_helper.OutDirectory().get() md5sum_dist_path = '%s/md5sum_dist' % build_dir assert os.path.exists(md5sum_dist_path), 'Please build md5sum.' command = 'push %s %s' % (md5sum_dist_path, MD5SUM_DEVICE_FOLDER) assert _HasAdbPushSucceeded(self._adb.SendCommand(command)) self._md5sum_build_dir = build_dir cmd = (MD5SUM_LD_LIBRARY_PATH + ' ' + self._util_wrapper + ' ' + MD5SUM_DEVICE_PATH + ' ' + device_path) device_hash_tuples = _ParseMd5SumOutput( self.RunShellCommand(cmd, timeout_time=2 * 60)) assert os.path.exists(host_path), 'Local path not found %s' % host_path md5sum_output = cmd_helper.GetCmdOutput( ['%s/md5sum_bin_host' % self._md5sum_build_dir, host_path]) host_hash_tuples = _ParseMd5SumOutput(md5sum_output.splitlines()) return (host_hash_tuples, device_hash_tuples) def GetFilesChanged(self, host_path, device_path): """Compares the md5sum of a host path against a device path. Note: Ignores extra files on the device. Args: host_path: Path (file or directory) on the host. device_path: Path on the device. Returns: A list of tuples of the form (host_path, device_path) for files whose md5sums do not match. """ host_hash_tuples, device_hash_tuples = self._RunMd5Sum( host_path, device_path) # Ignore extra files on the device. if len(device_hash_tuples) > len(host_hash_tuples): host_files = [os.path.relpath(os.path.normpath(p.path), os.path.normpath(host_path)) for p in host_hash_tuples] def HostHas(fname): return any(path in fname for path in host_files) device_hash_tuples = [h for h in device_hash_tuples if HostHas(h.path)] # Constructs the target device path from a given host path. Don't use when # only a single file is given as the base name given in device_path may # differ from that in host_path. def HostToDevicePath(host_file_path): return os.path.join(os.path.dirname(device_path), os.path.relpath( host_file_path, os.path.dirname(os.path.normpath(host_path)))) device_hashes = [h.hash for h in device_hash_tuples] return [(t.path, HostToDevicePath(t.path) if os.path.isdir(host_path) else device_path) for t in host_hash_tuples if t.hash not in device_hashes] def PushIfNeeded(self, host_path, device_path): """Pushes |host_path| to |device_path|. Works for files and directories. This method skips copying any paths in |test_data_paths| that already exist on the device with the same hash. All pushed files can be removed by calling RemovePushedFiles(). """ MAX_INDIVIDUAL_PUSHES = 50 assert os.path.exists(host_path), 'Local path not found %s' % host_path def GetHostSize(path): return int(cmd_helper.GetCmdOutput(['du', '-sb', path]).split()[0]) size = GetHostSize(host_path) self._pushed_files.append(device_path) self._potential_push_size += size changed_files = self.GetFilesChanged(host_path, device_path) if not changed_files: return def Push(host, device): # NOTE: We can't use adb_interface.Push() because it hardcodes a timeout # of 60 seconds which isn't sufficient for a lot of users of this method. push_command = 'push %s %s' % (host, device) self._LogShell(push_command) # Retry push with increasing backoff if the device is busy. retry = 0 while True: output = self._adb.SendCommand(push_command, timeout_time=30 * 60) if _HasAdbPushSucceeded(output): return if retry < 3: retry += 1 wait_time = 5 * retry logging.error('Push failed, retrying in %d seconds: %s' % (wait_time, output)) time.sleep(wait_time) else: raise Exception('Push failed: %s' % output) diff_size = 0 if len(changed_files) <= MAX_INDIVIDUAL_PUSHES: diff_size = sum(GetHostSize(f[0]) for f in changed_files) # TODO(craigdh): Replace this educated guess with a heuristic that # approximates the push time for each method. if len(changed_files) > MAX_INDIVIDUAL_PUSHES or diff_size > 0.5 * size: # We're pushing everything, remove everything first and then create it. self._actual_push_size += size if os.path.isdir(host_path): self.RunShellCommand('rm -r %s' % device_path, timeout_time=2 * 60) self.RunShellCommand('mkdir -p %s' % device_path) Push(host_path, device_path) else: for f in changed_files: Push(f[0], f[1]) self._actual_push_size += diff_size def GetPushSizeInfo(self): """Get total size of pushes to the device done via PushIfNeeded() Returns: A tuple: 1. Total size of push requests to PushIfNeeded (MB) 2. Total size that was actually pushed (MB) """ return (self._potential_push_size, self._actual_push_size) def GetFileContents(self, filename, log_result=False): """Gets contents from the file specified by |filename|.""" return self.RunShellCommand('cat "%s" 2>/dev/null' % filename, log_result=log_result) def SetFileContents(self, filename, contents): """Writes |contents| to the file specified by |filename|.""" with tempfile.NamedTemporaryFile() as f: f.write(contents) f.flush() self._adb.Push(f.name, filename) _TEMP_FILE_BASE_FMT = 'temp_file_%d' _TEMP_SCRIPT_FILE_BASE_FMT = 'temp_script_file_%d.sh' def _GetDeviceTempFileName(self, base_name): i = 0 while self.FileExistsOnDevice( self.GetExternalStorage() + '/' + base_name % i): i += 1 return self.GetExternalStorage() + '/' + base_name % i def CanAccessProtectedFileContents(self): """Returns True if Get/SetProtectedFileContents would work via "su". Devices running user builds don't have adb root, but may provide "su" which can be used for accessing protected files. """ r = self.RunShellCommand('su -c cat /dev/null') return r == [] or r[0].strip() == '' def GetProtectedFileContents(self, filename, log_result=False): """Gets contents from the protected file specified by |filename|. This is less efficient than GetFileContents, but will work for protected files and device files. """ # Run the script as root return self.RunShellCommand('su -c cat "%s" 2> /dev/null' % filename) def SetProtectedFileContents(self, filename, contents): """Writes |contents| to the protected file specified by |filename|. This is less efficient than SetFileContents, but will work for protected files and device files. """ temp_file = self._GetDeviceTempFileName(AndroidCommands._TEMP_FILE_BASE_FMT) temp_script = self._GetDeviceTempFileName( AndroidCommands._TEMP_SCRIPT_FILE_BASE_FMT) # Put the contents in a temporary file self.SetFileContents(temp_file, contents) # Create a script to copy the file contents to its final destination self.SetFileContents(temp_script, 'cat %s > %s' % (temp_file, filename)) # Run the script as root self.RunShellCommand('su -c sh %s' % temp_script) # And remove the temporary files self.RunShellCommand('rm ' + temp_file) self.RunShellCommand('rm ' + temp_script) def RemovePushedFiles(self): """Removes all files pushed with PushIfNeeded() from the device.""" for p in self._pushed_files: self.RunShellCommand('rm -r %s' % p, timeout_time=2 * 60) def ListPathContents(self, path): """Lists files in all subdirectories of |path|. Args: path: The path to list. Returns: A dict of {"name": (size, lastmod), ...}. """ # Example output: # /foo/bar: # -rw-r----- 1 user group 102 2011-05-12 12:29:54.131623387 +0100 baz.txt re_file = re.compile('^-(?P[^\s]+)\s+' '(?P[^\s]+)\s+' '(?P[^\s]+)\s+' '(?P[^\s]+)\s+' '(?P[^\s]+)\s+' '(?P