From 33e1651238901dda38278b034d8ee49813580dc3 Mon Sep 17 00:00:00 2001 From: Vinicius Moreira Date: Thu, 10 Oct 2019 19:27:57 -0300 Subject: [PATCH] [appimage][feature] downloading db files from github --- CHANGELOG.md | 3 +- bauh/gems/appimage/controller.py | 5 +-- bauh/gems/appimage/worker.py | 52 ++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 bauh/gems/appimage/worker.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 9af53481..4d315f32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [0.7.0] ### Features - AppImage support ( see below ) -- "Screenshots" button +- **Screenshots** button +- **Categories** filter ### Improvements - Flatpak: diff --git a/bauh/gems/appimage/controller.py b/bauh/gems/appimage/controller.py index d066bf23..12ead4d9 100644 --- a/bauh/gems/appimage/controller.py +++ b/bauh/gems/appimage/controller.py @@ -19,6 +19,7 @@ from bauh.commons.html import bold from bauh.commons.system import SystemProcess, new_subprocess, ProcessHandler, run_cmd from bauh.gems.appimage import query, INSTALLATION_PATH from bauh.gems.appimage.model import AppImage +from bauh.gems.appimage.worker import DatabaseUpdater DB_APPS_PATH = '{}/{}'.format(HOME_PATH, '.local/share/bauh/appimage/apps.db') DB_RELEASES_PATH = '{}/{}'.format(HOME_PATH, '.local/share/bauh/appimage/releases.db') @@ -41,6 +42,7 @@ class AppImageManager(SoftwareManager): self.http_client = context.http_client self.logger = context.logger self.file_downloader = context.file_downloader + self.dbs_updater = DatabaseUpdater(http_client=context.http_client, logger=context.logger) def _get_db_connection(self, db_path: str) -> sqlite3.Connection: if os.path.exists(db_path): @@ -335,8 +337,7 @@ class AppImageManager(SoftwareManager): return False def prepare(self): - # TODO - pass + self.dbs_updater.start() def list_updates(self, internet_available: bool) -> List[PackageUpdate]: # TODO diff --git a/bauh/gems/appimage/worker.py b/bauh/gems/appimage/worker.py new file mode 100644 index 00000000..55c5d9e4 --- /dev/null +++ b/bauh/gems/appimage/worker.py @@ -0,0 +1,52 @@ +import logging +import os +import time +from multiprocessing import Process +from threading import Thread + +from bauh.api.http import HttpClient + + +class DatabaseUpdater(Thread if bool(int(os.getenv('BAUH_DEBUG', 0))) else Process): + + URL_APPS = 'https://github.com/vinifmor/bauh-files/blob/master/appimage/apps.db?raw=true' + URL_RELEASES = 'https://github.com/vinifmor/bauh-files/blob/master/appimage/releases.db?raw=true' + APPS_PATH = '{}/.local/share/bauh/appimage/apps.db' + RELEASES_PATH = '{}/.local/share/bauh/appimage/releases.db' + + def __init__(self, http_client: HttpClient, logger: logging.Logger): + super(DatabaseUpdater, self).__init__(daemon=True) + self.http_client = http_client + self.logger = logger + self.enabled = bool(int(os.getenv('BAUH_APPIMAGE_DB_UPDATER', 1))) + self.sleep = 60 * 20 + + def _download_file(self, url: str, output: str): + res = self.http_client.get(url, headers={'Authorization': 'token {}'.format(os.getenv('GITHUB_TOKEN'))}) + + if res: + with open(output, 'wb+') as f: + f.write(res.content) + + self.logger.info("Database file saved at {}".format(output)) + else: + self.logger.warning('Could not download a database file from {}'.format(url)) + + def run(self): + if self.enabled: + while True: + self.logger.info('Retrieving AppImage databases') + + threads = [Thread(target=self._download_file, args=(self.URL_APPS, self.APPS_PATH)), + Thread(target=self._download_file, args=(self.URL_RELEASES, self.RELEASES_PATH))] + + for t in threads: + t.start() + + for t in threads: + t.join() + + self.logger.info('Sleeping') + time.sleep(self.sleep) + else: + self.logger.warning('Disabled')