Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Provides a variety of device interactions based on adb. | 5 """Provides a variety of device interactions based on adb. |
| 6 | 6 |
| 7 Eventually, this will be based on adb_wrapper. | 7 Eventually, this will be based on adb_wrapper. |
| 8 """ | 8 """ |
| 9 # pylint: disable=W0613 | 9 # pylint: disable=W0613 |
| 10 | 10 |
| (...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 428 | 428 |
| 429 See: http://developer.android.com/reference/android/view/KeyEvent.html | 429 See: http://developer.android.com/reference/android/view/KeyEvent.html |
| 430 | 430 |
| 431 Args: | 431 Args: |
| 432 keycode: A integer keycode to send to the device. | 432 keycode: A integer keycode to send to the device. |
| 433 timeout: Same as for |IsOnline|. | 433 timeout: Same as for |IsOnline|. |
| 434 retries: Same as for |IsOnline|. | 434 retries: Same as for |IsOnline|. |
| 435 """ | 435 """ |
| 436 self.old_interface.SendKeyEvent(keycode) | 436 self.old_interface.SendKeyEvent(keycode) |
| 437 | 437 |
| 438 PUSH_CHANGED_FILES_DEFAULT_TIMEOUT = 10 * _DEFAULT_TIMEOUT | |
| 439 PUSH_CHANGED_FILES_DEFAULT_RETRIES = _DEFAULT_RETRIES | |
| 440 | |
| 441 @decorators.WithTimeoutAndRetriesDefaults( | |
| 442 PUSH_CHANGED_FILES_DEFAULT_TIMEOUT, | |
| 443 PUSH_CHANGED_FILES_DEFAULT_RETRIES) | |
| 444 def PushChangedFiles(self, host_path, device_path, timeout=None, | |
| 445 retries=None): | |
| 446 """Push files to the device, skipping files that don't need updating. | |
| 447 | |
| 448 Args: | |
| 449 host_path: A string containing the path to the file or directory on the | |
| 450 host that should be minimally pushed to the device. | |
|
frankf
2014/06/28 00:21:35
specify whether this is absolute and/or relative.
jbudorick
2014/06/30 14:37:19
Done.
| |
| 451 device_path: A string containing the destination path on the device. | |
| 452 timeout: Same as for |IsOnline|. | |
| 453 retries: Same as for |IsOnline|. | |
| 454 """ | |
| 455 self.old_interface.PushIfNeeded(host_path, device_path) | |
| 456 | |
| 457 @decorators.WithTimeoutAndRetriesFromInstance() | |
| 458 def FileExists(self, device_path, timeout=None, retries=None): | |
| 459 """Checks whether the given file exists on the device. | |
| 460 | |
| 461 Args: | |
| 462 device_path: A string containing the path to the file on the device. | |
| 463 timeout: Same as for |IsOnline|. | |
| 464 retries: Same as for |IsOnline|. | |
| 465 Returns: | |
| 466 True if the file exists on the device, False otherwise. | |
| 467 """ | |
| 468 return self._FileExistsImpl(device_path) | |
| 469 | |
| 470 def _FileExistsImpl(self, device_path): | |
| 471 """Implementation of FileExists. | |
| 472 | |
| 473 This is split from FileExists to allow other DeviceUtils methods to call | |
| 474 FileExists without spawning a new timeout thread. | |
| 475 | |
| 476 Args: | |
| 477 device_path: Same as for |FileExists|. | |
| 478 Returns: | |
| 479 True if the file exists on the device, False otherwise. | |
| 480 """ | |
| 481 return self.old_interface.FileExistsOnDevice(device_path) | |
| 482 | |
| 483 @decorators.WithTimeoutAndRetriesFromInstance() | |
| 484 def PullFile(self, device_path, host_path, timeout=None, retries=None): | |
| 485 """Pull a file from the device. | |
| 486 | |
| 487 Args: | |
| 488 device_path: A string containing the path of the file to pull from the | |
| 489 device. | |
| 490 host_path: A string containing the destination path on the host. | |
| 491 timeout: Same as for |IsOnline|. | |
| 492 retries: Same as for |IsOnline|. | |
| 493 """ | |
| 494 self.old_interface.PullFileFromDevice(device_path, host_path) | |
| 495 | |
| 496 @decorators.WithTimeoutAndRetriesFromInstance() | |
| 497 def ReadFile(self, device_path, as_root=False, timeout=None, retries=None): | |
| 498 """Reads the contents of a file from the device. | |
| 499 | |
| 500 TODO(jbudorick) Do we actually want to return a list of lines after | |
| 501 the implementation switch? | |
|
frankf
2014/06/28 00:21:35
move the TODO outside of docstring
jbudorick
2014/06/30 14:37:20
Done.
| |
| 502 | |
| 503 Args: | |
| 504 device_path: A string containing the path of the file to read from the | |
| 505 device. | |
| 506 as_root: A boolean indicating whether the read should be executed with | |
| 507 root priveleges. | |
| 508 timeout: Same as for |IsOnline|. | |
| 509 retries: Same as for |IsOnline|. | |
| 510 Returns: | |
| 511 The contents of the file at |device_path| as a list of lines. | |
| 512 Raises: | |
| 513 CommandFailedError if the file can't be read. | |
| 514 """ | |
| 515 if as_root: | |
| 516 if not self.old_interface.CanAccessProtectedFileContents(): | |
| 517 raise device_errors.CommandFailedError( | |
| 518 'Cannot read from %s with root priveleges.' % device_path) | |
| 519 return self.old_interface.GetProtectedFileContents(device_path) | |
| 520 else: | |
| 521 return self.old_interface.GetFileContents(device_path) | |
| 522 | |
| 523 @decorators.WithTimeoutAndRetriesFromInstance() | |
| 524 def WriteFile(self, device_path, contents, as_root=False, timeout=None, | |
| 525 retries=None): | |
| 526 """Writes |contents| to a file on the device. | |
| 527 | |
| 528 Args: | |
| 529 device_path: A string containing the destination path on the device. | |
| 530 contents: A string containing the data to write to the device. | |
| 531 as_root: A boolean indicating whether the write should be executed with | |
| 532 root priveleges. | |
| 533 timeout: Same as for |IsOnline|. | |
| 534 retries: Same as for |IsOnline|. | |
| 535 Raises: | |
| 536 CommandFailedError if the file could not be written on the device. | |
| 537 """ | |
| 538 if as_root: | |
| 539 if not self.old_interface.CanAccessProtectedFileContents(): | |
| 540 raise device_errors.CommandFailedError( | |
| 541 'Cannot write to %s with root priveleges.' % device_path) | |
| 542 self.old_interface.SetProtectedFileContents(device_path, contents) | |
| 543 else: | |
| 544 self.old_interface.SetFileContents(device_path, contents) | |
| 545 | |
| 438 def __str__(self): | 546 def __str__(self): |
| 439 """Returns the device serial.""" | 547 """Returns the device serial.""" |
| 440 return self.old_interface.GetDevice() | 548 return self.old_interface.GetDevice() |
| 441 | 549 |
| 442 @staticmethod | 550 @staticmethod |
| 443 def parallel(devices=None, async=False): | 551 def parallel(devices=None, async=False): |
| 444 """Creates a Parallelizer to operate over the provided list of devices. | 552 """Creates a Parallelizer to operate over the provided list of devices. |
| 445 | 553 |
| 446 If |devices| is either |None| or an empty list, the Parallelizer will | 554 If |devices| is either |None| or an empty list, the Parallelizer will |
| 447 operate over all attached devices. | 555 operate over all attached devices. |
| 448 | 556 |
| 449 Args: | 557 Args: |
| 450 devices: A list of either DeviceUtils instances or objects from | 558 devices: A list of either DeviceUtils instances or objects from |
| 451 from which DeviceUtils instances can be constructed. If None, | 559 from which DeviceUtils instances can be constructed. If None, |
| 452 all attached devices will be used. | 560 all attached devices will be used. |
| 453 async: If true, returns a Parallelizer that runs operations | 561 async: If true, returns a Parallelizer that runs operations |
| 454 asynchronously. | 562 asynchronously. |
| 455 Returns: | 563 Returns: |
| 456 A Parallelizer operating over |devices|. | 564 A Parallelizer operating over |devices|. |
| 457 """ | 565 """ |
| 458 if not devices or len(devices) == 0: | 566 if not devices or len(devices) == 0: |
| 459 devices = pylib.android_commands.GetAttachedDevices() | 567 devices = pylib.android_commands.GetAttachedDevices() |
| 460 parallelizer_type = (parallelizer.Parallelizer if async | 568 parallelizer_type = (parallelizer.Parallelizer if async |
| 461 else parallelizer.SyncParallelizer) | 569 else parallelizer.SyncParallelizer) |
| 462 return parallelizer_type([ | 570 return parallelizer_type([ |
| 463 d if isinstance(d, DeviceUtils) else DeviceUtils(d) | 571 d if isinstance(d, DeviceUtils) else DeviceUtils(d) |
| 464 for d in devices]) | 572 for d in devices]) |
| 465 | 573 |
| OLD | NEW |