1 | #!/usr/bin/python3 |
---|
2 | import sys |
---|
3 | import os |
---|
4 | import threading |
---|
5 | import syslog |
---|
6 | import pkgutil |
---|
7 | import lliurexstore.plugins as plugins |
---|
8 | import json |
---|
9 | ###### |
---|
10 | #Ver. 1.0 of storeManager.py |
---|
11 | # This class manages the store and the related plugins |
---|
12 | # It's implemented as an action-drived class. |
---|
13 | # There're four(five) main actions and each of them could execute and undeterminated number of subprocess in their respective thread |
---|
14 | # Each of these actions returns EVER a list of dictionaries. |
---|
15 | ##### |
---|
16 | |
---|
17 | class StoreManager(): |
---|
18 | def __init__(self,*args,**kwargs): |
---|
19 | if 'dbg' in kwargs.keys(): |
---|
20 | self.dbg=kwargs['dbg'] |
---|
21 | self._propagate_dbg=False |
---|
22 | self.store=None |
---|
23 | self.stores={} |
---|
24 | self.related_actions={ |
---|
25 | 'load':['load'], |
---|
26 | 'search':['search','get_info','pkginfo'], |
---|
27 | 'list':['list','get_info','pkginfo'], |
---|
28 | 'info':['list','get_info','pkginfo'], |
---|
29 | 'list_sections':['list_sections'], |
---|
30 | 'install':['search','get_info','pkginfo','install'], |
---|
31 | 'remove':['search','get_info','pkginfo','remove'] |
---|
32 | } |
---|
33 | self.cli_mode=[] #List that controls cli_mode for plugins |
---|
34 | self.threads={} #Dict with the functions that must execute each action |
---|
35 | self.threads_progress={} #"" "" "" the progress for each launched thread |
---|
36 | self.running_threads={} #"" "" "" the running threads |
---|
37 | self.plugins_registered={} #Dict with the relation between plugins and actions |
---|
38 | self.register_action_progress={} #Dict with the progress for each function/parent_action pair |
---|
39 | self.action_progress={} #Progress of global actions based on average progress of individual processes |
---|
40 | self.extra_actions={} #Dict with the actions managed by plugins and no defined on the main class as related_actions |
---|
41 | self.result={} #Result of the actions |
---|
42 | self.lock=threading.Lock() #locker for functions related to threads (get_progress, is_action_running...) |
---|
43 | self.main(**kwargs) |
---|
44 | #def __init__ |
---|
45 | |
---|
46 | def main(self,**kwargs): |
---|
47 | self._define_functions_for_threads() #Function that loads the dictionary self.threads |
---|
48 | self.__init_plugins__(**kwargs) #Function that loads the plugins |
---|
49 | self.execute_action('load') #Initial load of the store |
---|
50 | #def main |
---|
51 | |
---|
52 | #### |
---|
53 | #Load and register the plugins from plugin dir |
---|
54 | #### |
---|
55 | def __init_plugins__(self,**kwargs): |
---|
56 | package=plugins |
---|
57 | for importer, mod, ispkg in pkgutil.walk_packages(path=package.__path__, prefix=package.__name__+'.',onerror=lambda x: None): |
---|
58 | import_mod='from %s import *'%mod |
---|
59 | try: |
---|
60 | self._debug("Importing %s"%mod) |
---|
61 | exec (import_mod) |
---|
62 | except Exception as e: |
---|
63 | print("Import failed for %s"%mod) |
---|
64 | print("Reason; %s"%e) |
---|
65 | |
---|
66 | for mod in (sys.modules.keys()): |
---|
67 | if 'plugins.' in mod: |
---|
68 | class_actions={} |
---|
69 | plugin_name_up=mod.split('.')[-1] |
---|
70 | plugin_name=plugin_name_up.lower() |
---|
71 | self._debug("Initializing %s"%plugin_name) |
---|
72 | sw_cli_mode=False |
---|
73 | try: |
---|
74 | target_class=eval(plugin_name)() |
---|
75 | class_actions=target_class.register() |
---|
76 | if 'disabled' in target_class.__dict__.keys(): |
---|
77 | if target_class.disabled==True: |
---|
78 | self._debug("Disabling plugin %s"%plugin_name) |
---|
79 | continue |
---|
80 | if target_class.disabled==None: |
---|
81 | self._debug("Plugin %s will set its status"%plugin_name) |
---|
82 | else: |
---|
83 | #Time to check if plugin is disabled or enabled by parm |
---|
84 | #Values for the plugins_registered dict must be the same as the parm name that enables the plugin |
---|
85 | for key,value in class_actions.items(): |
---|
86 | val=value |
---|
87 | break |
---|
88 | if val in kwargs.keys(): |
---|
89 | if kwargs[val]==True: |
---|
90 | if target_class.disabled: |
---|
91 | self._debug("Disabling plugin %s"%plugin_name) |
---|
92 | continue |
---|
93 | else: |
---|
94 | self._debug("Disabling plugin %s"%plugin_name) |
---|
95 | continue |
---|
96 | else: |
---|
97 | self._debug("Disabling plugin %s"%plugin_name) |
---|
98 | continue |
---|
99 | if 'cli_mode' in target_class.__dict__.keys(): |
---|
100 | if 'cli' in kwargs.keys(): |
---|
101 | sw_cli_mode=True |
---|
102 | self._debug("Enabling cli mode for %s"%plugin_name) |
---|
103 | except Exception as e: |
---|
104 | print ("Can't initialize %s %s"%(mod,target_class)) |
---|
105 | print ("Reason: %s"%e) |
---|
106 | pass |
---|
107 | |
---|
108 | for action in class_actions.keys(): |
---|
109 | if action not in self.plugins_registered: |
---|
110 | self.plugins_registered[action]={} |
---|
111 | full_module_name='plugins.'+plugin_name_up+'.'+plugin_name |
---|
112 | self.plugins_registered[action].update({class_actions[action]:full_module_name}) |
---|
113 | if sw_cli_mode: |
---|
114 | self.cli_mode.append(full_module_name) |
---|
115 | |
---|
116 | self._debug(str(self.plugins_registered)) |
---|
117 | #def __init_plugins__ |
---|
118 | |
---|
119 | def set_debug(self,dbg=True): |
---|
120 | self.dbg=dbg |
---|
121 | self._debug ("Debug enabled") |
---|
122 | #def set_debug |
---|
123 | |
---|
124 | def _debug(self,msg=''): |
---|
125 | if self.dbg==1: |
---|
126 | print ('DEBUG Store: %s'%msg) |
---|
127 | #def _debug |
---|
128 | |
---|
129 | def _log(self,msg=None): |
---|
130 | if msg: |
---|
131 | syslog.openlog('lliurex-store') |
---|
132 | syslog.syslog(msg) |
---|
133 | self._debug(msg) |
---|
134 | #### |
---|
135 | #dict of actions/related functions for threading |
---|
136 | #### |
---|
137 | def _define_functions_for_threads(self): |
---|
138 | self.threads['load']="threading.Thread(target=self._load_Store)" |
---|
139 | self.threads['get_info']="threading.Thread(target=self._get_App_Info,args=args,kwargs=kwargs)" |
---|
140 | self.threads['pkginfo']="threading.Thread(target=self._get_Extended_App_Info,args=args,kwargs=kwargs)" |
---|
141 | self.threads['search']='threading.Thread(target=self._search_Store,args=args,kwargs=kwargs)' |
---|
142 | self.threads['list']='threading.Thread(target=self._search_Store,args=args,kwargs=kwargs)' |
---|
143 | self.threads['info']='threading.Thread(target=self._search_Store,args=args,kwargs=kwargs)' |
---|
144 | self.threads['install']='threading.Thread(target=self._install_remove_App,args=args,kwargs=kwargs)' |
---|
145 | self.threads['remove']='threading.Thread(target=self._install_remove_App,args=args,kwargs=kwargs)' |
---|
146 | self.threads['list_sections']='threading.Thread(target=self._list_sections,args=args,kwargs=kwargs)' |
---|
147 | #def _define_functions_for_threads |
---|
148 | |
---|
149 | #### |
---|
150 | #Launch the appropiate threaded function for the desired action |
---|
151 | #Input: |
---|
152 | # - action to be executed |
---|
153 | # - parms for the action |
---|
154 | #### |
---|
155 | def execute_action(self,action,*args,**kwargs): |
---|
156 | #Action must be a kwarg but for retrocompatibility reasons we keep it as an arg |
---|
157 | kwargs.update({"action":action}) |
---|
158 | self._debug("Launching action: %s with args %s and kwargs %s"%(action,args,kwargs)) |
---|
159 | if self.is_action_running('load'): |
---|
160 | self._join_action('load') |
---|
161 | self._debug("Resumed action %s"%action) |
---|
162 | sw_track_status=False |
---|
163 | if action not in self.threads.keys(): |
---|
164 | #Attempt to add a new action managed by a plugin |
---|
165 | self._debug("Attempting to find a plugin for action %s"%action) |
---|
166 | if action in self.plugins_registered.keys(): |
---|
167 | for package_type,plugin in self.plugins_registered[action].items(): |
---|
168 | self.action_progress[action]=0 |
---|
169 | self.threads[action]='threading.Thread(target=self._execute_class_method(action,package_type,action).execute_action,args=[action],kwargs={kwargs})' |
---|
170 | break |
---|
171 | self._debug('Plugin for %s found: %s'%(action,self.plugins_registered[action])) |
---|
172 | self.related_actions.update({action:[action]}) |
---|
173 | sw_track_status=True |
---|
174 | if action in self.threads.keys(): |
---|
175 | if self.is_action_running(action): |
---|
176 | #join thread if we're performing the same action |
---|
177 | self._debug("Waiting for current action %s to end"%s) |
---|
178 | self.running_threads[action].join() |
---|
179 | try: |
---|
180 | if action in self.action_progress.keys(): |
---|
181 | self.action_progress[action]=0 |
---|
182 | self.action_progress[action]=0 |
---|
183 | self.result[action]={} |
---|
184 | self.running_threads[action]=eval(self.threads[action]) |
---|
185 | self.running_threads[action].start() |
---|
186 | if sw_track_status: |
---|
187 | self.result[action]['status']={'status':0,'msg':''} |
---|
188 | else: |
---|
189 | self.result[action]['status']={'status':-1,'msg':''} |
---|
190 | self._debug("Thread %s for action %s launched"%(self.running_threads[action],action)) |
---|
191 | |
---|
192 | except Exception as e: |
---|
193 | self._debug("Can't launch thread for action: %s"%action) |
---|
194 | self._debug("Reason: %s"%e) |
---|
195 | pass |
---|
196 | else: |
---|
197 | self._debug("No function associated with action %s"%action) |
---|
198 | #def execute_action |
---|
199 | |
---|
200 | #### |
---|
201 | #Launch the appropiate class function |
---|
202 | #Input: |
---|
203 | # - class action to be executed |
---|
204 | # - parms for the action |
---|
205 | # - parent action that demands the execution |
---|
206 | #Output |
---|
207 | # - The class method to execute |
---|
208 | #### |
---|
209 | def _execute_class_method(self,action,package_type,*args,launchedby=None,**kwargs): |
---|
210 | exe_function=None |
---|
211 | if not package_type: |
---|
212 | package_type="*" |
---|
213 | if action in self.plugins_registered: |
---|
214 | self._debug("Plugin for %s: %s"%(action,self.plugins_registered[action][package_type])) |
---|
215 | exe_function=eval(self.plugins_registered[action][package_type]+"("+','.join(args)+")") |
---|
216 | if self._propagate_dbg: |
---|
217 | exe_function.set_debug() |
---|
218 | if self.plugins_registered[action][package_type] in self.cli_mode: |
---|
219 | exe_function.cli_mode=True |
---|
220 | self._register_action_progress(action,exe_function,launchedby) |
---|
221 | else: |
---|
222 | self._debug("No plugin for action: %s"%action) |
---|
223 | if kwargs: |
---|
224 | self._debug("Parms: %s"%kwargs) |
---|
225 | return (exe_function) |
---|
226 | #def _execute_class_method |
---|
227 | |
---|
228 | ### |
---|
229 | #Tell if a a action is running |
---|
230 | #Input: |
---|
231 | # - action to monitorize |
---|
232 | #Output: |
---|
233 | # - status true/false |
---|
234 | ### |
---|
235 | def is_action_running(self,searched_action=None): |
---|
236 | status=False |
---|
237 | action_list=[] |
---|
238 | if searched_action: |
---|
239 | action_list.append(searched_action) |
---|
240 | else: |
---|
241 | action_list=self.related_actions.keys() |
---|
242 | |
---|
243 | for action in action_list: |
---|
244 | if action in self.running_threads.keys(): |
---|
245 | if self.running_threads[action].is_alive(): |
---|
246 | status=True |
---|
247 | break |
---|
248 | else: |
---|
249 | if action in self.related_actions.keys(): |
---|
250 | for related_action in self.related_actions[action]: |
---|
251 | if related_action in self.running_threads.keys(): |
---|
252 | if self.running_threads[related_action].is_alive(): |
---|
253 | status=True |
---|
254 | break |
---|
255 | return(status) |
---|
256 | #def is_action_running |
---|
257 | |
---|
258 | #### |
---|
259 | #Joins an action till finish |
---|
260 | #Input: |
---|
261 | # - action to join |
---|
262 | #### |
---|
263 | def _join_action(self,action): |
---|
264 | self._debug("Joining action: %s"%action) |
---|
265 | try: |
---|
266 | print("T: %s"%self.running_threads[action]) |
---|
267 | self.running_threads[action].join() |
---|
268 | except Exception as e: |
---|
269 | self._debug("Unable to join thread for: %s"%action) |
---|
270 | self._debug("Reason: %s"%e) |
---|
271 | finally: |
---|
272 | if action in self.running_threads.keys(): |
---|
273 | del(self.running_threads[action]) |
---|
274 | #def _join_action |
---|
275 | |
---|
276 | #### |
---|
277 | #Register the method and action/parent_action pair in the progress dict |
---|
278 | #Input: |
---|
279 | # - action launched |
---|
280 | # - function (a reference to the function) |
---|
281 | # - parent_action that owns the action (if any) |
---|
282 | #### |
---|
283 | def _register_action_progress(self,action,function,parent_action=None): |
---|
284 | if action in self.register_action_progress.keys(): |
---|
285 | self._debug("Appended process for action: %s and function: %s"%(action,function)) |
---|
286 | self.register_action_progress[action].append(function) |
---|
287 | else: |
---|
288 | self._debug("Registered process for action: %s and function %s"%(action,function)) |
---|
289 | self.register_action_progress[action]=[function] |
---|
290 | if parent_action: |
---|
291 | self._debug("Registered process for Parent Action: %s-%s and function: %s"%(action,parent_action,function)) |
---|
292 | if parent_action in self.threads_progress.keys(): |
---|
293 | self.threads_progress[parent_action].update({action:function}) |
---|
294 | else: |
---|
295 | self.threads_progress[parent_action]={action:function} |
---|
296 | #def _register_action_progress |
---|
297 | |
---|
298 | #### |
---|
299 | #Get the progress of the executed actions |
---|
300 | #Input |
---|
301 | # - action or none if we want all of the progress |
---|
302 | #Output: |
---|
303 | # - Dict of results indexed by actions |
---|
304 | #### |
---|
305 | def get_progress(self,action=None): |
---|
306 | progress={'search':0,'list':0,'install':0,'remove':0,'load':0,'list_sections':0} |
---|
307 | action_list=[] |
---|
308 | if action in self.register_action_progress.keys(): |
---|
309 | action_list=[action] |
---|
310 | else: |
---|
311 | action_list=self.register_action_progress.keys() |
---|
312 | self.lock.acquire() #prevent that any thread attempts to change the iterator |
---|
313 | for parent_action in self.related_actions.keys(): |
---|
314 | if self.is_action_running(parent_action): |
---|
315 | for action in action_list: |
---|
316 | if parent_action in self.threads_progress.keys(): |
---|
317 | acum_progress=0 |
---|
318 | for threadfunction,function in self.threads_progress[parent_action].items(): |
---|
319 | acum_progress=acum_progress+function.progress |
---|
320 | |
---|
321 | count=len(self.related_actions[parent_action]) |
---|
322 | self.action_progress[parent_action]=round(acum_progress/count,0) |
---|
323 | progress[parent_action]=self.action_progress[parent_action] |
---|
324 | else: |
---|
325 | #put a 100% just in case |
---|
326 | if parent_action in self.action_progress.keys(): |
---|
327 | self.action_progress[parent_action]=100 |
---|
328 | self.lock.release() |
---|
329 | return(self.action_progress) |
---|
330 | #def get_progress |
---|
331 | |
---|
332 | #### |
---|
333 | #Gets the result of an action |
---|
334 | #Input: |
---|
335 | # - action |
---|
336 | #Output: |
---|
337 | # - Dict of results indexed by actions |
---|
338 | #### |
---|
339 | def get_result(self,action=None): |
---|
340 | self.lock.acquire() #Prevent changes on results from threads |
---|
341 | result={} |
---|
342 | if action==None: |
---|
343 | for res in self.result.keys(): |
---|
344 | if res!='load': |
---|
345 | if 'data' in self.result[res]: |
---|
346 | result[res]=self.result[res]['data'] |
---|
347 | else: |
---|
348 | result[res]=[] |
---|
349 | else: |
---|
350 | self._debug("Checking result for action %s"%action) |
---|
351 | if self.is_action_running(action): |
---|
352 | self._join_action(action) |
---|
353 | result[action]=None |
---|
354 | if action in self.result: |
---|
355 | if 'data' in self.result[action]: |
---|
356 | result[action]=self.result[action]['data'] |
---|
357 | else: |
---|
358 | result[action]=[] |
---|
359 | self.lock.release() |
---|
360 | if action in self.extra_actions.keys(): |
---|
361 | self._load_Store() |
---|
362 | return(result) |
---|
363 | #def get_result |
---|
364 | |
---|
365 | #### |
---|
366 | #Gets the status of an action |
---|
367 | #Input. |
---|
368 | # - action |
---|
369 | #Output: |
---|
370 | # - Status dict of the action |
---|
371 | #### |
---|
372 | def get_status(self,action=None): |
---|
373 | self.lock.acquire() |
---|
374 | self._debug("Checking status for action %s"%action) |
---|
375 | result={} |
---|
376 | if action in self.result: |
---|
377 | result=self.result[action]['status'] |
---|
378 | try: |
---|
379 | err_file=open('/usr/share/lliurex-store/files/error.json').read() |
---|
380 | err_codes=json.loads(err_file) |
---|
381 | err_code=str(result['status']) |
---|
382 | if err_code in err_codes: |
---|
383 | result['msg']=err_codes[err_code] |
---|
384 | else: |
---|
385 | result['msg']=u"Unknown error" |
---|
386 | except: |
---|
387 | result['msg']=u"Unknown error" |
---|
388 | self.lock.release() |
---|
389 | return(result) |
---|
390 | #def get_status |
---|
391 | |
---|
392 | #### |
---|
393 | #Loads the store |
---|
394 | #### |
---|
395 | def _load_Store(self): |
---|
396 | action='load' |
---|
397 | #Load appstream metada first |
---|
398 | package_type='*' |
---|
399 | load_function=self._execute_class_method(action,package_type,launchedby=None) |
---|
400 | self.store=load_function.execute_action(action=action,store=self.store)['data'] |
---|
401 | #Once appstream is loaded load the appstream plugins for other package types (snap, appimage...) |
---|
402 | for package_type in self.plugins_registered[action]: |
---|
403 | if package_type!='*': |
---|
404 | load_function=self._execute_class_method(action,package_type,launchedby=None) |
---|
405 | self.store=load_function.execute_action(action=action,store=self.store)['data'] |
---|
406 | #def _load_Store |
---|
407 | |
---|
408 | #### |
---|
409 | #Loads the info related to one app |
---|
410 | #Input: |
---|
411 | # - List of App objects |
---|
412 | #Output: |
---|
413 | # - Dict with the related info |
---|
414 | #### |
---|
415 | def _get_App_Info(self,applist,launchedby=None): |
---|
416 | action='get_info' |
---|
417 | info_function=self._execute_class_method(action,None,launchedby=launchedby) |
---|
418 | info_applist=info_function.execute_action(self.store,action,applist) |
---|
419 | return(info_applist) |
---|
420 | #def _get_App_Info |
---|
421 | |
---|
422 | #### |
---|
423 | #Loads the extended info related to one app (slower) |
---|
424 | #Input: |
---|
425 | # - Dict off Apps (as returned by _get_app_info) |
---|
426 | #Output: |
---|
427 | # - Dict with the related info |
---|
428 | #### |
---|
429 | def _get_Extended_App_Info(self,info_applist,launchedby=None,fullsearch=True,channel=''): |
---|
430 | #Check if there's any plugin for the distinct type of packages |
---|
431 | action='pkginfo' |
---|
432 | types_dict={} |
---|
433 | result={} |
---|
434 | result['data']=[] |
---|
435 | result['status']={'status':0,'msg':''} |
---|
436 | processed=[] |
---|
437 | for app_info in info_applist: |
---|
438 | if channel: |
---|
439 | types_dict[channel]=[app_info] |
---|
440 | else: |
---|
441 | available_channels=self._check_package_type(app_info) |
---|
442 | for package_type in available_channels: |
---|
443 | if app_info['component']!='': |
---|
444 | if app_info['id'] in processed: |
---|
445 | self._debug("App %s processed"%app_info['id']) |
---|
446 | continue |
---|
447 | |
---|
448 | if package_type in types_dict: |
---|
449 | types_dict[package_type].append(app_info) |
---|
450 | else: |
---|
451 | types_dict[package_type]=[app_info] |
---|
452 | processed.append(app_info['id']) |
---|
453 | for package_type in types_dict: |
---|
454 | self._debug("Checking plugin for %s %s"%(action,package_type)) |
---|
455 | if package_type in self.plugins_registered[action]: |
---|
456 | #Only seach full info if it's required |
---|
457 | if (fullsearch==False and package_type=='deb'): |
---|
458 | result['data'].extend(types_dict[package_type]) |
---|
459 | continue |
---|
460 | self._debug("Retrieving info for %s"%types_dict[package_type]) |
---|
461 | info_function=self._execute_class_method(action,package_type,launchedby=launchedby) |
---|
462 | result['data'].extend(info_function.execute_action(action,types_dict[package_type])['data']) |
---|
463 | else: |
---|
464 | result['data'].append(app_info) |
---|
465 | return(result) |
---|
466 | #def _get_Extended_App_Info |
---|
467 | |
---|
468 | def _list_sections(self,searchItem='',action='list_sections',launchedby=None): |
---|
469 | result={} |
---|
470 | self._debug("Retrieving all sections") |
---|
471 | data={} |
---|
472 | status={} |
---|
473 | if action in self.plugins_registered.keys(): |
---|
474 | self._debug("Plugin for generic search: %s"%self.plugins_registered[action]['*']) |
---|
475 | finder=self.plugins_registered[action][('*')] |
---|
476 | search_function=eval(finder+"()") |
---|
477 | result=search_function.execute_action(self.store,action,searchItem) |
---|
478 | status=result['status'] |
---|
479 | data=result['data'] |
---|
480 | else: |
---|
481 | print("No plugin for action %s"%action) |
---|
482 | self.result[action]['data']=data |
---|
483 | self.result[action]['status']=status |
---|
484 | self._debug("Sections: %s"%self.result[action]['data']) |
---|
485 | self._debug("Status: %s"%self.result[action]['status']) |
---|
486 | |
---|
487 | #### |
---|
488 | #Search the store |
---|
489 | #Input: |
---|
490 | # - string search |
---|
491 | #Output: |
---|
492 | # - List of dicts with all the info |
---|
493 | #### |
---|
494 | def _search_Store(self,*args,**kwargs): |
---|
495 | search_item=args[0] |
---|
496 | return_msg=False |
---|
497 | action='search' |
---|
498 | if 'action' in kwargs.keys(): |
---|
499 | action=kwargs['action'] |
---|
500 | launchedby=None |
---|
501 | if 'launchedby' in kwargs.keys(): |
---|
502 | launchedby=kwargs['launchedby'] |
---|
503 | max_results=0 |
---|
504 | if 'max_results' in kwargs.keys(): |
---|
505 | max_results=kwargs['max_results'] |
---|
506 | fullsearch=False |
---|
507 | if 'fullsearch' in kwargs.keys(): |
---|
508 | fullsearch=kwargs['fullsearch'] |
---|
509 | result={} |
---|
510 | tmp_applist=[] |
---|
511 | if action=='list_sections': |
---|
512 | search_item='' |
---|
513 | elif action=='info': |
---|
514 | fullsearch=True |
---|
515 | if not launchedby: |
---|
516 | launchedby=action |
---|
517 | #Set the exact match to false for search method |
---|
518 | exact_match=True |
---|
519 | if (launchedby=='search'): |
---|
520 | exact_match=False |
---|
521 | target_channel='' |
---|
522 | if '=' in search_item: |
---|
523 | target_channel=search_item.split('=')[-1] |
---|
524 | search_item=search_item.split('=')[0] |
---|
525 | for package_type in self.plugins_registered[action]: |
---|
526 | self._debug("Searching package type %s"%package_type) |
---|
527 | search_function=self._execute_class_method(action,'*',launchedby=launchedby) |
---|
528 | result.update(search_function.execute_action(self.store,action,search_item,exact_match,max_results)) |
---|
529 | tmp_applist=result['data'] |
---|
530 | status=result['status'] |
---|
531 | realAction=action |
---|
532 | if status['status']==0: |
---|
533 | #1.- Get appstream metadata (faster) |
---|
534 | subordinate_action='get_info' |
---|
535 | self.result[subordinate_action]={} |
---|
536 | result=self._get_App_Info(tmp_applist,launchedby) |
---|
537 | self._debug("Add result for %s"%subordinate_action) |
---|
538 | self.result[subordinate_action]=result |
---|
539 | if fullsearch: |
---|
540 | #2.- Get rest of metadata (slower) |
---|
541 | subordinate_action='pkginfo' |
---|
542 | self._debug("Target channel: %s"%target_channel) |
---|
543 | result=self._get_Extended_App_Info(result['data'],launchedby,fullsearch,target_channel) |
---|
544 | if launchedby: |
---|
545 | realAction=launchedby |
---|
546 | self._debug("Assigned results of %s to %s"%(action,realAction)) |
---|
547 | if (result['status']['status']==0) or (result['status']['status']==9): |
---|
548 | return_msg=True |
---|
549 | if fullsearch: |
---|
550 | result['status']['status']=0 |
---|
551 | else: |
---|
552 | return_msg=False |
---|
553 | else: |
---|
554 | return_msg=False |
---|
555 | self.result[launchedby]['data']=result['data'] |
---|
556 | self.result[launchedby]['status']=result['status'] |
---|
557 | return(return_msg) |
---|
558 | #def _search_Store |
---|
559 | |
---|
560 | #### |
---|
561 | #Install or remove an app |
---|
562 | #Input: |
---|
563 | # - String with the app name |
---|
564 | #Output: |
---|
565 | # - Result of the operation |
---|
566 | #### |
---|
567 | def _install_remove_App(self,*args,**kwargs): |
---|
568 | appName=args[0] |
---|
569 | if 'action' in kwargs.keys(): |
---|
570 | action=kwargs['action'] |
---|
571 | self._log("Attempting to %s %s"%(action,appName)) |
---|
572 | result={} |
---|
573 | return_msg=False |
---|
574 | if (self._search_Store(appName,action='search',fullsearch=True,launchedby=action)): |
---|
575 | info_applist=self.result[action]['data'] |
---|
576 | types_dict={} |
---|
577 | #Check if package is installed if we want to remove it or vice versa |
---|
578 | for app_info in info_applist: |
---|
579 | #Appstream doesn't get the right status in all cases so we rely on the mechanisms given by the different plugins. |
---|
580 | if (action=='install' and app_info['state']=='installed') or (action=='remove' and app_info['state']=='available'): |
---|
581 | if (action=='remove' and app_info['state']=='available'): |
---|
582 | self.result[action]['status']={app_info['package']:3} |
---|
583 | self.result[action]['status']={'status':3} |
---|
584 | else: |
---|
585 | self.result[action]['status']={app_info['package']:4} |
---|
586 | self.result[action]['status']={'status':4} |
---|
587 | pass |
---|
588 | return_msg=False |
---|
589 | types_dict={} |
---|
590 | break |
---|
591 | processed=[] |
---|
592 | available_channels=self._check_package_type(app_info) |
---|
593 | for package_type in available_channels: |
---|
594 | if app_info['component']!='': |
---|
595 | if app_info['id'] in processed: |
---|
596 | self._debug("App %s processed"%app_info['id']) |
---|
597 | continue |
---|
598 | |
---|
599 | if package_type in types_dict: |
---|
600 | types_dict[package_type].append(app_info) |
---|
601 | else: |
---|
602 | types_dict[package_type]=[app_info] |
---|
603 | processed.append(app_info['id']) |
---|
604 | |
---|
605 | for package_type in types_dict: |
---|
606 | self._debug("Checking plugin for %s %s"%(action,package_type)) |
---|
607 | if package_type in self.plugins_registered[action]: |
---|
608 | install_function=self._execute_class_method(action,package_type,launchedby=action) |
---|
609 | if package_type=='zmd': |
---|
610 | #If it's a zmd the zomando must be present in the system |
---|
611 | zmd_info=[] |
---|
612 | for zmd_bundle in types_dict[package_type]: |
---|
613 | zmdInfo={} |
---|
614 | self._debug("Cheking presence of zmd %s"%zmd_bundle['package']) |
---|
615 | zmd='/usr/share/zero-center/zmds/'+app_info['package']+'.zmd' |
---|
616 | if not os.path.exists(zmd): |
---|
617 | zmdInfo['package']=zmd_bundle['package'] |
---|
618 | zmd_info.append(zmdInfo) |
---|
619 | if zmd_info: |
---|
620 | self._debug("Installing needed packages") |
---|
621 | install_depends_function=self._execute_class_method(action,"deb",launchedby=action) |
---|
622 | result=install_depends_function.execute_action(action,zmd_info) |
---|
623 | |
---|
624 | result=install_function.execute_action(action,types_dict[package_type]) |
---|
625 | self.result[action]=result |
---|
626 | #Deprecated. Earlier versions stored the "app" object so here the app could get marked as installed/removed without neeed of import or query anything |
---|
627 | #Python>=3.6 don't let us to store the app object in a queue (needed for the GUI) so this code becames deprecated. |
---|
628 | # if result['status']['status']==0: |
---|
629 | #Mark the apps as installed or available |
---|
630 | # for app in types_dict[package_type]: |
---|
631 | # if action=='install': |
---|
632 | # app['appstream_id'].set_state(1) |
---|
633 | # self._debug("App state changed to installed") |
---|
634 | # else: |
---|
635 | # app['appstream_id'].set_state(2) |
---|
636 | # self._debug("App state changed to available") |
---|
637 | return_msg=True |
---|
638 | self._log("Result %s: %s"%(action,self.result[action])) |
---|
639 | return(return_msg) |
---|
640 | #def install_App |
---|
641 | |
---|
642 | #### |
---|
643 | #Check the package type |
---|
644 | #Input: |
---|
645 | # - AppInfo dict (element of the list returned by _get_app_info) |
---|
646 | #Output: |
---|
647 | # - String with the type (deb, sh, zmd...) |
---|
648 | #### |
---|
649 | def _check_package_type(self,app_info): |
---|
650 | #Standalone installers must have the subcategory "installer" |
---|
651 | #Zomandos must have the subcategory "Zomando" |
---|
652 | self._debug("Checking package type for app "+app_info['name']) |
---|
653 | return_msg=[] |
---|
654 | if app_info['bundle']: |
---|
655 | return_msg.extend(app_info['bundle']) |
---|
656 | else: |
---|
657 | if "Zomando" in app_info['categories']: |
---|
658 | return_msg.append("zmd") |
---|
659 | if 'component' in app_info.keys(): |
---|
660 | if app_info['component']!='': |
---|
661 | return_msg.append('deb') |
---|
662 | #Standalone installers must have an installerUrl field loaded from a bundle type=script description |
---|
663 | if app_info['installerUrl']!='': |
---|
664 | return_msg.append("sh") |
---|
665 | return(return_msg) |
---|
666 | #def _check_package_type |
---|