1 | #The name of the main class must match the file name in lowercase |
---|
2 | import os |
---|
3 | import urllib |
---|
4 | import shutil |
---|
5 | import gi |
---|
6 | from gi.repository import Gio |
---|
7 | gi.require_version ('Snapd', '1') |
---|
8 | from gi.repository import Snapd |
---|
9 | gi.require_version('AppStreamGlib', '1.0') |
---|
10 | from gi.repository import AppStreamGlib as appstream |
---|
11 | import time |
---|
12 | #Needed for async find method, perhaps only on xenial |
---|
13 | wrap=Gio.SimpleAsyncResult() |
---|
14 | class snapmanager: |
---|
15 | |
---|
16 | def __init__(self): |
---|
17 | self.dbg=False |
---|
18 | self.progress=0 |
---|
19 | self.partial_progress=0 |
---|
20 | self.plugin_actions={'install':'snap','remove':'snap','pkginfo':'snap','load':'snap'} |
---|
21 | self.cache_dir="/var/lib/lliurexstore/bundles/cache" |
---|
22 | self.icons_folder=self.cache_dir+"/icons" |
---|
23 | self.result={} |
---|
24 | self.result['data']={} |
---|
25 | self.result['status']={} |
---|
26 | self.disabled=False |
---|
27 | self.icon_cache_enabled=True |
---|
28 | self.cli_mode=False |
---|
29 | if not os.path.isdir(self.icons_folder): |
---|
30 | try: |
---|
31 | os.makedirs(self.icons_folder) |
---|
32 | except: |
---|
33 | self.icon_cache_enabled=False |
---|
34 | self._debug("Icon cache disabled") |
---|
35 | #def __init__ |
---|
36 | |
---|
37 | def set_debug(self,dbg=True): |
---|
38 | self.dbg=dbg |
---|
39 | self._debug ("Debug enabled") |
---|
40 | #def set_debug |
---|
41 | |
---|
42 | def _debug(self,msg=''): |
---|
43 | if self.dbg: |
---|
44 | print ('DEBUG snap: '+msg) |
---|
45 | #def debug |
---|
46 | |
---|
47 | def register(self): |
---|
48 | return(self.plugin_actions) |
---|
49 | |
---|
50 | def enable(self,state=False): |
---|
51 | self.disabled=state |
---|
52 | |
---|
53 | def execute_action(self,action,applist=None,store=None,results=0): |
---|
54 | if store: |
---|
55 | self.store=store |
---|
56 | else: |
---|
57 | self.store=appstream.Store() |
---|
58 | self.progress=0 |
---|
59 | self.result['status']={'status':-1,'msg':''} |
---|
60 | self.result['data']='' |
---|
61 | |
---|
62 | |
---|
63 | if self.disabled==True: |
---|
64 | self._set_status(9) |
---|
65 | self.result['data']=self.store |
---|
66 | else: |
---|
67 | self.snap_client=Snapd.Client() |
---|
68 | self.snap_client.connect_sync(None) |
---|
69 | dataList=[] |
---|
70 | if action=='load': |
---|
71 | self.result['data']=self._load_snap_store(self.store) |
---|
72 | else: |
---|
73 | for app_info in applist: |
---|
74 | self.partial_progress=0 |
---|
75 | if action=='install': |
---|
76 | dataList.append(self._install_snap(app_info)) |
---|
77 | if action=='remove': |
---|
78 | dataList.append(self._remove_snap(app_info)) |
---|
79 | if action=='pkginfo': |
---|
80 | dataList.append(self._get_info(app_info)) |
---|
81 | self.progress+=round(self.partial_progress/len(applist),1) |
---|
82 | if self.progress>98: |
---|
83 | self.progress=98 |
---|
84 | self.result['data']=list(dataList) |
---|
85 | self.progress=100 |
---|
86 | return(self.result) |
---|
87 | |
---|
88 | def _set_status(self,status,msg=''): |
---|
89 | self.result['status']={'status':status,'msg':msg} |
---|
90 | #def _set_status |
---|
91 | |
---|
92 | def _callback(self,client,change, _,user_data): |
---|
93 | # Interate over tasks to determine the aggregate tasks for completion. |
---|
94 | total = 0 |
---|
95 | done = 0 |
---|
96 | for task in change.get_tasks(): |
---|
97 | total += task.get_progress_total() |
---|
98 | done += task.get_progress_done() |
---|
99 | self.progress = round((done/total)*100) |
---|
100 | #def _callback |
---|
101 | |
---|
102 | def _load_snap_store(self,store): |
---|
103 | pkgs=[] |
---|
104 | if self.cli_mode: |
---|
105 | pkgs=self._search_snap("*") |
---|
106 | else: |
---|
107 | pkgs=self._search_snap_async("*") |
---|
108 | self._set_status(1) |
---|
109 | for pkg in pkgs: |
---|
110 | store.add_app(self._generate_appstream_app(pkg)) |
---|
111 | return(store) |
---|
112 | |
---|
113 | def _generate_appstream_app(self,pkg): |
---|
114 | bundle=appstream.Bundle() |
---|
115 | app=appstream.App() |
---|
116 | #F*****g appstream have kinds undefined but kind_from_string works... wtf? |
---|
117 | # bundle.set_kind(appstream.BundleKind.SNAP) |
---|
118 | bundle.set_kind(bundle.kind_from_string('SNAP')) |
---|
119 | bundle.set_id(pkg.get_name()+'.snap') |
---|
120 | app.add_bundle(bundle) |
---|
121 | app.set_name("C",pkg.get_name()) |
---|
122 | app.add_pkgname(pkg.get_name()+'.snap') |
---|
123 | app.add_category("Snap") |
---|
124 | release=appstream.Release() |
---|
125 | release.set_version(pkg.get_version()) |
---|
126 | app.add_release(release) |
---|
127 | app.set_id("io.snapcraft.%s"%pkg.get_name()+'.snap') |
---|
128 | # app.set_id(pkg.get_name()+'.snap') |
---|
129 | app.set_id_kind=appstream.IdKind.DESKTOP |
---|
130 | description=("This is an Snap bundle of app %s. It hasn't been tested by our developers and comes from a 3rd party dev team. Please use it carefully."%pkg.get_name()) |
---|
131 | app.set_description("C",description+'<br>'+pkg.get_description()) |
---|
132 | app.set_comment("C",pkg.get_summary()) |
---|
133 | app.add_keyword("C",pkg.get_name()) |
---|
134 | for word in pkg.get_summary().split(' '): |
---|
135 | app.add_keyword("C",word) |
---|
136 | |
---|
137 | if pkg.get_icon(): |
---|
138 | app.set_icon_path(pkg.get_icon()) |
---|
139 | return(app) |
---|
140 | |
---|
141 | def _search_cb(self,obj,request,*args): |
---|
142 | global wrap |
---|
143 | wrap=request |
---|
144 | |
---|
145 | def _search_snap_async(self,tokens): |
---|
146 | self._debug("Async Searching %s"%tokens) |
---|
147 | pkgs=None |
---|
148 | global wrap |
---|
149 | self.snap_client.find_async(Snapd.FindFlags.MATCH_NAME, |
---|
150 | tokens, |
---|
151 | None, |
---|
152 | self._search_cb,(None,),None) |
---|
153 | while 'Snapd' not in str(type(wrap)): |
---|
154 | time.sleep(0.1) |
---|
155 | snaps=self.snap_client.find_finish(wrap) |
---|
156 | if type(snaps)!=type([]): |
---|
157 | pkgs=[snaps] |
---|
158 | else: |
---|
159 | pkgs=snaps |
---|
160 | stable_pkgs=[] |
---|
161 | for pkg in pkgs: |
---|
162 | if pkg.get_channel()=='stable': |
---|
163 | stable_pkgs.append(pkg) |
---|
164 | return(stable_pkgs) |
---|
165 | |
---|
166 | def _search_snap(self,tokens): |
---|
167 | self._debug("Searching %s"%tokens) |
---|
168 | pkg=None |
---|
169 | try: |
---|
170 | pkgs=self.snap_client.find_sync(Snapd.FindFlags.MATCH_NAME,tokens,None,None) |
---|
171 | except Exception as e: |
---|
172 | print(e) |
---|
173 | self._set_status(1) |
---|
174 | stable_pkgs=[] |
---|
175 | for pkg in pkgs: |
---|
176 | if pkg.get_channel()=='stable': |
---|
177 | stable_pkgs.append(pkg) |
---|
178 | self._debug("Done") |
---|
179 | return(stable_pkgs) |
---|
180 | #def _search_snap |
---|
181 | |
---|
182 | def _download_file(self,url,app_name): |
---|
183 | target_file=self.icons_folder+'/'+app_name+".png" |
---|
184 | if not os.path.isfile(target_file): |
---|
185 | self._debug("Downloading %s to %s"%(url,target_file)) |
---|
186 | try: |
---|
187 | with urllib.request.urlopen(url) as response, open(target_file, 'wb') as out_file: |
---|
188 | bf=16*1024 |
---|
189 | acumbf=0 |
---|
190 | file_size=int(response.info()['Content-Length']) |
---|
191 | while True: |
---|
192 | if acumbf>=file_size: |
---|
193 | break |
---|
194 | shutil.copyfileobj(response, out_file,bf) |
---|
195 | acumbf=acumbf+bf |
---|
196 | st = os.stat(target_file) |
---|
197 | os.chmod(target_file, st.st_mode | 0o111) |
---|
198 | except Exception as e: |
---|
199 | self._debug("Unable to download %s"%url) |
---|
200 | self._debug("Reason: %s"%e) |
---|
201 | return(target_file) |
---|
202 | #def _download_file |
---|
203 | |
---|
204 | |
---|
205 | def _get_info(self,app_info): |
---|
206 | #switch to launch async method when running under a gui |
---|
207 | #For an unknown reason request will block when sync mode under a gui and async blocks when on cli (really funny) |
---|
208 | if self.cli_mode: |
---|
209 | pkgs=self._search_snap(app_info['name']) |
---|
210 | else: |
---|
211 | pkgs=self._search_snap_async(app_info['name']) |
---|
212 | if type(pkgs)==type([]): |
---|
213 | for pkg in pkgs: |
---|
214 | if pkg.get_download_size(): |
---|
215 | app_info['size']=str(pkg.get_download_size()) |
---|
216 | else: |
---|
217 | app_info['size']=str(pkg.get_installed_size()) |
---|
218 | if pkg.get_icon(): |
---|
219 | if self.icon_cache_enabled: |
---|
220 | app_info['icon']=self._download_file(pkg.get_icon(),app_info['name']) |
---|
221 | else: |
---|
222 | app_info['icon']=pkg.get_icon() |
---|
223 | if pkg.get_status(): |
---|
224 | #Not working on xenial |
---|
225 | # if pkg.get_status()==Snapd.SnapStatus.INSTALLED or pkg.get_status()==3: |
---|
226 | # app_info['state']='installed' |
---|
227 | try: |
---|
228 | self.snap_client.list_one_sync(pkg.get_name()) |
---|
229 | app_info['state']='installed' |
---|
230 | except: |
---|
231 | app_info['state']='available' |
---|
232 | if pkg.get_screenshots(): |
---|
233 | screenshot_list=[] |
---|
234 | for screen in pkg.get_screenshots(): |
---|
235 | screenshot_list.append(screen.get_url()) |
---|
236 | app_info["screenshots"]=screenshot_list |
---|
237 | #Method not working in xenial, license default type assigned in infoManager |
---|
238 | # if pkg.get_license(): |
---|
239 | # app_info["license"]=pkg.get_license() |
---|
240 | break |
---|
241 | else: |
---|
242 | app_info['size']='0' |
---|
243 | self._debug("Info for %s"%app_info) |
---|
244 | self.partial_progress=100 |
---|
245 | return(app_info) |
---|
246 | #def _get_info |
---|
247 | |
---|
248 | def _install_snap(self,app_info): |
---|
249 | self._debug("Installing %s"%app_info['name']) |
---|
250 | if app_info['state']=='installed': |
---|
251 | self._set_status(4) |
---|
252 | else: |
---|
253 | try: |
---|
254 | self.snap_client.install_sync(app_info['name'], |
---|
255 | None, # channel |
---|
256 | self._callback, (None,), |
---|
257 | None) # cancellable |
---|
258 | app_info['state']='installed' |
---|
259 | self._set_status(0) |
---|
260 | except Exception as e: |
---|
261 | print("Install error %s"%e) |
---|
262 | self._set_status(5) |
---|
263 | |
---|
264 | self._debug("Installed %s"%app_info) |
---|
265 | return app_info |
---|
266 | #def _install_snap |
---|
267 | |
---|
268 | def _remove_snap(self,app_info): |
---|
269 | if app_info['state']=='available': |
---|
270 | self._set_status(3) |
---|
271 | else: |
---|
272 | try: |
---|
273 | self.snap_client.remove_sync(app_info['name'], |
---|
274 | self._callback, (None,), |
---|
275 | None) # cancellable |
---|
276 | app_info['state']='available' |
---|
277 | self._set_status(0) |
---|
278 | except Exception as e: |
---|
279 | print("Remove error %s"%e) |
---|
280 | self._set_status(6) |
---|
281 | return app_info |
---|
282 | #def _remove_snap |
---|