From de53af86530ce189657a77f6f49cc534525ddb0d Mon Sep 17 00:00:00 2001 From: askeing Date: Tue, 30 Jun 2015 10:50:49 +0800 Subject: [PATCH] Add adb root command for backup/restore. --- backup_restore_profile.py | 22 ++++++++++++++-------- utilities/adb_helper.py | 17 +++++++++++++++++ 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/backup_restore_profile.py b/backup_restore_profile.py index f49a6a3..6e6425f 100755 --- a/backup_restore_profile.py +++ b/backup_restore_profile.py @@ -189,15 +189,21 @@ def run(self): elif 'ANDROID_SERIAL' in os.environ and os.environ['ANDROID_SERIAL'] in devices: self.logger.debug('Setup serial to [{0}] by ANDROID_SERIAL'.format(os.environ['ANDROID_SERIAL'])) device_serial = os.environ['ANDROID_SERIAL'] + elif self.args.serial is None and not 'ANDROID_SERIAL' in os.environ: + if len(devices) == 1: + self.logger.debug('No serial, and only one device') + device_serial = None + else: + self.logger.debug('No serial, but there are more than one device') + self.logger.warning('Please specify the device by --serial option.') + exit(1) else: - if self.args.serial is None and not 'ANDROID_SERIAL' in os.environ: - if len(devices) == 1: - self.logger.debug('No serial, and only one device') - device_serial = None - else: - self.logger.debug('No serial, but there are more than one device') - self.logger.warning('Please specify the device by --serial option.') - exit(1) + device_serial = None + + # checking the adb root for backup/restore + if not AdbHelper.adb_root(serial=device_serial): + exit(2) + # Backup if self.args.backup: try: diff --git a/utilities/adb_helper.py b/utilities/adb_helper.py index 182c489..ea0fb84 100644 --- a/utilities/adb_helper.py +++ b/utilities/adb_helper.py @@ -79,3 +79,20 @@ def adb_shell(command, serial=None): AdbHelper.logger.debug('cmd: {0}'.format(cmd)) AdbHelper.logger.debug('output: {0}'.format(output)) return output + + @staticmethod + def adb_root(serial=None): + if serial is None: + cmd = 'adb root' + else: + cmd = 'adb -s %s root' % (serial,) + p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + output = p.communicate()[0] + AdbHelper.logger.debug('cmd: {0}'.format(cmd)) + AdbHelper.logger.debug('output: {0}'.format(output)) + if p.returncode is 0 and (not 'cannot' in output): + AdbHelper.logger.info(output) + return True + else: + AdbHelper.logger.warning(output) + return False