def backup_profile(self, local_dir, serial=None):
     self.logger.info("Backing up profile...")
     # Backup Wifi
     wifi_dir = local_dir + os.sep + self._LOCAL_DIR_WIFI + os.sep
     wifi_file = local_dir + os.sep + self._LOCAL_FILE_WIFI
     os.makedirs(wifi_dir)
     self.logger.info("Backing up Wifi information...")
     if not AdbHelper.adb_pull(self._REMOTE_FILE_WIFI, wifi_file, serial=serial):
         self.logger.warning("If you don't have root permission, you cannot backup Wifi information.")
     # Backup profile
     b2g_mozilla_dir = local_dir + os.sep + self._LOCAL_DIR_B2G + os.sep
     os.makedirs(b2g_mozilla_dir)
     self.logger.info("Backing up {0} to {1} ...".format(self._REMOTE_DIR_B2G, b2g_mozilla_dir))
     if not AdbHelper.adb_pull(self._REMOTE_DIR_B2G, b2g_mozilla_dir, serial=serial):
         self.logger.warning("Can not pull files from {0} to {1}".format(self._REMOTE_DIR_B2G, b2g_mozilla_dir))
     # Backup data/local
     datalocal_dir = local_dir + os.sep + self._LOCAL_DIR_DATA + os.sep
     os.makedirs(datalocal_dir)
     self.logger.info("Backing up {0} to {1} ...".format(self._REMOTE_DIR_DATA, datalocal_dir))
     if not AdbHelper.adb_pull(self._REMOTE_DIR_DATA, datalocal_dir, serial=serial):
         self.logger.warning("Can not pull files from {0} to {1}".format(self._REMOTE_DIR_DATA, datalocal_dir))
     # Remove "marketplace" app and "gaiamobile.org" apps from webapps
     webapps_dir = datalocal_dir + self._LOCAL_DIR_DATA_APPS
     for root, dirs, files in os.walk(webapps_dir):
         if (
             os.path.basename(root).startswith("marketplace")
             or os.path.basename(root).endswith("gaiamobile.org")
             or os.path.basename(root).endswith("allizom.org")
         ):
             self.logger.info("Removing Mozilla webapps: [{0}]".format(root))
             shutil.rmtree(root)
     self.logger.info("Backup profile done.")
 def backup_profile(self, local_dir, serial=None):
     self.logger.info('Backing up profile...')
     # Backup Wifi
     wifi_dir = local_dir + os.sep + self._LOCAL_DIR_WIFI + os.sep
     wifi_file = local_dir + os.sep + self._LOCAL_FILE_WIFI
     os.makedirs(wifi_dir)
     self.logger.info('Backing up Wifi information...')
     if not AdbHelper.adb_pull(self._REMOTE_FILE_WIFI, wifi_file, serial=serial):
         self.logger.warning('If you don\'t have root permission, you cannot backup Wifi information.')
     # Backup profile
     b2g_mozilla_dir = local_dir + os.sep + self._LOCAL_DIR_B2G + os.sep
     os.makedirs(b2g_mozilla_dir)
     self.logger.info('Backing up {0} to {1} ...'.format(self._REMOTE_DIR_B2G, b2g_mozilla_dir))
     if not AdbHelper.adb_pull(self._REMOTE_DIR_B2G, b2g_mozilla_dir, serial=serial):
         self.logger.warning('Can not pull files from {0} to {1}'.format(self._REMOTE_DIR_B2G, b2g_mozilla_dir))
     # Backup data/local
     datalocal_dir = local_dir + os.sep + self._LOCAL_DIR_DATA + os.sep
     os.makedirs(datalocal_dir)
     self.logger.info('Backing up {0} to {1} ...'.format(self._REMOTE_DIR_DATA, datalocal_dir))
     if not AdbHelper.adb_pull(self._REMOTE_DIR_DATA, datalocal_dir, serial=serial):
         self.logger.warning('Can not pull files from {0} to {1}'.format(self._REMOTE_DIR_DATA, datalocal_dir))
     # Remove "marketplace" app and "gaiamobile.org" apps from webapps
     webapps_dir = datalocal_dir + self._LOCAL_DIR_DATA_APPS
     for root, dirs, files in os.walk(webapps_dir):
         if (os.path.basename(root).startswith('marketplace') or
             os.path.basename(root).endswith('gaiamobile.org') or
             os.path.basename(root).endswith('allizom.org')):
             self.logger.info('Removing Mozilla webapps: [{0}]'.format(root))
             shutil.rmtree(root)
     self.logger.info('Backup profile done.')
 def check_profile_version(self, local_dir, serial=None):
     self.logger.info('Checking profile...')
     # get local version
     if os.path.isdir(local_dir):
         local_config = ConfigParser.ConfigParser()
         local_config.read(local_dir + os.sep + self._LOCAL_DIR_B2G + os.sep + self._FILE_PROFILE_INI)
         local_profile_path = local_config.get('Profile0', 'Path')
         local_config.read(local_dir + os.sep + self._LOCAL_DIR_B2G + os.sep + local_profile_path + os.sep + self._FILE_COMPATIBILITY_INI)
         version_of_backup = local_config.get('Compatibility', 'LastVersion')
         self.logger.info('The Version of Backup Profile: {}'.format(version_of_backup))
     else:
         return False
     # get remote version
     tmp_dir = tempfile.mkdtemp(prefix='backup_restore_')
     if not AdbHelper.adb_pull(self._REMOTE_DIR_B2G + os.sep + self._FILE_PROFILE_INI, tmp_dir, serial=serial):
         self.logger.warning('Can not pull {2} from {0} to {1}'.format(self._REMOTE_DIR_B2G, tmp_dir, self._FILE_PROFILE_INI))
         return False
     remote_config = ConfigParser.ConfigParser()
     remote_config.read(tmp_dir + os.sep + self._FILE_PROFILE_INI)
     remote_profile_path = local_config.get('Profile0', 'Path')
     if not AdbHelper.adb_pull(self._REMOTE_DIR_B2G + os.sep + remote_profile_path + os.sep + self._FILE_COMPATIBILITY_INI, tmp_dir, serial=serial):
         self.logger.warning('Can not pull {2} from {0} to {1}'.format(self._REMOTE_DIR_B2G, tmp_dir, self._FILE_COMPATIBILITY_INI))
         return False
     remote_config.read(tmp_dir + os.sep + self._FILE_COMPATIBILITY_INI)
     version_of_device = remote_config.get('Compatibility', 'LastVersion')
     self.logger.info('The Version of Device Profile: {}'.format(version_of_device))
     # compare
     version_of_backup_float = float(version_of_backup.split('_')[0])
     version_of_device_float = float(version_of_device.split('_')[0])
     if version_of_device_float >= version_of_backup_float:
         return True
     else:
         return False
 def check_profile_version(self, local_dir, serial=None):
     self.logger.info('Checking profile...')
     # get local version
     if os.path.isdir(local_dir):
         local_config = ConfigParser.ConfigParser()
         local_config.read(local_dir + os.sep + self._LOCAL_DIR_B2G + os.sep + self._FILE_PROFILE_INI)
         local_profile_path = local_config.get('Profile0', 'Path')
         local_config.read(local_dir + os.sep + self._LOCAL_DIR_B2G + os.sep + local_profile_path + os.sep + self._FILE_COMPATIBILITY_INI)
         version_of_backup = local_config.get('Compatibility', 'LastVersion')
         self.logger.info('The Version of Backup Profile: {}'.format(version_of_backup))
     else:
         return False
     # get remote version
     tmp_dir = tempfile.mkdtemp(prefix='backup_restore_')
     if not AdbHelper.adb_pull(self._REMOTE_DIR_B2G + os.sep + self._FILE_PROFILE_INI, tmp_dir, serial=serial):
         self.logger.warning('Can not pull {2} from {0} to {1}'.format(self._REMOTE_DIR_B2G, tmp_dir, self._FILE_PROFILE_INI))
         return False
     remote_config = ConfigParser.ConfigParser()
     remote_config.read(tmp_dir + os.sep + self._FILE_PROFILE_INI)
     remote_profile_path = remote_config.get('Profile0', 'Path')
     if not AdbHelper.adb_pull(self._REMOTE_DIR_B2G + os.sep + remote_profile_path + os.sep + self._FILE_COMPATIBILITY_INI, tmp_dir, serial=serial):
         self.logger.warning('Can not pull {2} from {0} to {1}'.format(self._REMOTE_DIR_B2G, tmp_dir, self._FILE_COMPATIBILITY_INI))
         return False
     remote_config.read(tmp_dir + os.sep + self._FILE_COMPATIBILITY_INI)
     version_of_device = remote_config.get('Compatibility', 'LastVersion')
     self.logger.info('The Version of Device Profile: {}'.format(version_of_device))
     # compare
     version_of_backup_float = float(version_of_backup.split('.')[0])
     version_of_device_float = float(version_of_device.split('.')[0])
     if version_of_device_float >= version_of_backup_float:
         return True
     else:
         return False
def main():
    if not AdbHelper.has_adb():
        print("No adb installed; terminated")
        sys.exit(1)
    options = ArgParse(sys.argv[1:])
    try:
        archive = {}
        prog = ConsoleApp()
        if option.full_flash:
            archives[PathParser._IMAGES] = option.full_flash
        else:
            if option.gecko:
                archives[PathParser._GECKO] = option.gecko
            if option.gaia:
                archives[PathParser._GAIA] = option.gaia
        if not archive:
            print("Nothing to flash; terminated")
            return sys.exit(1)
        ret_obj = prog.dialog.yes_no('Bulk Flasher Prompt', 'Warning: this program will flash all devices! Please make sure devices are the same. continue? (y/N)', ConsoleDialog._NO_CMD_INDEX)
        devices = AdbHelper.adb_devices().keys()
        for serial in devices:
            prog.do_flash(self.flash_params, archives, serial, keep_profile=self.target_keep_profile)
    except KeyboardInterrupt:
        print ''
        print '### Quit'
        sys.exit(0)
 def backup_sdcard(self, local_dir, serial=None):
     self.logger.info('Backing up SD card...')
     # try to get the /sdcard folder on device
     output = AdbHelper.adb_shell('ls -d {0}; echo $?'.format(
         self._REMOTE_DIR_SDCARD),
                                  serial=serial)
     output_list = [
         item for item in re.split(r'\n+', re.sub(r'\r+', '', output))
         if item
     ]
     ret_code = output_list[-1]
     output_list.remove(output_list[-1])
     ret_msg = '\n'.join(output_list)
     if ret_code == '0':
         target_dir = local_dir + os.sep + self._LOCAL_DIR_SDCARD + os.sep
         os.makedirs(target_dir)
         self.logger.info('Backup: {0} to {1}'.format(
             self._REMOTE_DIR_SDCARD, target_dir))
         if not AdbHelper.adb_pull(
                 self._REMOTE_DIR_SDCARD, target_dir, serial=serial):
             self.logger.warning(
                 'Can not pull files from {0} to {1}'.format(
                     self._REMOTE_DIR_SDCARD, target_dir))
     else:
         self.logger.info(ret_msg)
     self.logger.info('Backup SD card done.')
 def restore_profile(self, local_dir, serial=None):
     self.logger.info("Restoring profile...")
     if os.path.isdir(local_dir):
         # Restore Wifi
         wifi_file = local_dir + os.sep + self._LOCAL_FILE_WIFI
         if os.path.isfile(wifi_file):
             self.logger.info("Restoring Wifi information...")
             if not AdbHelper.adb_push(wifi_file, self._REMOTE_FILE_WIFI, serial=serial):
                 self.logger.warning("If you don't have root permission, you cannot restore Wifi information.")
             AdbHelper.adb_shell("chown {0} {1}".format(self._REMOTE_FILE_WIFI_OWNER, self._REMOTE_FILE_WIFI))
         # Restore profile
         b2g_mozilla_dir = local_dir + os.sep + self._LOCAL_DIR_B2G
         if os.path.isdir(b2g_mozilla_dir):
             self.logger.info("Restore from {0} to {1} ...".format(b2g_mozilla_dir, self._REMOTE_DIR_B2G))
             AdbHelper.adb_shell("rm -r {0}".format(self._REMOTE_DIR_B2G))
             if not AdbHelper.adb_push(b2g_mozilla_dir, self._REMOTE_DIR_B2G, serial=serial):
                 self.logger.warning(
                     "Can not push files from {0} to {1}".format(b2g_mozilla_dir, self._REMOTE_DIR_B2G)
                 )
         # Restore data/local
         datalocal_dir = local_dir + os.sep + self._LOCAL_DIR_DATA
         if os.path.isdir(datalocal_dir):
             self.logger.info("Restore from {0} to {1} ...".format(datalocal_dir, self._REMOTE_DIR_DATA))
             AdbHelper.adb_shell("rm -r {0}".format(self._REMOTE_DIR_DATA))
             if not AdbHelper.adb_push(datalocal_dir, self._REMOTE_DIR_DATA, serial=serial):
                 self.logger.warning(
                     "Can not push files from {0} to {1}".format(datalocal_dir, self._REMOTE_DIR_DATA)
                 )
         self.logger.info("Restore profile done.")
     else:
         self.logger.info("{0}: No such file or directory".format(local_dir))
         return
 def restore_profile(self, local_dir, serial=None):
     self.logger.info('Restoring profile...')
     if os.path.isdir(local_dir):
         # Restore Wifi
         wifi_file = local_dir + os.sep + self._LOCAL_FILE_WIFI
         if os.path.isfile(wifi_file):
             self.logger.info('Restoring Wifi information...')
             if not AdbHelper.adb_push(wifi_file, self._REMOTE_FILE_WIFI, serial=serial):
                 self.logger.warning('If you don\'t have root permission, you cannot restore Wifi information.')
             AdbHelper.adb_shell('chown {0} {1}'.format(self._REMOTE_FILE_WIFI_OWNER, self._REMOTE_FILE_WIFI))
         # Restore profile
         b2g_mozilla_dir = local_dir + os.sep + self._LOCAL_DIR_B2G
         if os.path.isdir(b2g_mozilla_dir):
             self.logger.info('Restore from {0} to {1} ...'.format(b2g_mozilla_dir, self._REMOTE_DIR_B2G))
             AdbHelper.adb_shell('rm -r {0}'.format(self._REMOTE_DIR_B2G))
             if not AdbHelper.adb_push(b2g_mozilla_dir, self._REMOTE_DIR_B2G, serial=serial):
                 self.logger.warning('Can not push files from {0} to {1}'.format(b2g_mozilla_dir, self._REMOTE_DIR_B2G))
         # Restore data/local
         datalocal_dir = local_dir + os.sep + self._LOCAL_DIR_DATA
         if os.path.isdir(datalocal_dir):
             self.logger.info('Restore from {0} to {1} ...'.format(datalocal_dir, self._REMOTE_DIR_DATA))
             AdbHelper.adb_shell('rm -r {0}'.format(self._REMOTE_DIR_DATA))
             if not AdbHelper.adb_push(datalocal_dir, self._REMOTE_DIR_DATA, serial=serial):
                 self.logger.warning('Can not push files from {0} to {1}'.format(datalocal_dir, self._REMOTE_DIR_DATA))
         self.logger.info('Restore profile done.')
     else:
         self.logger.info('{0}: No such file or directory'.format(local_dir))
         return
 def run(self):
     # get the device's serial number
     devices = AdbHelper.adb_devices()
     if len(devices) == 0:
         self.logger.warning("No device.")
         exit(1)
     else:
         if self.args.serial is not None and self.args.serial in devices:
             self.logger.debug("Setup serial to [{0}] by --serial".format(self.args.serial))
             device_serial = self.args.serial
         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"]
         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)
     # Backup
     if self.args.backup:
         try:
             self.logger.info("Target device [{0}]".format(device_serial))
             # Create temp folder
             tmp_dir = tempfile.mkdtemp(prefix="backup_restore_")
             # Stop B2G
             self.stop_b2g(serial=device_serial)
             # Backup User Profile
             self.backup_profile(local_dir=tmp_dir, serial=device_serial)
             # Backup SDCard
             if self.args.sdcard:
                 self.backup_sdcard(local_dir=tmp_dir, serial=device_serial)
             # Copy backup files from temp folder to target folder
             if os.path.isdir(self.args.profile_dir):
                 self.logger.warning("Removing [{0}] folder...".format(self.args.profile_dir))
                 shutil.rmtree(self.args.profile_dir)
             self.logger.info("Copy profile from [{0}] to [{1}].".format(tmp_dir, self.args.profile_dir))
             shutil.copytree(tmp_dir, self.args.profile_dir)
             # Start B2G
             if not self.args.no_reboot:
                 self.start_b2g(serial=device_serial)
         finally:
             self.logger.debug("Removing [{0}] folder...".format(tmp_dir))
             shutil.rmtree(tmp_dir)
     # Restore
     elif self.args.restore:
         self.logger.info("Target device [{0}]".format(device_serial))
         # Stop B2G
         self.stop_b2g(serial=device_serial)
         # Restore User Profile
         self.restore_profile(local_dir=self.args.profile_dir, serial=device_serial)
         # Restore SDCard
         if self.args.sdcard:
             self.restore_sdcard(local_dir=self.args.profile_dir, serial=device_serial)
         # Start B2G
         if not self.args.no_reboot:
             self.start_b2g(serial=device_serial)
 def backup_sdcard(self, local_dir, serial=None):
     self.logger.info("Backing up SD card...")
     # try to get the /sdcard folder on device
     output = AdbHelper.adb_shell("ls -d {0}; echo $?".format(self._REMOTE_DIR_SDCARD), serial=serial)
     output_list = [item for item in re.split(r"\n+", re.sub(r"\r+", "", output)) if item]
     ret_code = output_list[-1]
     output_list.remove(output_list[-1])
     ret_msg = "\n".join(output_list)
     if ret_code == "0":
         target_dir = local_dir + os.sep + self._LOCAL_DIR_SDCARD + os.sep
         os.makedirs(target_dir)
         self.logger.info("Backup: {0} to {1}".format(self._REMOTE_DIR_SDCARD, target_dir))
         if not AdbHelper.adb_pull(self._REMOTE_DIR_SDCARD, target_dir, serial=serial):
             self.logger.warning("Can not pull files from {0} to {1}".format(self._REMOTE_DIR_SDCARD, target_dir))
     else:
         self.logger.info(ret_msg)
     self.logger.info("Backup SD card done.")
    def run(self):
        devices = AdbHelper.adb_devices()
        is_no_color = self.args.no_color
        if 'NO_COLOR' in os.environ:
            try:
                is_no_color = bool(util.strtobool(os.environ['NO_COLOR'].lower()))
            except:
                print 'Invalid NO_COLOR value [{0}].'.format(os.environ['NO_COLOR'])

        if len(devices) == 0:
            print 'No device.'
            exit(1)
        elif len(devices) >= 1:
            # has --serial, then skip ANDROID_SERIAL, then list one device by --serial
            if (self.args.serial is not None):
                if self.args.serial in devices:
                    serial = self.args.serial
                    print 'Serial: {0} (State: {1})'.format(serial, devices[serial])
                    device_info = self.get_device_info(serial=serial)
                    self.print_device_info(device_info, no_color=is_no_color)
                    self.output_log([device_info])
                else:
                    print 'Can not found {0}.\nDevices:'.format(self.args.serial)
                    for device, state in devices.items():
                        print 'Serial: {0} (State: {1})'.format(device, state)
                    exit(1)
            # no --serial, but has ANDROID_SERIAL, then list one device by ANDROID_SERIAL
            elif (self.args.serial is None) and ('ANDROID_SERIAL' in os.environ):
                if os.environ['ANDROID_SERIAL'] in devices:
                    serial = os.environ['ANDROID_SERIAL']
                    print 'Serial: {0} (State: {1})'.format(serial, devices[serial])
                    device_info = self.get_device_info(serial=serial)
                    self.print_device_info(device_info, no_color=is_no_color)
                    self.output_log([device_info])
                else:
                    print 'Can not found {0}.\nDevices:'.format(os.environ['ANDROID_SERIAL'])
                    for device, state in devices.items():
                        print 'Serial: {0} (State: {1})'.format(device, state)
                    exit(1)
            # no --serial, no ANDROID_SERIAL, then list all devices
            if (self.args.serial is None) and (not 'ANDROID_SERIAL' in os.environ):
                if len(devices) > 1:
                    print 'More than one device.'
                    print 'You can specify ANDROID_SERIAL by "--serial" option.\n'
                device_info_list = []
                for device, state in devices.items():
                    print 'Serial: {0} (State: {1})'.format(device, state)
                    if state == 'device':
                        device_info = self.get_device_info(serial=device)
                        self.print_device_info(device_info, no_color=is_no_color)
                        device_info_list.append(device_info)
                    else:
                        print 'Skipped.\n'
                        device_info_list.append({'Serial': device, 'Skip': True})
                self.output_log(device_info_list)
 def restore_sdcard(self, local_dir, serial=None):
     self.logger.info('Restoring SD card...')
     target_dir = local_dir + os.sep + self._LOCAL_DIR_SDCARD
     if os.path.isdir(target_dir):
         self.logger.info('Restore: {0} to {1}'.format(target_dir, self._REMOTE_DIR_SDCARD))
         if not AdbHelper.adb_push(target_dir, self._REMOTE_DIR_SDCARD, serial=serial):
             self.logger.warning('Can not push files from {0} to {1}'.format(target_dir, self._REMOTE_DIR_SDCARD))
     else:
         self.logger.info('{0}: No such file or directory'.format(target_dir))
         return
     self.logger.info('Restore SD card done.')
 def restore_sdcard(self, local_dir, serial=None):
     self.logger.info("Restoring SD card...")
     target_dir = local_dir + os.sep + self._LOCAL_DIR_SDCARD
     if os.path.isdir(target_dir):
         self.logger.info("Restore: {0} to {1}".format(target_dir, self._REMOTE_DIR_SDCARD))
         if not AdbHelper.adb_push(target_dir, self._REMOTE_DIR_SDCARD, serial=serial):
             self.logger.warning("Can not push files from {0} to {1}".format(target_dir, self._REMOTE_DIR_SDCARD))
     else:
         self.logger.info("{0}: No such file or directory".format(target_dir))
         return
     self.logger.info("Restore SD card done.")
 def start_b2g(self, serial=None):
     self.logger.info('Start B2G.')
     output = AdbHelper.adb_shell('start b2g', serial=serial)
 def start_b2g(self, serial=None):
     self.logger.info("Start B2G.")
     output = AdbHelper.adb_shell("start b2g", serial=serial)
 def get_device_info(self, serial=None):
     try:
         tmp_dir = tempfile.mkdtemp(prefix='checkversions_')
         # pull data from device
         if not AdbHelper.adb_pull('/system/b2g/omni.ja', tmp_dir, serial=serial):
             print 'Error pulling Gecko file.'
         if not AdbHelper.adb_pull('/data/local/webapps/settings.gaiamobile.org/application.zip', tmp_dir, serial=serial):
             if not AdbHelper.adb_pull('/system/b2g/webapps/settings.gaiamobile.org/application.zip', tmp_dir, serial=serial):
                 print 'Error pulling Gaia file.'
         if not AdbHelper.adb_pull('/system/b2g/application.ini', tmp_dir, serial=serial):
             print 'Error pulling application.ini file.'
         # get Gaia info
         gaia_rev = 'n/a'
         gaia_date = 'n/a'
         application_zip_file = tmp_dir + os.sep + 'application.zip'
         if os.path.isfile(application_zip_file):
             f = open(application_zip_file, 'rb')
             z = zipfile.ZipFile(f)
             z.extract('resources/gaia_commit.txt', tmp_dir)
             f.close()
         else:
             print 'Can not find application.zip file.'
         gaiacommit_file = tmp_dir + os.sep + 'resources/gaia_commit.txt'
         if os.path.isfile(gaiacommit_file):
             f = open(gaiacommit_file, "r")
             gaia_rev = re.sub(r'\n+', '', f.readline())
             gaia_date_sec_from_epoch = re.sub(r'\n+', '', f.readline())
             f.close()
             gaia_date = datetime.utcfromtimestamp(int(gaia_date_sec_from_epoch)).strftime('%Y-%m-%d %H:%M:%S')
         else:
             print 'Can not get gaia_commit.txt file from application.zip file.'
         # deoptimize omni.ja for Gecko info
         gecko_rev = 'n/a'
         if os.path.isfile(tmp_dir + os.sep + 'omni.ja'):
             deopt_dir = tmp_dir + os.sep + 'deopt'
             deopt_file = deopt_dir + os.sep + 'omni.ja'
             deopt_exec = tmp_dir + os.sep + 'optimizejars.py'
             os.makedirs(deopt_dir)
             shutil.copyfile('./optimizejars.py', deopt_exec)
             cmd = 'python %s --deoptimize %s %s %s' % (deopt_exec, tmp_dir, tmp_dir, deopt_dir)
             p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
             output = p.communicate()[0]
             # unzip omni.ja to get Gecko info
             if os.path.isfile(deopt_file):
                 f = open(deopt_file, 'rb')
                 z = zipfile.ZipFile(f)
                 z.extract('chrome/toolkit/content/global/buildconfig.html', tmp_dir)
                 f.close()
             else:
                 print 'Can not deoptimize omni.ja file.'
                 gecko_rev = 'n/a'
             # get Gecko info from buildconfig.html file
             buildconfig_file = tmp_dir + os.sep + 'chrome/toolkit/content/global/buildconfig.html'
             if os.path.isfile(buildconfig_file):
                 for line in open(buildconfig_file, "r"):
                     if re.search(r'Built from', line):
                         ret = re.findall(r'>(.*?)<', line)
                         gecko_rev = ret[1]
                         break
             else:
                 print 'Can not get buildconfig.html file from omni.ja file.'
         else:
             print 'Can not find omni.ja file.'
         # get Gecko version, and B2G BuildID from application.ini file
         if os.path.isfile(tmp_dir + os.sep + 'application.ini'):
             for line in open(tmp_dir + os.sep + 'application.ini', "r"):
                 if re.search(r'^\s*BuildID', line):
                     ret = re.findall(r'.*?=(.*)', line)
                     build_id = ret[0]
                 if re.search(r'^\s*Version', line):
                     ret = re.findall(r'.*?=(.*)', line)
                     version = ret[0]
         else:
             build_id = 'n/a'
             version = 'n/a'
         # get device information by getprop command
         device_name = re.sub(r'\r+|\n+', '', AdbHelper.adb_shell('getprop ro.product.device', serial=serial))
         firmware_release = re.sub(r'\r+|\n+', '', AdbHelper.adb_shell('getprop ro.build.version.release', serial=serial))
         firmware_incremental = re.sub(r'\r+|\n+', '', AdbHelper.adb_shell('getprop ro.build.version.incremental', serial=serial))
         firmware_date = re.sub(r'\r+|\n+', '', AdbHelper.adb_shell('getprop ro.build.date', serial=serial))
         firmware_bootloader = re.sub(r'\r+|\n+', '', AdbHelper.adb_shell('getprop ro.boot.bootloader', serial=serial))
         # prepare the return information
         device_info = {}
         device_info['Serial'] = serial
         device_info['Build ID'] = build_id
         device_info['Gaia Revision'] = gaia_rev
         device_info['Gaia Date'] = gaia_date
         device_info['Gecko Revision'] = gecko_rev
         device_info['Gecko Version'] = version
         device_info['Device Name'] = device_name
         device_info['Firmware(Release)'] = firmware_release
         device_info['Firmware(Incremental)'] = firmware_incremental
         device_info['Firmware Date'] = firmware_date
         device_info['Bootloader'] = firmware_bootloader
     finally:
         shutil.rmtree(tmp_dir)
     return device_info
 def start_b2g(self, serial=None):
     self.logger.info('Start B2G.')
     output = AdbHelper.adb_shell('start b2g', serial=serial)
 def run(self):
     # get the device's serial number
     devices = AdbHelper.adb_devices()
     if len(devices) == 0:
         self.logger.warning('No device.')
         exit(1)
     else:
         if self.args.serial is not None and self.args.serial in devices:
             self.logger.debug('Setup serial to [{0}] by --serial'.format(self.args.serial))
             device_serial = self.args.serial
         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']
         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)
     # Backup
     if self.args.backup:
         try:
             self.logger.info('Target device [{0}]'.format(device_serial))
             # Create temp folder
             tmp_dir = tempfile.mkdtemp(prefix='backup_restore_')
             # Stop B2G
             self.stop_b2g(serial=device_serial)
             # Backup User Profile
             self.backup_profile(local_dir=tmp_dir, serial=device_serial)
             # Backup SDCard
             if self.args.sdcard:
                 self.backup_sdcard(local_dir=tmp_dir, serial=device_serial)
             # Copy backup files from temp folder to target folder
             if os.path.isdir(self.args.profile_dir):
                 self.logger.warning('Removing [{0}] folder...'.format(self.args.profile_dir))
                 shutil.rmtree(self.args.profile_dir)
             self.logger.info('Copy profile from [{0}] to [{1}].'.format(tmp_dir, self.args.profile_dir))
             shutil.copytree(tmp_dir, self.args.profile_dir)
             # Start B2G
             if not self.args.no_reboot:
                 self.start_b2g(serial=device_serial)
         finally:
             self.logger.debug('Removing [{0}] folder...'.format(tmp_dir))
             shutil.rmtree(tmp_dir)
     # Restore
     elif self.args.restore:
         self.logger.info('Target device [{0}]'.format(device_serial))
         # Checking the Version of Profile
         if self.check_profile_version(local_dir=self.args.profile_dir, serial=device_serial):
             # Stop B2G
             self.stop_b2g(serial=device_serial)
             # Restore User Profile
             self.restore_profile(local_dir=self.args.profile_dir, serial=device_serial)
             # Restore SDCard
             if self.args.sdcard:
                 self.restore_sdcard(local_dir=self.args.profile_dir, serial=device_serial)
             # Start B2G
             if not self.args.no_reboot:
                 self.start_b2g(serial=device_serial)
         else:
             self.logger.warn('The version on device is smaller than backup\'s version.')
                shutil.rmtree(tmp_dir)
        # Restore
        elif self.args.restore:
            self.logger.info("Target device [{0}]".format(device_serial))
            # Stop B2G
            self.stop_b2g(serial=device_serial)
            # Restore User Profile
            self.restore_profile(local_dir=self.args.profile_dir, serial=device_serial)
            # Restore SDCard
            if self.args.sdcard:
                self.restore_sdcard(local_dir=self.args.profile_dir, serial=device_serial)
            # Start B2G
            if not self.args.no_reboot:
                self.start_b2g(serial=device_serial)


if __name__ == "__main__":
    my_app = BackupRestoreHelper()
    # setup logger
    formatter = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
    if my_app.args.debug is True:
        logging.basicConfig(level=logging.DEBUG, format=formatter)
    else:
        logging.basicConfig(level=logging.INFO, format=formatter)
    # check adb
    if not AdbHelper.has_adb():
        print 'There is no "adb" in your environment PATH.'
        exit(1)
    # run
    my_app.run()
 def run(self):
     # get the device's serial number
     devices = AdbHelper.adb_devices()
     if len(devices) == 0:
         self.logger.warning('No device.')
         exit(1)
     else:
         if self.args.serial is not None and self.args.serial in devices:
             self.logger.debug('Setup serial to [{0}] by --serial'.format(
                 self.args.serial))
             device_serial = self.args.serial
         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']
         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)
     # Backup
     if self.args.backup:
         try:
             self.logger.info('Target device [{0}]'.format(device_serial))
             # Create temp folder
             tmp_dir = tempfile.mkdtemp(prefix='backup_restore_')
             # Stop B2G
             self.stop_b2g(serial=device_serial)
             # Backup User Profile
             self.backup_profile(local_dir=tmp_dir, serial=device_serial)
             # Backup SDCard
             if self.args.sdcard:
                 self.backup_sdcard(local_dir=tmp_dir, serial=device_serial)
             # Copy backup files from temp folder to target folder
             if os.path.isdir(self.args.profile_dir):
                 self.logger.warning('Removing [{0}] folder...'.format(
                     self.args.profile_dir))
                 shutil.rmtree(self.args.profile_dir)
             self.logger.info('Copy profile from [{0}] to [{1}].'.format(
                 tmp_dir, self.args.profile_dir))
             shutil.copytree(tmp_dir, self.args.profile_dir)
             # Start B2G
             if not self.args.no_reboot:
                 self.start_b2g(serial=device_serial)
         finally:
             self.logger.debug('Removing [{0}] folder...'.format(tmp_dir))
             shutil.rmtree(tmp_dir)
     # Restore
     elif self.args.restore:
         self.logger.info('Target device [{0}]'.format(device_serial))
         # Stop B2G
         self.stop_b2g(serial=device_serial)
         # Restore User Profile
         self.restore_profile(local_dir=self.args.profile_dir,
                              serial=device_serial)
         # Restore SDCard
         if self.args.sdcard:
             self.restore_sdcard(local_dir=self.args.profile_dir,
                                 serial=device_serial)
         # Start B2G
         if not self.args.no_reboot:
             self.start_b2g(serial=device_serial)
Exemple #21
0
 def get_device_info(self, serial=None):
     try:
         tmp_dir = tempfile.mkdtemp(prefix='checkversions_')
         # pull data from device
         if not AdbHelper.adb_pull(
                 '/system/b2g/omni.ja', tmp_dir, serial=serial):
             print 'Error pulling Gecko file.'
         if not AdbHelper.adb_pull(
                 '/data/local/webapps/settings.gaiamobile.org/application.zip',
                 tmp_dir,
                 serial=serial):
             if not AdbHelper.adb_pull(
                     '/system/b2g/webapps/settings.gaiamobile.org/application.zip',
                     tmp_dir,
                     serial=serial):
                 print 'Error pulling Gaia file.'
         if not AdbHelper.adb_pull(
                 '/system/b2g/application.ini', tmp_dir, serial=serial):
             print 'Error pulling application.ini file.'
         # get Gaia info
         gaia_rev = 'n/a'
         gaia_date = 'n/a'
         application_zip_file = tmp_dir + os.sep + 'application.zip'
         if os.path.isfile(application_zip_file):
             f = open(application_zip_file, 'rb')
             z = zipfile.ZipFile(f)
             z.extract('resources/gaia_commit.txt', tmp_dir)
             f.close()
         else:
             print 'Can not find application.zip file.'
         gaiacommit_file = tmp_dir + os.sep + 'resources/gaia_commit.txt'
         if os.path.isfile(gaiacommit_file):
             f = open(gaiacommit_file, "r")
             gaia_rev = re.sub(r'\n+', '', f.readline())
             gaia_date_sec_from_epoch = re.sub(r'\n+', '', f.readline())
             f.close()
             gaia_date = datetime.utcfromtimestamp(
                 int(gaia_date_sec_from_epoch)).strftime(
                     '%Y-%m-%d %H:%M:%S')
         else:
             print 'Can not get gaia_commit.txt file from application.zip file.'
         # deoptimize omni.ja for Gecko info
         gecko_rev = 'n/a'
         if os.path.isfile(tmp_dir + os.sep + 'omni.ja'):
             deopt_dir = tmp_dir + os.sep + 'deopt'
             deopt_file = deopt_dir + os.sep + 'omni.ja'
             deopt_exec = tmp_dir + os.sep + 'optimizejars.py'
             os.makedirs(deopt_dir)
             shutil.copyfile('./optimizejars.py', deopt_exec)
             cmd = 'python %s --deoptimize %s %s %s' % (deopt_exec, tmp_dir,
                                                        tmp_dir, deopt_dir)
             p = subprocess.Popen(cmd,
                                  shell=True,
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.STDOUT)
             output = p.communicate()[0]
             # unzip omni.ja to get Gecko info
             if os.path.isfile(deopt_file):
                 f = open(deopt_file, 'rb')
                 z = zipfile.ZipFile(f)
                 z.extract('chrome/toolkit/content/global/buildconfig.html',
                           tmp_dir)
                 f.close()
             else:
                 print 'Can not deoptimize omni.ja file.'
                 gecko_rev = 'n/a'
             # get Gecko info from buildconfig.html file
             buildconfig_file = tmp_dir + os.sep + 'chrome/toolkit/content/global/buildconfig.html'
             if os.path.isfile(buildconfig_file):
                 for line in open(buildconfig_file, "r"):
                     if re.search(r'Built from', line):
                         ret = re.findall(r'>(.*?)<', line)
                         gecko_rev = ret[1]
                         break
             else:
                 print 'Can not get buildconfig.html file from omni.ja file.'
         else:
             print 'Can not find omni.ja file.'
         # get Gecko version, and B2G BuildID from application.ini file
         if os.path.isfile(tmp_dir + os.sep + 'application.ini'):
             for line in open(tmp_dir + os.sep + 'application.ini', "r"):
                 if re.search(r'^\s*BuildID', line):
                     ret = re.findall(r'.*?=(.*)', line)
                     build_id = ret[0]
                 if re.search(r'^\s*Version', line):
                     ret = re.findall(r'.*?=(.*)', line)
                     version = ret[0]
         else:
             build_id = 'n/a'
             version = 'n/a'
         # get device information by getprop command
         device_name = re.sub(
             r'\r+|\n+', '',
             AdbHelper.adb_shell('getprop ro.product.device',
                                 serial=serial))
         firmware_release = re.sub(
             r'\r+|\n+', '',
             AdbHelper.adb_shell('getprop ro.build.version.release',
                                 serial=serial))
         firmware_incremental = re.sub(
             r'\r+|\n+', '',
             AdbHelper.adb_shell('getprop ro.build.version.incremental',
                                 serial=serial))
         firmware_date = re.sub(
             r'\r+|\n+', '',
             AdbHelper.adb_shell('getprop ro.build.date', serial=serial))
         firmware_bootloader = re.sub(
             r'\r+|\n+', '',
             AdbHelper.adb_shell('getprop ro.boot.bootloader',
                                 serial=serial))
         # prepare the return information
         device_info = {}
         device_info['Serial'] = serial
         device_info['Build ID'] = build_id
         device_info['Gaia Revision'] = gaia_rev
         device_info['Gaia Date'] = gaia_date
         device_info['Gecko Revision'] = gecko_rev
         device_info['Gecko Version'] = version
         device_info['Device Name'] = device_name
         device_info['Firmware(Release)'] = firmware_release
         device_info['Firmware(Incremental)'] = firmware_incremental
         device_info['Firmware Date'] = firmware_date
         device_info['Bootloader'] = firmware_bootloader
     finally:
         shutil.rmtree(tmp_dir)
     return device_info
Exemple #22
0
    def run(self):
        devices = AdbHelper.adb_devices()
        is_no_color = self.args.no_color
        if 'NO_COLOR' in os.environ:
            try:
                is_no_color = bool(
                    util.strtobool(os.environ['NO_COLOR'].lower()))
            except:
                print 'Invalid NO_COLOR value [{0}].'.format(
                    os.environ['NO_COLOR'])

        if len(devices) == 0:
            print 'No device.'
            exit(1)
        elif len(devices) >= 1:
            # has --serial, then skip ANDROID_SERIAL, then list one device by --serial
            if (self.args.serial is not None):
                if self.args.serial in devices:
                    serial = self.args.serial
                    print 'Serial: {0} (State: {1})'.format(
                        serial, devices[serial])
                    device_info = self.get_device_info(serial=serial)
                    self.print_device_info(device_info, no_color=is_no_color)
                    self.output_log([device_info])
                else:
                    print 'Can not found {0}.\nDevices:'.format(
                        self.args.serial)
                    for device, state in devices.items():
                        print 'Serial: {0} (State: {1})'.format(device, state)
                    exit(1)
            # no --serial, but has ANDROID_SERIAL, then list one device by ANDROID_SERIAL
            elif (self.args.serial is None) and ('ANDROID_SERIAL'
                                                 in os.environ):
                if os.environ['ANDROID_SERIAL'] in devices:
                    serial = os.environ['ANDROID_SERIAL']
                    print 'Serial: {0} (State: {1})'.format(
                        serial, devices[serial])
                    device_info = self.get_device_info(serial=serial)
                    self.print_device_info(device_info, no_color=is_no_color)
                    self.output_log([device_info])
                else:
                    print 'Can not found {0}.\nDevices:'.format(
                        os.environ['ANDROID_SERIAL'])
                    for device, state in devices.items():
                        print 'Serial: {0} (State: {1})'.format(device, state)
                    exit(1)
            # no --serial, no ANDROID_SERIAL, then list all devices
            if (self.args.serial is None) and (not 'ANDROID_SERIAL'
                                               in os.environ):
                if len(devices) > 1:
                    print 'More than one device.'
                    print 'You can specify ANDROID_SERIAL by "--serial" option.\n'
                device_info_list = []
                for device, state in devices.items():
                    print 'Serial: {0} (State: {1})'.format(device, state)
                    if state == 'device':
                        device_info = self.get_device_info(serial=device)
                        self.print_device_info(device_info,
                                               no_color=is_no_color)
                        device_info_list.append(device_info)
                    else:
                        print 'Skipped.\n'
                        device_info_list.append({
                            'Serial': device,
                            'Skip': True
                        })
                self.output_log(device_info_list)
Exemple #23
0
            # no --serial, no ANDROID_SERIAL, then list all devices
            if (self.args.serial is None) and (not 'ANDROID_SERIAL'
                                               in os.environ):
                if len(devices) > 1:
                    print 'More than one device.'
                    print 'You can specify ANDROID_SERIAL by "--serial" option.\n'
                device_info_list = []
                for device, state in devices.items():
                    print 'Serial: {0} (State: {1})'.format(device, state)
                    if state == 'device':
                        device_info = self.get_device_info(serial=device)
                        self.print_device_info(device_info,
                                               no_color=is_no_color)
                        device_info_list.append(device_info)
                    else:
                        print 'Skipped.\n'
                        device_info_list.append({
                            'Serial': device,
                            'Skip': True
                        })
                self.output_log(device_info_list)


if __name__ == "__main__":
    if not AdbHelper.has_adb():
        print 'There is no "adb" in your environment PATH.'
        exit(1)

    my_app = VersionChecker()
    my_app.run()
Exemple #24
0
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect the socket to the port where the server is listening
server_address = (control_server_ip, control_server_port)
print 'Connecting to %s:%s' % server_address
sock.connect(server_address)

data = sock.recv(1024).strip()
if data <> "<< Control Server Connected >>":
    print "Successfully connected to the control server.\n"
    sys.exit()

try:
    # Send data
    adb_devices = AdbHelper.adb_devices()
    for device in adb_devices.items():
        message = ""

        # Check for device type
        if device[1] == "device":
            device_type = re.sub(r'\r+|\n+', '', AdbHelper.adb_shell('getprop ro.product.device', serial=device[0]))
        else:
            device_type = "unknown"

        # Check for device build
        if device[1] == "device":
            device_build = re.sub(r'\r+|\n+', '', AdbHelper.adb_shell('getprop ro.build.version.incremental', serial=device[0]))
        else:
            device_build = "unknown"
        if self.args.log_text is not None:
            with open(self.args.log_text, 'w') as outfile:
                for device_serial, device_info in result.items():
                    outfile.write('# %s\n' % device_serial)
                    if 'Skip' in device_info and device_info['Skip'] is True:
                        outfile.write('%s=%s\n' % ('Skip', device_info['Skip']))
                    else:
                        for key, value in device_info.items():
                            outfile.write('%s=%s\n' % (re.sub(r'\s+|\(|\)', '', key), re.sub(r'\s+', '_', value)))
                        outfile.write('\n')
        if self.args.log_json is not None:
            with open(self.args.log_json, 'w') as outfile:
                json.dump(result, outfile, indent = 4)

if __name__ == "__main__":
    if not AdbHelper.has_adb():
        print 'There is no "adb" in your environment PATH.'
        exit(1)

    my_app = VersionChecker()
    devices = AdbHelper.adb_devices()

    if len(devices) == 0:
        print 'No device.'
        exit(1)
    elif len(devices) >= 1:
        # has --serial, then skip ANDROID_SERIAL, then list one device by --serial
        if (my_app.args.serial is not None):
            if my_app.args.serial in devices:
                serial = my_app.args.serial
                print 'Serial: {0} (State: {1})'.format(serial, devices[serial])