Merge lp:~larryprice/libertine-scope/libertine-store-internal-packages into lp:~larryprice/libertine-scope/libertine-store-search

Proposed by Larry Price on 2016-10-06
Status: Needs review
Proposed branch: lp:~larryprice/libertine-scope/libertine-store-internal-packages
Merge into: lp:~larryprice/libertine-scope/libertine-store-search
Diff against target: 251 lines (+72/-76)
4 files modified
service/libertine_service/appstream.py (+36/-9)
service/libertine_service/apt.py (+30/-65)
service/libertine_service/dbus.py (+2/-2)
service/libertine_service/tasks.py (+4/-0)
To merge this branch: bzr merge lp:~larryprice/libertine-scope/libertine-store-internal-packages
Reviewer Review Type Date Requested Status
Libertine Developers 2016-10-06 Pending
Review via email: mp+307870@code.launchpad.net

Commit message

Search internal container caches for appstream or aptcache data.

Description of the change

Search internal container caches for appstream or aptcache data.

When creating the 'palatine' container, 'appstream' will be installed by default such that we can use it's internal cache. In order to use the container cache currently, you'll need to restart the libertined service after creating the container. However, there is also an MP which refactors container creation that will resolve this.

To post a comment you must log in.

Unmerged revisions

112. By Larry Price on 2016-10-06

Removing raise

111. By Larry Price on 2016-10-06

Wrong endpoint for update

110. By Larry Price on 2016-10-06

Install appstream by default in any containers created by the service

109. By Larry Price on 2016-10-06

Cache classes attempt to use internal apt caches

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'service/libertine_service/appstream.py'
2--- service/libertine_service/appstream.py 2016-09-02 15:27:28 +0000
3+++ service/libertine_service/appstream.py 2016-10-06 18:10:43 +0000
4@@ -16,6 +16,13 @@
5 gi.require_version('AppStream', '1.0')
6 from gi.repository import AppStream
7
8+gi.require_version('Libertine', '1')
9+from gi.repository import Libertine
10+
11+from os import listdir
12+from os.path import isfile, join
13+from libertine import utils
14+
15
16 def xstr(s):
17 """Converts anything to a string"""
18@@ -24,15 +31,18 @@
19
20 class AppStreamCache(object):
21 """AppStreamCache"""
22- def __init__(self, config):
23+ def __init__(self, container_id):
24 super(AppStreamCache, self).__init__()
25- self.config = config
26- self.pool = AppStream.Pool()
27- self.pool.load()
28+ self._container = container_id
29+ self._pool = AppStream.Pool()
30+ self._loaded = False
31+ self._basepath = ""
32
33 def search(self, query):
34+ self._load()
35+
36 packages = []
37- for component in self.pool.search(query):
38+ for component in self._pool.search(query):
39 if component.is_valid():
40 package = {}
41 package["name"] = xstr(component.get_name())
42@@ -41,13 +51,16 @@
43 package["uri"] = xstr(component.get_url(AppStream.UrlKind.HOMEPAGE))
44
45 if len(component.get_icons()) > 0:
46- package["icon"] = xstr(self._get_largest_icon(component.get_icons()).get_filename())
47+ package["icon"] = join(self._basepath,
48+ xstr(self._get_largest_icon(component.get_icons()).get_filename()))
49 packages.append(package)
50
51 return packages[::-1]
52
53 def app_info(self, app_id):
54- apps = self.pool.get_components_by_id(app_id)
55+ self._load()
56+
57+ apps = self._pool.get_components_by_id(app_id)
58 app_data = {}
59 if len(apps) == 1 and apps[0].is_valid():
60 app = apps[0]
61@@ -63,12 +76,13 @@
62 if largest is not None:
63 if "screenshots" not in app_data:
64 app_data["screenshots"] = []
65- app_data["screenshots"].append(largest.get_url())
66+ app_data["screenshots"].append(join(self._basepath, largest.get_url()))
67
68 app_data["description"] = xstr(app.get_description())
69
70 if len(app.get_icons()) > 0:
71- app_data["icon"] = xstr(self._get_largest_icon(app.get_icons()).get_filename())
72+ app_data["icon"] = join(self._basepath,
73+ xstr(self._get_largest_icon(app.get_icons()).get_filename()))
74
75 return app_data
76
77@@ -78,3 +92,16 @@
78 if largest is None or icon.get_width() > largest.get_width():
79 largest = icon
80 return largest
81+
82+ def _load(self):
83+ if not self._loaded:
84+ path = join(Libertine.container_path(self._container), "var/cache/app-info/gv/")
85+ cachefiles = [f for f in listdir(path) if isfile(join(path, f))]
86+ if len(cachefiles) > 0:
87+ utils.get_logger().debug("Using appstream cache for container %s" % self._container)
88+ self._pool.load_cache_file(join(path, cachefiles[0]))
89+ self._basepath = path
90+ else:
91+ utils.get_logger().debug("Using system appstream cache")
92+ self._pool.load()
93+ self._loaded = True
94
95=== modified file 'service/libertine_service/apt.py'
96--- service/libertine_service/apt.py 2016-09-20 17:56:18 +0000
97+++ service/libertine_service/apt.py 2016-10-06 18:10:43 +0000
98@@ -13,87 +13,43 @@
99 # along with this program. If not, see <http://www.gnu.org/licenses/>.
100
101 import apt
102-import glob
103-import os
104 import re
105
106-def _use_system_gpg():
107- """ Configures APT to use the system-wide GPG store. Always."""
108- for key in 'Dir::Etc::Trusted', 'Dir::Etc::TrustedParts':
109- apt.apt_pkg.config.set(key, apt.apt_pkg.config.find_file(key))
110-
111-
112-def _get_system_sources_list():
113- """Concatenates all the system apt/sources.list files into a single text buffer."""
114- sources_path = os.path.join('/etc', 'apt')
115- sources_list = glob.glob(os.path.join(sources_path, 'sources.list.d', '*.list'))
116- sources_list.append(os.path.join(sources_path, 'sources.list'))
117- sources = ''
118- for source in sources_list:
119- with open(source) as f:
120- sources += f.read()
121- return sources
122-
123-
124-def _write_local_sources_list(rootdir):
125- """ Writes out a user-local sources.list file by copying all the systemwide ones. """
126- sources = _get_system_sources_list()
127- sources_path = os.path.join(rootdir, 'etc', 'apt')
128- os.makedirs(sources_path, exist_ok=True)
129- srcfile = os.path.join(sources_path, 'sources.list')
130- with open(srcfile, 'w') as f:
131- f.write(sources)
132- return srcfile
133-
134-
135-def _get_local_apt_cache():
136- """ Gets a user-local APT cache. """
137- rootdir = os.path.join('/tmp', '.cache', 'libertine', 'apt')
138- srcfile = _write_local_sources_list(rootdir)
139-
140- apt_cache = apt.Cache(rootdir=rootdir, memonly=True)
141- apt.apt_pkg.config.clear("APT::Update::Post-Invoke-Success")
142- apt_cache.update(sources_list=srcfile)
143- apt_cache.open()
144-
145- return apt_cache
146-
147-
148-def _get_apt_cache(config):
149- """ Factory function to get the APT cache obejct. """
150- _use_system_gpg()
151- if config.use_local_cache:
152- return _get_local_apt_cache()
153- else:
154- return apt.Cache()
155+import gi
156+gi.require_version('Libertine', '1')
157+from gi.repository import Libertine
158+
159+from libertine import utils
160+
161
162 class AptCache(object):
163 """docstring for AptCache"""
164- def __init__(self, config):
165+ def __init__(self, container_id):
166 super(AptCache, self).__init__()
167- self.config = config
168+ self._cache = None
169+ self._container = container_id
170
171 def search(self, query):
172+ self._load()
173+ pkg_keys = [key for key in self._cache.keys() if re.match(query, key)]
174 apps = []
175-
176- cache = _get_apt_cache(self.config)
177- pkg_keys = [key for key in cache.keys() if re.match(query, key)]
178 for key in pkg_keys:
179 app = {}
180- app["name"] = cache[key].name
181- app["id"] = cache[key].name
182- app["package"] = cache[key].name
183- if len(cache[key].versions) > 0:
184- app["summary"] = cache[key].versions[0].summary
185- app["website"] = cache[key].versions[0].homepage
186+ app["name"] = self._cache[key].name
187+ app["id"] = self._cache[key].name
188+ app["package"] = self._cache[key].name
189+ if len(self._cache[key].versions) > 0:
190+ app["summary"] = self._cache[key].versions[0].summary
191+ app["website"] = self._cache[key].versions[0].homepage
192 apps.append(app)
193 return apps
194
195 def app_info(self, app_id):
196+ self._load()
197+
198 app_data = {}
199- cache = _get_apt_cache(self.config)
200- if app_id in cache:
201- app = cache[app_id]
202+ if app_id in self._cache:
203+ app = self._cache[app_id]
204 app_data["name"] = app.name
205 app_data["id"] = app.name
206 app_data["package"] = app.name
207@@ -104,3 +60,12 @@
208 app_data["package"] = app.name
209
210 return app_data
211+
212+ def _load(self):
213+ if self._cache is None:
214+ try:
215+ utils.get_logger().debug("Trying aptcache for container %s" % self._container)
216+ self._cache = apt.Cache(rootdir=Libertine.container_path(self._container))
217+ except PermissionError:
218+ utils.get_logger().debug("Trying system aptcache")
219+ self._cache = apt.Cache()
220
221=== modified file 'service/libertine_service/dbus.py'
222--- service/libertine_service/dbus.py 2016-09-20 18:30:26 +0000
223+++ service/libertine_service/dbus.py 2016-10-06 18:10:43 +0000
224@@ -75,10 +75,10 @@
225 raise
226
227 try:
228- self.cache = appstream.AppStreamCache(config)
229+ self.cache = appstream.AppStreamCache(CONTAINER_ID)
230 except:
231 log.warning("AppStream backend unavailable, falling back to Apt backend.")
232- self.cache = apt.AptCache(config)
233+ self.cache = apt.AptCache(CONTAINER_ID)
234
235 self.container = container.Container(log)
236 self.progress = {}
237
238=== modified file 'service/libertine_service/tasks.py'
239--- service/libertine_service/tasks.py 2016-09-16 21:35:57 +0000
240+++ service/libertine_service/tasks.py 2016-10-06 18:10:43 +0000
241@@ -134,6 +134,10 @@
242 self.log.debug("Creating container '%s' failed" % self.container)
243 self.config.delete_container(self.container)
244 return
245+
246+ container.install_package("appstream") # install appstream for searching packages
247+ container.update_libertine_container() # update apt cache to generate appstream cache
248+
249 self.config.update_container_install_status(self.container, "ready")
250
251 def _before(self):

Subscribers

People subscribed via source and target branches

to all changes: