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 absolute path to the file or directory |
| 450 on the host that should be minimally pushed to the device. |
| 451 device_path: A string containing the absolute path of the destination on |
| 452 the device. |
| 453 timeout: Same as for |IsOnline|. |
| 454 retries: Same as for |IsOnline|. |
| 455 """ |
| 456 self.old_interface.PushIfNeeded(host_path, device_path) |
| 457 |
| 458 @decorators.WithTimeoutAndRetriesFromInstance() |
| 459 def FileExists(self, device_path, timeout=None, retries=None): |
| 460 """Checks whether the given file exists on the device. |
| 461 |
| 462 Args: |
| 463 device_path: A string containing the absolute path to the file on the |
| 464 device. |
| 465 timeout: Same as for |IsOnline|. |
| 466 retries: Same as for |IsOnline|. |
| 467 Returns: |
| 468 True if the file exists on the device, False otherwise. |
| 469 """ |
| 470 return self._FileExistsImpl(device_path) |
| 471 |
| 472 def _FileExistsImpl(self, device_path): |
| 473 """Implementation of FileExists. |
| 474 |
| 475 This is split from FileExists to allow other DeviceUtils methods to call |
| 476 FileExists without spawning a new timeout thread. |
| 477 |
| 478 Args: |
| 479 device_path: Same as for |FileExists|. |
| 480 Returns: |
| 481 True if the file exists on the device, False otherwise. |
| 482 """ |
| 483 return self.old_interface.FileExistsOnDevice(device_path) |
| 484 |
| 485 @decorators.WithTimeoutAndRetriesFromInstance() |
| 486 def PullFile(self, device_path, host_path, timeout=None, retries=None): |
| 487 """Pull a file from the device. |
| 488 |
| 489 Args: |
| 490 device_path: A string containing the absolute path of the file to pull |
| 491 from the device. |
| 492 host_path: A string containing the absolute path of the destination on |
| 493 the host. |
| 494 timeout: Same as for |IsOnline|. |
| 495 retries: Same as for |IsOnline|. |
| 496 """ |
| 497 self.old_interface.PullFileFromDevice(device_path, host_path) |
| 498 |
| 499 @decorators.WithTimeoutAndRetriesFromInstance() |
| 500 def ReadFile(self, device_path, as_root=False, timeout=None, retries=None): |
| 501 """Reads the contents of a file from the device. |
| 502 |
| 503 Args: |
| 504 device_path: A string containing the absolute path of the file to read |
| 505 from the 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 # TODO(jbudorick) Evaluate whether we actually want to return a list of |
| 516 # lines after the implementation switch. |
| 517 if as_root: |
| 518 if not self.old_interface.CanAccessProtectedFileContents(): |
| 519 raise device_errors.CommandFailedError( |
| 520 'Cannot read from %s with root priveleges.' % device_path) |
| 521 return self.old_interface.GetProtectedFileContents(device_path) |
| 522 else: |
| 523 return self.old_interface.GetFileContents(device_path) |
| 524 |
| 525 @decorators.WithTimeoutAndRetriesFromInstance() |
| 526 def WriteFile(self, device_path, contents, as_root=False, timeout=None, |
| 527 retries=None): |
| 528 """Writes |contents| to a file on the device. |
| 529 |
| 530 Args: |
| 531 device_path: A string containing the absolute path to the file to write |
| 532 on the device. |
| 533 contents: A string containing the data to write to the device. |
| 534 as_root: A boolean indicating whether the write should be executed with |
| 535 root priveleges. |
| 536 timeout: Same as for |IsOnline|. |
| 537 retries: Same as for |IsOnline|. |
| 538 Raises: |
| 539 CommandFailedError if the file could not be written on the device. |
| 540 """ |
| 541 if as_root: |
| 542 if not self.old_interface.CanAccessProtectedFileContents(): |
| 543 raise device_errors.CommandFailedError( |
| 544 'Cannot write to %s with root priveleges.' % device_path) |
| 545 self.old_interface.SetProtectedFileContents(device_path, contents) |
| 546 else: |
| 547 self.old_interface.SetFileContents(device_path, contents) |
| 548 |
438 def __str__(self): | 549 def __str__(self): |
439 """Returns the device serial.""" | 550 """Returns the device serial.""" |
440 return self.old_interface.GetDevice() | 551 return self.old_interface.GetDevice() |
441 | 552 |
442 @staticmethod | 553 @staticmethod |
443 def parallel(devices=None, async=False): | 554 def parallel(devices=None, async=False): |
444 """Creates a Parallelizer to operate over the provided list of devices. | 555 """Creates a Parallelizer to operate over the provided list of devices. |
445 | 556 |
446 If |devices| is either |None| or an empty list, the Parallelizer will | 557 If |devices| is either |None| or an empty list, the Parallelizer will |
447 operate over all attached devices. | 558 operate over all attached devices. |
448 | 559 |
449 Args: | 560 Args: |
450 devices: A list of either DeviceUtils instances or objects from | 561 devices: A list of either DeviceUtils instances or objects from |
451 from which DeviceUtils instances can be constructed. If None, | 562 from which DeviceUtils instances can be constructed. If None, |
452 all attached devices will be used. | 563 all attached devices will be used. |
453 async: If true, returns a Parallelizer that runs operations | 564 async: If true, returns a Parallelizer that runs operations |
454 asynchronously. | 565 asynchronously. |
455 Returns: | 566 Returns: |
456 A Parallelizer operating over |devices|. | 567 A Parallelizer operating over |devices|. |
457 """ | 568 """ |
458 if not devices or len(devices) == 0: | 569 if not devices or len(devices) == 0: |
459 devices = pylib.android_commands.GetAttachedDevices() | 570 devices = pylib.android_commands.GetAttachedDevices() |
460 parallelizer_type = (parallelizer.Parallelizer if async | 571 parallelizer_type = (parallelizer.Parallelizer if async |
461 else parallelizer.SyncParallelizer) | 572 else parallelizer.SyncParallelizer) |
462 return parallelizer_type([ | 573 return parallelizer_type([ |
463 d if isinstance(d, DeviceUtils) else DeviceUtils(d) | 574 d if isinstance(d, DeviceUtils) else DeviceUtils(d) |
464 for d in devices]) | 575 for d in devices]) |
465 | 576 |
OLD | NEW |