[appimage][feature] downloading db files from github

This commit is contained in:
Vinicius Moreira
2019-10-10 19:27:57 -03:00
parent cbc9080cb8
commit 33e1651238
3 changed files with 57 additions and 3 deletions

View File

@@ -7,7 +7,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## [0.7.0] ## [0.7.0]
### Features ### Features
- AppImage support ( see below ) - AppImage support ( see below )
- "Screenshots" button - **Screenshots** button
- **Categories** filter
### Improvements ### Improvements
- Flatpak: - Flatpak:

View File

@@ -19,6 +19,7 @@ from bauh.commons.html import bold
from bauh.commons.system import SystemProcess, new_subprocess, ProcessHandler, run_cmd from bauh.commons.system import SystemProcess, new_subprocess, ProcessHandler, run_cmd
from bauh.gems.appimage import query, INSTALLATION_PATH from bauh.gems.appimage import query, INSTALLATION_PATH
from bauh.gems.appimage.model import AppImage 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_APPS_PATH = '{}/{}'.format(HOME_PATH, '.local/share/bauh/appimage/apps.db')
DB_RELEASES_PATH = '{}/{}'.format(HOME_PATH, '.local/share/bauh/appimage/releases.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.http_client = context.http_client
self.logger = context.logger self.logger = context.logger
self.file_downloader = context.file_downloader 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: def _get_db_connection(self, db_path: str) -> sqlite3.Connection:
if os.path.exists(db_path): if os.path.exists(db_path):
@@ -335,8 +337,7 @@ class AppImageManager(SoftwareManager):
return False return False
def prepare(self): def prepare(self):
# TODO self.dbs_updater.start()
pass
def list_updates(self, internet_available: bool) -> List[PackageUpdate]: def list_updates(self, internet_available: bool) -> List[PackageUpdate]:
# TODO # TODO

View File

@@ -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')