Merge lp:~mvo/software-center/track-axi-changes into lp:software-center

Proposed by Michael Vogt
Status: Merged
Merged at revision: 2960
Proposed branch: lp:~mvo/software-center/track-axi-changes
Merge into: lp:software-center
Diff against target: 151 lines (+63/-4)
3 files modified
softwarecenter/db/database.py (+28/-2)
softwarecenter/paths.py (+4/-0)
test/test_database.py (+31/-2)
To merge this branch: bzr merge lp:~mvo/software-center/track-axi-changes
Reviewer Review Type Date Requested Status
Gary Lasker (community) Approve
Review via email: mp+101494@code.launchpad.net

Description of the change

This branch adds a monitor for changes in the apt-xapian-index to ensure the DB is reopened
if the cron job changes the apt-xapian-index database.

To post a comment you must log in.
2924. By Michael Vogt

softwarecenter/db/database.py: add comment about approach

2925. By Michael Vogt

add proper test and disconnect the signal to avoid multiple tracking of the changed signal

Revision history for this message
Michael Vogt (mvo) wrote :

This should fix bug #507836

2926. By Michael Vogt

merged from trunk

Revision history for this message
Gary Lasker (gary-lasker) wrote :

Very nice! And thanks for the unit test!!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'softwarecenter/db/database.py'
--- softwarecenter/db/database.py 2012-03-20 11:13:05 +0000
+++ softwarecenter/db/database.py 2012-04-11 07:26:17 +0000
@@ -28,7 +28,7 @@
28from softwarecenter.db.pkginfo import get_pkg_info28from softwarecenter.db.pkginfo import get_pkg_info
29import softwarecenter.paths29import softwarecenter.paths
3030
31from gi.repository import GObject31from gi.repository import GObject, Gio
3232
33#from softwarecenter.utils import *33#from softwarecenter.utils import *
34from softwarecenter.enums import (34from softwarecenter.enums import (
@@ -152,6 +152,7 @@
152 # so no memory leak152 # so no memory leak
153 self._db_per_thread = {}153 self._db_per_thread = {}
154 self._parser_per_thread = {}154 self._parser_per_thread = {}
155 self._axi_stamp_monitor = None
155156
156 @property157 @property
157 def xapiandb(self):158 def xapiandb(self):
@@ -174,7 +175,8 @@
174 xapiandb = xapian.Database(self._db_pathname)175 xapiandb = xapian.Database(self._db_pathname)
175 if self._use_axi:176 if self._use_axi:
176 try:177 try:
177 axi = xapian.Database("/var/lib/apt-xapian-index/index")178 axi = xapian.Database(
179 softwarecenter.paths.APT_XAPIAN_INDEX_DB_PATH)
178 xapiandb.add_database(axi)180 xapiandb.add_database(axi)
179 except:181 except:
180 LOG.exception("failed to add apt-xapian-index")182 LOG.exception("failed to add apt-xapian-index")
@@ -218,8 +220,22 @@
218 self._use_axi = use_axi220 self._use_axi = use_axi
219 self._use_agent = use_agent221 self._use_agent = use_agent
220 if use_axi:222 if use_axi:
223 if self._axi_stamp_monitor:
224 self._axi_stamp_monitor.disconnect_by_func(
225 self._on_axi_stamp_changed)
221 self._axi_values = parse_axi_values_file()226 self._axi_values = parse_axi_values_file()
222 self.nr_databases += 1227 self.nr_databases += 1
228 # mvo: we could monitor changes in
229 # softwarecenter.paths.APT_XAPIAN_INDEX_DB_PATH here too
230 # as its a text file that points to the current DB
231 # *if* we do that, we need to change the event == ATTRIBUTE
232 # change in _on_axi_stamp_changed too
233 self._axi_stamp = Gio.File.new_for_path(
234 softwarecenter.paths.APT_XAPIAN_INDEX_UPDATE_STAMP_PATH)
235 self._timeout_id = None
236 self._axi_stamp_monitor = self._axi_stamp.monitor_file(0, None)
237 self._axi_stamp_monitor.connect(
238 "changed", self._on_axi_stamp_changed)
223 if use_agent:239 if use_agent:
224 self.nr_databases += 1240 self.nr_databases += 1
225 # additional dbs241 # additional dbs
@@ -227,6 +243,16 @@
227 self.nr_databases += 1243 self.nr_databases += 1
228 self.emit("open", self._db_pathname)244 self.emit("open", self._db_pathname)
229245
246 def _on_axi_stamp_changed(self, monitor, afile, otherfile, event):
247 # we only care about the utime() update from update-a-x-i
248 if not event == Gio.FileMonitorEvent.ATTRIBUTE_CHANGED:
249 return
250 LOG.info("afile '%s' changed" % afile)
251 if self._timeout_id:
252 GObject.source_remove(self._timeout_id)
253 self._timeout_id = None
254 self._timeout_id = GObject.timeout_add(500, self.reopen)
255
230 def add_database(self, database):256 def add_database(self, database):
231 self._additional_databases.append(database)257 self._additional_databases.append(database)
232 self.xapiandb.add_database(database)258 self.xapiandb.add_database(database)
233259
=== modified file 'softwarecenter/paths.py'
--- softwarecenter/paths.py 2012-03-19 21:27:43 +0000
+++ softwarecenter/paths.py 2012-04-11 07:26:17 +0000
@@ -64,6 +64,10 @@
64 "software-center-agent.db")64 "software-center-agent.db")
65XAPIAN_PATH = os.path.join(XAPIAN_BASE_PATH, "xapian")65XAPIAN_PATH = os.path.join(XAPIAN_BASE_PATH, "xapian")
6666
67# AXI
68APT_XAPIAN_INDEX_BASE_PATH = "/var/lib/apt-xapian-index"
69APT_XAPIAN_INDEX_DB_PATH = APT_XAPIAN_INDEX_BASE_PATH + "/index"
70APT_XAPIAN_INDEX_UPDATE_STAMP_PATH = APT_XAPIAN_INDEX_BASE_PATH + "/update-timestamp"
6771
68# ratings&review72# ratings&review
69# relative to datadir73# relative to datadir
7074
=== modified file 'test/test_database.py'
--- test/test_database.py 2012-03-07 22:27:15 +0000
+++ test/test_database.py 2012-04-11 07:26:17 +0000
@@ -3,6 +3,8 @@
3import apt3import apt
4import os4import os
5import re5import re
6import tempfile
7import time
6import unittest8import unittest
7import xapian9import xapian
810
@@ -12,7 +14,7 @@
12from testutils import setup_test_env14from testutils import setup_test_env
13setup_test_env()15setup_test_env()
1416
1517import softwarecenter.paths
16from softwarecenter.db.application import Application, AppDetails18from softwarecenter.db.application import Application, AppDetails
17from softwarecenter.db.database import StoreDatabase19from softwarecenter.db.database import StoreDatabase
18from softwarecenter.db.enquire import AppEnquire20from softwarecenter.db.enquire import AppEnquire
@@ -33,8 +35,9 @@
33 PkgStates,35 PkgStates,
34 )36 )
35from softwarecenter.testutils import (37from softwarecenter.testutils import (
36 get_test_db, 38 get_test_db,
37 get_test_pkg_info,39 get_test_pkg_info,
40 do_events,
38 make_software_center_agent_subscription_dict,41 make_software_center_agent_subscription_dict,
39 make_software_center_agent_app_dict,42 make_software_center_agent_app_dict,
40 )43 )
@@ -676,6 +679,32 @@
676 details.force_not_automatic_archive_suite("")679 details.force_not_automatic_archive_suite("")
677 self.assertEqual(app.archive_suite, "")680 self.assertEqual(app.archive_suite, "")
678681
682class TrackDBTestCase(unittest.TestCase):
683
684 def test_track_db_open(self):
685 tmpdir = tempfile.mkdtemp()
686 tmpstamp = os.path.join(tmpdir, "update-stamp")
687 open(tmpstamp, "w")
688 softwarecenter.paths.APT_XAPIAN_INDEX_UPDATE_STAMP_PATH = tmpstamp
689 softwarecenter.paths.APT_XAPIAN_INDEX_DB_PATH = \
690 softwarecenter.paths.XAPIAN_PATH
691 db = get_test_db()
692 db._axi_stamp_monitor = None
693 db._on_axi_stamp_changed = Mock()
694 self.assertFalse(db._on_axi_stamp_changed.called)
695 db.open(use_axi=True)
696 do_events()
697 self.assertFalse(db._on_axi_stamp_changed.called)
698 do_events()
699 #print "modifiyng stampfile: ", tmpstamp
700 os.utime(tmpstamp, (0, 0))
701 # wait up to 5s until the gvfs delivers the signal
702 for i in range(50):
703 do_events()
704 time.sleep(0.1)
705 if db._on_axi_stamp_changed.called:
706 break
707 self.assertTrue(db._on_axi_stamp_changed.called)
679708
680if __name__ == "__main__":709if __name__ == "__main__":
681 #import logging710 #import logging

Subscribers

People subscribed via source and target branches