mirror of
https://github.com/spalencsar/bearhub.git
synced 2026-07-09 08:44:15 +02:00
[arch] fix: not filling cached data for installed suggestions
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from typing import Type
|
from typing import Type, Optional, Any, Dict
|
||||||
|
|
||||||
from bauh.api.abstract.cache import MemoryCache
|
from bauh.api.abstract.cache import MemoryCache
|
||||||
from bauh.api.abstract.model import SoftwarePackage
|
from bauh.api.abstract.model import SoftwarePackage
|
||||||
@@ -29,6 +29,12 @@ class DiskCacheLoader:
|
|||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def read(self, pkg: SoftwarePackage) -> Optional[Dict[str, Any]]:
|
||||||
|
"""
|
||||||
|
returns the cached data from the given package
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class DiskCacheLoaderFactory(ABC):
|
class DiskCacheLoaderFactory(ABC):
|
||||||
|
|
||||||
|
|||||||
@@ -3714,6 +3714,17 @@ class ArchManager(SoftwareManager, SettingsController):
|
|||||||
if suggestions:
|
if suggestions:
|
||||||
output.update(suggestions)
|
output.update(suggestions)
|
||||||
|
|
||||||
|
def _fill_cached_if_unset(self, pkg: ArchPackage, loader: DiskCacheLoader):
|
||||||
|
data = loader.read(pkg)
|
||||||
|
|
||||||
|
if data:
|
||||||
|
for attr, cached_val in data.items():
|
||||||
|
if cached_val:
|
||||||
|
current_val = getattr(pkg, attr)
|
||||||
|
|
||||||
|
if current_val is None:
|
||||||
|
setattr(pkg, attr, cached_val)
|
||||||
|
|
||||||
def list_suggestions(self, limit: int, filter_installed: bool) -> Optional[List[PackageSuggestion]]:
|
def list_suggestions(self, limit: int, filter_installed: bool) -> Optional[List[PackageSuggestion]]:
|
||||||
if limit == 0:
|
if limit == 0:
|
||||||
return []
|
return []
|
||||||
@@ -3791,6 +3802,11 @@ class ArchManager(SoftwareManager, SettingsController):
|
|||||||
if full_data and full_data.get('signed'):
|
if full_data and full_data.get('signed'):
|
||||||
full_data = full_data['signed']
|
full_data = full_data['signed']
|
||||||
|
|
||||||
|
disk_loader, caching_threads = None, None
|
||||||
|
if not filter_installed:
|
||||||
|
disk_loader = self.context.disk_loader_factory.new()
|
||||||
|
caching_threads = list()
|
||||||
|
|
||||||
suggestions = []
|
suggestions = []
|
||||||
for name in suggestion_by_priority:
|
for name in suggestion_by_priority:
|
||||||
pkg_data = available_suggestions[name]
|
pkg_data = available_suggestions[name]
|
||||||
@@ -3801,18 +3817,27 @@ class ArchManager(SoftwareManager, SettingsController):
|
|||||||
description = pkg_full_data.get('description')
|
description = pkg_full_data.get('description')
|
||||||
|
|
||||||
pkg_updates_ignored = pkg_data['i'] and ignored_updates and name in ignored_updates
|
pkg_updates_ignored = pkg_data['i'] and ignored_updates and name in ignored_updates
|
||||||
|
pkg = ArchPackage(name=name,
|
||||||
|
version=pkg_data['v'],
|
||||||
|
latest_version=pkg_data['v'],
|
||||||
|
repository=pkg_data['r'],
|
||||||
|
installed=pkg_data['i'],
|
||||||
|
description=description,
|
||||||
|
categories=self.categories.get(name),
|
||||||
|
i18n=self.i18n,
|
||||||
|
maintainer=pkg_data['r'],
|
||||||
|
update_ignored=pkg_updates_ignored)
|
||||||
|
|
||||||
suggestions.append(PackageSuggestion(package=ArchPackage(name=name,
|
if disk_loader:
|
||||||
version=pkg_data['v'],
|
t = Thread(target=self._fill_cached_if_unset, args=(pkg, disk_loader))
|
||||||
latest_version=pkg_data['v'],
|
t.start()
|
||||||
repository=pkg_data['r'],
|
caching_threads.append(t)
|
||||||
installed=pkg_data['i'],
|
|
||||||
description=description,
|
suggestions.append(PackageSuggestion(package=pkg, priority=name_priority[name]))
|
||||||
categories=self.categories.get(name),
|
|
||||||
i18n=self.i18n,
|
if caching_threads:
|
||||||
maintainer=pkg_data['r'],
|
for t in caching_threads:
|
||||||
update_ignored=pkg_updates_ignored),
|
t.join()
|
||||||
priority=name_priority[name]))
|
|
||||||
|
|
||||||
return suggestions
|
return suggestions
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import logging
|
|||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
from threading import Thread, Lock
|
from threading import Thread, Lock
|
||||||
from typing import Type, Dict
|
from typing import Type, Dict, Any, Optional
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
@@ -37,6 +37,22 @@ class AsyncDiskCacheLoader(Thread, DiskCacheLoader):
|
|||||||
else:
|
else:
|
||||||
self.pkgs.append(pkg)
|
self.pkgs.append(pkg)
|
||||||
|
|
||||||
|
def read(self, pkg: SoftwarePackage) -> Optional[Dict[str, Any]]:
|
||||||
|
if pkg and pkg.supports_disk_cache() and os.path.exists(pkg.get_disk_cache_path()):
|
||||||
|
disk_path = pkg.get_disk_data_path()
|
||||||
|
ext = disk_path.split('.')[-1]
|
||||||
|
|
||||||
|
with open(disk_path) as f:
|
||||||
|
if ext == 'json':
|
||||||
|
cached_data = json.loads(f.read())
|
||||||
|
elif ext in {'yml', 'yaml'}:
|
||||||
|
cached_data = yaml.load(f.read())
|
||||||
|
else:
|
||||||
|
raise Exception(f'The cached data file {disk_path} has an unsupported format')
|
||||||
|
|
||||||
|
if cached_data:
|
||||||
|
return cached_data
|
||||||
|
|
||||||
def stop_working(self):
|
def stop_working(self):
|
||||||
self._work = False
|
self._work = False
|
||||||
|
|
||||||
@@ -58,26 +74,16 @@ class AsyncDiskCacheLoader(Thread, DiskCacheLoader):
|
|||||||
self._working = False
|
self._working = False
|
||||||
|
|
||||||
def _fill_cached_data(self, pkg: SoftwarePackage) -> bool:
|
def _fill_cached_data(self, pkg: SoftwarePackage) -> bool:
|
||||||
if os.path.exists(pkg.get_disk_data_path()):
|
cached_data = self.read(pkg)
|
||||||
disk_path = pkg.get_disk_data_path()
|
|
||||||
ext = disk_path.split('.')[-1]
|
|
||||||
|
|
||||||
with open(disk_path) as f:
|
if cached_data:
|
||||||
if ext == 'json':
|
pkg.fill_cached_data(cached_data)
|
||||||
cached_data = json.loads(f.read())
|
cache = self.cache_map.get(pkg.__class__)
|
||||||
elif ext in {'yml', 'yaml'}:
|
|
||||||
cached_data = yaml.load(f.read())
|
|
||||||
else:
|
|
||||||
raise Exception('The cached data file {} has an unsupported format'.format(disk_path))
|
|
||||||
|
|
||||||
if cached_data:
|
if cache:
|
||||||
pkg.fill_cached_data(cached_data)
|
cache.add_non_existing(str(pkg.id), cached_data)
|
||||||
cache = self.cache_map.get(pkg.__class__)
|
|
||||||
|
|
||||||
if cache:
|
return True
|
||||||
cache.add_non_existing(str(pkg.id), cached_data)
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user