From b78aa28f15d30435c7da40ab8dee541f4a7d74a6 Mon Sep 17 00:00:00 2001 From: midoks Date: Wed, 13 Jul 2022 15:39:24 +0800 Subject: [PATCH] up --- class/plugin/orm.py | 24 +++++--- plugins/mysql-yum/class/mysqlDb.py | 99 ------------------------------ plugins/mysql-yum/class/orm.py | 96 ----------------------------- plugins/mysql-yum/index.py | 81 +++++++++++------------- plugins/mysql/class/mysqlDb.py | 98 ----------------------------- plugins/mysql/class/orm.py | 97 ----------------------------- plugins/mysql/index.py | 27 +++++--- 7 files changed, 71 insertions(+), 451 deletions(-) delete mode 100755 plugins/mysql-yum/class/mysqlDb.py delete mode 100755 plugins/mysql-yum/class/orm.py delete mode 100755 plugins/mysql/class/mysqlDb.py delete mode 100755 plugins/mysql/class/orm.py diff --git a/class/plugin/orm.py b/class/plugin/orm.py index a5d12c956..3bbd663fd 100755 --- a/class/plugin/orm.py +++ b/class/plugin/orm.py @@ -21,13 +21,23 @@ class ORM: def __Conn(self): '''连接MYSQL数据库''' try: - try: - self.__DB_CONN = connector.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS, - port=self.__DB_PORT, charset="utf8", connect_timeout=1, unix_socket=self.__DB_SOCKET) - except Exception as e: - self.__DB_HOST = '127.0.0.1' - self.__DB_CONN = connector.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS, - port=self.__DB_PORT, charset="utf8", connect_timeout=1, unix_socket=self.__DB_SOCKET) + + if os.path.exists(self.__DB_SOCKET): + try: + self.__DB_CONN = connector.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS, + port=self.__DB_PORT, charset="utf8", connect_timeout=1, unix_socket=self.__DB_SOCKET) + except Exception as e: + self.__DB_HOST = '127.0.0.1' + self.__DB_CONN = connector.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS, + port=self.__DB_PORT, charset="utf8", connect_timeout=1, unix_socket=self.__DB_SOCKET) + else: + try: + self.__DB_CONN = connector.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS, + port=self.__DB_PORT, charset="utf8", connect_timeout=1) + except Exception as e: + self.__DB_HOST = '127.0.0.1' + self.__DB_CONN = connector.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS, + port=self.__DB_PORT, charset="utf8", connect_timeout=1) self.__DB_CUR = self.__DB_CONN.cursor() return True diff --git a/plugins/mysql-yum/class/mysqlDb.py b/plugins/mysql-yum/class/mysqlDb.py deleted file mode 100755 index 6f5d74ecc..000000000 --- a/plugins/mysql-yum/class/mysqlDb.py +++ /dev/null @@ -1,99 +0,0 @@ -# coding: utf-8 - -import re -import os -import sys - -# sys.path.append("/usr/local/lib/python3.9/site-packages") - -sys.path.append(os.getcwd() + "/class/core") -import mw - -# if mw.isAppleSystem(): -# cmd = 'ls /usr/local/lib/ | grep python | cut -d \\ -f 1 | awk \'END {print}\'' -# info = mw.execShell(cmd) -# p = "/usr/local/lib/" + info[0].strip() + "/site-packages" -# sys.path.append(p) - - -class mysqlDb: - __DB_PASS = None - __DB_USER = 'root' - __DB_PORT = 3306 - __DB_HOST = 'localhost' - __DB_CONN = None - __DB_CUR = None - __DB_ERR = None - __DB_CNF = '/etc/my.cnf' - - def __Conn(self): - '''连接MYSQL数据库''' - try: - import mw - socket = '/www/server/mysql-ya/mysql.sock' - try: - import MySQLdb - except Exception as ex: - # print('dd') - self.__DB_ERR = ex - return False - try: - myconf = mw.readFile(self.__DB_CNF) - rep = "port\s*=\s*([0-9]+)" - self.__DB_PORT = int(re.search(rep, myconf).groups()[0]) - except: - self.__DB_PORT = 3306 - # print self.__DB_PASS - #self.__DB_PASS = mw.M('config').where('id=?', (1,)).getField('mysql_root') - try: - self.__DB_CONN = MySQLdb.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS, - port=self.__DB_PORT, charset="utf8", connect_timeout=1, unix_socket=socket) - except MySQLdb.Error as e: - self.__DB_HOST = '127.0.0.1' - self.__DB_CONN = MySQLdb.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS, - port=self.__DB_PORT, charset="utf8", connect_timeout=1, unix_socket=socket) - self.__DB_CUR = self.__DB_CONN.cursor() - return True - except MySQLdb.Error as e: - self.__DB_ERR = e - return False - - def setDbConf(self, conf): - self.__DB_CNF = conf - - def setPwd(self, pwd): - self.__DB_PASS = pwd - - def getPwd(self): - return self.__DB_PASS - - def execute(self, sql): - # 执行SQL语句返回受影响行 - if not self.__Conn(): - return self.__DB_ERR - try: - result = self.__DB_CUR.execute(sql) - self.__DB_CONN.commit() - self.__Close() - return result - except Exception as ex: - return ex - - def query(self, sql): - # 执行SQL语句返回数据集 - if not self.__Conn(): - return self.__DB_ERR - try: - self.__DB_CUR.execute(sql) - result = self.__DB_CUR.fetchall() - # 将元组转换成列表 - # data = list(map(list, result)) - self.__Close() - return result - except Exception as ex: - return ex - - # 关闭连接 - def __Close(self): - self.__DB_CUR.close() - self.__DB_CONN.close() diff --git a/plugins/mysql-yum/class/orm.py b/plugins/mysql-yum/class/orm.py deleted file mode 100755 index 281d5cac0..000000000 --- a/plugins/mysql-yum/class/orm.py +++ /dev/null @@ -1,96 +0,0 @@ -# coding: utf-8 - -import re -import os -import sys - - -from mysql import connector - -sys.path.append(os.getcwd() + "/class/core") -import mw - -# if mw.isAppleSystem(): -# cmd = 'ls /usr/local/lib/ | grep python | cut -d \\ -f 1 | awk \'END {print}\'' -# info = mw.execShell(cmd) -# p = "/usr/local/lib/" + info[0].strip() + "/site-packages" -# sys.path.append(p) - - -class ORM: - __DB_PASS = None - __DB_USER = 'root' - __DB_PORT = 3306 - __DB_HOST = 'localhost' - __DB_CONN = None - __DB_CUR = None - __DB_ERR = None - __DB_CNF = '/etc/my.cnf' - - def __Conn(self): - '''连接MYSQL数据库''' - try: - try: - myconf = mw.readFile(self.__DB_CNF) - rep = "port\s*=\s*([0-9]+)" - self.__DB_PORT = int(re.search(rep, myconf).groups()[0]) - - rep = "socket\s*=\s*(.*)" - socket = re.search(rep, myconf).groups()[0] - except: - self.__DB_PORT = 3306 - socket = '/www/server/mysql-yum/mysql.sock' - - try: - self.__DB_CONN = connector.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS, - port=self.__DB_PORT, charset="utf8", connect_timeout=1, unix_socket=socket) - except Exception as e: - self.__DB_HOST = '127.0.0.1' - self.__DB_CONN = connector.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS, - port=self.__DB_PORT, charset="utf8", connect_timeout=1, unix_socket=socket) - - self.__DB_CUR = self.__DB_CONN.cursor() - return True - except Exception as e: - self.__DB_ERR = e - return False - - def setDbConf(self, conf): - self.__DB_CNF = conf - - def setPwd(self, pwd): - self.__DB_PASS = pwd - - def getPwd(self): - return self.__DB_PASS - - def execute(self, sql): - # 执行SQL语句返回受影响行 - if not self.__Conn(): - return self.__DB_ERR - try: - result = self.__DB_CUR.execute(sql) - self.__DB_CONN.commit() - self.__Close() - return result - except Exception as ex: - return ex - - def query(self, sql): - # 执行SQL语句返回数据集 - if not self.__Conn(): - return self.__DB_ERR - try: - self.__DB_CUR.execute(sql) - result = self.__DB_CUR.fetchall() - # 将元组转换成列表 - # data = list(map(list, result)) - self.__Close() - return result - except Exception as ex: - return ex - - def __Close(self): - # 关闭连接 - self.__DB_CUR.close() - self.__DB_CONN.close() diff --git a/plugins/mysql-yum/index.py b/plugins/mysql-yum/index.py index 148e2fc20..c681667fe 100755 --- a/plugins/mysql-yum/index.py +++ b/plugins/mysql-yum/index.py @@ -35,39 +35,14 @@ def getPluginName(): def getPluginDir(): return mw.getPluginDir() + '/' + getPluginName() -sys.path.append(getPluginDir() + "/class") -import mysqlDb - def getServerDir(): return mw.getServerDir() + '/' + getPluginName() -def is_number(s): - try: - float(s) - return True - except ValueError: - pass - - try: - import unicodedata - unicodedata.numeric(s) - return True - except (TypeError, ValueError): - pass - - return False - - def getArgs(): args = sys.argv[2:] - # print(args) - - # if is_number(args): - # args = sys.argv[3:] - tmp = {} args_len = len(args) @@ -95,6 +70,38 @@ def getConf(): return path +def getDataDir(): + file = getConf() + content = mw.readFile(file) + rep = 'datadir\s*=\s*(.*)' + tmp = re.search(rep, content) + return tmp.groups()[0].strip() + + +def getPidFile(): + file = getConf() + content = mw.readFile(file) + rep = 'pid-file\s*=\s*(.*)' + tmp = re.search(rep, content) + return tmp.groups()[0].strip() + + +def getDbPort(): + file = getConf() + content = mw.readFile(file) + rep = 'port\s*=\s*(.*)' + tmp = re.search(rep, content) + return tmp.groups()[0].strip() + + +def getSocketFile(): + file = getConf() + content = mw.readFile(file) + rep = 'socket\s*=\s*(.*)' + tmp = re.search(rep, content) + return tmp.groups()[0].strip() + + def contentReplace(content): service_path = mw.getServerDir() content = content.replace('{$ROOT_PATH}', mw.getRootDir()) @@ -125,12 +132,10 @@ def pSqliteDb(dbname='databases'): def pMysqlDb(): - sys.path.append(getPluginDir() + "/class") - import orm - - db = orm.ORM() + db = mw.getMyORM() db.__DB_CNF = getConf() - db.setDbConf(getConf()) + db.__DB_PORT = getDbPort() + db.__DB_SOCKET = getSocketFile() db.setPwd(pSqliteDb('config').where('id=?', (1,)).getField('mysql_root')) return db @@ -176,22 +181,6 @@ def status(version=''): return 'start' -def getDataDir(): - file = getConf() - content = mw.readFile(file) - rep = 'datadir\s*=\s*(.*)' - tmp = re.search(rep, content) - return tmp.groups()[0].strip() - - -def getPidFile(): - file = getConf() - content = mw.readFile(file) - rep = 'pid-file\s*=\s*(.*)' - tmp = re.search(rep, content) - return tmp.groups()[0].strip() - - def binLog(): args = getArgs() conf = getConf() diff --git a/plugins/mysql/class/mysqlDb.py b/plugins/mysql/class/mysqlDb.py deleted file mode 100755 index 0f27e56e9..000000000 --- a/plugins/mysql/class/mysqlDb.py +++ /dev/null @@ -1,98 +0,0 @@ -# coding: utf-8 - -import re -import os -import sys - -# sys.path.append("/usr/local/lib/python3.9/site-packages") - -sys.path.append(os.getcwd() + "/class/core") -import mw - -# if mw.isAppleSystem(): -# cmd = 'ls /usr/local/lib/ | grep python | cut -d \\ -f 1 | awk \'END {print}\'' -# info = mw.execShell(cmd) -# p = "/usr/local/lib/" + info[0].strip() + "/site-packages" -# sys.path.append(p) - - -class mysqlDb: - __DB_PASS = None - __DB_USER = 'root' - __DB_PORT = 3306 - __DB_HOST = 'localhost' - __DB_CONN = None - __DB_CUR = None - __DB_ERR = None - __DB_CNF = '/etc/my.cnf' - - def __Conn(self): - '''连接MYSQL数据库''' - try: - import mw - socket = '/www/server/mysql/mysql.sock' - try: - import MySQLdb - except Exception as ex: - # print('dd') - self.__DB_ERR = ex - return False - try: - myconf = mw.readFile(self.__DB_CNF) - rep = "port\s*=\s*([0-9]+)" - self.__DB_PORT = int(re.search(rep, myconf).groups()[0]) - except: - self.__DB_PORT = 3306 - - try: - self.__DB_CONN = MySQLdb.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS, - port=self.__DB_PORT, charset="utf8", connect_timeout=1, unix_socket=socket) - except MySQLdb.Error as e: - self.__DB_HOST = '127.0.0.1' - self.__DB_CONN = MySQLdb.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS, - port=self.__DB_PORT, charset="utf8", connect_timeout=1, unix_socket=socket) - self.__DB_CUR = self.__DB_CONN.cursor() - return True - except MySQLdb.Error as e: - self.__DB_ERR = e - return False - - def setDbConf(self, conf): - self.__DB_CNF = conf - - def setPwd(self, pwd): - self.__DB_PASS = pwd - - def getPwd(self): - return self.__DB_PASS - - def execute(self, sql): - # 执行SQL语句返回受影响行 - if not self.__Conn(): - return self.__DB_ERR - try: - result = self.__DB_CUR.execute(sql) - self.__DB_CONN.commit() - self.__Close() - return result - except Exception as ex: - return ex - - def query(self, sql): - # 执行SQL语句返回数据集 - if not self.__Conn(): - return self.__DB_ERR - try: - self.__DB_CUR.execute(sql) - result = self.__DB_CUR.fetchall() - # 将元组转换成列表 - # data = list(map(list, result)) - self.__Close() - return result - except Exception as ex: - return ex - - # 关闭连接 - def __Close(self): - self.__DB_CUR.close() - self.__DB_CONN.close() diff --git a/plugins/mysql/class/orm.py b/plugins/mysql/class/orm.py deleted file mode 100755 index 2410917d8..000000000 --- a/plugins/mysql/class/orm.py +++ /dev/null @@ -1,97 +0,0 @@ -# coding: utf-8 - -import re -import os -import sys - - -from mysql import connector - -sys.path.append(os.getcwd() + "/class/core") -import mw - -# if mw.isAppleSystem(): -# cmd = 'ls /usr/local/lib/ | grep python | cut -d \\ -f 1 | awk \'END {print}\'' -# info = mw.execShell(cmd) -# p = "/usr/local/lib/" + info[0].strip() + "/site-packages" -# sys.path.append(p) - - -class ORM: - __DB_PASS = None - __DB_USER = 'root' - __DB_PORT = 3306 - __DB_HOST = 'localhost' - __DB_CONN = None - __DB_CUR = None - __DB_ERR = None - __DB_CNF = '/etc/my.cnf' - - def __Conn(self): - '''连接MYSQL数据库''' - try: - - try: - myconf = mw.readFile(self.__DB_CNF) - rep = "port\s*=\s*([0-9]+)" - self.__DB_PORT = int(re.search(rep, myconf).groups()[0]) - - rep = "socket\s*=\s*(.*)" - socket = re.search(rep, myconf).groups()[0] - except: - self.__DB_PORT = 3306 - socket = '/www/server/mysql/mysql.sock' - - try: - self.__DB_CONN = connector.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS, - port=self.__DB_PORT, charset="utf8", connect_timeout=1, unix_socket=socket) - except Exception as e: - self.__DB_HOST = '127.0.0.1' - self.__DB_CONN = connector.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS, - port=self.__DB_PORT, charset="utf8", connect_timeout=1, unix_socket=socket) - - self.__DB_CUR = self.__DB_CONN.cursor() - return True - except Exception as e: - self.__DB_ERR = e - return False - - def setDbConf(self, conf): - self.__DB_CNF = conf - - def setPwd(self, pwd): - self.__DB_PASS = pwd - - def getPwd(self): - return self.__DB_PASS - - def execute(self, sql): - # 执行SQL语句返回受影响行 - if not self.__Conn(): - return self.__DB_ERR - try: - result = self.__DB_CUR.execute(sql) - self.__DB_CONN.commit() - self.__Close() - return result - except Exception as ex: - return ex - - def query(self, sql): - # 执行SQL语句返回数据集 - if not self.__Conn(): - return self.__DB_ERR - try: - self.__DB_CUR.execute(sql) - result = self.__DB_CUR.fetchall() - # 将元组转换成列表 - # data = list(map(list, result)) - self.__Close() - return result - except Exception as ex: - return ex - - def __Close(self): - # 关闭连接 - self.__DB_CUR.close() - self.__DB_CONN.close() diff --git a/plugins/mysql/index.py b/plugins/mysql/index.py index 310babef6..8af90b93f 100755 --- a/plugins/mysql/index.py +++ b/plugins/mysql/index.py @@ -35,9 +35,6 @@ def getPluginName(): def getPluginDir(): return mw.getPluginDir() + '/' + getPluginName() -sys.path.append(getPluginDir() + "/class") -import mysqlDb - def getServerDir(): return mw.getServerDir() + '/' + getPluginName() @@ -79,6 +76,22 @@ def getConf(): return path +def getDbPort(): + file = getConf() + content = mw.readFile(file) + rep = 'port\s*=\s*(.*)' + tmp = re.search(rep, content) + return tmp.groups()[0].strip() + + +def getSocketFile(): + file = getConf() + content = mw.readFile(file) + rep = 'socket\s*=\s*(.*)' + tmp = re.search(rep, content) + return tmp.groups()[0].strip() + + def getInitdTpl(version=''): path = getPluginDir() + '/init.d/mysql' + version + '.tpl' if not os.path.exists(path): @@ -121,12 +134,10 @@ def pSqliteDb(dbname='databases'): def pMysqlDb(): - sys.path.append(getPluginDir() + "/class") - import orm - - db = orm.ORM() + db = mw.getMyORM() db.__DB_CNF = getConf() - db.setDbConf(getConf()) + db.__DB_PORT = getDbPort() + db.__DB_SOCKET = getSocketFile() db.setPwd(pSqliteDb('config').where('id=?', (1,)).getField('mysql_root')) return db