From 1be7115e28cf970f4642231dea4552b3f9a6ca23 Mon Sep 17 00:00:00 2001 From: luoxueyi Date: Fri, 21 Oct 2022 11:26:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=95=B0=E6=8D=AE=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/SystemUpdater/Core/DataAcquisition.py | 9 +- backend/SystemUpdater/Core/Database.py | 92 +++++++++++++------ backend/SystemUpdater/backend/__init__.py | 3 +- 3 files changed, 66 insertions(+), 38 deletions(-) diff --git a/backend/SystemUpdater/Core/DataAcquisition.py b/backend/SystemUpdater/Core/DataAcquisition.py index 2c15f98..33b0d20 100644 --- a/backend/SystemUpdater/Core/DataAcquisition.py +++ b/backend/SystemUpdater/Core/DataAcquisition.py @@ -310,7 +310,7 @@ class MessageSend(): except AttributeError: logging.error("Call UploadMessage: Attribute Error.") self.Send_finally(retval, retid, PackageInfo, UploadMessage, encodeMsg) - + def Send_finally(self, retval, retid, json_PackageInfo, json_UploadMessage, encodeMsg): # 根据发送结果进行处理 result = '' @@ -349,13 +349,6 @@ class MessageSend(): def GetLocalTid(self, key): # 试图获取本地tid try: - # if os.path.exists(LOCALTIDDIR+LOCALTIDFILE): - # with open(LOCALTIDDIR+LOCALTIDFILE, "r") as f: - # self.localtid = f.readline() - # # print(self.localtid) - # else: - # self.localtid = "" - # 存放至数据库 tid = self.collector.updateManager.sqlite3_server.select_from_tid("tid",key) if tid == "None" or tid == None: diff --git a/backend/SystemUpdater/Core/Database.py b/backend/SystemUpdater/Core/Database.py index 80f2c75..9564c7d 100644 --- a/backend/SystemUpdater/Core/Database.py +++ b/backend/SystemUpdater/Core/Database.py @@ -17,41 +17,53 @@ from SystemUpdater.Core.utils import get_config_patch import apt_pkg from ..backend import InstallBackend + DB_FILE = os.path.join("/var/cache/kylin-system-updater/kylin-system-updater.db") -UMDB_FILE = os.path.join("/var/cache/kylin-system-updater/kylin-system-updater.db") +# UMDB_FILE = os.path.join("/var/cache/kylin-system-updater/kylin-system-updater.db") INSTALLED_LIST = [{"item": "errorcode", "type": "int", "default": "0"}] DISPALY_LIST = [] class Sqlite3Server(object): - def __init__(self, window_main): + def __init__(self, updateManager): self.connect = None - self.window_main = window_main + self.window_main = updateManager self.config_path = get_config_patch() - logging.info(_("Init Sqlite3Server...")) self.init_sqlit() # uncoverable配置文件 self.ucconfigs = UpgradeConfig(datadir = "/etc/kylin-version", name = "kylin-system-version.conf") self._system_version_config() - - # 初始化连接数据库 + # 初始化连接数据库,修改为使用时连接 def init_sqlit(self): try: - logging.info(_("Initialize the connection to the database ...")) - if os.path.isfile(DB_FILE): - self.connect = sqlite3.connect(DB_FILE, check_same_thread=False) - self.cursor = self.connect.cursor() - self.insert_new_field() - else: + logging.info(_("Initialize database files ...")) + if not os.path.isfile(DB_FILE): if not os.path.isdir(os.path.dirname(DB_FILE)): os.makedirs(os.path.dirname(DB_FILE)) shutil.copy("/usr/share/kylin-system-updater/kylin-system-updater.db", os.path.dirname(DB_FILE)) - self.connect = sqlite3.connect(DB_FILE, check_same_thread=False) - self.cursor = self.connect.cursor() - self.insert_new_field() except Exception as e: - logging.error(_("Failed to initialize the database: %s"), str(e)) + logging.error("Failed to initialize database files: %s", str(e)) + + #connect连接数据库 + def connect_database(self): + try: + logging.debug("Connect database ...") + self.connect = sqlite3.connect(DB_FILE, check_same_thread=False) + self.cursor = self.connect.cursor() + except Exception as e: + logging.error("Failed to connect database: %s", str(e)) + + #disconnect连接数据库 + def disconnect_database(self): + try: + logging.debug("Disconnect database ...") + if self.connect != None: + self.connect.close() + if self.connect != None: + del self.cursor + except Exception as e: + logging.error("Failed to disconnect database: %s", str(e)) # 数据库表格中动态增加新的字段用于扩展 def insert_new_field(self): @@ -99,33 +111,42 @@ class Sqlite3Server(object): # 写入数据到installed表中 def insert_into_installed(self, *args, **kwargs): + self.connect_database() self.cursor.execute( "insert into installed (appname, version, time, description, icon, statue, keyword, errorcode) values(?," "?,?,?,?,?,?,?)", (args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7])) self.connect.commit() + self.disconnect_database() # 写入数据到display表中 def insert_into_display(self, *args, **kwargs): + self.connect_database() try: sql = "update display set " + args[0] + "='" + args[1] + "' where id = 1" self.cursor.execute(sql) self.connect.commit() except Exception as e: logging.error("Insert error: %s.", str(e)) + self.disconnect_database() return False logging.info("Database: Insert (%s=%s) To display Complete ...", args[0], args[1]) + self.disconnect_database() return True # 写入数据到tid_search表中 def insert_into_tid(self, *args, **kwargs): + self.connect_database() self.cursor.execute( "insert into tid_search (key, tid) values(?,?)", (args[0], args[1])) self.connect.commit() + self.disconnect_database() # 搜索tid_search表,获取tid值 def select_from_tid(self, *args, **kwargs): + retval = '' + self.connect_database() try: sql = "select "+args[0]+" from tid_search where key='"+args[1]+"'" self.cursor.execute(sql) @@ -133,33 +154,42 @@ class Sqlite3Server(object): if len(rets)!= 0: if len(rets[0])!=0: ret_first = rets[0] - return str(ret_first[0]) + retval = str(ret_first[0]) except Exception as e: logging.error("Insert error: %s.", str(e)) + self.disconnect_database() logging.info(_("Database: Select data Complete...")) - return "" + self.disconnect_database() + return retval # 读出display表中数据 def select_from_display(self, *args, **kwargs): try: + self.connect_database() sql = "select "+args[0]+" from display" self.cursor.execute(sql) self.connect.commit() - return str(self.cursor.fetchone()[0]) + retval = str(self.cursor.fetchone()[0]) + self.disconnect_database() + return retval except Exception as e: logging.error("select error: %s.", str(e)) + self.disconnect_database() return "Error" # 写入updateinfos表中 def insert_into_updateinfo(self, *args, **kwargs): try: + self.connect_database() self.cursor.execute( "insert into updateinfos (appname, version, description, date, status, keyword, errorcode, appname_cn, status_cn, changelog) values(?," "?,?,?,?,?,?,?,?,?)", (args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9])) self.connect.commit() + self.disconnect_database() except Exception as e: logging.error("Insert error: %s.", str(e)) + self.disconnect_database() logging.info(_("Database: Insert To Complete...")) # 接收更新列表与信息,生成数据并插入数据库中 @@ -425,19 +455,20 @@ class Sqlite3Server(object): pass return pkgs_install,pkgs_upgrade,pkgs_remove - #查找数据库 - def find_msg_from_datebase(self, table, field, action = 'check', cid = 0): - # 查询数据 - try: - sql = "select "+field+" from "+table - self.cursor.execute(sql) - update_count = self.cursor.fetchone()[0] - logging.info("%d history updates detected.", update_count) - except Exception as e: - logging.error("Check update error: %s", str(e)) + # #查找数据库 + # def find_msg_from_datebase(self, table, field, action = 'check', cid = 0): + # # 查询数据 + # try: + # sql = "select "+field+" from "+table + # self.cursor.execute(sql) + # update_count = self.cursor.fetchone()[0] + # logging.info("%d history updates detected.", update_count) + # except Exception as e: + # logging.error("Check update error: %s", str(e)) def _system_version_config(self): try: + self.connect_database() sql = "select init_version from display where id=1" self.cursor.execute(sql) _is_init_verison = self.cursor.fetchone()[0] @@ -448,8 +479,10 @@ class Sqlite3Server(object): sql = "update display set init_version = 'no'" self.cursor.execute(sql) self.connect.commit() + self.disconnect_database() except Exception as e: logging.error(str(e)) + self.disconnect_database() def _refresh_system_version(self, update_version='', os_version = '', pseudo_version = False): try: @@ -545,6 +578,7 @@ class Sqlite3Server(object): cursor.execute(sql) connect.commit() retval = cursor.fetchone() + connect.close() if retval != None and len(retval) != 0: return str(retval[0]) else: diff --git a/backend/SystemUpdater/backend/__init__.py b/backend/SystemUpdater/backend/__init__.py index 06b1de0..80197dc 100644 --- a/backend/SystemUpdater/backend/__init__.py +++ b/backend/SystemUpdater/backend/__init__.py @@ -362,6 +362,7 @@ class InstallBackend(): # 没找到包或格式不正确 self.window_main.dbusController.UpdateInstallFinished(False, pkgs_install, "'"+pkg_name+"' is not in cache", "") return + self._start_install_lock(caller=caller) self.commit(self.ACTION_INSTALL,pkgs_install,[],[]) except Exception as e: logging.error(str(e)) @@ -850,7 +851,7 @@ class InstallBackend(): # self.window_main.collector.Upgrade_Process_Msg(self.action, UpdateMsg.copy()) self.window_main.dbusController.UpdateInstallFinished(success,self.now_upgrade.upgrade_content,error_string,error_desc) - + def _start_install_lock(self, caller='Kylin System Updater'): self.window_main.configs_uncover.setValue("SystemStatus","abnormal_reboot",str(True))