diff --git a/SystemUpdater/Core/UpdateList.py b/SystemUpdater/Core/UpdateList.py index 8540895..241370f 100644 --- a/SystemUpdater/Core/UpdateList.py +++ b/SystemUpdater/Core/UpdateList.py @@ -29,6 +29,7 @@ import json import yaml import shutil import apt_pkg +from gi.repository import Gio OUTPUT_CONFIG_PATH = apt_pkg.config.find_dir("Kylin-system-updater::OutputConfigDir", '/var/lib/kylin-system-updater') @@ -45,6 +46,18 @@ class UpdateList(): #所有的组升级安装列表 self.local_upgrade_list = {} + if 'XDG_CURRENT_DESKTOP' in os.environ: + self.current_desktop = os.environ.get('XDG_CURRENT_DESKTOP') + else: + self.current_desktop = '' + + if 'XDG_DATA_DIRS' in os.environ and os.environ['XDG_DATA_DIRS']: + data_dirs = os.environ['XDG_DATA_DIRS'] + else: + data_dirs = '/usr/local/share/:/usr/share/' + + self.application_dirs = [os.path.join(base, 'applications') + for base in data_dirs.split(':')] #清空上次输出的分组JSON文件 def _empty_output_dir(self): try: @@ -233,17 +246,97 @@ class UpdateList(): logging.warning("Generate Jsonfile to failed... ") logging.error(e) + def _rate_application_for_package(self, application, pkg): + score = 0 + desktop_file = os.path.basename(application.get_filename()) + application_id = os.path.splitext(desktop_file)[0] + + if application.should_show(): + score += 1 + + if application_id == pkg.name: + score += 5 + + return score + + def _file_is_application(self, file_path): + # WARNING: This is called often if there's a lot of updates. A poor + # performing call here has a huge impact on the overall performance! + if not file_path.endswith(".desktop"): + # First the obvious case: If the path doesn't end in a .desktop + # extension, this isn't a desktop file. + return False + + file_path = os.path.abspath(file_path) + for app_dir in self.application_dirs: + if file_path.startswith(app_dir): + return True + return False + + def _get_application_for_package(self, pkg): + desktop_files = [] + rated_applications = [] + + for installed_file in pkg.installed_files: + if self._file_is_application(installed_file): + desktop_files.append(installed_file) + + for desktop_file in desktop_files: + try: + application = Gio.DesktopAppInfo.new_from_filename( + desktop_file) + application.set_desktop_env(self.current_desktop) + except Exception as e: + logging.warning("Error loading .desktop file %s: %s" % + (desktop_file, e)) + continue + score = self._rate_application_for_package(application, pkg) + if score > 0: + rated_applications.append((score, application)) + + rated_applications.sort(key=lambda app: app[0], reverse=True) + if len(rated_applications) > 0: + return rated_applications[0][1] + else: + return None + def _make_single_upgrade(self,cache,pkg_list): try: for pkg in pkg_list: - pkgs_json = self._make_pkg_info_json(cache,[pkg]) - - #FIXME: 确定输出文件的文件名 以及放置位置 + zh_name = '' + groups_base_info = {} + output_json = {} output_config_name = OUTPUT_CONFIG_PATH + pkg + '.json' + pkg_cache = cache[pkg] + + if pkg_cache.is_installed: + app = self._get_application_for_package(pkg_cache) + if app is not None: + zh_name = app.get_display_name() + en_name = getattr(pkg_cache.candidate, "summary", '') + pkgs_json = self._make_pkg_info_json(cache,[pkg]) + + #4、添加一些基础信息 + groups_base_info.update({"package":pkg}) + + groups_base_info.update({"cur_version":getattr(pkg_cache.installed, "version", '')}) + groups_base_info.update({"can_version":getattr(pkg_cache.candidate, "version", '')}) + groups_base_info.update({"name":{"zh_CN":zh_name,"en_US":en_name}}) + groups_base_info.update({"description":getattr(pkg_cache.candidate, "description", '')}) + groups_base_info.update({"icon":''}) + + #5、添加升级的内容 + output_json.update(groups_base_info) + if pkg_cache.is_installed: + output_json.update({"upgrade_list":pkgs_json}) + output_json.update({"install_list":{}}) + else: + output_json.update({"upgrade_list":{}}) + output_json.update({"install_list":pkgs_json}) #产生JSON文件 with open(output_config_name, 'w', encoding='utf-8') as f: - json.dump(pkgs_json, f, ensure_ascii=False, indent=4) + json.dump(output_json, f, ensure_ascii=False, indent=4) logging.info("Generate Jsonfile(%s) to complete... ",output_config_name) except Exception as e: logging.warning("Generate Jsonfile to failed... ")