Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.

Support pull directories #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions adb/adb_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import cStringIO
import os
import socket
import stat

from M2Crypto import RSA

Expand Down Expand Up @@ -171,19 +172,39 @@ 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()

filemode, _, _= self.Stat(device_filename)

if stat.S_ISDIR(filemode):
if dest_file is None:
raise ValueError("Must specify dest_file when pulling a directory")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a check for dest_file being a directory (or not existing)? If they specify an existing file then we'll error out in a weird place down in Pull

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, if they call without dest_file, can we return a map of StringIO objects?

dest_file = collections.defaultdict(cStringIO.StringIO)
...
self.Pull(
    ..., os.path.join(dest_file, device_file.filename)
         if isinstance(dest_file, basestring) else dest_file[device_file.filename])

then after the loop do:

if isinstance(dest_file, dict):
  return {fn: data.getvalue() for fn, data in dest_file.iteritems()}

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dest_file can't be None, so are you expecting it to be an empty string? If so, os.path.join('', filename) == filename, so it shouldn't matter. The only other case is if dest_file is False or something falsey but not a string, which should be caught in the check above (by it failing or os.path.exists failing)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So from where I'm sitting, this really only means that the conditional at L189 should be removed. Is that correct?

)

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."""
Expand Down