[appimage] new config file

This commit is contained in:
Vinícius Moreira
2019-12-19 15:10:27 -03:00
parent 894e2d9988
commit 86bb1b55d6
5 changed files with 62 additions and 32 deletions

View File

@@ -0,0 +1,12 @@
from bauh.api.constants import CONFIG_PATH
from bauh.commons.config import read_config as read
def read_config(update_file: bool = False) -> dict:
default = {
'db_updater': {
'interval': 60 * 20,
'enabled': True
}
}
return read('{}/appimage.yml'.format(CONFIG_PATH), default, update_file=update_file)

View File

@@ -8,7 +8,7 @@ import subprocess
import traceback
from datetime import datetime
from pathlib import Path
from threading import Lock
from threading import Lock, Thread
from typing import Set, Type, List
from colorama import Fore
@@ -23,6 +23,7 @@ from bauh.api.constants import HOME_PATH
from bauh.commons.html import bold
from bauh.commons.system import SystemProcess, new_subprocess, ProcessHandler, run_cmd, SimpleProcess
from bauh.gems.appimage import query, INSTALLATION_PATH, suggestions, LOCAL_PATH
from bauh.gems.appimage.config import read_config
from bauh.gems.appimage.model import AppImage
from bauh.gems.appimage.worker import DatabaseUpdater
@@ -48,7 +49,6 @@ class AppImageManager(SoftwareManager):
self.logger = context.logger
self.file_downloader = context.file_downloader
self.db_locks = {DB_APPS_PATH: Lock(), DB_RELEASES_PATH: Lock()}
self.dbs_updater = DatabaseUpdater(http_client=context.http_client, logger=context.logger, db_locks=self.db_locks)
def _get_db_connection(self, db_path: str) -> sqlite3.Connection:
if os.path.exists(db_path):
@@ -387,8 +387,19 @@ class AppImageManager(SoftwareManager):
def requires_root(self, action: str, pkg: AppImage):
return False
def _start_updater(self):
local_config = read_config(update_file=True)
interval = local_config['db_updater']['interval'] or 20 * 60
updater = DatabaseUpdater(http_client=self.context.http_client, logger=self.context.logger,
db_locks=self.db_locks, interval=interval)
if local_config['db_updater']['enabled']:
updater.start()
else:
updater.download_databases() # only once
def prepare(self):
self.dbs_updater.start()
Thread(target=self._start_updater()).start()
def list_updates(self, internet_available: bool) -> List[PackageUpdate]:
res = self.read_installed(disk_loader=None, internet_available=internet_available)

View File

@@ -15,19 +15,24 @@ from bauh.gems.appimage import LOCAL_PATH
class DatabaseUpdater(Thread):
URL_DB = 'https://raw.githubusercontent.com/vinifmor/bauh-files/master/appimage/dbs.tar.gz'
COMPRESS_FILE_PATH = LOCAL_PATH + '/db.tar.gz'
def __init__(self, http_client: HttpClient, logger: logging.Logger, db_locks: dict):
def __init__(self, http_client: HttpClient, logger: logging.Logger, db_locks: dict, interval: int):
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.db_locks = db_locks
self.sleep = int(os.getenv('BAUH_APPIMAGE_DB_UPDATER_TIME', 60 * 20))
self.sleep = interval
def download_databases(self):
try:
if not internet.is_available(self.http_client, self.logger):
return
except requests.exceptions.ConnectionError:
self.logger.warning('The internet connection seems to be off.')
return
def _download_databases(self):
self.logger.info('Retrieving AppImage databases')
res = self.http_client.get(self.URL_DB)
@@ -71,15 +76,7 @@ class DatabaseUpdater(Thread):
self.logger.warning('Could not download the database file {}'.format(self.URL_DB))
def run(self):
if self.enabled:
while True:
try:
if internet.is_available(self.http_client, self.logger):
self._download_databases()
except requests.exceptions.ConnectionError:
self.logger.warning('The internet connection seems to be off.')
self.logger.info('Sleeping')
time.sleep(self.sleep)
else:
self.logger.warning('AppImage database updater disabled')
while True:
self.download_databases()
self.logger.info('Sleeping')
time.sleep(self.sleep)