生成JSON文件内容的函数

This commit is contained in:
wangsong 2021-09-07 15:36:51 +08:00
parent 7a8200b8f3
commit df38199677
4 changed files with 110 additions and 36 deletions

View File

@ -28,7 +28,7 @@ import warnings
warnings.filterwarnings("ignore", "Accessed deprecated property",
DeprecationWarning)
from gettext import gettext as _
from gettext import gettext as _, install
import apt
import logging
import itertools
@ -221,7 +221,7 @@ class UpdateList():
self.pkgs_remove = []
#FIXME: 最好将这个常量通过配置文件读
self.GROUPS_JSON_PKG = 'kylin-calculator'
self.GROUPS_JSON_PKG = 'kylin-update-desktop-config'
# a stable machine uniq id
try:
@ -497,18 +497,21 @@ class UpdateList():
'''
self.distUpgradeWouldDelete = cache.saveDistUpgrade()
#安装JSON分组配置文件包 安装完毕会重新调start_available --> update --> here FIXME: 在此加入判断是否安装失败多次安装 重复次数 超出退出
#安装JSON分组配置文件包 安装完毕会重新调start_available --> update --> here
try:
pkg_json = cache[self.GROUPS_JSON_PKG]
#是否安装
if pkg_json.is_installed:
#是否可升级
if pkg_json.is_upgradable:
logging.info('groups JSON Config start upgrading...')
start_install_alone(pkgs_install = [],pkgs_upgrade = [self.GROUPS_JSON_PKG],pkgs_remove = [])
else:
logging.info('groups JSON Config start new installing...')
start_install_alone(pkgs_install = [self.GROUPS_JSON_PKG],pkgs_upgrade = [],pkgs_remove = [])
except KeyError:
pass
#FIXME: 错误处理未做
except Exception as e:
logging.error(e)
# Find all upgradable packages
for pkg in cache:
@ -522,21 +525,85 @@ class UpdateList():
self.pkgs_upgrade.append(pkg.name)
elif pkg.marked_delete:
self.pkgs_remove.append(pkg.name)
except KeyError:
pass
#支持输出json文件 包含新安装 and 升级 and 移除
# pkgs_json = {"install": self.pkgs_install, "upgrade": self.pkgs_upgrade,"remove": self.pkgs_remove}
# # FIXME: 配置文件防止位置需要确定
# with open('pkgs.json', 'w', encoding='utf-8') as f:
# json.dump(pkgs_json, f, ensure_ascii=False, indent=4)
except Exception as e:
logging.error(e)
#FIXME: 目前此功能不使用 但是以此按应用进行分组是更好的展示升级列表的方式
# self.update_groups = self._make_groups(cache, upgrade_pkgs,
# self.update_groups = self._make_groups(cache, self.pkgs_upgrade,
# eventloop_callback)
path = '/home/x/share/config'
try:
files = os.listdir(path) #获得文件夹中所有文件的名称列表
for file in files:
#判是否是目录以及是否以JSON结尾
if not os.path.isdir(file) and file.endswith('.json'):
with open(path+"/"+file,'r') as f:
data = json.load(f)
output_json = {}
install_pkgs_list = data['install_list']
upgrade_pkgs_list = data['upgrade_list']
hold_pkgs_list = data['hold_list']
remove_pkgs_list = data['remove_list']
#进行交集 查找两个列表同时存在的
upgrade_intersection_pkgs = list(set(self.pkgs_upgrade) & set(upgrade_pkgs_list))
#在总升级列表中移除这些包
for pkg in upgrade_intersection_pkgs:
self.pkgs_upgrade.remove(pkg)
#生成需要升级的包的JSON内容
size = 0
total_size = 0
upgrade_pkgs_json = {}
for pkg_name in upgrade_intersection_pkgs:
try:
pkg = cache[pkg_name]
#获取下载大小
size = getattr(pkg.candidate, "size", 0)
total_size = total_size + size
upgrade_pkgs_json.update({pkg_name:{"size":size}})
except Exception as e:
pass
upgrade_pkgs_json.update({"total_size":humanize_size(total_size)})
#生成安装的软件列表
install_pkgs_json = {}
total_size = 0
for pkg_name in install_pkgs_list:
try:
#会存在不在cache的包直接抛出异常
pkg = cache[pkg_name]
#如果这个包已经安装则不计算在内
if pkg.is_installed:
continue
#获取下载大小
size = getattr(pkg.candidate, "size", 0)
total_size = total_size + size
install_pkgs_json.update({pkg_name:{"size":size}})
except Exception as e:
pass
# logging.DEBUG(e)
install_pkgs_json.update({"total_size":humanize_size(total_size)})
output_json.update({"upgrade_list":upgrade_pkgs_json})
output_json.update({"install_list":install_pkgs_json})
output_json.update({"hold_list":hold_pkgs_list})
output_json.update({"remove_list":remove_pkgs_list})
# # FIXME: 配置文件防止位置需要确定
with open('pkgs.json', 'w', encoding='utf-8') as f:
json.dump(output_json, f, ensure_ascii=False, indent=4)
else:
pass
except Exception as e:
logging.error(e)
# fu = filter.UpdateListFilterCache("/")
# upgrade_pkgs
@ -553,4 +620,12 @@ class UpdateList():
# #blacklist_filter
# blacklist_filter_pkgs = fu.is_pkgname_in_blacklist(whitelist_filter_upgrade_pkgs)
# print("blacklist_filter_pkgs: %s"%" ".join([i.name for i in blacklist_filter_pkgs]))
#支持输出json文件 包含新安装 and 升级 and 移除
# pkgs_json = {"install": self.pkgs_install, "upgrade": self.pkgs_upgrade,"remove": self.pkgs_remove}
# # FIXME: 配置文件防止位置需要确定
# with open('pkgs.json', 'w', encoding='utf-8') as f:
# json.dump(pkgs_json, f, ensure_ascii=False, indent=4)

View File

@ -111,7 +111,7 @@ class InstallBackendAptdaemon(InstallBackend):
# trans.connect("download-changed", self._on_download_changed)
#trans.connect("config-file-conflict", self._on_config_file_conflict)
trans.connect("config-file-conflict", self._on_config_file_conflict)
# yield trans.set_debconf_frontend("kylin")
yield trans.run()

View File

@ -36,24 +36,24 @@ class InstallBackend():
# Get a fresh cache in case update-manager's is outdated to
# skip operations that already took place
fresh_cache = Cache(rootdir=self.window_main.cache.rootdir)
for pkg in self.window_main.cache:
try:
if pkg.marked_install \
and not fresh_cache[pkg.name].is_installed:
pkgname = pkg.name
if pkg.is_auto_installed:
pkgname += "#auto"
pkgs_install.append(pkgname)
elif (pkg.marked_upgrade
and fresh_cache[pkg.name].is_upgradable):
pkgs_upgrade.append(pkg.name)
elif (pkg.marked_delete
and fresh_cache[pkg.name].is_installed):
pkgs_remove.append(pkg.name)
except KeyError:
# pkg missing from fresh_cache can't be modified
pass
# fresh_cache = Cache(rootdir=self.window_main.cache.rootdir)
# for pkg in self.window_main.cache:
# try:
# if pkg.marked_install \
# and not fresh_cache[pkg.name].is_installed:
# pkgname = pkg.name
# if pkg.is_auto_installed:
# pkgname += "#auto"
# pkgs_install.append(pkgname)
# elif (pkg.marked_upgrade
# and fresh_cache[pkg.name].is_upgradable):
# pkgs_upgrade.append(pkg.name)
# elif (pkg.marked_delete
# and fresh_cache[pkg.name].is_installed):
# pkgs_remove.append(pkg.name)
# except KeyError:
# # pkg missing from fresh_cache can't be modified
# pass
self.commit(pkgs_install, pkgs_upgrade, pkgs_remove)
else:

View File

@ -38,4 +38,3 @@ if __name__ == "__main__":
app = UpdateManager(options)
app.start_update()
Gtk.main()