changeset 4991:d39bdb239da4

Xapian2009: py.test.importorskip in tests was removed, tests try import Xapian, and on ImportError skip a test. Index.indexPages now takes a pages parameter - list of pages which must be indexed.
author Dmitrijs Milajevs <dimazest@gmail.com>
date Mon, 10 Aug 2009 21:30:41 +0200
parents 6f87dbd8f895
children c6da526a8c30
files MoinMoin/search/Xapian.py MoinMoin/search/_tests/test_search.py MoinMoin/search/builtin.py
diffstat 3 files changed, 42 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/search/Xapian.py	Mon Aug 10 16:58:49 2009 +0200
+++ b/MoinMoin/search/Xapian.py	Mon Aug 10 21:30:41 2009 +0200
@@ -605,8 +605,8 @@
                 logging.debug('attachment %s from %s removed from index' %
                     (doc['values']['attachment'], doc['values']['pagename']))
 
-    def _index_pages(self, request, files=None, mode='update'):
-        """ Index all pages (and all given files)
+    def _index_pages(self, request, files=None, mode='update', pages=None):
+        """ Index pages (and all given files)
 
         This should be called from indexPages or indexPagesInNewThread only!
 
@@ -617,10 +617,14 @@
         and this method must release it when it finishes or fails.
 
         @param request: the current request
-        @keyword files: an optional list of files to index
-        @keyword mode: how to index the files, either 'add', 'update' or
-                       'rebuild'
+        @param files: an optional list of files to index
+        @param mode: how to index the files, either 'add', 'update' or 'rebuild'
+        @param pages: list of pages to index, if not given, all pages are indexed
+
         """
+        if pages is None:
+            # Index all pages
+            pages = request.rootpage.getPageList(user='', exists=1)
 
         # rebuilding the DB: delete it and add everything
         if mode == 'rebuild':
@@ -629,10 +633,8 @@
             mode = 'add'
 
         try:
-            self.touch()
             connection = MoinIndexerConnection(self.dir)
-            # writer.configure(self.prefixMap, self.indexValueMap)
-            pages = request.rootpage.getPageList(user='', exists=1)
+            self.touch()
             logging.debug("indexing all (%d) pages..." % len(pages))
             for pagename in pages:
                 self._index_page(request, connection, pagename, mode=mode)
@@ -640,11 +642,8 @@
                 logging.debug("indexing all files...")
                 for fname in files:
                     fname = fname.strip()
-                    # XXX refactor _index_file
-                    # self._index_file(request, connection, fname, mode)
+                    self._index_file(request, connection, fname, mode)
             connection.flush()
+        finally:
             connection.close()
-        finally:
-            pass
-            #connection.__del__()
 
--- a/MoinMoin/search/_tests/test_search.py	Mon Aug 10 16:58:49 2009 +0200
+++ b/MoinMoin/search/_tests/test_search.py	Mon Aug 10 21:30:41 2009 +0200
@@ -247,22 +247,14 @@
 
     def setup_method(self, method):
 
-        py.test.importorskip('MoinMoin.support.xappy')
-        from MoinMoin.search.Xapian import Index, MoinIndexerConnection
+        try:
+            from MoinMoin.search.Xapian import Index
+        except ImportError:
+            py.test.skip('xapian is not installed')
 
         nuke_xapian_index(self.request)
-
         index = Index(self.request)
-        assert index.lock.acquire(1.0)
-        try:
-            connection = MoinIndexerConnection(index.dir)
-            index._unsign()
-            for page in self.pages:
-                index._index_page(self.request, connection, page, mode='add')
-            index._sign()
-        finally:
-            index.lock.release()
-            connection.close()
+        index.indexPages(mode='add', pages=self.pages)
 
     def teardown_method(self, method):
         nuke_xapian_index(self.request)
@@ -271,17 +263,20 @@
 class TestXapianIndexingInNewThread(object):
     """ search: test Xapian indexing """
 
+    class Config(wikiconfig.Config):
+
+        xapian_search = True
+
     def test_index_in_new_thread(self):
         """ search: kicks off indexing for a single pages in Xapian """
-        py.test.skip('XXX takes a lot of time')
+        try:
+            from MoinMoin.search.Xapian import Index
+        except ImportError:
+            py.test.skip('xapian is not installed')
 
-        py.test.importorskip('MoinMoin.support.xappy')
-        from MoinMoin.search import Xapian
-
-        # This only tests that the call to indexing doesn't raise.
         nuke_xapian_index(self.request)
-        idx = Xapian.Index(self.request)
-        idx.indexPagesInNewThread(mode='add') # slow: builds an index of all pages
+        index = Index(self.request)
+        index.indexPagesInNewThread(mode='add')
 
         nuke_xapian_index(self.request)
 
--- a/MoinMoin/search/builtin.py	Mon Aug 10 16:58:49 2009 +0200
+++ b/MoinMoin/search/builtin.py	Mon Aug 10 21:30:41 2009 +0200
@@ -248,13 +248,15 @@
         if now:
             self._do_queued_updates_InNewThread()
 
-    def indexPages(self, files=None, mode='update'):
-        """ Index all pages (and files, if given)
+    def indexPages(self, files=None, mode='update', pages=None):
+        """ Index pages (and files, if given)
 
         Can be called only from a script. To index pages during a user
         request, use indexPagesInNewThread.
-        @keyword files: iterator or list of files to index additionally
-        @keyword mode: set the mode of indexing the pages, either 'update', 'add' or 'rebuild'
+
+        @param files: iterator or list of files to index additionally
+        @param mode: set the mode of indexing the pages, either 'update', 'add' or 'rebuild'
+        @param pages: list of pages to index, if not given, all pages are indexed
         """
         if not self.lock.acquire(1.0):
             logging.warning("can't index: can't acquire lock")
@@ -263,15 +265,15 @@
             self._unsign()
             start = time.time()
             request = self._indexingRequest(self.request)
-            self._index_pages(request, files, mode)
+            self._index_pages(request, files, mode, pages=pages)
             logging.info("indexing completed successfully in %0.2f seconds." %
                         (time.time() - start))
             self._sign()
         finally:
             self.lock.release()
 
-    def indexPagesInNewThread(self, files=None, mode='update'):
-        """ Index all pages in a new thread
+    def indexPagesInNewThread(self, files=None, mode='update', pages=None):
+        """ Index pages in a new thread
 
         Should be called from a user request. From a script, use indexPages.
         """
@@ -280,7 +282,7 @@
             return
 
         from threading import Thread
-        indexThread = Thread(target=self._index_pages, args=(self.request, files, mode))
+        indexThread = Thread(target=self._index_pages, args=(self.request, files, mode, pages))
         indexThread.setDaemon(True)
 
         # Join the index thread after current request finish, prevent
@@ -294,7 +296,7 @@
         self.request.finish = joinDecorator(self.request.finish)
         indexThread.start()
 
-    def _index_pages(self, request, files=None, mode='update'):
+    def _index_pages(self, request, files=None, mode='update', pages=None):
         """ Index all pages (and all given files)
 
         This should be called from indexPages or indexPagesInNewThread only!
@@ -306,9 +308,11 @@
         and this method must release it when it finishes or fails.
 
         @param request: current request
-        @keyword files: iterator or list of files to index additionally
-        @keyword mode: set the mode of indexing the pages, either 'update',
+        @param files: iterator or list of files to index additionally
+        @param mode: set the mode of indexing the pages, either 'update',
         'add' or 'rebuild'
+        @param pages: list of pages to index, if not given, all pages are indexed
+
         """
         raise NotImplemented('...')