#The name of the main class must match the file name in lowercase #Init: Could accept parameters if we declare them in storeManager's threads dict import os #import lliurexstore.plugins.debManager import lliurexstore.plugins.appImageManager import lliurexstore.plugins.snapManager import lliurexstore.plugins.infoManager import lliurexstore.plugins.zmdManager import random import sqlite3 import threading import time import psutil from queue import Queue as pool class cachemanager: def __init__(self): self.dbg=False self.progress=0 self.plugin_actions={'cache':'*','pkginfo':'*'} self.cli_mode=False self.autostart_actions={'cache':'store=self.store'} #List with actions that storeManager must launch automatically. The parameter list refers to 'stringfieds' storeManager members !! # self.postaction_actions={'install':'app','remove':'app'} self.requires={'cache':'store=self.store'} self.cache_dir=os.getenv("HOME")+"/.cache/lliurex-store" self.cache_db=self.cache_dir+'/data/info.db' self.db_cursor='' self.db='' self.processed=0 self.total=0 self.result={} self.disabled=None self.infomanager=lliurexstore.plugins.infoManager.infomanager() self.debmanager=lliurexstore.plugins.debManager.debmanager() self.appimagemanager=lliurexstore.plugins.appImageManager.appimagemanager() self.snapmanager=lliurexstore.plugins.snapManager.snapmanager() self.zmdmanager=lliurexstore.plugins.zmdManager.zmdmanager() self.insert_count=0 self.data_pool=pool() self.apps_per_cycle=5 #Apps that will be processed per cycle self.cycles_for_commit=1 #Processed cycles needed for a commit self.sleep_between_cycles=10 #Time the cache plugin will sleep between a process cycle and the next self.sleep_between_apps=0.1 #Time the cache plugin will sleep between process one app and the next #def __init__ def set_debug(self,dbg=True): self.dbg=dbg #self._debug ("Debug enabled") #def set_debug def _debug(self,msg=''): if self.dbg: print ('DEBUG Cache: %s'%msg) #def debug def register(self): return(self.plugin_actions) #def register def execute_action(self,action,store=None,applist=None): self.progress=0 self.store=store self.result['status']={'status':-1,'msg':''} self.result['data']='' if self.disabled: self._set_status(9) else: self._set_db() if action=='cache': self._build_cache() if action=='install': self._update(applist,'installed') if action=='remove': self._update(applist,'available') if action=='pkginfo': dataList=[] for appinfo in applist: #self._debug("Looking for %s"%appinfo['package']) dataList.append(self._get_info(appinfo)) self.result['data']=list(dataList) self.progress=100 #When all actions are launched we must assure that progress=100. self.db.close() return(self.result) #def execute_action def set_sleep_between_apps(self,seconds): self.sleep_between_apps=seconds #def set_sleep_between_apps def set_sleep_between_cycles(self,seconds): self.sleep_between_cycles=seconds #def set_sleep_between_cycles def set_cycles_for_commit(self,count): self.cycles_for_commit=count #def set_cycles_for_commit def set_apps_per_cycle(self,count): self.apps_per_cycle=count #def set_apps_per_cycle def _callback(self): self.progress=self.progress+1 #def _callback def _set_status(self,status,msg=''): self.result['status']={'status':status,'msg':msg} #def _set_status def _set_db(self): sw_db_exists=False if os.path.isfile(self.cache_db): sw_db_exists=True else: try: os.makedirs(os.path.dirname(self.cache_db)) except Exception as e: #self._debug(e) pass try: self.db=sqlite3.connect(self.cache_db) except Exception as e: #self._debug(e) pass self.db_cursor=self.db.cursor() if sw_db_exists==False: #self._debug("Creating cache table") self.db_cursor.execute('''CREATE TABLE data(app TEXT PRIMARY KEY, size INTEGER, state TEXT, version TEXT)''') self.db_cursor.execute("SELECT count(*) FROM data") self.processed=self.db_cursor.fetchone() #self._debug("%s apps present"%self.processed) #def _set_db def _build_cache(self): threads=[] semaphore = threading.BoundedSemaphore(value=self.apps_per_cycle) storeapps=self.store.get_apps() processed=[] for item in range(len(storeapps)-1): cursor=random.randint(0,len(storeapps)-1) app=storeapps[cursor] pkgname=app.get_pkgname_default() while pkgname in processed and len(processed)