def __init__(self, editor, schema_name): mforms.AppView.__init__(self, False, "schema_inspector", False) self.editor = editor self.tabview = mforms.newTabView() self.add(self.tabview, True, True) self.pages = [] tabs = [ SchemaInfoPanel, TableManagerParent, ColumnManager, IndexManager, TriggerManager, ViewManager, ProcedureManager, FunctionManager, GrantsManager ] if self.editor.serverVersion.majorNumber > 5 or ( self.editor.serverVersion.majorNumber == 5 and self.editor.serverVersion.minorNumber >= 1): tabs.append(EventManager) # tabs.append(AccessManager) for Tab in tabs: try: if Tab is IndexManager: tab = Tab(editor, schema_name, self) else: tab = Tab(editor, schema_name) setattr(self, "tab_" + tab.node_name, tab) self.pages.append(tab) self.tabview.add_page(tab, tab.caption) except Exception: import traceback log_error("Error initializing tab %s: %s\n" % (tab.node_name, traceback.format_exc())) self.refresh()
def prepare_new_table(self): try: self._editor.executeManagementCommand( """ CREATE TABLE %s (%s)""" % (self._table_w_prefix, ", ".join([ "`%s` %s" % (col['name'], col["type"]) for col in self._mapping if col['active'] is True ])), 1) self.update_progress(0.0, "Prepared new table") # wee need to setup dest_col for each row, as the mapping is empty if we're creating new table for col in self._mapping: col['dest_col'] = col['name'] return True except Exception, e: log_error("Error creating table for import: %s" % e) if len(e.args ) == 2 and e.args[1] == 1050 and self._force_drop_table: try: self.update_progress(0.0, "Drop existing table") self._editor.executeManagementCommand( """ DROP TABLE %s""" % self._table_w_prefix, 1) self.prepare_new_table() return True except: log_error("Error dropping table for import: %s" % e) raise raise
def launch_process(self, command, working_directory): is_windows = platform.system() == 'Windows' real_command = "" info = None if is_windows: info = subprocess.STARTUPINFO() info.dwFlags |= _subprocess.STARTF_USESHOWWINDOW info.wShowWindow = _subprocess.SW_HIDE # Command line can contain object names in case of export and filename in case of import # Object names must be in utf-8 but filename must be encoded in the filesystem encoding, # which probably isn't utf-8 in windows. fse = sys.getfilesystemencoding() real_command = command.encode(fse) if isinstance( command, unicode) else command else: real_command = command try: log_debug("Executing command: %s\n" % real_command) proc = subprocess.Popen(real_command, cwd=working_directory, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT, startupinfo=info) except OSError, exc: log_error("Error executing command %s\n%s\n" % (real_command, exc)) import traceback traceback.print_exc() raise RuntimeError("Error executing %s:\n%s" % (real_command, str(exc)))
def openAdminSectionSE(editor, section): context = grt.fromgrt(editor.customData["adminContext"]) if context: context.open_into_section(section, True) else: log_error( "No context found for editor in call to openAdminSection\n")
def cmd_executor(cmd): p1 = None if platform.system() != "Windows": try: p1 = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr=subprocess.PIPE, shell = True) except OSError as exc: log_error("Error executing command %s\n%s\n" % (cmd, exc)); import traceback traceback.print_exc() else: try: info = subprocess.STARTUPINFO() info.dwFlags |= subprocess.STARTF_USESHOWWINDOW info.wShowWindow = subprocess.SW_HIDE # Command line can contain object names in case of export and filename in case of import # Object names must be in utf-8 but filename must be encoded in the filesystem encoding, # which probably isn't utf-8 in windows. if isinstance(cmd, list): for idx,item in enumerate(cmd): cmd[idx] = item.encode("utf8") if isinstance(item,str) else item log_debug("Executing command: %s\n" % "".join(cmd)) else: cmd = cmd.encode("utf8") if isinstance(cmd,str) else cmd log_debug("Executing command: %s\n" % cmd) p1 = subprocess.Popen(str(cmd), stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, startupinfo=info, shell = True) except OSError as exc: log_error("Error executing command %s\n%s\n" % (cmd, exc)) import traceback traceback.print_exc() p1 = None return p1
def get_info(self): cmd = "%s -al -so %s" % (get_exe_path("ogrinfo"), self.get_path()) p1 = cmd_executor(cmd) sout, serr = p1.communicate(input) if serr: log_error("There was an error getting file information: %s" % serr) import re p = re.compile("^(\w+):\s(\w+)\s\(([0-9\.]+)\)", re.IGNORECASE) for line in sout.splitlines(): if line.startswith("Layer name: "): self.layer_name_lbl.set_text(line.split(':')[1].strip()) self.layer_name = line.split(':')[1].strip() self.table_name.set_value(line.split(':')[1].strip()) else: m = p.match(line) if m is not None: row = self.column_list.add_node() row.set_bool(0, True) row.set_string(1, m.group(1)) from grt.modules import Utilities res = Utilities.fetchAuthorityCodeFromFile( "%s.prj" % os.path.splitext(self.get_path())[0]) if res: self.epsg_lbl.set_text(res) else: self.epsg_lbl.set_text(0) log_info("Can't find EPSG fallback to 0")
def get_info(self): cmd = "%s -al -so %s" % (get_exe_path("ogrinfo"), self.get_path()) p1 = cmd_executor(cmd) sout, serr = p1.communicate(input) if serr: log_error("There was an error getting file information: %s" % serr) import re p = re.compile("^(\w+):\s(\w+)\s\(([0-9\.]+)\)", re.IGNORECASE) for line in sout.splitlines(): if line.startswith("Layer name: "): self.layer_name_lbl.set_text(line.split(':')[1].strip()) self.layer_name = line.split(':')[1].strip() self.table_name.set_value(line.split(':')[1].strip()) else: m = p.match(line) if m is not None: row = self.column_list.add_node() row.set_bool(0, True) row.set_string(1, m.group(1)) from grt.modules import Utilities res = Utilities.fetchAuthorityCodeFromFile("%s.prj" % os.path.splitext(self.get_path())[0]) if res: self.epsg_lbl.set_text(res) else: self.epsg_lbl.set_text(0) log_info("Can't find EPSG fallback to 0")
def set_persist_value(self, name, value): try: if name in wb_admin_variable_list.ro_persistable: mforms.Utilities.show_message( 'Information', 'This value can\'t be persisted: %s' % name, 'Ok', '', '') return False if self.variable_info and name in self.variable_info and self.variable_info[ name][1]: if not self.store_persist_value(name, value): mforms.Utilities.show_message( 'Information', 'Unable to store status of the value: %s' % name, 'Ok', '', '') return False return True else: if not self.store_persist_value(name, value, True): mforms.Utilities.show_message( 'Information', 'Unable to store status of the value: %s' % name, 'Ok', '', '') return False return True except Exception, e: log_error("Error occured while persisting variables: %s" % e) mforms.Utilities.show_error( 'Access denied', 'You need (at least one of) the SUPER or SYSTEM_VARIABLES_ADMIN privilege(s) for this operation.', 'Ok', '', '') return False
def enable_mdl_instrumentation(self): try: self.ctrl_be.exec_sql("UPDATE performance_schema.setup_instruments SET enabled='YES' WHERE name = 'wait/lock/metadata/sql/mdl'") except Exception, e: log_error("Error enabling MDL instrumentation: %s\n" % e) mforms.Utilities.show_error("Enable MDL Instrumentation", "Error enabling performance_schema MDL instrumentation.\n%s" % e, "OK", "", "") return
def prepare_import(self): if self.importer and self.importer.is_running: mforms.Utilities.show_message("Importing...", "Import thread is already running.", "Ok", "", "") raise RuntimeError("Import is already running") self.importer = SpatialImporter() self.importer.filepath = self.get_path() if self.importer.filepath == None or not os.path.isfile(self.importer.filepath): raise RuntimeError("Unable to open specified file: %s" % self.importer.filepath) self.importer.my_pwd = self.get_mysql_password(self.main.editor.connection) if self.importer.my_pwd == None: log_error("Cancelled MySQL password input\n") raise RuntimeError("Cancelled MySQL password input") self.importer.my_host = self.main.editor.connection.parameterValues.hostName self.importer.my_port = self.main.editor.connection.parameterValues.port self.importer.my_user = self.main.editor.connection.parameterValues.userName self.importer.my_schema = self.main.selected_schema self.importer.skipfailures = self.main.content_preview_page.skipfailures_chb.get_active() self.importer.import_overwrite = self.main.content_preview_page.skipfailures_chb.get_active() self.importer.import_append = self.main.content_preview_page.append_chb.get_active() if self.main.content_preview_page.table_name.get_string_value() != "": self.importer.import_table = self.main.content_preview_page.table_name.get_string_value() self.importer.selected_fields = ",".join(self.main.content_preview_page.get_fields()) return True
def run_query(self): try: self.result = self.execute() error = None except Exception as e: error = str(e) log_error("Error executing '%s': %s\n" % (self.get_query(), error))
def poll(self): output = StringIO.StringIO() if self.ctrl_be.server_helper.execute_command( "/usr/bin/uptime", output_handler=output.write) == 0: data = output.getvalue().strip(" \r\t\n,:.").split("\n")[-1] self._cpu_stat_return = None load_value = data.split()[-3] # in some systems, the format is x.xx x.xx x.xx and in others, it's x.xx, x.xx, x.xx load_value = load_value.rstrip(",") try: result = float(load_value.replace(',', '.')) except (ValueError, TypeError): log_error( "Shell source %s returned wrong value. Expected int or float but got '%s'\n" % (self.name, load_value)) result = 0 if self.widget is not None: self.widget.set_value( self.calc_cb(result) if self.calc_cb else result) if self.label_cb is not None: self.ctrl_be.uitask(self.label.set_text, self.label_cb(result)) else: value = output.getvalue() if value != self._cpu_stat_return: self._cpu_stat_return = value log_debug("CPU stat command returned error: %s\n" % value)
def enable_mdl_instrumentation(self): try: self.ctrl_be.exec_sql("UPDATE performance_schema.setup_instruments SET enabled='YES' WHERE name = 'wait/lock/metadata/sql/mdl'") except Exception, e: log_error("Error enabling MDL instrumentation: %s\n" % e) mforms.Utilities.show_error("Enable MDL Instrumentation", "Error enabling performance_schema MDL instrumentation.\n%s" % e, "OK", "", "") return
def get_table_columns(self, table): cols = [] try: rset = self.main.editor.executeManagementQuery("SHOW COLUMNS FROM `%s`.`%s`" % (table['schema'], table['table']), 1) except grt.DBError, e: log_error("SHOW COLUMNS FROM `%s`.`%s` : %s" % (table['schema'], table['table'], e)) rset = None
def work(self, files): try: if self.ctrl_be.target_version >= Version(5, 7, 10): self.importer.reset_schemas() else: location = download_server_install_script(self.ctrl_be) if location: workbench_version_string = get_current_sys_version(None) server_version_string = get_sys_version_from_script(location) maj, min, rel = [int(i) for i in workbench_version_string.split(".")] workbench_version = Version(maj, min, rel) maj, min, rel = [int(i) for i in server_version_string.split(".")] server_version = Version(maj, min, rel) if server_version >= workbench_version: log_info("Installing sys schema supplied by the server: %s\n" % str(location)) self.install_scripts([(location, None)], "Installing server script") return else: log_info("Server sys schema install script exists but it's outdated compared to the one supplied by Workbench...\n") log_info("Installing sys schema supplied by workbench\n") self.install_scripts(files, "Installing Workbench script") except Exception as e: log_error("Runtime error when installing the sys schema: %s\n" % str(e)) self._worker_queue.put(e) # This makes the screen refresh self._worker_queue.put(None)
def __init__(self, editor, schema_name): mforms.AppView.__init__(self, False, "schema_inspector", False) self.editor = editor self.tabview = mforms.newTabView() self.add(self.tabview, True, True) self.pages = [] tabs = [SchemaInfoPanel, TableManagerParent, ColumnManager, IndexManager, TriggerManager, ViewManager, ProcedureManager, FunctionManager, GrantsManager] if self.editor.serverVersion.majorNumber > 5 or (self.editor.serverVersion.majorNumber == 5 and self.editor.serverVersion.minorNumber >= 1): tabs.append(EventManager) # tabs.append(AccessManager) for Tab in tabs: try: if Tab is IndexManager: tab = Tab(editor, schema_name, self) else: tab = Tab(editor, schema_name) setattr(self, "tab_"+tab.node_name, tab) self.pages.append(tab) self.tabview.add_page(tab, tab.caption) except Exception: import traceback log_error("Error initializing tab %s: %s\n" % (tab.node_name, traceback.format_exc())) self.refresh()
def show_schema_manager(editor, selection, table_maintenance=False): try: editor.executeManagementQuery("select 1", 0) except grt.DBError, e: mforms.Utilities.show_error("Schema Inspector", "Can not launch the Schema Inspector because the server is unreacheble.", "OK", "", "") log_error("Can not launch the Schema Inspector because the server is unreacheble.\n") return False
def analyze_file(self): data = [] with open(self._filepath, 'rb') as f: try: data = json.load(f) except ValueError as e: log_error("JSON file is invalid: %s\n" % (self._filepath)) self._last_analyze = False return False if len(data) == 0: log_error("JSON file contains no data: %s\n" % (self._filepath)) self._last_analyze = False return False self._columns = [] if type( data ) == dict: # We need to have list so if it's dict after that we will be able to handle it. data = [data] for elem in data[0]: self._columns.append({ 'name': elem, 'type': 'text', 'is_string': True, 'is_geometry': False, 'is_bignumber': False, 'is_number': False, 'is_date_or_time': False, 'is_bin': False, 'is_float': False, 'is_json': False, 'value': [] }) for row in data: for i, elem in enumerate(row): if type(row[elem]) in [dict, list]: self._columns[i]['is_string'] = False self._columns[i]['is_json'] = True self._columns[i]['value'].append(row[elem]) for col in self._columns: gtype = self.guess_type(col['value']) if gtype not in self._type_map: raise Exception("Unhandled type: %s in %s" % (gtype, self._type_map)) else: col['type'] = gtype for attrib in col: if attrib.startswith("is_"): if attrib == self._type_map[gtype]: col[attrib] = True else: col[attrib] = False self._last_analyze = True return True
def get_zombies(query, fields): try: result = self.ctrl_be.exec_query(query) except Exception, e: log_error( "Could not get list of invalid privs: %s\nQuery: %s\n" % (e, query)) return []
def validate(self): try: res = self._main_view.editor.executeManagementQuery("select @@performance_schema", 0) if res.goToFirstRow(): return res.stringFieldValue(0) == "1" except grt.DBError as e: log_error("MySQL error retrieving the performance_schema variable: %s\n" % e) return False
def get_message(self, port): if port not in self.tunnel_by_port: log_error("Looking up invalid port %s\n" % port) return None tunnel = self.tunnel_by_port[port] try: return tunnel.q.get_nowait() except Queue.Empty: return None
def clear_button_clicked(self): for filename in os.listdir(self.main.results_path): filepath = os.path.join(self.main.results_path, filename) try: if os.path.isfile(filepath): os.unlink(filepath) except Exception, e: log_error("SSL Wizard: Unable to remove file %s\n%s" % (filepath, str(e))) return
def get_message(self, port): if port not in self.tunnel_by_port: log_error("Looking up invalid port %s\n" % port) return None tunnel = self.tunnel_by_port[port] try: return tunnel.q.get_nowait() except Queue.Empty: return None
def cmd_executor(cmd): p1 = None if platform.system() != "Windows": try: p1 = subprocess.Popen("exec " + cmd, stdout = subprocess.PIPE, stderr=subprocess.PIPE, shell = True) except OSError, exc: log_error("Error executing command %s\n%s\n" % (cmd, exc)); import traceback traceback.print_ext()
def generateCertificates(parent, conn, conn_id): try: log_info("Running SSL Wizard\nParent: %s\nUser Folder: %s\nConn Parameters: %s\nConn ID: %s\n" % (str(parent), mforms.App.get().get_user_data_folder(), str(conn.parameterValues), conn_id)) p = mforms.fromgrt(parent) log_info("Running SSL Wizard\n%s\n" % str(p)) r = SSLWizard(p, conn, conn_id) r.run(True) except Exception, e: log_error("There was an exception running SSL Wizard.\n%s\n\n%s" % (str(e), traceback.format_exc()))
def shutdown(self): log_error("shutting down admn\n") dprint_ex(2, " closing") self.closing = True for tab in self.tabs: if hasattr(tab, "shutdown"): res = tab.shutdown() if res is False: # It has to explicitely return False to cancel shutdown self.closing = False
def generateCertificates(parent, conn, conn_id): try: log_info("Running SSL Wizard\nParent: %s\nUser Folder: %s\nConn Parameters: %s\nConn ID: %s\n" % (str(parent), mforms.App.get().get_user_data_folder(), str(conn.parameterValues), conn_id)) p = mforms.fromgrt(parent) log_info("Running SSL Wizard\n%s\n" % str(p)) r = SSLWizard(p, conn, conn_id) r.run(True) except Exception, e: log_error("There was an exception running SSL Wizard.\n%s\n\n%s" % (str(e), traceback.format_exc()))
def get_view_columns(self): if self.columns: return self.columns else: if not self.view: log_error("report '%s' is missing column list\n" % self.caption) return [] return PSHelperViewTab.get_view_columns(self)
def clear_button_clicked(self): for filename in os.listdir(self.main.results_path): filepath = os.path.join(self.main.results_path, filename) try: if os.path.isfile(filepath): os.unlink(filepath) except Exception, e: log_error("SSL Wizard: Unable to remove file %s\n%s" % (filepath, str(e))) return
def analyze_file(self): with open(self._filepath, 'rb') as csvfile: if self.dialect is None: csvsample = [] for i in range(0,2): #read two lines as a sample csvsample.append(csvfile.readline()) csvsample = "".join(csvsample) self.dialect = csv.Sniffer().sniff(csvsample) self.has_header = csv.Sniffer().has_header(csvsample) csvfile.seek(0) else: self.dialect.delimiter = self.options['filedseparator']['value'] self.dialect.lineterminator = self.options['lineseparator']['value'] self.dialect.quotechar = self.options['encolsestring']['value'] try: reader = UniReader(csvfile, self.dialect, encoding=self._encoding) self._columns = [] row = None try: row = reader.next() except StopIteration, e: pass if row: for col_value in row: self._columns.append({'name': col_value, 'type': 'text', 'is_string': True, 'is_number': False, 'is_date_or_time': False, 'is_bin': False, 'is_float':False, 'value': []}) for i, row in enumerate(reader): #we will read only first few rows if i < 5: for j, col_value in enumerate(row): self._columns[j]['value'].append(col_value) else: break for col in self._columns: # Means the file is missing some data or is mallformed if len(col['value']) == 0: return False gtype = self.guess_type(col['value']) if gtype not in self._type_map: raise Exception("Unhandled type: %s in %s" % (gtype, self._type_map)) else: col['type'] = gtype for attrib in col: if attrib.startswith("is_"): if attrib == self._type_map[gtype]: col[attrib] = True else: col[attrib] = False except (UnicodeError, UnicodeDecodeError), e: import traceback log_error("Error analyzing file, probably encoding issue: %s\n Traceback is: %s" % (e, traceback.format_exc())) self._last_analyze = False return False
def load_privileges(self, host): # Clears any previously loaded privileges self._granted_privileges = {} if host in self.applicant_hosts: # If there are hosts it means there are privileges applicable for the user # On the indicated host result = self.context.ctrl_be.exec_query( "SHOW GRANTS FOR `%s`@`%s`" % (self.user, host)) context = grt.modules.MySQLParserServices.createParserContext( self._character_sets, self._target_version, 'STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION', 1) if result: while result.nextRow(): statement = result.stringByIndex(1) grant_data = None try: grant_data = grt.modules.MySQLParserServices.parseStatementDetails( context, statement) if not grant_data['error']: # Gets the target scope for the privileges target_string = grant_data['target'] target = None # Search for an already existing target for tgt in self._granted_privileges.keys(): if tgt.identical(target_string): target = tgt # If not found, creates one if not target: target = PrivilegeTarget() target.set_from_string(target_string) # Gets the privilege list priv_list = grant_data['privileges'] # Adds the privileges to the granted list self.add_privileges(target, priv_list) else: log_error( 'An error occurred parsing GRANT statement: %s\n -> %s\n' % (statement, grant_data['error'])) except Exception, exc: log_error( 'An error occurred parsing GRANT statement: %s\n -> %s\n' % (statement, exc)) else: log_warning('There are no grants defined for %s@%s\n' % (self.user, self.host))
def cmd_executor(cmd): p1 = None if platform.system() != "Windows": try: p1 = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr=subprocess.PIPE, shell = True) except OSError, exc: log_error("Error executing command %s\n%s\n" % (cmd, exc)); import traceback traceback.print_exc()
def shutdown(self): log_error("shutting down admn\n") dprint_ex(2, " closing") self.closing = True for tab in self.tabs: if hasattr(tab, "shutdown"): res = tab.shutdown() if res is False: # It has to explicitely return False to cancel shutdown self.closing = False
def repaint(self, cr, x, y, w, h): xoffs, yoffs = self.parent.relayout() self.offset = xoffs, yoffs c = Context(cr) try: self.canvas.repaint(c, xoffs, yoffs, w, h) except Exception: import traceback log_error("Exception rendering dashboard: %s\n" % traceback.format_exc())
def reset_persist_single_value(self, value_name): if mforms.Utilities.show_message('Confirmation', 'Do you really want to reset selected persistent variable: %s?' % value_name, 'Ok', 'Cancel', '') == mforms.ResultOk: try: self.ctrl_be.exec_query("RESET PERSIST %s" % value_name) return True except Exception as e: log_error("Error occured while unsetting persisting variables: %s" % e) mforms.Utilities.show_error('Access denied', 'You need (at least one of) the SUPER or SYSTEM_VARIABLES_ADMIN privilege(s) for this operation.', 'Ok', '', '') return False
def repaint(self, cr, x, y, w, h): xoffs, yoffs = self.parent.relayout() self.offset = xoffs, yoffs c = Context(cr) try: self.canvas.repaint(c, xoffs, yoffs, w, h) except Exception: import traceback log_error("Exception rendering dashboard: %s\n" % traceback.format_exc())
def refresh(self): try: rset = self.editor.executeManagementQuery( "select * from information_schema.schemata WHERE schema_name = '%s'" % self._schema, 0) except grt.DBError, e: log_error( "select * from information_schema.schemata WHERE schema_name = '%s' : %s\n" % (self._schema, e)) rset = None
def load_dest_columns(self): try: rset = self.main.editor.executeManagementQuery( "SHOW COLUMNS FROM %s.%s" % (self.main.destination_table['schema'], self.main.destination_table['table']), 1) except Exception, e: log_error("SHOW COLUMNS FROM %s.%s : %s" % (self.main.destination_table['schema'], self.main.destination_table['table'], e)) rset = None
def show_schema_manager(editor, selection, table_maintenance=False): try: editor.executeManagementQuery("select 1", 0) except grt.DBError, e: mforms.Utilities.show_error( "Schema Inspector", "Can not launch the Schema Inspector because the server is unreacheble.", "OK", "", "") log_error( "Can not launch the Schema Inspector because the server is unreacheble.\n" ) return False
def do_delete_account(self, username, host): query = REMOVE_USER % {"user":escape_sql_string(username), "host":escape_sql_string(host)} try: self.ctrl_be.exec_sql("use mysql") self.ctrl_be.exec_sql(query) except QueryError, e: log_error('Error removing account %s@%s:\n%s' % (username, host, str(e))) if e.error == 1227: # MySQL error code 1227 (ER_SPECIFIC_ACCESS_DENIED_ERROR) raise Exception('Error removing the account %s@%s:' % (username, host), 'You must have the global CREATE USER privilege or the DELETE privilege for the mysql ' 'database') raise e
def do_delete_account(self, username, host): query = REMOVE_USER % {"user":escape_sql_string(username), "host":escape_sql_string(host)} try: self.ctrl_be.exec_sql("use mysql") self.ctrl_be.exec_sql(query) except QueryError, e: log_error('Error removing account %s@%s:\n%s' % (username, host, str(e))) if e.error == 1227: # MySQL error code 1227 (ER_SPECIFIC_ACCESS_DENIED_ERROR) raise Exception('Error removing the account %s@%s:' % (username, host), 'You must have the global CREATE USER privilege or the DELETE privilege for the mysql ' 'database') raise e
def load_dest_columns(self): try: rset = self.main.editor.executeManagementQuery( u"SHOW COLUMNS FROM `%s`.`%s`" % (self.main.destination_table['schema'], self.main.destination_table['table']), 1) except Exception, e: log_error( u"SHOW COLUMNS FROM `%s`.`%s` : %s" % (self.main.destination_table['schema'], self.main.destination_table['table'], to_unicode(e.message))) rset = None
def tab_changed(self): if self.old_active_tab and hasattr(self.old_active_tab, "page_deactivated"): self.old_active_tab.page_deactivated() i = self.tabview.get_active_tab() panel = self.tabs[i] if panel is not None and hasattr(panel, "page_activated"): try: panel.page_activated() except Exception, e: import traceback log_error("Unhandled exception in Admin for %s: %s\n" % (panel, traceback.format_exc())) mforms.Utilities.show_error("Error", "An unhandled exception occurred (%s). Please refer to the log files for details." % e, "OK", "", "")
def set_keepalive(self, port, keepalive): if keepalive == 0: log_info("SSH KeepAlive setting skipped.\n") return tunnel = self.tunnel_by_port.get(port) if not tunnel: log_error("Looking up invalid port %s\n" % port) return transport = tunnel._client.get_transport() if transport is None: log_error("SSHTransport not ready yet %d\n" % port) return transport.set_keepalive(keepalive)
def kill(self): self.abort_requested = True if self.process_handle: if platform.system() == 'Windows': cmd = "taskkill /F /T /PID %i" % self.process_handle.pid log_debug("Killing task: %s\n" % cmd) subprocess.Popen(cmd , shell=True) else: import signal try: log_debug("Sending SIGTERM to task %s\n" % self.process_handle.pid) os.kill(self.process_handle.pid, signal.SIGTERM) except OSError, exc: log_error("Exception sending SIGTERM to task: %s\n" % exc) self.print_log_message("kill task: %s" % str(exc))
def run_command(self, command, output_to = subprocess.PIPE): try: set_shell = True if sys.platform == "win32" else False p = subprocess.Popen(command, stdout=output_to, stderr=subprocess.PIPE, shell=set_shell) out = p.communicate() if p.returncode != 0: log_error("Running command: %s\nOutput(retcode: %d):\n%s\n" % (str(command), p.returncode, str(out))) return False return True except ValueError, e: log_error("Running command: %s\nValueError exception\n" % (str(e.cmd))) return False
def enable_disable_instrumentation(self): selected_conn = self.connection_list.get_selection() if not selected_conn: return instr_state = 'YES' if selected_conn[0].get_string(11).startswith('YES'): instr_state = 'No' for sel in selected_conn: connid = sel.get_long(7) try: self.ctrl_be.exec_sql("UPDATE performance_schema.threads SET instrumented = '%s' WHERE thread_id = %d LIMIT 1" % (instr_state, connid)) except Exception, e: log_error("Error enabling thread instrumentation: %s\n" % e) mforms.Utilities.show_error("Toggle Thread Instrumentation", "Error setting instrumentation for thread %d: %s" % (connid, e), "OK", "", "") break
def load_data(self): data = None try: result = self.ctrl_be.exec_query("SELECT sys.ps_thread_stack(%d, %s)" % (self.thread_id, "TRUE" if self.enable_debug_info else "FALSE")) if result is not None: if result.nextRow(): data = result.stringByIndex(1) if data: data = data.replace("\0", "") # workaround for server bug return self.parse_data(json.loads(data)) except Exception, e: import traceback #open("/tmp/data.js", "w+").write(data) log_error("Exception during sys.ps_thread_stack(%d, %s):\n%s\n" % (self.thread_id, "TRUE" if self.enable_debug_info else "FALSE", traceback.format_exc())) mforms.Utilities.show_error("Error Getting Thread Stack", "The thread stack for thread %d can't be loaded, please check if your sys schema is properly installed and available.\n%s" % (self.thread_id, e), "OK", "", "") return None
def load_privileges(self, host): # Clears any previously loaded privileges self._granted_privileges = {} if host in self.applicant_hosts: # If there are hosts it means there are privileges applicable for the user # On the indicated host result = self.context.ctrl_be.exec_query("SHOW GRANTS FOR `%s`@`%s`" % (self.user, host)) context = grt.modules.MySQLParserServices.createParserContext(self._character_sets, self._target_version, 'STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION', 1) if result: while result.nextRow(): statement = result.stringByIndex(1) grant_data = None try: grant_data = grt.modules.MySQLParserServices.parseStatementDetails(context, statement) if not grant_data['error']: # Gets the target scope for the privileges target_string = grant_data['target'] target = None # Search for an already existing target for tgt in self._granted_privileges.keys(): if tgt.identical(target_string): target = tgt # If not found, creates one if not target: target = PrivilegeTarget() target.set_from_string(target_string) # Gets the privilege list priv_list = grant_data['privileges'] # Adds the privileges to the granted list self.add_privileges(target, priv_list) else: log_error('An error occurred parsing GRANT statement: %s\n -> %s\n' % (statement, grant_data['error'])) except Exception, exc: log_error('An error occurred parsing GRANT statement: %s\n -> %s\n' % (statement, exc)) else: log_warning('There are no grants defined for %s@%s\n' % (self.user, self.host))
def refresh_attr_list(self): self.attributes_list.clear() try: nodes = self.connection_list.get_selection() if nodes and len(nodes) == 1: connid = nodes[0].get_long(0) result = self.ctrl_be.exec_query("SELECT * FROM performance_schema.session_connect_attrs WHERE processlist_id = %s ORDER BY ORDINAL_POSITION" % connid) while result and result.nextRow(): node = self.attributes_list.add_node() node.set_string(0, result.stringByName("ATTR_NAME")) node.set_string(1, result.stringByName("ATTR_VALUE")) except Exception, e: import traceback log_error("Error looking up attribute information: %s\n" % traceback.format_exc()) mforms.Utilities.show_error("Lookup Connection Attributes", "Error looking up connection attributes: %s" % e, "OK", "", "")
def commit_changes(self): if self.change_count: # Gets the statements needed to commit the changes on the different # sections. for element in self.sections.values(): if element.change_count: statements = element.get_commit_statements() for statement in statements: try: self.ctrl_be.exec_sql(statement) except QueryError, err: log_error('ERROR : [%s] %s [%s]\n' % (err.error, err.msg, err.errortext)) raise self.reset_changes()
def prepare_new_table(self): try: self._editor.executeManagementCommand(""" CREATE TABLE %s (%s)""" % (self._table_w_prefix, ", ".join(["`%s` %s" % (col['name'], col["type"]) for col in self._mapping])), 1) self.update_progress(0.0, "Prepared new table") # wee need to setup dest_col for each row, as the mapping is empty if we're creating new table for col in self._mapping: col['dest_col'] = col['name'] return True except Exception, e: log_error("Error creating table for import: %s" % e) if len(e.args) == 2 and e.args[1] == 1050 and self._force_drop_table: try: self.update_progress(0.0, "Drop existing table") self._editor.executeManagementCommand(""" DROP TABLE %s""" % self._table_w_prefix, 1) self.prepare_new_table() return True except: log_error("Error dropping table for import: %s" % e) raise raise
def showInspector(editor, selection): schema_insp = [] table_insp = [] table_insp_idx = [] for s in selection: if s.type == "db.Schema": schema_insp.append(s.schemaName) elif (s.type == "db.Table") or (s.type == "db.View"): table_insp.append((s.schemaName, s.name)) elif s.type == "db.Index": table_insp_idx.append((s.schemaName, s.owner.name)) else: log_error("Unsupported inspector type: %s\n" % s.type) if len(schema_insp): show_schema_manager(editor, schema_insp, False) if len(table_insp): show_table_inspector(editor, table_insp) if len(table_insp_idx): show_table_inspector(editor, table_insp_idx, "indexes") return 0
def generate_certificate(self, tool, out_path, out_name, ca_cert, ca_key, config_file, days=3600): key = os.path.join(out_path, out_name+"-key.pem") req = os.path.join(out_path, out_name+"-req.pem") cert = os.path.join(out_path, out_name+"-cert.pem") serial_file = os.path.join(out_path, out_name+".serial") req_cmd = [tool, "req", "-newkey", "rsa:2048", "-days", str(days), "-nodes", "-keyout", key, "-out", req, "-config", config_file] if not self.run_command(req_cmd): log_error("Unable to generate key.\n") return False, key, req, cert rsa_cmd = [tool, "rsa", "-in", key, "-out", key] if not self.run_command(rsa_cmd): log_error("Unable to generate key.\n") return False, key, req, cert rsa_cmd = [tool, "x509", "-req", "-in", req, "-days", str(days), "-CA", ca_cert, "-CAkey", ca_key, "-CAserial", serial_file, "-CAcreateserial", "-out", cert] if not self.run_command(rsa_cmd): log_error("Unable to generate certificate serial.\n") return False, key, req, cert return True, key, req, cert
def execute_cmd(self, cmd, progress_notify): p1 = cmd_executor(cmd) self.process_handle = p1 pct = 0.0 progress_notify(pct, "0 %") while p1 and p1.returncode is None: p1.poll() char = p1.stdout.read(1) if char == "0": continue else: if char == ".": pct = pct + 0.03 else: try: num = float(char) except ValueError: #it means that child process pass away, we can do nothing with it break if num == 1.0 and pct > 3.0: progress_notify(pct, "Finished") break pct = num/10.0 progress_notify(pct, "%d %%" % int(pct * 100)) sout, serr = p1.communicate() self.returncode = p1.returncode if self.returncode !=0: if self.user_cancel: log_info("Execute command failed with error: %s, the exit code was: %d.\n" % (serr, self.returncode)) raise grt.UserInterrupt() else: log_error("Execute command failed with error: %s, the exit code was: %d.\n" % (serr, self.returncode)) raise Exception(serr) log_info("Execute command succeeed.\n")
def testInstanceSettingByName(what, connection, server_instance): global test_ssh_connection log_debug("Test %s in %s\n" % (what, connection.name)) profile = ServerProfile(connection, server_instance) if what == "connect_to_host": if test_ssh_connection: test_ssh_connection = None log_info("Instance test: Connecting to %s\n" % profile.ssh_hostname) try: test_ssh_connection = wb_admin_control.WbAdminControl(profile, None, connect_sql=False, test_only = True) test_ssh_connection.init() grt.send_info("connected.") except Exception, exc: log_error("Exception: %s" % exc.message) import traceback log_debug2("Backtrace was: " % traceback.format_stack()) return "ERROR "+str(exc) except:
def run(self, progress_notify): cmd_args = {} cmd_args['host'] = self.my_host cmd_args['schema'] = self.my_schema cmd_args['user'] = self.my_user cmd_args['pwd'] = self.my_pwd cmd_args['port'] = self.my_port cmd_args['features_per_transcation'] = self.features_per_transcation if self.import_table: cmd_args['table_name'] = " -nln %s" % self.import_table else: cmd_args['table_name'] = "" cmd_args['opts'] = "-lco GEOMETRY_NAME=%s" % self.my_geo_column if self.skipfailures: cmd_args['opts'] = cmd_args['opts'] + " -skipfailures" if self.import_append: cmd_args['opts'] = cmd_args['opts'] + " -append" if self.import_overwrite: cmd_args['opts'] = cmd_args['opts'] + " -overwrite" if self.selected_fields: cmd_args['opts'] = cmd_args['opts'] + " -select " + self.selected_fields cmd_args['filepath'] = self.filepath cmd = """ogr2ogr -f "MySQL" MySQL:"%(schema)s,host=%(host)s,user=%(user)s,password=%(pwd)s,port=%(port)d" %(filepath)s %(table_name)s %(opts)s -progress -gt %(features_per_transcation)d -lco ENGINE=InnoDb -lco SPATIAL_INDEX=NO""" % cmd_args self.is_running = True try: self.execute_cmd(cmd, progress_notify) except grt.UserInterrupt: log_info("User cancelled") raise except Exception, exc: import traceback log_error("An error occurred during execution of ogr2ogr file import: %s, stack: %s\n" % (exc, traceback.format_exc())) raise
def analyze_file(self): data = [] with open(self._filepath, 'rb') as f: prevchar = None stropen = False inside = 0 datachunk = [] rowcount = 0 while True: if f.tell() >= 20480: log_error("JSON file contains data that's in unknown structure: %s" % (self._filepath)) return False c = f.read(1) if c == "": break if c == '"' and prevchar != '\\': stropen = True if stropen == False else False if stropen == False: if c == '{' and prevchar != '\\': inside = inside + 1 if c == '}' and prevchar != '\\': inside = inside - 1 if inside == 0: if rowcount >= 4: datachunk.append(c) datachunk.append(']') break else: rowcount = rowcount + 1 datachunk.append(c) prevchar = c try: data = json.loads("".join(datachunk)) except Exception, e: log_error("Unable to parse JSON file: %s,%s " % (self._filepath, e)) self._last_analyze = False return False
def poll(self): output = StringIO.StringIO() if self.ctrl_be.server_helper.execute_command("/usr/bin/uptime", output_handler=output.write) == 0: data = output.getvalue().strip(" \r\t\n,:.").split("\n")[-1] self._cpu_stat_return = None load_value = data.split()[-3] # in some systems, the format is x.xx x.xx x.xx and in others, it's x.xx, x.xx, x.xx load_value = load_value.rstrip(",") try: result = float(load_value.replace(',','.')) except (ValueError, TypeError): log_error("Shell source %s returned wrong value. Expected int or float but got '%s'\n" % (self.name, load_value)) result = 0 if self.widget is not None: self.widget.set_value(self.calc_cb(result) if self.calc_cb else result) if self.label_cb is not None: self.ctrl_be.uitask(self.label.set_text, self.label_cb(result)) else: value = output.getvalue() if value != self._cpu_stat_return: self._cpu_stat_return = value log_debug("CPU stat command returned error: %s\n" % value)