Chromium Code Reviews| Index: build/android/pylib/device/device_utils.py |
| diff --git a/build/android/pylib/device/device_utils.py b/build/android/pylib/device/device_utils.py |
| index 272e613b6817f915a2cee039caf6ff53de13bea7..53f9e44f3f9397236f88834f8d08d65ff3eab551 100644 |
| --- a/build/android/pylib/device/device_utils.py |
| +++ b/build/android/pylib/device/device_utils.py |
| @@ -58,6 +58,7 @@ def RestartServer(): |
| class DeviceUtils(object): |
| + _MAX_ADB_COMMAND_LENGTH = 512 |
| _VALID_SHELL_VARIABLE = re.compile('^[a-zA-Z_][a-zA-Z0-9_]*$') |
| def __init__(self, device, default_timeout=_DEFAULT_TIMEOUT, |
| @@ -369,6 +370,25 @@ class DeviceUtils(object): |
| raise device_errors.CommandFailedError( |
| str(e), str(self)), None, sys.exc_info()[2] |
| + def _PrepareShellCommand(self, cmd, cwd=None, env=None, as_root=False): |
|
jbudorick
2014/11/27 17:17:34
Why is this separate from RunShellCommand? Would w
perezju
2014/11/28 15:39:34
see below vv
|
| + def env_quote(key, value): |
| + if not DeviceUtils._VALID_SHELL_VARIABLE.match(key): |
| + raise KeyError('Invalid shell variable name %r' % key) |
| + # using double quotes here to allow interpolation of shell variables |
| + return '%s=%s' % (key, cmd_helper.DoubleQuote(value)) |
| + |
| + if not isinstance(cmd, basestring): |
| + cmd = ' '.join(cmd_helper.SingleQuote(s) for s in cmd) |
| + if env: |
| + env = ' '.join(env_quote(k, v) for k, v in env.iteritems()) |
| + cmd = '%s %s' % (env, cmd) |
| + if cwd: |
| + cmd = 'cd %s && %s' % (cmd_helper.SingleQuote(cwd), cmd) |
| + if as_root and self.NeedsSU(): |
| + # "su -c sh -c" allows using shell features in |cmd| |
| + cmd = 'su -c sh -c %s' % cmd_helper.SingleQuote(cmd) |
| + return cmd |
| + |
| @decorators.WithTimeoutAndRetriesFromInstance() |
| def RunShellCommand(self, cmd, check_return=False, cwd=None, env=None, |
| as_root=False, single_line=False, |
| @@ -421,32 +441,25 @@ class DeviceUtils(object): |
| CommandTimeoutError on timeout. |
| DeviceUnreachableError on missing device. |
| """ |
| - def env_quote(key, value): |
| - if not DeviceUtils._VALID_SHELL_VARIABLE.match(key): |
| - raise KeyError('Invalid shell variable name %r' % key) |
| - # using double quotes here to allow interpolation of shell variables |
| - return '%s=%s' % (key, cmd_helper.DoubleQuote(value)) |
| + def do_run(cmd): |
| + try: |
| + return self.adb.Shell(cmd) |
| + except device_errors.AdbCommandFailedError as exc: |
| + if check_return: |
| + raise |
| + else: |
| + return exc.output |
| - if not isinstance(cmd, basestring): |
| - cmd = ' '.join(cmd_helper.SingleQuote(s) for s in cmd) |
| - if env: |
| - env = ' '.join(env_quote(k, v) for k, v in env.iteritems()) |
| - cmd = '%s %s' % (env, cmd) |
| - if cwd: |
| - cmd = 'cd %s && %s' % (cmd_helper.SingleQuote(cwd), cmd) |
| - if as_root and self.NeedsSU(): |
| - # "su -c sh -c" allows using shell features in |cmd| |
| - cmd = 'su -c sh -c %s' % cmd_helper.SingleQuote(cmd) |
| if timeout is None: |
| timeout = self._default_timeout |
| - try: |
| - output = self.adb.Shell(cmd) |
| - except device_errors.AdbCommandFailedError as e: |
| - if check_return: |
| - raise |
| - else: |
| - output = e.output |
| + cmd = self._PrepareShellCommand(cmd, cwd=cwd, env=env, as_root=as_root) |
| + if len(cmd) < self._MAX_ADB_COMMAND_LENGTH: |
| + output = do_run(cmd) |
| + else: |
| + with device_temp_file.DeviceTempFile(self.adb, suffix='.sh') as script: |
| + self.PushContents(cmd, script.name) |
| + output = do_run('sh %s' % script.name_quoted) |
| output = output.splitlines() |
| if single_line: |
| @@ -884,6 +897,30 @@ class DeviceUtils(object): |
| return self.old_interface.GetFileContents(device_path) |
| @decorators.WithTimeoutAndRetriesFromInstance() |
| + def PushContents(self, contents, device_path, timeout=None, retries=None): |
|
jbudorick
2014/11/27 17:17:34
I don't think this should be part of the public in
perezju
2014/11/28 15:39:34
The argument order is consistent with that of Push
jbudorick
2014/11/28 18:49:01
The three should probably be consistent. Perhaps w
perezju
2014/11/28 19:43:37
I'm not sure we want to do that. That would requir
|
| + """Pushes |contents| to a file on the device. |
| + |
| + Note: If the contents are small, or root access is needed, then |WriteFile| |
| + is a better alternative. |
| + |
| + Args: |
| + contents: A string containing the data to write to the device. |
| + device_path: A string containing the absolute path to the file to write |
| + on the device. |
| + timeout: timeout in seconds |
| + retries: number of retries |
| + |
| + Raises: |
| + CommandFailedError if the file could not be written on the device. |
| + CommandTimeoutError on timeout. |
| + DeviceUnreachableError on missing device. |
| + """ |
| + with tempfile.NamedTemporaryFile() as host_temp: |
| + host_temp.write(contents) |
| + host_temp.flush() |
| + self.adb.Push(host_temp.name, device_path) |
| + |
| + @decorators.WithTimeoutAndRetriesFromInstance() |
| def WriteFile(self, device_path, contents, as_root=False, force_push=False, |
| timeout=None, retries=None): |
| """Writes |contents| to a file on the device. |
| @@ -905,24 +942,24 @@ class DeviceUtils(object): |
| CommandTimeoutError on timeout. |
| DeviceUnreachableError on missing device. |
| """ |
| - if len(contents) < 512 and not force_push: |
| - cmd = 'echo -n %s > %s' % (cmd_helper.SingleQuote(contents), |
| - cmd_helper.SingleQuote(device_path)) |
| - self.RunShellCommand(cmd, as_root=as_root, check_return=True) |
| + if not force_push and len(contents) < self._MAX_ADB_COMMAND_LENGTH: |
| + cmd = self._PrepareShellCommand( |
| + 'echo -n %s > %s' % (cmd_helper.SingleQuote(contents), |
| + cmd_helper.SingleQuote(device_path)), |
| + as_root=as_root) |
|
perezju
2014/11/28 15:39:34
^^ this is where I need to prepare the command bef
jbudorick
2014/11/28 18:49:01
If you "prepare" a shell command twice -- as you w
perezju
2014/11/28 19:43:37
Nothing happens. Because the second "prepare" gets
|
| + if len(cmd) < self._MAX_ADB_COMMAND_LENGTH: |
| + self.RunShellCommand(cmd, check_return=True) |
| + return |
| + if as_root and self.NeedsSU(): |
| + with device_temp_file.DeviceTempFile(self.adb) as device_temp: |
| + self.PushContents(contents, device_temp.name) |
| + # Here we need 'cp' rather than 'mv' because the temp and |
| + # destination files might be on different file systems (e.g. |
| + # on internal storage and an external sd card) |
| + self.RunShellCommand(['cp', device_temp.name, device_path], |
| + as_root=True, check_return=True) |
| else: |
| - with tempfile.NamedTemporaryFile() as host_temp: |
| - host_temp.write(contents) |
| - host_temp.flush() |
| - if as_root and self.NeedsSU(): |
| - with device_temp_file.DeviceTempFile(self) as device_temp: |
| - self.adb.Push(host_temp.name, device_temp.name) |
| - # Here we need 'cp' rather than 'mv' because the temp and |
| - # destination files might be on different file systems (e.g. |
| - # on internal storage and an external sd card) |
| - self.RunShellCommand(['cp', device_temp.name, device_path], |
| - as_root=True, check_return=True) |
| - else: |
| - self.adb.Push(host_temp.name, device_path) |
| + self.PushContents(contents, device_path) |
| @decorators.WithTimeoutAndRetriesFromInstance() |
| def Ls(self, device_path, timeout=None, retries=None): |