Created
July 23, 2015 07:59
-
-
Save faried/35d42dae8f2f9135648d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import optparse | |
import subprocess | |
import sys | |
import tempfile | |
import zlib | |
def doit(opts, pkg): | |
"""Do the work.""" | |
cmd = ['adb', 'backup'] | |
if opts.apk: | |
cmd.append('-apk') | |
else: | |
cmd.append('-noapk') | |
cmd.append(pkg) | |
if opts.verbose: | |
print('executing %s...' % ' '.join(cmd)) | |
ret = subprocess.call(cmd, shell=False) | |
if opts.verbose: | |
if ret == 0: | |
print('returncode: okay!') | |
else: | |
print('returncode: %s' % ret) | |
if opts.verbose: | |
print('reading backup file...') | |
lines = file('backup.ab').readlines() | |
# skip the first four lines | |
lines = lines[4:] | |
if opts.verbose: | |
print('decompressing data...') | |
data = zlib.decompress(''.join(lines)) | |
if opts.verbose: | |
print('writing to %s...' % opts.backuptar) | |
fout = file(opts.backuptar, 'w') | |
fout.write(data) | |
fout.close() | |
def main(): | |
"""Main branching logic.""" | |
parser = optparse.OptionParser(usage='%prog [-h] [-v] -f backupfile.tar [-a] com.example.app') | |
parser.add_option('-f', '--file', dest='backuptar', | |
metavar='backupfile.tar', | |
help='tar file to create with the adb backup (required).') | |
parser.add_option('-a', '--apk', dest='apk', | |
default=False, action='store_true', | |
help='include apk in backup (default: no)') | |
parser.add_option('-v', '--verbose', dest='verbose', | |
default=False, action='store_true', | |
help='be noisy (default: no)') | |
(opts, args) = parser.parse_args() | |
if not len(args) == 1: | |
parser.error('need a name of a package to backup') | |
if not opts.backuptar: | |
parser.error('need an output tar filename with -f') | |
doit(opts, args[0]) | |
if __name__ == '__main__': | |
main() | |
# eof |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment