def backup_sdcard(self, local_dir, serial=None):
        """
        Backup data from device's SDCard to local folder.

        @param local_dir: the target local folder, will store data from device's SDCard to this folder.
        """
        logger.info('Backing up SD card...')
        # try to get the /sdcard folder on device
        output, retcode = AdbWrapper.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 = os.path.join(local_dir, self._LOCAL_DIR_SDCARD)
            os.makedirs(target_dir)
            logger.info('Backup: {0} to {1}'.format(self._REMOTE_DIR_SDCARD, target_dir))
            try:
                AdbWrapper.adb_pull(self._REMOTE_DIR_SDCARD, target_dir, serial=serial)
            except Exception as e:
                logger.debug(e)
                logger.error('Can not pull files from {0} to {1}.'.format(self._REMOTE_DIR_SDCARD, target_dir))
            logger.info('Backup SD card done.')
        else:
            logger.info(ret_msg)
 def backup_sdcard(self, local_dir, serial=None):
     logger.info('Backing up SD card...')
     # try to get the /sdcard folder on device
     output, retcode = AdbWrapper.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 = os.path.join(local_dir, self._LOCAL_DIR_SDCARD)
         os.makedirs(target_dir)
         logger.info('Backup: {0} to {1}'.format(self._REMOTE_DIR_SDCARD,
                                                 target_dir))
         try:
             AdbWrapper.adb_pull(self._REMOTE_DIR_SDCARD,
                                 target_dir,
                                 serial=serial)
         except:
             logger.warning('Can not pull files from {0} to {1}'.format(
                 self._REMOTE_DIR_SDCARD, target_dir))
     else:
         logger.info(ret_msg)
     logger.info('Backup SD card done.')
    def backup_sdcard(self, local_dir, serial=None):
        """
        Backup data from device's SDCard to local folder.

        @param local_dir: the target local folder, will store data from device's SDCard to this folder.
        """
        logger.info('Backing up SD card...')
        # try to get the /sdcard folder on device
        output, retcode = AdbWrapper.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 = os.path.join(local_dir, self._LOCAL_DIR_SDCARD)
            os.makedirs(target_dir)
            logger.info('Backup: {0} to {1}'.format(self._REMOTE_DIR_SDCARD,
                                                    target_dir))
            try:
                AdbWrapper.adb_pull(self._REMOTE_DIR_SDCARD,
                                    target_dir,
                                    serial=serial)
            except Exception as e:
                logger.debug(e)
                logger.error('Can not pull files from {0} to {1}.'.format(
                    self._REMOTE_DIR_SDCARD, target_dir))
            logger.info('Backup SD card done.')
        else:
            logger.info(ret_msg)
    def set_certapps(self, enable=True, serial=None):
        AdbWrapper.adb_root(serial=serial)
        logger.info('{} Full Privilege for WebIDE...'.format('Enabling' if enable else 'Disabling'))

        need_restart = True
        try:
            tmp_dir = tempfile.mkdtemp(prefix='enablecertapps_')
            # get profile folder name xxx.default under /data/b2g/mozilla/
            profile_dir_name, retcode = AdbWrapper.adb_shell('ls /data/b2g/mozilla/ | grep default', serial=serial)
            device_src_file = os.path.join('/data/b2g/mozilla/', profile_dir_name, 'prefs.js')
            dest_temp_file = os.path.join(tmp_dir, 'prefs.js.origin')
            try:
                logger.info('Pulling prefs.js file...')
                AdbWrapper.adb_pull(device_src_file, dest_temp_file, serial=serial)
            except:
                raise Exception('Error pulling prefs.js file.')

            dest_file = os.path.join(tmp_dir, 'prefs.js')
            with open(dest_temp_file, 'r') as fr:
                with open(dest_file, 'w') as fw:
                    match = False
                    is_forbid = 'false' if enable else 'true'
                    logger.debug('is_forbid: [{}]'.format(is_forbid))
                    for line in fr:
                        if 'devtools.debugger.forbid-certified-apps' in line:
                            logger.debug('line: [{}] to [{}]'.format(line, is_forbid))
                            if is_forbid in line:
                                # do not need to restart if the setting isn't changed
                                logger.info('The full privilege is already {}.'.format('enabled' if enable else 'disabled'))
                                need_restart = False
                                break
                            else:
                                logger.info('Changing setting of pref.js file...')
                                fw.write('user_pref("devtools.debugger.forbid-certified-apps", {});\n'.format(is_forbid))
                            match = True
                        else:
                            fw.write(line)
                    if not match:
                        if not enable:
                            # the forbid is true when there is no setting
                            logger.info('The full privilege is already disabled.')
                            need_restart = False
                        else:
                            if need_restart:
                                # adding setting when there is no setting and need to enable certapps
                                logger.info('Adding setting of pref.js file...')
                                fw.write('user_pref("devtools.debugger.forbid-certified-apps", {});\n'.format(is_forbid))
            if need_restart:
                B2GHelper.stop_b2g(serial=serial)
                try:
                    logger.info('Pushing prefs.js file...')
                    AdbWrapper.adb_push(dest_file, device_src_file, serial=serial)
                except:
                    raise Exception('Error pushing prefs.js file.')
        finally:
            if need_restart:
                B2GHelper.start_b2g(serial=serial)
            shutil.rmtree(tmp_dir)
    def _check_profile_version(self, local_dir, serial=None):
        """
        Check the versions of backup and device.
        The lower backup can restore to device. However the higher backup cannot.

        @param local_dir: the local backup folder.
        @param serial: device serial number. (optional)

        @return: True if backup version is lower than device's.

        @raise exception: if cannot load profiles or versions.
        """
        if self.skip_version_check:
            logger.info('Skip version check.')
            return True
        logger.info('Checking profile...')
        # get local version
        if os.path.isdir(local_dir):
            local_profile_path = self._get_profile_path(
                os.path.join(local_dir, self._LOCAL_DIR_B2G, self._FILE_PROFILE_INI))
            version_of_backup = self._get_version_from_profile(
                os.path.join(local_dir, self._LOCAL_DIR_B2G, local_profile_path, self._FILE_COMPATIBILITY_INI))
        else:
            raise Exception('Can not load profile from [{}]'.format(os.path.abspath(local_dir)))
        tmp_dir = None
        try:
            # get remote version
            tmp_dir = tempfile.mkdtemp(prefix='backup_restore_')
            logger.debug('TEMP Folder for check profile: {}'.format(tmp_dir))
            try:
                AdbWrapper.adb_pull(os.path.join(self._REMOTE_DIR_B2G, self._FILE_PROFILE_INI), tmp_dir, serial=serial)
            except:
                raise Exception(
                    'Can not pull {2} from {0} to {1}. '
                    'Please run with --skip-version-check if you want to restore.'.format(
                        self._REMOTE_DIR_B2G, tmp_dir, self._FILE_PROFILE_INI))
            remote_profile_path = self._get_profile_path(os.path.join(tmp_dir, self._FILE_PROFILE_INI))
            try:
                AdbWrapper.adb_pull(
                    os.path.join(self._REMOTE_DIR_B2G, remote_profile_path, self._FILE_COMPATIBILITY_INI), tmp_dir,
                    serial=serial)
            except:
                raise Exception(
                    'Can not pull {2} from {0} to {1}. '
                    'Please run with --skip-version-check if you want to restore.'.format(
                        self._REMOTE_DIR_B2G, tmp_dir, self._FILE_COMPATIBILITY_INI))
            version_of_device = self._get_version_from_profile(
                os.path.join(os.path.join(tmp_dir, self._FILE_COMPATIBILITY_INI)))
            # compare
            return self._compare_version(version_of_backup, version_of_device)
        finally:
            if tmp_dir:
                logger.debug('Removing [{0}] folder...'.format(tmp_dir))
                shutil.rmtree(tmp_dir)
                logger.debug('TEMP Folder for check profile removed: {}'.format(tmp_dir))
 def check_profile_version(self, local_dir, serial=None):
     logger.info('Checking profile...')
     # get local version
     if os.path.isdir(local_dir):
         local_config = ConfigParser.ConfigParser()
         local_config.read(os.path.join(local_dir, self._LOCAL_DIR_B2G, self._FILE_PROFILE_INI))
         local_profile_path = local_config.get('Profile0', 'Path')
         local_config.read(os.path.join(local_dir, self._LOCAL_DIR_B2G, local_profile_path, self._FILE_COMPATIBILITY_INI))
         logger.debug('Local Profile: {}'.format(local_config._sections))
         version_of_backup = local_config.get('Compatibility', 'LastVersion')
         logger.info('The Version of Backup Profile: {}'.format(version_of_backup))
     else:
         return False
     try:
         # get remote version
         tmp_dir = tempfile.mkdtemp(prefix='backup_restore_')
         logger.debug('TEMP Folder for check profile: {}'.format(tmp_dir))
         try:
             AdbWrapper.adb_pull(os.path.join(self._REMOTE_DIR_B2G, self._FILE_PROFILE_INI), tmp_dir, serial=serial)
         except:
             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(os.path.join(tmp_dir, self._FILE_PROFILE_INI))
         logger.debug('Remote Profile to get path: {}'.format(remote_config._sections))
         remote_profile_path = remote_config.get('Profile0', 'Path')
         try:
             AdbWrapper.adb_pull(os.path.join(self._REMOTE_DIR_B2G, remote_profile_path, self._FILE_COMPATIBILITY_INI), tmp_dir, serial=serial)
         except:
             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(os.path.join(tmp_dir, self._FILE_COMPATIBILITY_INI))
         logger.debug('Remote Profile: {}'.format(remote_config._sections))
         version_of_device = remote_config.get('Compatibility', 'LastVersion')
         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])
         logger.debug('Local Ver: {}, Remote Ver: {}'.format(version_of_backup_float, version_of_device_float))
         if version_of_device_float >= version_of_backup_float:
             return True
         else:
             return False
     finally:
         logger.debug('Removing [{0}] folder...'.format(tmp_dir))
         shutil.rmtree(tmp_dir)
         logger.debug('TEMP Folder for check profile removed: {}'.format(tmp_dir))
 def backup_sdcard(self, local_dir, serial=None):
     logger.info('Backing up SD card...')
     # try to get the /sdcard folder on device
     output, retcode = AdbWrapper.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 = os.path.join(local_dir, self._LOCAL_DIR_SDCARD)
         os.makedirs(target_dir)
         logger.info('Backup: {0} to {1}'.format(self._REMOTE_DIR_SDCARD, target_dir))
         try:
             AdbWrapper.adb_pull(self._REMOTE_DIR_SDCARD, target_dir, serial=serial)
         except:
             logger.warning('Can not pull files from {0} to {1}'.format(self._REMOTE_DIR_SDCARD, target_dir))
     else:
         logger.info(ret_msg)
     logger.info('Backup SD card done.')
Ejemplo n.º 8
0
    def _check_profile_version(self, local_dir, serial=None):
        '''
        Check the versions of backup and device.
        The lower backup can restore to device. However the higher backup cannot.

        @param local_dir: the local backup folder.
        @param serial: device serial number. (optional)

        @return: True if backup version is lower than device's.

        @raise exception: if cannot load profiles or versions.
        '''
        if self.args.skip_version_check:
            logger.info('Skip version check.')
            return True
        logger.info('Checking profile...')
        # get local version
        if os.path.isdir(local_dir):
            local_profile_path = self._get_profile_path(os.path.join(local_dir, self._LOCAL_DIR_B2G, self._FILE_PROFILE_INI))
            version_of_backup = self._get_version_from_profile(os.path.join(local_dir, self._LOCAL_DIR_B2G, local_profile_path, self._FILE_COMPATIBILITY_INI))
        else:
            raise Exception('Can not load profile from [{}]'.format(os.path.abspath(local_dir)))
        try:
            # get remote version
            tmp_dir = tempfile.mkdtemp(prefix='backup_restore_')
            logger.debug('TEMP Folder for check profile: {}'.format(tmp_dir))
            try:
                AdbWrapper.adb_pull(os.path.join(self._REMOTE_DIR_B2G, self._FILE_PROFILE_INI), tmp_dir, serial=serial)
            except:
                raise Exception('Can not pull {2} from {0} to {1}. Please run with --skip-version-check if you want to restore.'.format(self._REMOTE_DIR_B2G, tmp_dir, self._FILE_PROFILE_INI))
            remote_profile_path = self._get_profile_path(os.path.join(tmp_dir, self._FILE_PROFILE_INI))
            try:
                AdbWrapper.adb_pull(os.path.join(self._REMOTE_DIR_B2G, remote_profile_path, self._FILE_COMPATIBILITY_INI), tmp_dir, serial=serial)
            except:
                raise Exception('Can not pull {2} from {0} to {1}. Please run with --skip-version-check if you want to restore.'.format(self._REMOTE_DIR_B2G, tmp_dir, self._FILE_COMPATIBILITY_INI))
            version_of_device = self._get_version_from_profile(os.path.join(os.path.join(tmp_dir, self._FILE_COMPATIBILITY_INI)))
            # compare
            return self._compare_version(version_of_backup, version_of_device)
        finally:
            logger.debug('Removing [{0}] folder...'.format(tmp_dir))
            shutil.rmtree(tmp_dir)
            logger.debug('TEMP Folder for check profile removed: {}'.format(tmp_dir))
    def backup_profile(self, local_dir, serial=None):
        """
        Backup B2G user profile from device to local folder.

        @param local_dir: the target local folder, the backup data will store to this folder.
        @param serial: device serial number. (optional)
        """
        logger.info('Backing up profile...')
        # Backup Wifi
        wifi_dir = os.path.join(local_dir, self._LOCAL_DIR_WIFI)
        wifi_file = os.path.join(local_dir, self._LOCAL_FILE_WIFI)
        os.makedirs(wifi_dir)
        logger.info('Backing up Wifi information...')
        try:
            AdbWrapper.adb_pull(self._REMOTE_FILE_WIFI, wifi_file, serial=serial)
        except Exception as e:
            logger.debug(e)
            logger.error('If you don\'t have root permission, you cannot backup Wifi information.')
        # Backup profile
        b2g_mozilla_dir = os.path.join(local_dir, self._LOCAL_DIR_B2G)
        os.makedirs(b2g_mozilla_dir)
        logger.info('Backing up {0} to {1} ...'.format(self._REMOTE_DIR_B2G, b2g_mozilla_dir))
        try:
            AdbWrapper.adb_pull(self._REMOTE_DIR_B2G, b2g_mozilla_dir, serial=serial)
        except Exception as e:
            logger.debug(e)
            logger.error('Can not pull files from {0} to {1}'.format(self._REMOTE_DIR_B2G, b2g_mozilla_dir))
        # Backup data/local
        datalocal_dir = os.path.join(local_dir, self._LOCAL_DIR_DATA)
        os.makedirs(datalocal_dir)
        logger.info('Backing up {0} to {1} ...'.format(self._REMOTE_DIR_DATA, datalocal_dir))
        try:
            AdbWrapper.adb_pull(self._REMOTE_DIR_DATA, datalocal_dir, serial=serial)
        except Exception as e:
            logger.debug(e)
            logger.error('Can not pull files from {0} to {1}'.format(self._REMOTE_DIR_DATA, datalocal_dir))

        # remove gecko.mstone value, so that gecko can check the apps under /system/b2g/webapps again.
        # (Thanks SC Chien's help!)
        local_profile_path = self._get_profile_path(os.path.join(b2g_mozilla_dir, self._FILE_PROFILE_INI))
        local_perf = os.path.join(b2g_mozilla_dir, local_profile_path, self._FILE_PERF_JS)
        perf_contents = []
        with open(local_perf, 'r') as f:
            for line in f.readlines():
                if 'gecko.mstone' not in line:
                    perf_contents.append(line)
                else:
                    logger.debug('Remove from {}: {}'.format(local_perf, line))
        with open(local_perf, 'w') as f:
            for line in perf_contents:
                f.write(line)
        logger.info('Backup profile done.')
 def backup_profile(self, local_dir, serial=None):
     logger.info('Backing up profile...')
     # Backup Wifi
     wifi_dir = os.path.join(local_dir, self._LOCAL_DIR_WIFI)
     wifi_file = os.path.join(local_dir, self._LOCAL_FILE_WIFI)
     os.makedirs(wifi_dir)
     logger.info('Backing up Wifi information...')
     try:
         AdbWrapper.adb_pull(self._REMOTE_FILE_WIFI,
                             wifi_file,
                             serial=serial)
     except:
         logger.warning(
             'If you don\'t have root permission, you cannot backup Wifi information.'
         )
     # Backup profile
     b2g_mozilla_dir = os.path.join(local_dir, self._LOCAL_DIR_B2G)
     os.makedirs(b2g_mozilla_dir)
     logger.info('Backing up {0} to {1} ...'.format(self._REMOTE_DIR_B2G,
                                                    b2g_mozilla_dir))
     try:
         AdbWrapper.adb_pull(self._REMOTE_DIR_B2G,
                             b2g_mozilla_dir,
                             serial=serial)
     except:
         logger.warning('Can not pull files from {0} to {1}'.format(
             self._REMOTE_DIR_B2G, b2g_mozilla_dir))
     # Backup data/local
     datalocal_dir = os.path.join(local_dir, self._LOCAL_DIR_DATA)
     os.makedirs(datalocal_dir)
     logger.info('Backing up {0} to {1} ...'.format(self._REMOTE_DIR_DATA,
                                                    datalocal_dir))
     try:
         AdbWrapper.adb_pull(self._REMOTE_DIR_DATA,
                             datalocal_dir,
                             serial=serial)
     except:
         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')):
             logger.info('Removing Mozilla webapps: [{0}]'.format(root))
             shutil.rmtree(root)
     logger.info('Backup profile done.')
    def backup_profile(self, local_dir, serial=None):
        '''
        Backup B2G user profile from device to local folder.

        @param local_dir: the target local folder, the backup data will store to this folder.
        @param serial: device serial number. (optional)
        '''
        logger.info('Backing up profile...')
        # Backup Wifi
        wifi_dir = os.path.join(local_dir, self._LOCAL_DIR_WIFI)
        wifi_file = os.path.join(local_dir, self._LOCAL_FILE_WIFI)
        os.makedirs(wifi_dir)
        logger.info('Backing up Wifi information...')
        try:
            AdbWrapper.adb_pull(self._REMOTE_FILE_WIFI, wifi_file, serial=serial)
        except:
            logger.error('If you don\'t have root permission, you cannot backup Wifi information.')
        # Backup profile
        b2g_mozilla_dir = os.path.join(local_dir, self._LOCAL_DIR_B2G)
        os.makedirs(b2g_mozilla_dir)
        logger.info('Backing up {0} to {1} ...'.format(self._REMOTE_DIR_B2G, b2g_mozilla_dir))
        try:
            AdbWrapper.adb_pull(self._REMOTE_DIR_B2G, b2g_mozilla_dir, serial=serial)
        except:
            logger.error('Can not pull files from {0} to {1}'.format(self._REMOTE_DIR_B2G, b2g_mozilla_dir))
        # Backup data/local
        datalocal_dir = os.path.join(local_dir, self._LOCAL_DIR_DATA)
        os.makedirs(datalocal_dir)
        logger.info('Backing up {0} to {1} ...'.format(self._REMOTE_DIR_DATA, datalocal_dir))
        try:
            AdbWrapper.adb_pull(self._REMOTE_DIR_DATA, datalocal_dir, serial=serial)
        except:
            logger.error('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')):
                logger.info('Removing Mozilla webapps: [{0}]'.format(root))
                shutil.rmtree(root)
        logger.info('Backup profile done.')
Ejemplo n.º 12
0
    def set_certapps(self, enable=True, serial=None):
        '''
        Set the devtools permission for certapps.
        @param enable: True will turn on the permission. False will turn off the permission.
        @param serial: device serial number. (optional)
        @raise exception: When it cannot pulling/pushing the pref.js file of device.
        '''
        AdbWrapper.adb_root(serial=serial)
        logger.info('{} Full Privilege for WebIDE...'.format(
            'Enabling' if enable else 'Disabling'))

        need_restart = True
        try:
            tmp_dir = tempfile.mkdtemp(prefix='enablecertapps_')
            # get profile folder name xxx.default under /data/b2g/mozilla/
            profile_dir_name, retcode = AdbWrapper.adb_shell(
                'ls /data/b2g/mozilla/ | grep default', serial=serial)
            device_src_file = os.path.join('/data/b2g/mozilla/',
                                           profile_dir_name, 'prefs.js')
            dest_temp_file = os.path.join(tmp_dir, 'prefs.js.origin')
            try:
                logger.info('Pulling prefs.js file...')
                AdbWrapper.adb_pull(device_src_file,
                                    dest_temp_file,
                                    serial=serial)
            except:
                raise Exception('Error pulling prefs.js file.')

            dest_file = os.path.join(tmp_dir, 'prefs.js')
            with open(dest_temp_file, 'r') as fr:
                with open(dest_file, 'w') as fw:
                    match = False
                    is_forbid = 'false' if enable else 'true'
                    logger.debug('is_forbid: [{}]'.format(is_forbid))
                    for line in fr:
                        if 'devtools.debugger.forbid-certified-apps' in line:
                            logger.debug('line: [{}] to [{}]'.format(
                                line, is_forbid))
                            if is_forbid in line:
                                # do not need to restart if the setting isn't changed
                                logger.info(
                                    'The full privilege is already {}.'.format(
                                        'enabled' if enable else 'disabled'))
                                need_restart = False
                                break
                            else:
                                logger.info(
                                    'Changing setting of pref.js file...')
                                fw.write(
                                    'user_pref("devtools.debugger.forbid-certified-apps", {});\n'
                                    .format(is_forbid))
                            match = True
                        else:
                            fw.write(line)
                    if not match:
                        if not enable:
                            # the forbid is true when there is no setting
                            logger.info(
                                'The full privilege is already disabled.')
                            need_restart = False
                        else:
                            if need_restart:
                                # adding setting when there is no setting and need to enable certapps
                                logger.info(
                                    'Adding setting of pref.js file...')
                                fw.write(
                                    'user_pref("devtools.debugger.forbid-certified-apps", {});\n'
                                    .format(is_forbid))
            if need_restart:
                B2GHelper.stop_b2g(serial=serial)
                try:
                    logger.info('Pushing prefs.js file...')
                    AdbWrapper.adb_push(dest_file,
                                        device_src_file,
                                        serial=serial)
                except:
                    raise Exception('Error pushing prefs.js file.')
        finally:
            if need_restart:
                B2GHelper.start_b2g(serial=serial)
            shutil.rmtree(tmp_dir)
Ejemplo n.º 13
0
 def get_device_info(serial=None):
     """
     Get the device information, include Gaia Version, Gecko Version, and so on.
     @param serial: device serial number. (optional)
     @return: the information dict object.
     """
     tmp_dir = None
     try:
         tmp_dir = tempfile.mkdtemp(prefix='checkversions_')
         # pull data from device
         try:
             AdbWrapper.adb_pull('/system/b2g/omni.ja', tmp_dir, serial=serial)
         except Exception as e:
             logger.debug(e)
             logger.error('Error pulling Gecko file.')
         try:
             AdbWrapper.adb_pull('/data/local/webapps/settings.gaiamobile.org/application.zip', tmp_dir,
                                 serial=serial)
         except Exception as e:
             logger.debug(e)
             try:
                 AdbWrapper.adb_pull('/system/b2g/webapps/settings.gaiamobile.org/application.zip', tmp_dir,
                                     serial=serial)
             except Exception as e:
                 logger.debug(e)
                 logger.error('Error pulling Gaia file.')
         try:
             AdbWrapper.adb_pull('/system/b2g/application.ini', tmp_dir, serial=serial)
         except Exception as e:
             logger.debug(e)
             logger.error('Error pulling application.ini file.')
         # get Gaia info
         gaia_rev = 'n/a'
         gaia_date = 'n/a'
         application_zip_file = os.path.join(tmp_dir, 'application.zip')
         if os.path.isfile(application_zip_file):
             with open(application_zip_file, 'rb') as f:
                 z = zipfile.ZipFile(f)
                 z.extract('resources/gaia_commit.txt', tmp_dir)
         else:
             logger.warning('Can not find application.zip file.')
         gaiacommit_file = os.path.join(tmp_dir, 'resources/gaia_commit.txt')
         if os.path.isfile(gaiacommit_file):
             with open(gaiacommit_file, "r") as f:
                 gaia_rev = re.sub(r'\n+', '', f.readline())
                 gaia_date_sec_from_epoch = re.sub(r'\n+', '', f.readline())
             gaia_date = datetime.utcfromtimestamp(int(gaia_date_sec_from_epoch)).strftime('%Y-%m-%d %H:%M:%S')
         else:
             logger.warning('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(os.path.join(tmp_dir, 'omni.ja')):
             deopt_dir = os.path.join(tmp_dir, 'deopt')
             deopt_file = os.path.join(deopt_dir, 'omni.ja')
             deopt_exec = os.path.join(tmp_dir, 'optimizejars.py')
             os.makedirs(deopt_dir)
             # TODO rewrite optimizejars.py if possible
             current_dir = os.path.dirname(os.path.abspath(__file__))
             current_exec = os.path.join(current_dir, 'misc', 'optimizejars.py')
             shutil.copyfile(current_exec, 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]
             logger.debug('optimizejars.py stdout: {}'.format(output))
             # unzip omni.ja to get Gecko info
             if os.path.isfile(deopt_file):
                 with open(deopt_file, 'rb') as f:
                     z = zipfile.ZipFile(f)
                     z.extract('chrome/toolkit/content/global/buildconfig.html', tmp_dir)
             else:
                 logger.warning('Can not deoptimize omni.ja file.')
                 gecko_rev = 'n/a'
             # get Gecko info from buildconfig.html file
             buildconfig_file = os.path.join(tmp_dir, '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:
                 logger.warning('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
         build_id = 0
         version = 0
         if os.path.isfile(os.path.join(tmp_dir, 'application.ini')):
             for line in open(os.path.join(tmp_dir, '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+', '', AdbWrapper.adb_shell('getprop ro.product.device', serial=serial)[0])
         firmware_release = re.sub(r'\r+|\n+', '',
                                   AdbWrapper.adb_shell('getprop ro.build.version.release', serial=serial)[0])
         firmware_incremental = re.sub(r'\r+|\n+', '',
                                       AdbWrapper.adb_shell('getprop ro.build.version.incremental', serial=serial)[
                                           0])
         firmware_date = re.sub(r'\r+|\n+', '', AdbWrapper.adb_shell('getprop ro.build.date', serial=serial)[0])
         firmware_bootloader = re.sub(r'\r+|\n+', '',
                                      AdbWrapper.adb_shell('getprop ro.boot.bootloader', serial=serial)[0])
         # prepare the return information
         device_info = {'Serial': serial,
                        'Build ID': build_id,
                        'Gaia Revision': gaia_rev,
                        'Gaia Date': gaia_date,
                        'Gecko Revision': gecko_rev,
                        'Gecko Version': version,
                        'Device Name': device_name,
                        'Firmware(Release)': firmware_release,
                        'Firmware(Incremental)': firmware_incremental,
                        'Firmware Date': firmware_date,
                        'Bootloader': firmware_bootloader}
     finally:
         if tmp_dir:
             shutil.rmtree(tmp_dir)
             logger.debug('Remove {}.'.format(tmp_dir))
     return device_info
Ejemplo n.º 14
0
 def get_device_info(self, serial=None):
     """
     Get the device information, include Gaia Version, Gecko Version, and so on.
     @param serial: device serial number. (optional)
     @return: the information dict object.
     """
     try:
         tmp_dir = tempfile.mkdtemp(prefix="checkversions_")
         # pull data from device
         try:
             AdbWrapper.adb_pull("/system/b2g/omni.ja", tmp_dir, serial=serial)
         except:
             logger.error("Error pulling Gecko file.")
         try:
             AdbWrapper.adb_pull(
                 "/data/local/webapps/settings.gaiamobile.org/application.zip", tmp_dir, serial=serial
             )
         except:
             try:
                 AdbWrapper.adb_pull(
                     "/system/b2g/webapps/settings.gaiamobile.org/application.zip", tmp_dir, serial=serial
                 )
             except:
                 logger.error("Error pulling Gaia file.")
         try:
             AdbWrapper.adb_pull("/system/b2g/application.ini", tmp_dir, serial=serial)
         except:
             logger.error("Error pulling application.ini file.")
         # get Gaia info
         gaia_rev = "n/a"
         gaia_date = "n/a"
         application_zip_file = os.path.join(tmp_dir, "application.zip")
         if os.path.isfile(application_zip_file):
             with open(application_zip_file, "rb") as f:
                 z = zipfile.ZipFile(f)
                 z.extract("resources/gaia_commit.txt", tmp_dir)
         else:
             logger.warning("Can not find application.zip file.")
         gaiacommit_file = os.path.join(tmp_dir, "resources/gaia_commit.txt")
         if os.path.isfile(gaiacommit_file):
             with open(gaiacommit_file, "r") as f:
                 gaia_rev = re.sub(r"\n+", "", f.readline())
                 gaia_date_sec_from_epoch = re.sub(r"\n+", "", f.readline())
             gaia_date = datetime.utcfromtimestamp(int(gaia_date_sec_from_epoch)).strftime("%Y-%m-%d %H:%M:%S")
         else:
             logger.warning("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(os.path.join(tmp_dir, "omni.ja")):
             deopt_dir = os.path.join(tmp_dir, "deopt")
             deopt_file = os.path.join(deopt_dir, "omni.ja")
             deopt_exec = os.path.join(tmp_dir, "optimizejars.py")
             os.makedirs(deopt_dir)
             # TODO rewrite optimizejars.py if possible
             current_dir = cur = os.path.dirname(os.path.abspath(__file__))
             current_exec = os.path.join(current_dir, "misc", "optimizejars.py")
             shutil.copyfile(current_exec, 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):
                 with open(deopt_file, "rb") as f:
                     z = zipfile.ZipFile(f)
                     z.extract("chrome/toolkit/content/global/buildconfig.html", tmp_dir)
             else:
                 logger.warning("Can not deoptimize omni.ja file.")
                 gecko_rev = "n/a"
             # get Gecko info from buildconfig.html file
             buildconfig_file = os.path.join(tmp_dir, "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:
                 logger.warning("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(os.path.join(tmp_dir, "application.ini")):
             for line in open(os.path.join(tmp_dir, "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+", "", AdbWrapper.adb_shell("getprop ro.product.device", serial=serial)[0])
         firmware_release = re.sub(
             r"\r+|\n+", "", AdbWrapper.adb_shell("getprop ro.build.version.release", serial=serial)[0]
         )
         firmware_incremental = re.sub(
             r"\r+|\n+", "", AdbWrapper.adb_shell("getprop ro.build.version.incremental", serial=serial)[0]
         )
         firmware_date = re.sub(r"\r+|\n+", "", AdbWrapper.adb_shell("getprop ro.build.date", serial=serial)[0])
         firmware_bootloader = re.sub(
             r"\r+|\n+", "", AdbWrapper.adb_shell("getprop ro.boot.bootloader", serial=serial)[0]
         )
         # 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
Ejemplo n.º 15
0
 def get_device_info(self, serial=None):
     '''
     Get the device information, include Gaia Version, Gecko Version, and so on.
     @param serial: device serial number. (optional)
     @return: the information dict object.
     '''
     try:
         tmp_dir = tempfile.mkdtemp(prefix='checkversions_')
         # pull data from device
         try:
             AdbWrapper.adb_pull('/system/b2g/omni.ja',
                                 tmp_dir,
                                 serial=serial)
         except:
             logger.error('Error pulling Gecko file.')
         try:
             AdbWrapper.adb_pull(
                 '/data/local/webapps/settings.gaiamobile.org/application.zip',
                 tmp_dir,
                 serial=serial)
         except:
             try:
                 AdbWrapper.adb_pull(
                     '/system/b2g/webapps/settings.gaiamobile.org/application.zip',
                     tmp_dir,
                     serial=serial)
             except:
                 logger.error('Error pulling Gaia file.')
         try:
             AdbWrapper.adb_pull('/system/b2g/application.ini',
                                 tmp_dir,
                                 serial=serial)
         except:
             logger.error('Error pulling application.ini file.')
         # get Gaia info
         gaia_rev = 'n/a'
         gaia_date = 'n/a'
         application_zip_file = os.path.join(tmp_dir, 'application.zip')
         if os.path.isfile(application_zip_file):
             with open(application_zip_file, 'rb') as f:
                 z = zipfile.ZipFile(f)
                 z.extract('resources/gaia_commit.txt', tmp_dir)
         else:
             logger.warning('Can not find application.zip file.')
         gaiacommit_file = os.path.join(tmp_dir,
                                        'resources/gaia_commit.txt')
         if os.path.isfile(gaiacommit_file):
             with open(gaiacommit_file, "r") as f:
                 gaia_rev = re.sub(r'\n+', '', f.readline())
                 gaia_date_sec_from_epoch = re.sub(r'\n+', '', f.readline())
             gaia_date = datetime.utcfromtimestamp(
                 int(gaia_date_sec_from_epoch)).strftime(
                     '%Y-%m-%d %H:%M:%S')
         else:
             logger.warning(
                 '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(os.path.join(tmp_dir, 'omni.ja')):
             deopt_dir = os.path.join(tmp_dir, 'deopt')
             deopt_file = os.path.join(deopt_dir, 'omni.ja')
             deopt_exec = os.path.join(tmp_dir, 'optimizejars.py')
             os.makedirs(deopt_dir)
             # TODO rewrite optimizejars.py if possible
             current_dir = cur = os.path.dirname(os.path.abspath(__file__))
             current_exec = os.path.join(current_dir, 'misc',
                                         'optimizejars.py')
             shutil.copyfile(current_exec, 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):
                 with open(deopt_file, 'rb') as f:
                     z = zipfile.ZipFile(f)
                     z.extract(
                         'chrome/toolkit/content/global/buildconfig.html',
                         tmp_dir)
             else:
                 logger.warning('Can not deoptimize omni.ja file.')
                 gecko_rev = 'n/a'
             # get Gecko info from buildconfig.html file
             buildconfig_file = os.path.join(
                 tmp_dir, '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:
                 logger.warning(
                     '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(os.path.join(tmp_dir, 'application.ini')):
             for line in open(os.path.join(tmp_dir, '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+', '',
             AdbWrapper.adb_shell('getprop ro.product.device',
                                  serial=serial)[0])
         firmware_release = re.sub(
             r'\r+|\n+', '',
             AdbWrapper.adb_shell('getprop ro.build.version.release',
                                  serial=serial)[0])
         firmware_incremental = re.sub(
             r'\r+|\n+', '',
             AdbWrapper.adb_shell('getprop ro.build.version.incremental',
                                  serial=serial)[0])
         firmware_date = re.sub(
             r'\r+|\n+', '',
             AdbWrapper.adb_shell('getprop ro.build.date',
                                  serial=serial)[0])
         firmware_bootloader = re.sub(
             r'\r+|\n+', '',
             AdbWrapper.adb_shell('getprop ro.boot.bootloader',
                                  serial=serial)[0])
         # 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 backup_profile(self, local_dir, serial=None):
        """
        Backup B2G user profile from device to local folder.

        @param local_dir: the target local folder, the backup data will store to this folder.
        @param serial: device serial number. (optional)
        """
        logger.info('Backing up profile...')
        # Backup Wifi
        wifi_dir = os.path.join(local_dir, self._LOCAL_DIR_WIFI)
        wifi_file = os.path.join(local_dir, self._LOCAL_FILE_WIFI)
        os.makedirs(wifi_dir)
        logger.info('Backing up Wifi information...')
        try:
            AdbWrapper.adb_pull(self._REMOTE_FILE_WIFI,
                                wifi_file,
                                serial=serial)
        except Exception as e:
            logger.debug(e)
            logger.error(
                'If you don\'t have root permission, you cannot backup Wifi information.'
            )
        # Backup profile
        b2g_mozilla_dir = os.path.join(local_dir, self._LOCAL_DIR_B2G)
        os.makedirs(b2g_mozilla_dir)
        logger.info('Backing up {0} to {1} ...'.format(self._REMOTE_DIR_B2G,
                                                       b2g_mozilla_dir))
        try:
            AdbWrapper.adb_pull(self._REMOTE_DIR_B2G,
                                b2g_mozilla_dir,
                                serial=serial)
        except Exception as e:
            logger.debug(e)
            logger.error('Can not pull files from {0} to {1}'.format(
                self._REMOTE_DIR_B2G, b2g_mozilla_dir))
        # Backup data/local
        datalocal_dir = os.path.join(local_dir, self._LOCAL_DIR_DATA)
        os.makedirs(datalocal_dir)
        logger.info('Backing up {0} to {1} ...'.format(self._REMOTE_DIR_DATA,
                                                       datalocal_dir))
        try:
            AdbWrapper.adb_pull(self._REMOTE_DIR_DATA,
                                datalocal_dir,
                                serial=serial)
        except Exception as e:
            logger.debug(e)
            logger.error('Can not pull files from {0} to {1}'.format(
                self._REMOTE_DIR_DATA, datalocal_dir))

        # remove gecko.mstone value, so that gecko can check the apps under /system/b2g/webapps again.
        # (Thanks SC Chien's help!)
        local_profile_path = self._get_profile_path(
            os.path.join(b2g_mozilla_dir, self._FILE_PROFILE_INI))
        local_perf = os.path.join(b2g_mozilla_dir, local_profile_path,
                                  self._FILE_PERF_JS)
        perf_contents = []
        with open(local_perf, 'r') as f:
            for line in f.readlines():
                if 'gecko.mstone' not in line:
                    perf_contents.append(line)
                else:
                    logger.debug('Remove from {}: {}'.format(local_perf, line))
        with open(local_perf, 'w') as f:
            for line in perf_contents:
                f.write(line)
        logger.info('Backup profile done.')
 def check_profile_version(self, local_dir, serial=None):
     logger.info('Checking profile...')
     # get local version
     if os.path.isdir(local_dir):
         local_config = ConfigParser.ConfigParser()
         local_config.read(
             os.path.join(local_dir, self._LOCAL_DIR_B2G,
                          self._FILE_PROFILE_INI))
         local_profile_path = local_config.get('Profile0', 'Path')
         local_config.read(
             os.path.join(local_dir, self._LOCAL_DIR_B2G,
                          local_profile_path, self._FILE_COMPATIBILITY_INI))
         logger.debug('Local Profile: {}'.format(local_config._sections))
         version_of_backup = local_config.get('Compatibility',
                                              'LastVersion')
         logger.info(
             'The Version of Backup Profile: {}'.format(version_of_backup))
     else:
         return False
     try:
         # get remote version
         tmp_dir = tempfile.mkdtemp(prefix='backup_restore_')
         logger.debug('TEMP Folder for check profile: {}'.format(tmp_dir))
         try:
             AdbWrapper.adb_pull(os.path.join(self._REMOTE_DIR_B2G,
                                              self._FILE_PROFILE_INI),
                                 tmp_dir,
                                 serial=serial)
         except:
             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(os.path.join(tmp_dir, self._FILE_PROFILE_INI))
         logger.debug('Remote Profile to get path: {}'.format(
             remote_config._sections))
         remote_profile_path = remote_config.get('Profile0', 'Path')
         try:
             AdbWrapper.adb_pull(os.path.join(self._REMOTE_DIR_B2G,
                                              remote_profile_path,
                                              self._FILE_COMPATIBILITY_INI),
                                 tmp_dir,
                                 serial=serial)
         except:
             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(
             os.path.join(tmp_dir, self._FILE_COMPATIBILITY_INI))
         logger.debug('Remote Profile: {}'.format(remote_config._sections))
         version_of_device = remote_config.get('Compatibility',
                                               'LastVersion')
         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])
         logger.debug('Local Ver: {}, Remote Ver: {}'.format(
             version_of_backup_float, version_of_device_float))
         if version_of_device_float >= version_of_backup_float:
             return True
         else:
             return False
     finally:
         logger.debug('Removing [{0}] folder...'.format(tmp_dir))
         shutil.rmtree(tmp_dir)
         logger.debug(
             'TEMP Folder for check profile removed: {}'.format(tmp_dir))