def download_update_async(self, name, url): if not self.dialog_session: return '', '' response = urlopen(url, timeout=10) return name, response.read()
def fetch_latest_fw_versions(report_error_fn): result = LatestFirmwares({}, {}, {}, {}, {}, {}) exception = None for i in range(3): try: with urlopen(LATEST_VERSIONS_URL, timeout=10) as response: latest_versions_data = response.read().decode('utf-8') with urlopen(ALL_VERSIONS_URL, timeout=10) as response: all_versions_data = response.read().decode('utf-8') break except Exception as e: exception = e if isinstance(exception, urllib.error.HTTPError): report_error_fn(ERROR_SERVER_ERROR) return None elif isinstance(exception, urllib.error.URLError): report_error_fn(ERROR_DOWNLOAD) return None elif exception is not None: report_error_fn(ERROR_UNKNOWN_WHILE_DOWNLOADING) return None for line in latest_versions_data.split('\n'): line = line.strip() if len(line) < 1: continue parsed = parse_versions_line(line, report_error_fn) if parsed is None: return None parts, latest_version = parsed if parts[0] == 'tools': tool_info = ToolInfo() tool_info.firmware_version_latest = latest_version result.tool_infos[parts[1]] = tool_info elif parts[0] == 'bricks': result.firmware_infos[parts[1]] = refresh_firmware_info( parts[1], latest_version) elif parts[0] == 'bricklets': result.plugin_infos[parts[1]] = refresh_plugin_info( parts[1], latest_version) elif parts[0] == 'extensions': result.extension_firmware_infos[ parts[1]] = refresh_extension_firmware_info( parts[1], latest_version) elif parts[0] == 'red_images': result.red_image_infos[parts[1]] = refresh_red_image_info( parts[1], latest_version) elif parts[0] == 'bindings': info = refresh_bindings_info(parts[1], latest_version) if info is not None: result.bindings_infos[parts[1]] = info for line in all_versions_data.split('\n'): line = line.strip() if len(line) < 1: continue parsed = parse_versions_line(line, report_error_fn) if parsed is None: return None parts, version = parsed d = { 'tools': result.tool_infos, 'bricks': result.firmware_infos, 'bricklets': result.plugin_infos, 'extensions': result.extension_firmware_infos, 'red_images': result.red_image_infos, 'bindings': result.bindings_infos, } if parts[0] not in d or parts[1] not in d[parts[0]]: continue d[parts[0]][parts[1]].firmware_versions.append(version) return result
def restore_clicked(self): progress = self.create_progress_bar('Factory Calibration') uid = self.parent.parent.uid progress.setLabelText('Downloading factory calibration') progress.setMaximum(0) progress.setValue(0) progress.show() try: imu_calibration_text = b'# This is the factory calibration\n\n' response = urlopen(IMU_CALIBRATION_URL + '{0}.txt'.format(uid)) chunk = response.read(1024) while len(chunk) > 0: imu_calibration_text += chunk chunk = response.read(1024) response.close() except urllib.error.HTTPError as e: if e.code == 404: progress.cancel() self.popup_ok('Factory Calibration', 'No factory calibration available') return else: progress.cancel() self.popup_fail('Factory Calibration', 'Could not download factory calibration') return except urllib.error.URLError: progress.cancel() self.popup_fail('Factory Calibration', 'Could not download factory calibration') return try: imu_calibration_text = imu_calibration_text.decode('utf-8') except UnicodeDecodeError as e: self.popup_fail('Factory Calibration', ( "Factory calibration is malformed, please report this error to " + "<a href='mailto:[email protected]'>[email protected]</a>:<br/><br/> {0}" ).format(html.escape(str(e)))) return progress.cancel() self.text_edit.setPlainText(imu_calibration_text) try: parsed = parse_imu_calibration(imu_calibration_text) except (ValueError, TypeError): self.popup_fail( 'Factory Calibration', "Factory calibration is malformed, please report this error to " + "<a href='mailto:[email protected]'>[email protected]</a>" ) return try: for value in parsed: self.imu.set_calibration(value[0], value[1]) except ip_connection.Error as e: self.popup_fail('Factory Calibration', 'Could not apply calibration: ' + e.description) return self.parent.refresh_values() self.popup_ok('Factory Calibration', 'Successfully restored factory calibration')
def cb_update_tf_software_get_installed_versions(result): if not self.dialog_session: return self.set_current_state(self.STATE_NO_UPDATES_AVAILABLE) okay, message = check_script_result(result) if not okay: msg = self.MESSAGE_ERR_GET_INSTALLED_VERSIONS + ':\n' + message + '\n\n' self.set_current_state(self.STATE_INIT) self.tedit_main.setText(msg) return updates_available_main = False update_info = { 'brickv': {}, 'processed': 0, 'temp_dir': '', 'bindings': [], 'error': False, 'updates_total': 0, 'error_messages': '' } installed_versions = json.loads(result.stdout) if not isinstance(installed_versions, dict): self.set_current_state(self.STATE_INIT) self.tedit_main.setText( self.MESSAGE_ERR_GET_INSTALLED_VERSIONS + '.') return for key, value in installed_versions.items(): if key == 'brickv': update_info['brickv']['to'] = '0' update_info['brickv']['name'] = '-' update_info['brickv']['from'] = '0' update_info['brickv']['data'] = None update_info['brickv']['update'] = False update_info['brickv']['display_name'] = '-' if isinstance(value, str) and value != '' and value != '-': update_info['brickv']['from'] = value update_info['brickv']['name'] = 'brickv' continue else: self.set_current_state(self.STATE_INIT) self.tedit_main.setText( self.MESSAGE_ERR_GET_INSTALLED_VERSIONS + '.') return elif key == 'bindings': if isinstance(value, dict) and value: for k, v in value.items(): d = {} d['to'] = '0' d['name'] = '-' d['from'] = '0' d['data'] = None d['update'] = False d['display_name'] = '-' if isinstance(v, str) and v != '' and v != '-': d['from'] = v d['name'] = k.strip() update_info['bindings'].append(d) else: self.set_current_state(self.STATE_INIT) self.tedit_main.setText( self.MESSAGE_ERR_GET_INSTALLED_VERSIONS + '.') return continue else: self.set_current_state(self.STATE_INIT) self.tedit_main.setText( self.MESSAGE_ERR_GET_INSTALLED_VERSIONS + '.') return else: self.set_current_state(self.STATE_INIT) self.tedit_main.setText( self.MESSAGE_ERR_GET_INSTALLED_VERSIONS + '.') return # Try to get the latest version numbers. try: response = urlopen(self.URL_LATEST_VERSIONS, timeout=10) response_data = response.read().decode('utf-8') except urllib.error.URLError: self.set_current_state(self.STATE_INIT) self.tedit_main.setText( self.MESSAGE_ERR_CHECK_LATEST_VERSIONS) return response_data_lines = response_data.splitlines() if len(response_data_lines) < 1: self.set_current_state(self.STATE_INIT) self.tedit_main.setText( self.MESSAGE_ERR_CHECK_LATEST_VERSIONS) return for l in response_data_lines: l_split = l.strip().split(':') if len(l_split) != 3: self.set_current_state(self.STATE_INIT) self.tedit_main.setText( self.MESSAGE_ERR_CHECK_LATEST_VERSIONS) return else: if l_split[0] == 'tools' and l_split[1] == 'brickv': update_info['brickv']['to'] = l_split[2] update_info['brickv'][ 'display_name'] = 'Brick Viewer' version_to = StrictVersion(l_split[2].strip()) version_from = StrictVersion( update_info['brickv']['from'].strip()) if version_to > version_from: updates_available_main = True update_info['brickv']['update'] = True else: update_info['brickv']['update'] = False continue elif l_split[0] == 'bindings': if l_split[1] == 'c': found, updates_available = self.update_latest_version_info( update_info, l_split[1], l_split[2], 'C/C++ Bindings') if not found: self.set_current_state(self.STATE_INIT) self.tedit_main.setText( self.MESSAGE_ERR_CHECK_LATEST_VERSIONS) return if updates_available: updates_available_main = True elif l_split[1] == 'csharp': found, updates_available = self.update_latest_version_info( update_info, l_split[1], l_split[2], 'C#/Mono Bindings') if not found: self.set_current_state(self.STATE_INIT) self.tedit_main.setText( self.MESSAGE_ERR_CHECK_LATEST_VERSIONS) return if updates_available: updates_available_main = True elif l_split[1] == 'delphi': found, updates_available = self.update_latest_version_info( update_info, l_split[1], l_split[2], 'Delphi/Lazarus Bindings') if not found: self.set_current_state(self.STATE_INIT) self.tedit_main.setText( self.MESSAGE_ERR_CHECK_LATEST_VERSIONS) return if updates_available: updates_available_main = True elif l_split[1] == 'java': found, updates_available = self.update_latest_version_info( update_info, l_split[1], l_split[2], 'Java Bindings') if not found: self.set_current_state(self.STATE_INIT) self.tedit_main.setText( self.MESSAGE_ERR_CHECK_LATEST_VERSIONS) return if updates_available: updates_available_main = True elif l_split[1] == 'javascript': found, updates_available = self.update_latest_version_info( update_info, l_split[1], l_split[2], 'JavaScript Bindings') if not found: self.set_current_state(self.STATE_INIT) self.tedit_main.setText( self.MESSAGE_ERR_CHECK_LATEST_VERSIONS) return if updates_available: updates_available_main = True elif l_split[1] == 'matlab': found, updates_available = self.update_latest_version_info( update_info, l_split[1], l_split[2], 'Octave Bindings') if not found: self.set_current_state(self.STATE_INIT) self.tedit_main.setText( self.MESSAGE_ERR_CHECK_LATEST_VERSIONS) return if updates_available: updates_available_main = True elif l_split[1] == 'perl': found, updates_available = self.update_latest_version_info( update_info, l_split[1], l_split[2], 'Perl Bindings') if not found: self.set_current_state(self.STATE_INIT) self.tedit_main.setText( self.MESSAGE_ERR_CHECK_LATEST_VERSIONS) return if updates_available: updates_available_main = True elif l_split[1] == 'php': found, updates_available = self.update_latest_version_info( update_info, l_split[1], l_split[2], 'PHP Bindings') if not found: self.set_current_state(self.STATE_INIT) self.tedit_main.setText( self.MESSAGE_ERR_CHECK_LATEST_VERSIONS) return if updates_available: updates_available_main = True elif l_split[1] == 'python': found, updates_available = self.update_latest_version_info( update_info, l_split[1], l_split[2], 'Python Bindings') if not found: self.set_current_state(self.STATE_INIT) self.tedit_main.setText( self.MESSAGE_ERR_CHECK_LATEST_VERSIONS) return if updates_available: updates_available_main = True elif l_split[1] == 'ruby': found, updates_available = self.update_latest_version_info( update_info, l_split[1], l_split[2], 'Ruby Bindings') if not found: self.set_current_state(self.STATE_INIT) self.tedit_main.setText( self.MESSAGE_ERR_CHECK_LATEST_VERSIONS) return if updates_available: updates_available_main = True elif l_split[1] == 'shell': found, updates_available = self.update_latest_version_info( update_info, l_split[1], l_split[2], 'Shell Bindings') if not found: self.set_current_state(self.STATE_INIT) self.tedit_main.setText( self.MESSAGE_ERR_CHECK_LATEST_VERSIONS) return if updates_available: updates_available_main = True elif l_split[1] == 'vbnet': found, updates_available = self.update_latest_version_info( update_info, l_split[1], l_split[2], 'VB.NET Bindings') if not found: self.set_current_state(self.STATE_INIT) self.tedit_main.setText( self.MESSAGE_ERR_CHECK_LATEST_VERSIONS) return if updates_available: updates_available_main = True self.red_plugin.bindings_version_success(result) _check_update_available = self.check_update_available( update_info) if not _check_update_available: self.set_current_state(self.STATE_INIT) self.tedit_main.setText( self.MESSAGE_ERR_GET_INSTALLED_VERSIONS + '.') return if updates_available_main: self.set_current_state(self.STATE_UPDATES_AVAILABLE) self.do_update_available_message(update_info) else: self.set_current_state(self.STATE_NO_UPDATES_AVAILABLE) self.tedit_main.setText( self.MESSAGE_INFO_STATE_NO_UPDATES_AVAILABLE)