class SettingConfig(QObject): def __init__(self): QObject.__init__(self) self.config_file = get_config_file("config.ini") if os.path.exists(self.config_file): self.config = Config(self.config_file, DEFAULT_CONFIG) self.config.load() else: self.config = Config(self.config_file, DEFAULT_CONFIG) self.config.write() @pyqtSlot(str, bool) def update_trayicon_config(self, config_id, config_value): with self.config.save_config(): self.config.set("trayicon", config_id, config_value) @pyqtSlot(str, str) def update_translate_config(self, config_id, config_value): with self.config.save_config(): self.config.set("translate", config_id, config_value) @pyqtSlot(str, result=bool) def get_trayicon_config(self, option): return is_true(self.config.get_config("trayicon", option)) @pyqtSlot(str, result=str) def get_translate_config(self, option): return self.config.get_config("translate", option)
def run(): if not os.path.exists(data_newest_id_path): newest_data_id_config = Config(data_newest_id_path) newest_data_id_config.load() newest_data_id_config.set("newest", "data_id", "") newest_data_id_config.set("newest", "update_date", "") newest_data_id_config.write() else: newest_data_id_config = Config(data_newest_id_path) newest_data_id_config.load() try: update_date = newest_data_id_config.get("newest", "update_date") except Exception: update_date = "" if newest_data_id_config.get("newest", "data_id") == "" or update_date != UPDATE_DATE: clean() newest_data_id = str(uuid.uuid4()) newest_data_dir = os.path.join(DATA_DIR, "update", newest_data_id) print "进行第一次数据解压..." for data_file in os.listdir(data_origin_dir): with tarfile.open(os.path.join(data_origin_dir, data_file), "r:gz") as tar_file: tar_file.extractall(newest_data_dir) print "进行第一次数据解压完成" newest_data_id_config.set("newest", "data_id", newest_data_id) newest_data_id_config.set("newest", "update_date", UPDATE_DATE) newest_data_id_config.write()
def save_skin_name(self): ''' Internal function to save skin name. ''' skin_config = Config(self.skin_config_file) skin_config.load() if skin_config.get("skin", "skin_name") != self.skin_name: skin_config.set("skin", "skin_name", self.skin_name) skin_config.write(self.skin_config_file)
def data_init(): global data_init_flag global DATA_ID if not data_init_flag: data_init_flag = True data_newest_id_config = Config(DATA_NEWEST_ID_CONFIG_FILE) data_newest_id_config.load() DATA_ID = data_newest_id_config.get("newest", "data_id") if not os.path.exists(DATA_CURRENT_ID_CONFIG_FILE): touch_file(DATA_CURRENT_ID_CONFIG_FILE) os.chmod(DATA_CURRENT_ID_CONFIG_FILE, 0777) data_current_id_config = Config(DATA_CURRENT_ID_CONFIG_FILE) data_current_id_config.load() data_current_id_config.set("current", "data_id", DATA_ID) data_current_id_config.write()
def init_config(self): if os.path.exists(CONFIG_INFO_PATH): config = Config(CONFIG_INFO_PATH) config.load() uid = config.get("statistics", 'uid') if not uid: uid = uuid.uuid4().hex config.set("statistics", 'uid', uid) config.set("statistics", 'last_date', '') config.write() else: touch_file(CONFIG_INFO_PATH) uid = uuid.uuid4().hex config = Config(CONFIG_INFO_PATH) config.load() config.set("statistics", 'uid', uid) config.set("statistics", 'last_date', '') config.write() return config
def data_exit(): data_current_id_config = Config(DATA_CURRENT_ID_CONFIG_FILE) data_current_id_config.load() data_current_id_config.set("current", "data_id", "") data_current_id_config.write()
class UpdateDataService(dbus.service.Object): ''' class docs ''' def __init__(self, system_bus, mainloop): ''' init docs ''' # Init dbus service. dbus.service.Object.__init__(self, system_bus, DSC_UPDATER_PATH) self.mainloop = mainloop self.data_origin_dir = os.path.join(DATA_DIR, "origin") self.data_newest_dir = os.path.join(DATA_DIR, "newest") self.data_patch_dir = os.path.join(DATA_DIR, "patch") self.data_patch_config_filepath = os.path.join(DATA_DIR, "patch_status.ini") self.data_newest_id_path = os.path.join(DATA_DIR, "data_newest_id.ini") def get_unique_id(self): return str(uuid.uuid4()) def run(self, test): # Init ini files. if not os.path.exists(self.data_newest_id_path): self.newest_data_id_config = Config(self.data_newest_id_path) self.newest_data_id_config.load() self.newest_data_id_config.set("newest", "data_id", "") self.newest_data_id_config.set("newest", "update_date", "") self.newest_data_id_config.write() else: self.newest_data_id_config = Config(self.data_newest_id_path) self.newest_data_id_config.load() try: update_date = self.newest_data_id_config.get("newest", "update_date") except Exception: update_date = "" if self.newest_data_id_config.get("newest", "data_id") == "" or update_date != UPDATE_DATE: self.clean() newest_data_id = self.get_unique_id() newest_data_dir = os.path.join(DATA_DIR, "update", newest_data_id) print "进行第一次数据解压..." log("进行第一次数据解压...") for data_file in os.listdir(self.data_origin_dir): with tarfile.open(os.path.join(self.data_origin_dir, data_file), "r:gz") as tar_file: tar_file.extractall(newest_data_dir) print "进行第一次数据解压完成" log("进行第一次数据解压完成") self.newest_data_id_config.set("newest", "data_id", newest_data_id) self.newest_data_id_config.set("newest", "update_date", UPDATE_DATE) self.newest_data_id_config.write() if not os.path.exists(self.data_patch_config_filepath): self.patch_status_config = Config(self.data_patch_config_filepath) self.patch_status_config.load() for space_name in DATA_SPACE_NAME: self.patch_status_config.set("data_md5", space_name, "") self.patch_status_config.write() else: self.patch_status_config = Config(self.data_patch_config_filepath) self.patch_status_config.load() self.have_update = [] # Download update data. for data_file in os.listdir(self.data_origin_dir): self.download_data(data_file, test) if self.have_update: # Apply update data. for space_name in self.have_update: self.apply_data(space_name) # Extra data. newest_data_id = self.get_unique_id() newest_data_dir = os.path.join(DATA_DIR, "update", newest_data_id) for space_name in DATA_SPACE_NAME: data_filename = "%s.tar.gz" % space_name origin_data_file = os.path.join(self.data_origin_dir, data_filename) newest_data_file = os.path.join(self.data_newest_dir, data_filename) if not os.path.exists(newest_data_file): os.system('cp %s %s' % (origin_data_file, newest_data_file)) print "解压最新数据..." log("解压最新数据...") for data_file in os.listdir(self.data_newest_dir): newest_file = os.path.join(self.data_newest_dir, data_file) with tarfile.open(newest_file, "r:gz") as tar_file: tar_file.extractall(newest_data_dir) print "解压最新数据完成" log("解压最新数据完成") self.previous_data_id = self.newest_data_id_config.get("newest", "data_id") self.newest_data_id_config.set("newest", "data_id", newest_data_id) self.newest_data_id_config.write() if self.is_fontend_running(): print 'Frontend is running, clear data next time!' log('Frontend is running, clear data next time!') else: print 'Clear unused data.' log('Clear unused data.') self.clear_data_folder() print 'Done!' log("Done!") glib.timeout_add(200, self.mainloop.quit) def is_fontend_running(self): if os.path.exists(DATA_CURRENT_ID_CONFIG_PATH): config = Config(DATA_CURRENT_ID_CONFIG_PATH) config.load() data_id = config.get('current', 'data_id') if data_id: return True else: return False else: False def clear_data_folder(self): # clear data when ui is not running # judge which data is in using if os.path.exists(DATA_CURRENT_ID_CONFIG_PATH): current_data_id_config = Config(DATA_CURRENT_ID_CONFIG_PATH) current_data_id_config.load() current_data_id = current_data_id_config.get("current", "data_id") else: current_data_id = None self.newest_data_id_config.load() newest_data_id = self.newest_data_id_config.get("newest", "data_id") data_file_list = ["newest", "origin", "patch", "update", "data_newest_id.ini", "patch_status.ini", "cache_soft.db", "origin_data_time" ] data_id_list = (current_data_id, newest_data_id) for data_file in os.listdir(DATA_DIR): if data_file not in data_file_list: remove_directory(os.path.join(DATA_DIR, data_file)) print ">> remove file: %s" % data_file log(">> remove file: %s" % data_file) elif data_file == "update": for data_id in os.listdir(os.path.join(DATA_DIR, "update")): if data_id not in data_id_list: remove_directory(os.path.join(DATA_DIR, "update", data_id)) print '>> remove old data: %s' % data_id log('>> remove old data: %s' % data_id) def download_data(self, data_file, test): origin_data_md5 = md5_file(os.path.join(self.data_origin_dir, data_file)) space_name = data_file.split(".tar.gz")[0] patch_dir = os.path.join(self.data_patch_dir, space_name) # Create download directory. create_directory(patch_dir) if test: remote_url = "http://%s.%s/test" % (space_name, UPDATE_DATA_URL) else: remote_url = "http://%s.%s/3.1" % (space_name, UPDATE_DATA_URL) patch_list_url = "%s/patch/%s/patch_md5.json" % (remote_url, origin_data_md5) try: patch_list_json = json.load(urllib2.urlopen(patch_list_url)) except Exception, e: patch_list_json = "" if patch_list_json != "": patch_name = patch_list_json["current_patch"][0]["name"].encode("utf-8") patch_md5 = patch_list_json["current_patch"][0]["md5"].encode("utf-8") local_patch_info = self.patch_status_config.get("data_md5", space_name) if local_patch_info == '' or (local_patch_info != '' and eval(local_patch_info)[1] != patch_md5): # Start download. download_url = "%s/patch/%s/%s" % (remote_url, origin_data_md5, patch_name) local_patch_file = os.path.join(patch_dir, patch_name) # TODO: 此处添加下载返回值判断 os.system("wget %s -t 5 -c -O %s" % (download_url, local_patch_file)) try: download_md5 = md5_file(local_patch_file) if download_md5 == patch_md5: self.have_update.append(space_name) if local_patch_info: remove_file(os.path.join(self.data_patch_dir, eval(local_patch_info)[0])) self.patch_status_config.set("data_md5", space_name, [patch_name, patch_md5]) self.patch_status_config.write() print "%s: 补丁%s下载成功" % (space_name, patch_name) log("%s: 补丁%s下载成功" % (space_name, patch_name)) else: print "%s: 补丁%s下载错误" (space_name, patch_name) log("%s: 补丁%s下载错误" (space_name, patch_name)) except: print "%s: 补丁%s下载失败" (space_name, patch_name) log("%s: 补丁%s下载失败" (space_name, patch_name)) else: print "%s: 当前数据是最新的" % space_name log("%s: 当前数据是最新的" % space_name) else: print "%s: 网络问题或者远端没有任何更新补丁" % space_name log("%s: 网络问题或者远端没有任何更新补丁" % space_name)