From a0f39a5d11b3562ab1435073274cb31f996cb159 Mon Sep 17 00:00:00 2001 From: Tarek Galal Date: Thu, 28 May 2015 18:16:19 +0200 Subject: [PATCH 1/2] Support pull directories --- adb/adb_commands.py | 49 +++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/adb/adb_commands.py b/adb/adb_commands.py index 59ac496..7449ec2 100644 --- a/adb/adb_commands.py +++ b/adb/adb_commands.py @@ -25,6 +25,7 @@ import cStringIO import os import socket +import stat from M2Crypto import RSA @@ -171,19 +172,41 @@ def Pull(self, device_filename, dest_file=None, timeout_ms=None): Returns: The file data if dest_file is not set. """ - if isinstance(dest_file, basestring): - dest_file = open(dest_file, 'w') - elif not dest_file: - dest_file = cStringIO.StringIO() - connection = self.protocol_handler.Open( - self.handle, destination='sync:', - timeout_ms=timeout_ms) - self.filesync_handler.Pull(connection, device_filename, dest_file) - connection.Close() - # An empty call to cStringIO.StringIO returns an instance of - # cStringIO.OutputType. - if isinstance(dest_file, cStringIO.OutputType): - return dest_file.getvalue() + + if dest_file: + print("pull: %s -> %s" % (device_filename, dest_file)) + + filemode, _, _= self.Stat(device_filename) + + if stat.S_ISDIR(filemode): + assert dest_file, "Must specify out dir when pulling a directory" + file_list = self.List(device_filename) + if not os.path.exists(dest_file): + os.makedirs(dest_file) + + for device_file in file_list: + if device_file.filename not in (".", ".."): + self.Pull( + os.path.join(device_filename, device_file.filename), + os.path.join(dest_file, device_file.filename) if dest_file else None + ) + + else: + if stat.S_ISLNK(filemode) or stat.S_ISSOCK(filemode): + return + if isinstance(dest_file, basestring): + dest_file = open(dest_file, 'w') + elif not dest_file: + dest_file = cStringIO.StringIO() + connection = self.protocol_handler.Open( + self.handle, destination='sync:', + timeout_ms=timeout_ms) + self.filesync_handler.Pull(connection, device_filename, dest_file) + connection.Close() + # An empty call to cStringIO.StringIO returns an instance of + # cStringIO.OutputType. + if isinstance(dest_file, cStringIO.OutputType): + return dest_file.getvalue() def Stat(self, device_filename): """Get a file's stat() information.""" From 311014f8a626662f8057590955e00156db05412f Mon Sep 17 00:00:00 2001 From: Tarek Galal Date: Sun, 31 May 2015 08:44:13 +0200 Subject: [PATCH 2/2] No printing and raise ValueError instead of AssertionError --- adb/adb_commands.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/adb/adb_commands.py b/adb/adb_commands.py index 7449ec2..22881c4 100644 --- a/adb/adb_commands.py +++ b/adb/adb_commands.py @@ -173,13 +173,11 @@ def Pull(self, device_filename, dest_file=None, timeout_ms=None): The file data if dest_file is not set. """ - if dest_file: - print("pull: %s -> %s" % (device_filename, dest_file)) - filemode, _, _= self.Stat(device_filename) if stat.S_ISDIR(filemode): - assert dest_file, "Must specify out dir when pulling a directory" + if dest_file is None: + raise ValueError("Must specify dest_file when pulling a directory") file_list = self.List(device_filename) if not os.path.exists(dest_file): os.makedirs(dest_file)