Skip to content

Commit

Permalink
Get rid of the hideous _sprintf wrapper too.
Browse files Browse the repository at this point in the history
  • Loading branch information
divVerent committed Nov 27, 2018
1 parent c75950b commit 6dde901
Showing 1 changed file with 17 additions and 30 deletions.
47 changes: 17 additions & 30 deletions adb-sync
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,6 @@ import sys
import time


def _sprintf(s, *args):
# To be able to use string formatting, we first have to covert to
# unicode strings; however, we must do so in a way that preserves all
# bytes, and convert back at the end. An encoding that maps all byte
# values to different Unicode codepoints is cp437.
return (s.decode('cp437') % tuple([
(x.decode('cp437') if type(x) == bytes else x) for x in args
])).encode('cp437')


class AdbFileSystem(object):
"""Mimics os's file interface but uses the adb utility."""

Expand Down Expand Up @@ -201,8 +191,8 @@ class AdbFileSystem(object):
good = False
with self.Stdout(
self.adb +
[b'shell',
_sprintf(b'date +%s', self.QuoteArgument(test_string))]) as stdout:
[b'shell', b'date +%s' %
(self.QuoteArgument(test_string),)]) as stdout:
for line in stdout:
line = line.rstrip(b'\r\n')
if line == test_string:
Expand All @@ -215,8 +205,8 @@ class AdbFileSystem(object):
"""List the contents of a directory, caching them for later lstat calls."""
with self.Stdout(
self.adb +
[b'shell',
_sprintf(b'ls -al %s', self.QuoteArgument(path + b'/'))]) as stdout:
[b'shell', b'ls -al %s' %
(self.QuoteArgument(path + b'/'),)]) as stdout:
for line in stdout:
if line.startswith(b'total '):
continue
Expand All @@ -237,8 +227,7 @@ class AdbFileSystem(object):
return self.stat_cache[path]
with self.Stdout(
self.adb +
[b'shell', _sprintf(b'ls -ald %s', self.QuoteArgument(path))]
) as stdout:
[b'shell', b'ls -ald %s' % (self.QuoteArgument(path),)]) as stdout:
for line in stdout:
if line.startswith(b'total '):
continue
Expand All @@ -251,46 +240,44 @@ class AdbFileSystem(object):
def unlink(self, path): # os's name, so pylint: disable=g-bad-name
"""Delete a file."""
if subprocess.call(
self.adb +
[b'shell', _sprintf(b'rm %s', self.QuoteArgument(path))]) != 0:
self.adb + [b'shell', b'rm %s' % (self.QuoteArgument(path),)]) != 0:
raise OSError('unlink failed')

def rmdir(self, path): # os's name, so pylint: disable=g-bad-name
"""Delete a directory."""
if subprocess.call(
self.adb +
[b'shell', _sprintf(b'rmdir %s', self.QuoteArgument(path))]) != 0:
[b'shell', b'rmdir %s' % (self.QuoteArgument(path),)]) != 0:
raise OSError('rmdir failed')

def makedirs(self, path): # os's name, so pylint: disable=g-bad-name
"""Create a directory."""
if subprocess.call(
self.adb +
[b'shell', _sprintf(b'mkdir -p %s', self.QuoteArgument(path))]) != 0:
[b'shell', b'mkdir -p %s' % (self.QuoteArgument(path),)]) != 0:
raise OSError('mkdir failed')

def utime(self, path, times):
# TODO(rpolzer): Find out why this does not work (returns status 255).
"""Set the time of a file to a specified unix time."""
atime, mtime = times
timestr = time.strftime(b'%Y%m%d.%H%M%S', time.localtime(mtime))
if subprocess.call(self.adb + [
b'shell',
_sprintf(b'touch -mt %s %s', timestr, self.QuoteArgument(path))
]) != 0:
if subprocess.call(
self.adb +
[b'shell',
b'touch -mt %s %s' % (timestr, self.QuoteArgument(path))]) != 0:
raise OSError('touch failed')
timestr = time.strftime(b'%Y%m%d.%H%M%S', time.localtime(atime))
if subprocess.call(self.adb + [
b'shell',
_sprintf(b'touch -at %s %s', timestr, self.QuoteArgument(path))
]) != 0:
if subprocess.call(
self.adb +
[b'shell',
b'touch -at %s %s' % (timestr, self.QuoteArgument(path))]) != 0:
raise OSError('touch failed')

def glob(self, path):
with self.Stdout(
self.adb +
[b'shell', _sprintf(b'for p in %s; do echo "$p"; done', path)]
) as stdout:
[b'shell', b'for p in %s; do echo "$p"; done' % (path,)]) as stdout:
for line in stdout:
yield line.rstrip(b'\r\n')

Expand Down

0 comments on commit 6dde901

Please sign in to comment.