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
1=== modified file 'softwarecenter/db/database.py'
2--- softwarecenter/db/database.py 2012-03-20 11:13:05 +0000
3+++ softwarecenter/db/database.py 2012-04-11 07:26:17 +0000
4@@ -28,7 +28,7 @@
5 from softwarecenter.db.pkginfo import get_pkg_info
6 import softwarecenter.paths
7
8-from gi.repository import GObject
9+from gi.repository import GObject, Gio
10
11 #from softwarecenter.utils import *
12 from softwarecenter.enums import (
13@@ -152,6 +152,7 @@
14 # so no memory leak
15 self._db_per_thread = {}
16 self._parser_per_thread = {}
17+ self._axi_stamp_monitor = None
18
19 @property
20 def xapiandb(self):
21@@ -174,7 +175,8 @@
22 xapiandb = xapian.Database(self._db_pathname)
23 if self._use_axi:
24 try:
25- axi = xapian.Database("/var/lib/apt-xapian-index/index")
26+ axi = xapian.Database(
27+ softwarecenter.paths.APT_XAPIAN_INDEX_DB_PATH)
28 xapiandb.add_database(axi)
29 except:
30 LOG.exception("failed to add apt-xapian-index")
31@@ -218,8 +220,22 @@
32 self._use_axi = use_axi
33 self._use_agent = use_agent
34 if use_axi:
35+ if self._axi_stamp_monitor:
36+ self._axi_stamp_monitor.disconnect_by_func(
37+ self._on_axi_stamp_changed)
38 self._axi_values = parse_axi_values_file()
39 self.nr_databases += 1
40+ # mvo: we could monitor changes in
41+ # softwarecenter.paths.APT_XAPIAN_INDEX_DB_PATH here too
42+ # as its a text file that points to the current DB
43+ # *if* we do that, we need to change the event == ATTRIBUTE
44+ # change in _on_axi_stamp_changed too
45+ self._axi_stamp = Gio.File.new_for_path(
46+ softwarecenter.paths.APT_XAPIAN_INDEX_UPDATE_STAMP_PATH)
47+ self._timeout_id = None
48+ self._axi_stamp_monitor = self._axi_stamp.monitor_file(0, None)
49+ self._axi_stamp_monitor.connect(
50+ "changed", self._on_axi_stamp_changed)
51 if use_agent:
52 self.nr_databases += 1
53 # additional dbs
54@@ -227,6 +243,16 @@
55 self.nr_databases += 1
56 self.emit("open", self._db_pathname)
57
58+ def _on_axi_stamp_changed(self, monitor, afile, otherfile, event):
59+ # we only care about the utime() update from update-a-x-i
60+ if not event == Gio.FileMonitorEvent.ATTRIBUTE_CHANGED:
61+ return
62+ LOG.info("afile '%s' changed" % afile)
63+ if self._timeout_id:
64+ GObject.source_remove(self._timeout_id)
65+ self._timeout_id = None
66+ self._timeout_id = GObject.timeout_add(500, self.reopen)
67+
68 def add_database(self, database):
69 self._additional_databases.append(database)
70 self.xapiandb.add_database(database)
71
72=== modified file 'softwarecenter/paths.py'
73--- softwarecenter/paths.py 2012-03-19 21:27:43 +0000
74+++ softwarecenter/paths.py 2012-04-11 07:26:17 +0000
75@@ -64,6 +64,10 @@
76 "software-center-agent.db")
77 XAPIAN_PATH = os.path.join(XAPIAN_BASE_PATH, "xapian")
78
79+# AXI
80+APT_XAPIAN_INDEX_BASE_PATH = "/var/lib/apt-xapian-index"
81+APT_XAPIAN_INDEX_DB_PATH = APT_XAPIAN_INDEX_BASE_PATH + "/index"
82+APT_XAPIAN_INDEX_UPDATE_STAMP_PATH = APT_XAPIAN_INDEX_BASE_PATH + "/update-timestamp"
83
84 # ratings&review
85 # relative to datadir
86
87=== modified file 'test/test_database.py'
88--- test/test_database.py 2012-03-07 22:27:15 +0000
89+++ test/test_database.py 2012-04-11 07:26:17 +0000
90@@ -3,6 +3,8 @@
91 import apt
92 import os
93 import re
94+import tempfile
95+import time
96 import unittest
97 import xapian
98
99@@ -12,7 +14,7 @@
100 from testutils import setup_test_env
101 setup_test_env()
102
103-
104+import softwarecenter.paths
105 from softwarecenter.db.application import Application, AppDetails
106 from softwarecenter.db.database import StoreDatabase
107 from softwarecenter.db.enquire import AppEnquire
108@@ -33,8 +35,9 @@
109 PkgStates,
110 )
111 from softwarecenter.testutils import (
112- get_test_db,
113+ get_test_db,
114 get_test_pkg_info,
115+ do_events,
116 make_software_center_agent_subscription_dict,
117 make_software_center_agent_app_dict,
118 )
119@@ -676,6 +679,32 @@
120 details.force_not_automatic_archive_suite("")
121 self.assertEqual(app.archive_suite, "")
122
123+class TrackDBTestCase(unittest.TestCase):
124+
125+ def test_track_db_open(self):
126+ tmpdir = tempfile.mkdtemp()
127+ tmpstamp = os.path.join(tmpdir, "update-stamp")
128+ open(tmpstamp, "w")
129+ softwarecenter.paths.APT_XAPIAN_INDEX_UPDATE_STAMP_PATH = tmpstamp
130+ softwarecenter.paths.APT_XAPIAN_INDEX_DB_PATH = \
131+ softwarecenter.paths.XAPIAN_PATH
132+ db = get_test_db()
133+ db._axi_stamp_monitor = None
134+ db._on_axi_stamp_changed = Mock()
135+ self.assertFalse(db._on_axi_stamp_changed.called)
136+ db.open(use_axi=True)
137+ do_events()
138+ self.assertFalse(db._on_axi_stamp_changed.called)
139+ do_events()
140+ #print "modifiyng stampfile: ", tmpstamp
141+ os.utime(tmpstamp, (0, 0))
142+ # wait up to 5s until the gvfs delivers the signal
143+ for i in range(50):
144+ do_events()
145+ time.sleep(0.1)
146+ if db._on_axi_stamp_changed.called:
147+ break
148+ self.assertTrue(db._on_axi_stamp_changed.called)
149
150 if __name__ == "__main__":
151 #import logging

Subscribers

People subscribed via source and target branches