From 4ff7d3d84a003d1bcd3caa45c2601c192e264648 Mon Sep 17 00:00:00 2001 From: Vinicius Moreira Date: Fri, 13 Sep 2019 19:02:39 -0300 Subject: [PATCH] Reading installed snaps now takes around 95% less time --- CHANGELOG.md | 1 + bauh/gems/snap/controller.py | 3 +-- bauh/gems/snap/model.py | 2 +- bauh/gems/snap/snap.py | 17 +++++++++++++++-- bauh/gems/snap/worker.py | 1 - 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 514294f1..537ec7cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [0.6.0] ### Improvements: +- Reading installed snaps now takes around 95% less time. ### Fixes: diff --git a/bauh/gems/snap/controller.py b/bauh/gems/snap/controller.py index 7fdd8c7c..a0049f0b 100644 --- a/bauh/gems/snap/controller.py +++ b/bauh/gems/snap/controller.py @@ -6,7 +6,6 @@ from bauh.api.abstract.disk import DiskCacheLoader from bauh.api.abstract.handler import ProcessWatcher from bauh.api.abstract.model import SoftwarePackage, PackageHistory, PackageUpdate, PackageSuggestion from bauh.commons.system import SystemProcess, ProcessHandler - from bauh.gems.snap import snap, suggestions from bauh.gems.snap.model import SnapApplication from bauh.gems.snap.worker import SnapAsyncDataLoader @@ -29,7 +28,7 @@ class SnapManager(SoftwareManager): name=app_json.get('name'), version=app_json.get('version'), latest_version=app_json.get('version'), - description=app_json.get('description')) + description=app_json.get('description', app_json.get('summary'))) if app.publisher: app.publisher = app.publisher.replace('*', '') diff --git a/bauh/gems/snap/model.py b/bauh/gems/snap/model.py index 4ca9bf76..730c6fb9 100644 --- a/bauh/gems/snap/model.py +++ b/bauh/gems/snap/model.py @@ -46,7 +46,7 @@ class SnapApplication(SoftwarePackage): return self.get_default_icon_path() def is_application(self): - return not self.type and (self.type not in ('core', 'base', 'snapd') and not self._name_starts_with(('gtk-', 'gnome-', 'kde-', 'gtk2-'))) + return not self.type and (self.name != 'snapd' and self.type not in ('core', 'base', 'os') and not self._name_starts_with(('gtk-', 'gnome-', 'kde-', 'gtk2-'))) def _name_starts_with(self, words: tuple): for word in words: diff --git a/bauh/gems/snap/snap.py b/bauh/gems/snap/snap.py index 36cab302..886723e8 100644 --- a/bauh/gems/snap/snap.py +++ b/bauh/gems/snap/snap.py @@ -1,8 +1,9 @@ import re import subprocess +import time from typing import List -from bauh.commons.system import new_root_subprocess, run_cmd +from bauh.commons.system import new_root_subprocess, run_cmd, new_subprocess BASE_CMD = 'snap' @@ -43,7 +44,6 @@ def app_str_to_json(app: str) -> dict: 'notes': app_data[5] if len(app_data) >= 6 else None } - app_json.update(get_info(app_json['name'], ('summary', 'type', 'description'))) return app_json @@ -83,6 +83,19 @@ def read_installed() -> List[dict]: if idx > 0 and app_str: apps.append(app_str_to_json(app_str)) + info_out = new_subprocess(['cat', *['/var/lib/snapd/snap/{}/current/meta/snap.yaml'.format(a['name']) for a in apps]]).stdout + + idx = -1 + for o in new_subprocess(['grep', '-E', '(summary|type)', '--colour=never'], stdin=info_out).stdout: + if o: + line = o.decode() + + if line.startswith('summary:'): + idx += 1 + apps[idx]['summary'] = line.split(':')[1].strip() + else: + apps[idx]['type'] = line.split(':')[1].strip() + return apps diff --git a/bauh/gems/snap/worker.py b/bauh/gems/snap/worker.py index fce9e339..72dd76c2 100644 --- a/bauh/gems/snap/worker.py +++ b/bauh/gems/snap/worker.py @@ -5,7 +5,6 @@ from bauh.api.abstract.cache import MemoryCache from bauh.api.abstract.context import ApplicationContext from bauh.api.abstract.controller import SoftwareManager from bauh.api.abstract.model import PackageStatus - from bauh.gems.snap import snap from bauh.gems.snap.constants import SNAP_API_URL from bauh.gems.snap.model import SnapApplication