1 | import urllib.request as urllib2 |
---|
2 | import os |
---|
3 | import tempfile |
---|
4 | import datetime |
---|
5 | import time |
---|
6 | import json |
---|
7 | import re |
---|
8 | import gi |
---|
9 | gi.require_version('Gtk', '3.0') |
---|
10 | from gi.repository import Gtk |
---|
11 | |
---|
12 | HEADER = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11' } |
---|
13 | |
---|
14 | |
---|
15 | class ResourcesManager: |
---|
16 | |
---|
17 | def __init__(self): |
---|
18 | |
---|
19 | self.distro_name="lliurex" |
---|
20 | self.base_url="http://appstream.ubuntu.com/data/" |
---|
21 | self.icons_url_file="icons-64x64.tar.gz" |
---|
22 | self.icons_path="/var/lib/app-info/icons/" |
---|
23 | self.dists=["xenial","xenial-updates","xenial-security"] |
---|
24 | self.components=["main","restricted","universe","multiverse"] |
---|
25 | self.download_cache="/tmp/lliurex-store-cache/" |
---|
26 | self.icon_dates_file="downloaded_icons.dates" |
---|
27 | |
---|
28 | self.distro_name="ubuntu" |
---|
29 | |
---|
30 | self.icon_db=Gtk.IconTheme() |
---|
31 | self.icon_db.set_custom_theme("Vibrancy-Dark-Orange") |
---|
32 | self.package_icon=self.icon_db.lookup_icon("package",256,Gtk.IconLookupFlags.FORCE_SVG ).get_filename() |
---|
33 | |
---|
34 | #def init |
---|
35 | |
---|
36 | def get_icon(self,pkg_info): |
---|
37 | |
---|
38 | debian_name=pkg_info["package"] |
---|
39 | icon=pkg_info["icon"] |
---|
40 | if icon==None: |
---|
41 | icon="" |
---|
42 | |
---|
43 | if os.path.isfile(icon): |
---|
44 | return(icon) |
---|
45 | |
---|
46 | if icon.startswith("http"): |
---|
47 | cache_dir=os.getenv("HOME")+"/.cache/lliurex-store/icons" |
---|
48 | icon_name=icon.split("/")[-1] |
---|
49 | if not os.path.exists(cache_dir): |
---|
50 | os.makedirs(cache_dir) |
---|
51 | if os.path.isfile(cache_dir+"/"+icon_name): |
---|
52 | return(cache_dir+"/"+icon_name) |
---|
53 | icon_file=cache_dir+"/"+icon_name |
---|
54 | url=icon |
---|
55 | try: |
---|
56 | req=urllib2.Request(url,headers=HEADER) |
---|
57 | res=urllib2.urlopen(req) |
---|
58 | x=open(icon_file,"wb") |
---|
59 | x.write(res.read()) |
---|
60 | x.close() |
---|
61 | except Exception as e: |
---|
62 | icon_file='' |
---|
63 | finally: |
---|
64 | return(icon_file) |
---|
65 | |
---|
66 | |
---|
67 | component=pkg_info["component"] |
---|
68 | ret_icon=self.icon_db.lookup_icon(debian_name,256,Gtk.IconLookupFlags.FORCE_SVG) |
---|
69 | if ret_icon!=None: |
---|
70 | return ret_icon.get_filename() |
---|
71 | |
---|
72 | ret_icon=self.icon_db.lookup_icon(icon,256,Gtk.IconLookupFlags.FORCE_SVG) |
---|
73 | if ret_icon!=None: |
---|
74 | return ret_icon.get_filename() |
---|
75 | |
---|
76 | |
---|
77 | if len(icon)>0: |
---|
78 | |
---|
79 | for dist in ["xenial-updates","xenial-security","xenial"]: |
---|
80 | # "64x64/" is included in pkg_info["icon"] |
---|
81 | # if "64x64/" not in icon: |
---|
82 | if not re.match("[0-9]*x[0-9]*\/",icon): |
---|
83 | icon="64x64/" + icon |
---|
84 | if debian_name+"_"+debian_name not in icon: |
---|
85 | icon=icon.replace(debian_name,debian_name+"_"+debian_name) |
---|
86 | if not icon.endswith(".png"): |
---|
87 | icon=icon+'.png' |
---|
88 | if "pyromaths" in icon: |
---|
89 | icon="64x64/pyromaths_pyromaths.png" |
---|
90 | |
---|
91 | ret_icon=self.icons_path+"%s/%s"%(component,icon) |
---|
92 | if os.path.exists(ret_icon): |
---|
93 | return ret_icon |
---|
94 | else: |
---|
95 | ret_icon=ret_icon.replace("64x64","128x128") |
---|
96 | if os.path.exists(ret_icon): |
---|
97 | return ret_icon |
---|
98 | icon="64x64/" + pkg_info['icon'] |
---|
99 | ret_icon=self.icons_path+"%s/%s"%(component,icon) |
---|
100 | if os.path.exists(ret_icon): |
---|
101 | return ret_icon |
---|
102 | else: |
---|
103 | #Last attempt |
---|
104 | if len(pkg_info['id'].split('.'))>2: |
---|
105 | pkg_id=pkg_info['id'].split('.')[-2] |
---|
106 | icon="64x64/%s_%s.png"%(debian_name,pkg_id) |
---|
107 | ret_icon=self.icons_path+"%s/%s"%(component,icon) |
---|
108 | if os.path.exists(ret_icon): |
---|
109 | return ret_icon |
---|
110 | else: |
---|
111 | #Unaccurate icon search... |
---|
112 | if os.path.isdir(self.icons_path+"/"+component+"/64x64"): |
---|
113 | for icon_file in os.listdir(self.icons_path+"/"+component+"/64x64/"): |
---|
114 | if re.search("^.*"+pkg_info['icon']+".*\.png",icon_file): |
---|
115 | ret_icon=self.icons_path+"%s/64x64/%s"%(component,icon_file) |
---|
116 | if os.path.exists(ret_icon): |
---|
117 | return ret_icon |
---|
118 | else: |
---|
119 | #The very last attempt. We'll look for the icon in the desktop (if any) |
---|
120 | desktop_file=pkg_info['id'] |
---|
121 | if not desktop_file.endswith(".desktop"): |
---|
122 | desktop_file=desktop_file+".desktop" |
---|
123 | if os.path.isfile("/usr/share/applications/%s"%desktop_file): |
---|
124 | f=open("/usr/share/applications/%s"%desktop_file,'r') |
---|
125 | for l in f.readlines(): |
---|
126 | if l.startswith("Icon"): |
---|
127 | icon_name=l.split('=')[-1].strip('\n') |
---|
128 | if os.path.isfile(icon_name): |
---|
129 | return(icon_name) |
---|
130 | else: |
---|
131 | ret_icon=self.icon_db.lookup_icon(icon_name,256,Gtk.IconLookupFlags.FORCE_SVG) |
---|
132 | if ret_icon: |
---|
133 | return(ret_icon.get_filename()) |
---|
134 | |
---|
135 | ret_icon=self.package_icon |
---|
136 | return ret_icon |
---|
137 | |
---|
138 | #def get_icon |
---|
139 | |
---|
140 | |
---|
141 | def check_perms(self): |
---|
142 | |
---|
143 | try: |
---|
144 | f=open("/run/lliurex-store.background.pid","w") |
---|
145 | f.close() |
---|
146 | return True |
---|
147 | |
---|
148 | except: |
---|
149 | return False |
---|
150 | |
---|
151 | #def check_perms |
---|
152 | |
---|
153 | |
---|
154 | def create_icons_dir(self): |
---|
155 | |
---|
156 | if not os.path.exists(self.icons_path): |
---|
157 | os.makedirs(self.icons_path) |
---|
158 | |
---|
159 | #def create_icons_dir |
---|
160 | |
---|
161 | |
---|
162 | def write_local_icons_info(self,info): |
---|
163 | |
---|
164 | f=open(self.icon_dates_file,"w") |
---|
165 | f.write(json.dumps(info,indent=4)) |
---|
166 | f.close() |
---|
167 | |
---|
168 | #def write_local_icons_info |
---|
169 | |
---|
170 | |
---|
171 | def check_local_icons_files(self): |
---|
172 | |
---|
173 | self.local_icons_dates={} |
---|
174 | try: |
---|
175 | f=open(self.icon_dates_file) |
---|
176 | self.local_icons_dates=json.load(f) |
---|
177 | f.close() |
---|
178 | except Exception as e: |
---|
179 | print(e) |
---|
180 | |
---|
181 | for dist in self.dists: |
---|
182 | self.local_icons_dates.setdefault(dist,{}) |
---|
183 | for component in self.components: |
---|
184 | self.local_icons_dates[dist].setdefault(component,-1) |
---|
185 | |
---|
186 | #def check_local_icons_files |
---|
187 | |
---|
188 | |
---|
189 | |
---|
190 | def check_remote_icons_files(self): |
---|
191 | |
---|
192 | self.remote_icons_dates={} |
---|
193 | |
---|
194 | for dist in self.dists: |
---|
195 | self.remote_icons_dates[dist]={} |
---|
196 | for component in self.components: |
---|
197 | |
---|
198 | url=self.base_url+dist+"/"+component |
---|
199 | |
---|
200 | try: |
---|
201 | |
---|
202 | req=urllib2.Request(url,headers=HEADER) |
---|
203 | res=urllib2.urlopen(req) |
---|
204 | x=tempfile.NamedTemporaryFile() |
---|
205 | x.write(res.read()) |
---|
206 | x.seek(0) |
---|
207 | |
---|
208 | for line in x.readlines(): |
---|
209 | line=line.decode("utf-8") |
---|
210 | if self.icons_url_file in line: |
---|
211 | line=line.split('<td align="right">')[1].split(" ") |
---|
212 | icons_date=line[0] |
---|
213 | icons_date=time.mktime(datetime.datetime.strptime(icons_date, "%Y-%m-%d").timetuple()) |
---|
214 | self.remote_icons_dates[dist][component]=icons_date |
---|
215 | break |
---|
216 | x.close() |
---|
217 | |
---|
218 | except Exception as e: |
---|
219 | pass |
---|
220 | |
---|
221 | if component not in self.remote_icons_dates[dist]: |
---|
222 | self.remote_icons_dates[dist][component]=-1 |
---|
223 | |
---|
224 | #def download_home_info |
---|
225 | |
---|
226 | |
---|
227 | def update_icons(self): |
---|
228 | |
---|
229 | self.check_local_icons_files() |
---|
230 | self.check_remote_icons_files() |
---|
231 | |
---|
232 | self.current_icons_dates={} |
---|
233 | |
---|
234 | if not os.path.exists(self.download_cache): |
---|
235 | os.makedirs(self.download_cache) |
---|
236 | |
---|
237 | for dist in self.dists: |
---|
238 | self.current_icons_dates[dist]={} |
---|
239 | for component in self.components: |
---|
240 | |
---|
241 | if self.remote_icons_dates[dist][component] > self.local_icons_dates[dist][component]: |
---|
242 | |
---|
243 | url=self.base_url+dist+"/"+component+"/"+self.icons_url_file |
---|
244 | try: |
---|
245 | icon_file=self.download_cache+"%s_%s_icons.tar.gz"%(dist,component) |
---|
246 | path=self.icons_path+self.distro_name+"-"+dist+"-"+component+"/64x64/" |
---|
247 | |
---|
248 | req=urllib2.Request(url,headers=HEADER) |
---|
249 | res=urllib2.urlopen(req) |
---|
250 | x=open(icon_file,"wb") |
---|
251 | x.write(res.read()) |
---|
252 | x.close() |
---|
253 | |
---|
254 | self.current_icons_dates[dist][component]=self.remote_icons_dates[dist][component] |
---|
255 | |
---|
256 | command="tar -xf %s -C %s"%(icon_file,path) |
---|
257 | if not os.path.exists(path): |
---|
258 | os.makedirs(path) |
---|
259 | |
---|
260 | os.system(command) |
---|
261 | |
---|
262 | |
---|
263 | except Exception as e: |
---|
264 | self.current_icons_dates[dist][component]=self.local_icons_dates[dist][component] |
---|
265 | |
---|
266 | else: |
---|
267 | self.current_icons_dates[dist][component]=self.local_icons_dates[dist][component] |
---|
268 | |
---|
269 | #def update_icons |
---|
270 | |
---|
271 | |
---|
272 | #class ResourcesManager |
---|
273 | |
---|
274 | |
---|
275 | if __name__=="__main__": |
---|
276 | |
---|
277 | rm=ResourcesManager() |
---|
278 | #rm.check_remote_icons_files() |
---|
279 | #rm.check_local_icons_files() |
---|
280 | rm.update_icons() |
---|
281 | pkg={} |
---|
282 | pkg["package"]="zsnes" |
---|
283 | pkg["component"]="universe" |
---|
284 | pkg["icon"]="64x64/zsnes_zsnes.png" |
---|
285 | |
---|
286 | print(rm.get_icon(pkg)) |
---|
287 | |
---|
288 | |
---|
289 | |
---|