From 8bacae7c2f534675c535c692cb5759d8ff4fcac6 Mon Sep 17 00:00:00 2001 From: Vinicius Moreira Date: Tue, 8 Oct 2019 19:23:49 -0300 Subject: [PATCH] appimage: update, update-check and search returning installed applications --- bauh/gems/appimage/controller.py | 66 ++++++++++++++++++++++++-- bauh/gems/appimage/model.py | 4 +- bauh/gems/appimage/query.py | 1 + bauh/gems/appimage/resources/locale/en | 2 +- 4 files changed, 67 insertions(+), 6 deletions(-) diff --git a/bauh/gems/appimage/controller.py b/bauh/gems/appimage/controller.py index b4cb2f71..e42ff7fa 100644 --- a/bauh/gems/appimage/controller.py +++ b/bauh/gems/appimage/controller.py @@ -45,6 +45,8 @@ class AppImageManager(SoftwareManager): def _get_db_connection(self, db_path: str) -> sqlite3.Connection: if os.path.exists(db_path): return sqlite3.connect(db_path) + else: + self.logger.warning("Could not get a database connection. File '{}' not found".format(db_path)) def search(self, words: str, disk_loader: DiskCacheLoader, limit: int = -1) -> SearchResult: res = SearchResult([], [], 0) @@ -55,9 +57,27 @@ class AppImageManager(SoftwareManager): cursor = connection.cursor() cursor.execute(query.SEARCH_APPS_BY_NAME_OR_DESCRIPTION.format(words, words)) + found_map = {} + idx = 0 for l in cursor.fetchall(): app = AppImage(*l) res.new.append(app) + found_map['{}_{}'.format(app.name.lower(), app.github.lower())] = {'app': app, 'idx': idx} + idx += 1 + + if res.new: + installed = self.read_installed(disk_loader, limit, only_apps=False, pkg_types=None, internet_available=True).installed + + if installed: + for iapp in installed: + key = '{}_{}'.format(iapp.name.lower(), iapp.github.lower()) + + new_found = found_map.get(key) + + if new_found: + del res.new[new_found['idx']] + res.installed.append(iapp) + finally: connection.close() else: @@ -73,6 +93,7 @@ class AppImageManager(SoftwareManager): installed = run_cmd('ls {}*/data.json'.format(INSTALLATION_PATH), print_error=False) if installed: + names, githubs = set(), set() for path in installed.split('\n'): if path: with open(path) as f: @@ -80,6 +101,29 @@ class AppImageManager(SoftwareManager): app.icon_url = app.icon_path res.installed.append(app) + names.add("'{}'".format(app.name.lower())) + githubs.add("'{}'".format(app.github.lower())) + + if res.installed: + con = self._get_db_connection(DB_APPS_PATH) + + if con: + try: + cursor = con.cursor() + cursor.execute(query.FIND_APPS_LATEST_VERSIONS.format(','.join(names), ','.join(githubs))) + + for tup in cursor.fetchall(): + for app in res.installed: + if app.name.lower() == tup[0].lower() and app.github.lower() == tup[1].lower(): + app.update = tup[2] > app.version + + if app.update: + app.latest_version = tup[2] + app.url_download_latest_version = tup[3] + + break + finally: + con.close() res.total = len(res.installed) return res @@ -116,13 +160,23 @@ class AppImageManager(SoftwareManager): type_=MessageType.ERROR) else: watcher.show_message(title=self.i18n['error'], - body=self.i18n['appimage.downgrade.uninstall_current_version'].format(bold(pkg.name)), + body=self.i18n['appimage.error.uninstall_current_version'].format(bold(pkg.name)), type_=MessageType.ERROR) return False - def update(self, pkg: AppImage, root_password: str, watcher: ProcessWatcher) -> SystemProcess: - pass + def update(self, pkg: AppImage, root_password: str, watcher: ProcessWatcher) -> bool: + if not self.uninstall(pkg, root_password, watcher): + watcher.show_message(title=self.i18n['error'], + body=self.i18n['appimage.error.uninstall_current_version'], + type_=MessageType.ERROR) + return False + + if self.install(pkg, root_password, watcher): + self.cache_to_disk(pkg, None, False) + return True + + return False def uninstall(self, pkg: AppImage, root_password: str, watcher: ProcessWatcher) -> bool: if os.path.exists(pkg.get_disk_cache_path()): @@ -201,7 +255,11 @@ class AppImageManager(SoftwareManager): out_dir = INSTALLATION_PATH + pkg.name.lower() Path(out_dir).mkdir(parents=True, exist_ok=True) - file_name = pkg.url_download.split('/')[-1] + appimage_url = pkg.url_download_latest_version if pkg.update else pkg.url_download + file_name = appimage_url.split('/')[-1] + pkg.version = pkg.latest_version + pkg.url_download = appimage_url + file_path = out_dir + '/' + file_name downloaded = self.file_downloader.download(file_url=pkg.url_download, watcher=watcher, output_path=file_path, cwd=HOME_PATH) diff --git a/bauh/gems/appimage/model.py b/bauh/gems/appimage/model.py index bcb097db..bbafbf07 100644 --- a/bauh/gems/appimage/model.py +++ b/bauh/gems/appimage/model.py @@ -11,7 +11,8 @@ class AppImage(SoftwarePackage): def __init__(self, name: str = None, description: str = None, github: str = None, source: str = None, version: str = None, url_download: str = None, url_icon: str = None, license: str = None, author: str = None, - pictures: List[str] = None, icon_path: str = None, installed: bool = False): + pictures: List[str] = None, icon_path: str = None, installed: bool = False, + url_download_latest_version: str = None): super(AppImage, self).__init__(id=name, name=name, version=version, latest_version=version, icon_url=url_icon, license=license, description=description, installed=installed) @@ -21,6 +22,7 @@ class AppImage(SoftwarePackage): self.url_download = url_download self.icon_path = icon_path self.author = author + self.url_download_latest_version = url_download_latest_version def __repr__(self): return "{} (name={}, github={})".format(self.__class__.__name__, self.name, self.github) diff --git a/bauh/gems/appimage/query.py b/bauh/gems/appimage/query.py index b4741694..abe720c5 100644 --- a/bauh/gems/appimage/query.py +++ b/bauh/gems/appimage/query.py @@ -4,4 +4,5 @@ RELEASE_ATTRS = ('version', 'url_download', 'published_at') SEARCH_APPS_BY_NAME_OR_DESCRIPTION = "SELECT {} FROM apps".format(','.join(APP_ATTRS)) + " WHERE lower(name) LIKE '%{}%' or lower(description) LIKE '%{}%'" FIND_APP_ID_BY_NAME_AND_GITHUB = "SELECT id FROM apps WHERE lower(name) = '{}' and lower(github) = '{}'" +FIND_APPS_LATEST_VERSIONS = "SELECT name, github, version, url_download FROM apps WHERE lower(name) IN ({}) AND lower(github) in ({})" FIND_RELEASES_BY_APP_ID = "SELECT {} FROM releases".format(','.join(RELEASE_ATTRS)) + " WHERE app_id = {} ORDER BY version desc" diff --git a/bauh/gems/appimage/resources/locale/en b/bauh/gems/appimage/resources/locale/en index 7a0a72e6..437b32e9 100644 --- a/bauh/gems/appimage/resources/locale/en +++ b/bauh/gems/appimage/resources/locale/en @@ -12,5 +12,5 @@ appimage.downgrade.impossible.title=Impossible to downgrade appimage.downgrade.impossible.body={} has only one published version. appimage.downgrade.unknown_version.body=It was not possible to identify {} current version in its versions history appimage.downgrade.first_version={} is in its first published version -appimage.downgrade.uninstall_current_version=It was not possible to uninstall the current version of {} +appimage.error.uninstall_current_version=It was not possible to uninstall the current version of {} appimage.downgrade.install_version=It was not possible to install the version {} ({}) \ No newline at end of file