1 | import os |
---|
2 | import gi |
---|
3 | from gi.repository import Gio |
---|
4 | gi.require_version('AppStreamGlib', '1.0') |
---|
5 | from gi.repository import AppStreamGlib as appstream |
---|
6 | import subprocess |
---|
7 | import json |
---|
8 | import re |
---|
9 | |
---|
10 | class loadstore: |
---|
11 | def __init__(self): |
---|
12 | self.dbg=0 |
---|
13 | self.pluginInfo={'load':'*'} |
---|
14 | self.store='' |
---|
15 | self.progress=0 |
---|
16 | self.error=0 |
---|
17 | #def __init__ |
---|
18 | |
---|
19 | def set_debug(self,dbg='1'): |
---|
20 | self.dbg=int(dbg) |
---|
21 | self._debug ("Debug enabled") |
---|
22 | #def set_debug |
---|
23 | |
---|
24 | def _debug(self,msg=''): |
---|
25 | if self.dbg==1: |
---|
26 | print ('DEBUG Load: '+str(msg)) |
---|
27 | #def _debug |
---|
28 | |
---|
29 | def register(self): |
---|
30 | return(self.pluginInfo) |
---|
31 | #def register |
---|
32 | |
---|
33 | def execute_action(self,action,store=None,loadBundles=False): |
---|
34 | self.progress=0 |
---|
35 | if store: |
---|
36 | self.store=store |
---|
37 | else: |
---|
38 | self.store=appstream.Store() |
---|
39 | if action=='load': |
---|
40 | self._load_store(self.store,loadBundles) |
---|
41 | self.progress=100 |
---|
42 | return (self.store) |
---|
43 | #def execute_action |
---|
44 | |
---|
45 | def get_error(self): |
---|
46 | return (self.error) |
---|
47 | #def get_error |
---|
48 | |
---|
49 | def _load_store(self,store,loadBundles=False): |
---|
50 | iconPath='/usr/share/icons/hicolor/128x128' |
---|
51 | flags=[appstream.StoreLoadFlags.APP_INFO_SYSTEM,appstream.StoreLoadFlags.APP_INSTALL,appstream.StoreLoadFlags.APP_INFO_USER,appstream.StoreLoadFlags.DESKTOP,appstream.StoreLoadFlags.APPDATA,appstream.StoreLoadFlags.ALLOW_VETO] |
---|
52 | for flag in flags: |
---|
53 | try: |
---|
54 | self._debug("Loading "+str(flag)) |
---|
55 | store.load(flag) |
---|
56 | except: |
---|
57 | print ("Failed to load"+str(flag)) |
---|
58 | pass |
---|
59 | store=self._sanitize_store(store) |
---|
60 | if loadBundles: |
---|
61 | store=self.load_appImg_catalog(store) |
---|
62 | self.store=store |
---|
63 | return(store) |
---|
64 | #def load_store |
---|
65 | |
---|
66 | def load_appImg_catalog(self,store): |
---|
67 | iconPath='/usr/share/icons/hicolor/128x128' |
---|
68 | # lliurex_dir="/home/juanma/svn/xenial/devtools/appImgdep11/dep11" |
---|
69 | lliurex_dir="/tmp" |
---|
70 | if os.path.exists(lliurex_dir): |
---|
71 | for lliurex in os.listdir(lliurex_dir): |
---|
72 | if lliurex.endswith('appdata.xml'): |
---|
73 | storePath=Gio.File.new_for_path(lliurex_dir+'/'+lliurex) |
---|
74 | self._debug("Adding file "+lliurex_dir+'/'+lliurex) |
---|
75 | try: |
---|
76 | store.from_file(storePath,iconPath,None) |
---|
77 | except Exception as e: |
---|
78 | self._debug("Couldn't add file "+lliurex+" to store") |
---|
79 | self._debug("Reason: "+str(e)) |
---|
80 | return(store) |
---|
81 | |
---|
82 | #def load_appImg_catalog(self) |
---|
83 | |
---|
84 | def _parse_desktop(self,store): #DEPRECATED. Loads the apps from the available desktop files |
---|
85 | desktopDir='/usr/share/applications' |
---|
86 | applist=[] |
---|
87 | for desktopFile in os.listdir(desktopDir): |
---|
88 | if desktopFile.endswith('desktop'): |
---|
89 | a=appstream.App() |
---|
90 | try: |
---|
91 | a.parse_file(desktopDir+'/'+desktopFile,16) |
---|
92 | a.set_priority(0) |
---|
93 | for veto in a.get_vetos(): |
---|
94 | a.remove_veto(veto) |
---|
95 | store.add_app(a) |
---|
96 | self._debug("Adding app from desktop "+desktopFile) |
---|
97 | except: |
---|
98 | pass |
---|
99 | return(store) |
---|
100 | #def _parse_desktop |
---|
101 | |
---|
102 | def _sanitize_store(self,store): |
---|
103 | applist=store.get_apps() |
---|
104 | uniqDict={} |
---|
105 | lliurexApps={} |
---|
106 | zmdList=[] |
---|
107 | for app in applist: |
---|
108 | #Zomandos get max priority |
---|
109 | if app.has_category('Zomando'): |
---|
110 | self._debug("Prioritize zmd "+str(app.get_id())) |
---|
111 | app.set_priority(400) |
---|
112 | lliurexApps.update({app.get_id_filename():app}) |
---|
113 | id=str(app.get_id_filename()).replace('zero-lliurex-','') |
---|
114 | zmdList.append(id) |
---|
115 | #Prioritize Lliurex apps |
---|
116 | elif app.has_category('Lliurex'): |
---|
117 | self._debug("Prioritize app "+str(app.get_id())) |
---|
118 | app.set_priority(200) |
---|
119 | lliurexApps.update({app.get_id_filename():app}) |
---|
120 | elif str(app.get_origin()).find('lliurex')>=0: |
---|
121 | self._debug("Prioritize app "+app.get_id()) |
---|
122 | app.set_priority(100) |
---|
123 | lliurexApps.update({app.get_id_filename():app}) |
---|
124 | else: |
---|
125 | app.set_priority(0) |
---|
126 | if app.get_id_filename() in lliurexApps.keys(): |
---|
127 | # app.set_merge_kind(appstream.AppMergeKind.APPEND) |
---|
128 | self._debug("Mergin app "+str(app.get_id())+" as is in Lliurex") |
---|
129 | # app.set_id(lliurexApps[app.get_id_filename()]) |
---|
130 | lliurexApps[app.get_id_filename()].subsume_full(app,appstream.AppSubsumeFlags.BOTH_WAYS) |
---|
131 | store.remove_app(app) |
---|
132 | #Remove apps whitout pkgname |
---|
133 | if not app.get_pkgnames(): |
---|
134 | store.remove_app(app) |
---|
135 | #Remove add-on apps (as are included in the main packages) |
---|
136 | if app.get_kind()==appstream.AppKind.ADDON: |
---|
137 | self._debug("Removed addon "+str(app.get_pkgnames())) |
---|
138 | store.remove_app(app) |
---|
139 | #Remove duplicated apps |
---|
140 | #Unlike gnome-store we'll try to compare the info of the package in order of discard only the "part-of" packages |
---|
141 | pkg=app.get_pkgname_default() |
---|
142 | if pkg in uniqDict.keys(): |
---|
143 | fn=app.get_id_no_prefix() |
---|
144 | self._debug("Comparing "+fn+" with "+uniqDict[pkg]['fn']) |
---|
145 | if fn != uniqDict[pkg]['fn']: |
---|
146 | if fn != pkg: |
---|
147 | self._debug("Removed duplicated "+app.get_id()) |
---|
148 | store.remove_app(app) |
---|
149 | else: |
---|
150 | self._debug("Removed duplicated "+uniqDict[pkg]['app'].get_id()) |
---|
151 | uniqDict.update({pkg:{'fn':app.get_id_no_prefix(),'app':app}}) |
---|
152 | store.remove_app(uniqDict[pkg]['app']) |
---|
153 | elif pkg: |
---|
154 | # self._debug("Adding "+app.get_id_filename()+" to uniq dict") |
---|
155 | uniqDict.update({pkg:{'fn':app.get_id_filename(),'app':app}}) |
---|
156 | #Delete zomando-related debs |
---|
157 | store=self._purge_zomandos(zmdList,store) |
---|
158 | #Check the blacklist |
---|
159 | store=self._apply_blacklist(store) |
---|
160 | return (store) |
---|
161 | #def _sanitize_store |
---|
162 | |
---|
163 | def _purge_zomandos(self,zmdList,store): |
---|
164 | for appId in zmdList: |
---|
165 | self._debug("Searching debs related to "+appId) |
---|
166 | purgeList=store.get_apps_by_id(appId) |
---|
167 | purgeList.extend(store.get_apps_by_id(appId+".desktop")) |
---|
168 | for purgeApp in purgeList: |
---|
169 | if purgeApp: |
---|
170 | if not purgeApp.has_category('Zomando'): |
---|
171 | self._debug("Removed related zomando app "+str(purgeApp.get_id())) |
---|
172 | store.remove_app(purgeApp) |
---|
173 | return(store) |
---|
174 | #def _purge_zomandos |
---|
175 | |
---|
176 | def _apply_blacklist(self,store): |
---|
177 | try: |
---|
178 | flavour=subprocess.check_output(["lliurex-version","-f"]).rstrip() |
---|
179 | flavour=flavour.decode("utf-8") |
---|
180 | if flavour=='None': |
---|
181 | self._debug("Unknown flavour. Switching to desktop") |
---|
182 | flavour='desktop' |
---|
183 | except (subprocess.CalledProcessError,FileNotFoundError) as e: |
---|
184 | self._debug("Running on a non Lliurex host") |
---|
185 | flavour='desktop' |
---|
186 | try: |
---|
187 | if os.path.isfile('/usr/share/lliurex-store/files/blacklist.json'): |
---|
188 | blFile=open('/usr/share/lliurex-store/files/blacklist.json').read() |
---|
189 | blacklist=json.loads(blFile) |
---|
190 | blApps=[] |
---|
191 | if flavour in blacklist: |
---|
192 | blApps=blacklist[flavour] |
---|
193 | if "all" in blacklist: |
---|
194 | blApps.extend(blacklist["all"]) |
---|
195 | blRegEx=[] |
---|
196 | for blApp in blApps: |
---|
197 | self._debug("Blacklisted app: "+blApp) |
---|
198 | resultRe=re.search('([^a-zA-Z0-9_-])',blApp) |
---|
199 | # if blApp[-1]!='*': |
---|
200 | if resultRe: |
---|
201 | if blApp[0]=='*': |
---|
202 | blApp='.'+blApp |
---|
203 | blRegEx.append("("+blApp+")") |
---|
204 | else: |
---|
205 | app=store.get_app_by_pkgname(blApp) |
---|
206 | if app: |
---|
207 | self._debug("Removed "+str(app)) |
---|
208 | store.remove_app(app) |
---|
209 | else: |
---|
210 | self._debug("App "+blApp+" from blacklist not found in store. Assigned to RE blacklist") |
---|
211 | blRegEx.append("("+blApp+")") |
---|
212 | if blRegEx: |
---|
213 | self._debug("Attempting to remove apps by RE match") |
---|
214 | for app in store.get_apps(): |
---|
215 | for blApp in blRegEx: |
---|
216 | resultRe=re.search(blApp,app.get_id()) |
---|
217 | if resultRe: |
---|
218 | # if blApp.lower() in app.get_id_filename().lower(): |
---|
219 | store.remove_app(app) |
---|
220 | self._debug("Removed "+str(app.get_id()) +" as matches with "+blApp) |
---|
221 | else: |
---|
222 | self._debug('No blacklist to check') |
---|
223 | except Exception as e: |
---|
224 | self._debug("Error processing blacklist: "+str(e)) |
---|
225 | finally: |
---|
226 | return(store) |
---|
227 | #def _apply_blacklist |
---|
228 | |
---|