refactoring: distro check for arch and snap gems

This commit is contained in:
Vinicius Moreira
2019-10-09 18:14:54 -03:00
parent dd56a515fb
commit 69dd9b9396
6 changed files with 22 additions and 7 deletions

View File

@@ -12,7 +12,7 @@ class ApplicationContext:
def __init__(self, disk_cache: bool, download_icons: bool, http_client: HttpClient, app_root_dir: str, i18n: dict,
cache_factory: MemoryCacheFactory, disk_loader_factory: DiskCacheLoaderFactory,
logger: logging.Logger, file_downloader: FileDownloader):
logger: logging.Logger, file_downloader: FileDownloader, distro: str):
"""
:param disk_cache: if package data should be cached to disk
:param download_icons: if packages icons should be downloaded
@@ -22,7 +22,8 @@ class ApplicationContext:
:param cache_factory:
:param disk_loader_factory:
:param logger: a logger instance
:param file_downloader:
:param file_downloader
:param distro
"""
self.disk_cache = disk_cache
self.download_icons = download_icons
@@ -34,6 +35,7 @@ class ApplicationContext:
self.logger = logger
self.file_downloader = file_downloader
self.arch_x86_64 = sys.maxsize > 2**32
self.distro = distro
def is_system_x86_64(self):
return self.arch_x86_64

View File

@@ -32,6 +32,7 @@ def main():
icon_cache = cache_factory.new(args.icon_exp)
http_client = HttpClient(logger)
context = ApplicationContext(i18n=i18n,
http_client=http_client,
disk_cache=args.disk_cache,
@@ -40,6 +41,7 @@ def main():
cache_factory=cache_factory,
disk_loader_factory=DefaultDiskCacheLoaderFactory(disk_cache_enabled=args.disk_cache, logger=logger),
logger=logger,
distro=util.get_distro(),
file_downloader=AdaptableFileDownloader(logger, bool(args.download_mthread),
i18n, http_client))
user_config = config.read()

View File

@@ -50,7 +50,7 @@ class ArchManager(SoftwareManager):
self.comp_optimizer = ArchCompilationOptimizer(context.logger)
self.logger = context.logger
self.enabled = True
self.arch_distro = os.path.exists('/etc/arch-release')
self.arch_distro = context.distro == 'arch'
def _upgrade_search_result(self, apidata: dict, installed_pkgs: dict, downgrade_enabled: bool, res: SearchResult, disk_loader: DiskCacheLoader):
app = self.mapper.map_api_data(apidata, installed_pkgs['not_signed'])

View File

@@ -26,6 +26,7 @@ class SnapManager(SoftwareManager):
self.enabled = True
self.http_client = context.http_client
self.logger = context.logger
self.ubuntu_distro = context.distro == 'ubuntu'
def map_json(self, app_json: dict, installed: bool, disk_loader: DiskCacheLoader, internet: bool = True) -> SnapApplication:
app = SnapApplication(publisher=app_json.get('publisher'),
@@ -83,7 +84,7 @@ class SnapManager(SoftwareManager):
def read_installed(self, disk_loader: DiskCacheLoader, limit: int = -1, only_apps: bool = False, pkg_types: Set[Type[SoftwarePackage]] = None, internet_available: bool = None) -> SearchResult:
if snap.is_snapd_running():
installed = [self.map_json(app_json, installed=True, disk_loader=disk_loader, internet=internet_available) for app_json in snap.read_installed()]
installed = [self.map_json(app_json, installed=True, disk_loader=disk_loader, internet=internet_available) for app_json in snap.read_installed(self.ubuntu_distro)]
return SearchResult(installed, None, len(installed))
else:
return SearchResult([], None, 0)

View File

@@ -9,7 +9,6 @@ from bauh.gems.snap.model import SnapApplication
BASE_CMD = 'snap'
RE_SNAPD_STATUS = re.compile('\s+')
SNAPD_RUNNING_STATUS = {'listening', 'running'}
UBUNTU_DISTRO = 'ubuntu' in run_cmd('cat /proc/version').lower()
def is_installed():
@@ -86,7 +85,7 @@ def get_info(app_name: str, attrs: tuple = None):
return data
def read_installed() -> List[dict]:
def read_installed(ubuntu_distro: bool) -> List[dict]:
res = run_cmd('{} list'.format(BASE_CMD), print_error=False)
apps = []
@@ -99,7 +98,7 @@ def read_installed() -> List[dict]:
if idx > 0 and app_str:
apps.append(app_str_to_json(app_str))
if UBUNTU_DISTRO:
if ubuntu_distro:
path = '/snap/{}/current/meta/snap.yaml'
else:
path = '/var/lib/snapd/snap/{}/current/meta/snap.yaml'

View File

@@ -8,6 +8,7 @@ from typing import Tuple
from PyQt5.QtCore import QCoreApplication
from bauh import __app_name__
from bauh.commons.system import run_cmd
from bauh.view.util import resource
@@ -62,3 +63,13 @@ def restart_app(show_panel: bool):
subprocess.Popen(restart_cmd)
QCoreApplication.exit()
def get_distro():
if os.path.exists('/etc/arch-release'):
return 'arch'
if os.path.exists('/proc/version'):
if 'ubuntu' in run_cmd('cat /proc/version').lower():
return 'ubuntu'
return 'unknown'