Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/bin/index-service
blob: a2ff83c54da2986b8d8bc6a553fc36a7a4d2f655 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python

""" Async index service for the Datastore.

Subscribes to the create/update/delete messages of the Datastore and
performs the indexing. When this service is enabled the Datastore
access the Xapian repository in read only mode.
"""


try: from ore.main import Application
except ImportError: Application = object

from olpc.datastore.datastore import DS_SERVICE, DS_OBJECT_PATH
from olpc.datastore.datastore import DS_DBUS_INTERFACE
from olpc.datastore.indexer import Indexer
import dbus
import dbus.mainloop.glib
import logging
import sys
import os
import signal

profile = os.environ.get('SUGAR_PROFILE', 'default')
base_dir = os.path.join(os.path.expanduser('~'), '.sugar', profile)
repo_dir = os.path.join(base_dir, 'datastore')
fulltext_dir = os.path.join(repo_dir, 'fulltext')

log_dir = os.path.join(base_dir, "logs")
if not os.path.exists(log_dir): os.makedirs(log_dir)

os.chdir(repo_dir)

# setup logger
filename = None
if not sys.stdin.isatty():
    filename = os.path.join(log_dir, "indexer.log")
logging.basicConfig(level=logging.DEBUG,
                    format="%(asctime)-15s %(levelname)s: %(message)s",
                    filename = filename,
                    )

logger = logging.getLogger('org.laptop.sugar.Indexer')
logger.setLevel(logging.DEBUG)

class IndexService(Application):
    def manage_options(self):
        self.parser.add_option("--olpc.fulltext.repo",
                               dest="fulltext_dir",
                               action="store", default='fulltext',
                               help="""Location of the FullText Repository""")


    def main(self):
        logging.debug('Starting the index service at %s' % self.options.fulltext_dir)
        dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
        bus = dbus.SessionBus()
        self.fulltext = Indexer(self.options.fulltext_dir)
        self.fulltext.use_fulltext = True
        
        ds = bus.get_object(DS_SERVICE, DS_OBJECT_PATH)
        self.ds = dbus.Interface(ds, dbus_interface=DS_DBUS_INTERFACE)

        self.ds.connect_to_signal("Created", self.created,
                                  dbus_interface=DS_DBUS_INTERFACE)

        self.ds.connect_to_signal("Updated", self.updated,
                                  dbus_interface=DS_DBUS_INTERFACE)

        self.ds.connect_to_signal("Deleted", self.deleted,
                                  dbus_interface=DS_DBUS_INTERFACE)


        self.ds.connect_to_signal("Stopped", self.stopped,
                                  dbus_interface=DS_DBUS_INTERFACE)

        self.eventloop.run()

    def get_textprops(self, uid):
        # text properties also get full text indexing
        # currently this is still searched with the 'fulltext'
        # parameter of find()
        textprops = {}
        for k,v in self.ds.get_properties(uid, dict(type='text')).items():
            textprops[str(k)] = v and str(v) or ''
        return textprops

    def created(self, uid):
        """An object was created on the bus and we want to index it"""
        # because the file isn't encoded anywhere accessible in the
        # create call we must actually get the filename and trigger
        # the indexing on that
        filename = self.ds.get_filename(uid)
        r = None
        if filename:
            mime_type = self.ds.get_properties(uid, {}).get('mime_type', None)
            r = self.fulltext.fulltext_index(uid, filename, mime_type,
                                             self.get_textprops(uid))
        if r is True:
            logger.debug("index creation of %s" % uid)
        elif r is False:
            logger.debug("unable to index creation of  %s" % uid)
        else:
            logger.debug("nothing to index on creation of %s" % uid)
            
    def updated(self, uid):
        """An object was updated on the bus and we want to index it"""
        # because the file isn't encoded anywhere accessible in the
        # create call we must actually get the filename and trigger
        # the indexing on that
        filename = self.ds.get_filename(uid)
        r = None
        if filename:
            mime_type = self.ds.get_properties(uid, {}).get('mime_type',
                                                        None)
            r = self.fulltext.fulltext_index(uid, filename, mime_type,
                                             self.get_textprops(uid))
        if r is True:
            logger.debug("index update of %s" % uid)
        elif r is False:
            logger.debug("unable to index update of  %s" % uid)
        else:
            logger.debug("nothing to index on update of %s" % uid)
            
        
    def deleted(self, uid):
        """An object was updated on the bus and we want to index it"""
        # because the file isn't encoded anywhere accessible in the
        # create call we must actually get the filename and trigger
        # the indexing on that
        try:
            self.fulltext.fulltext_unindex(uid)
            logger.debug("unindex deletion  of %s" % uid);
        except KeyError: pass

        
    def stopped(self):
        """Respond to the datastore being stopped by shutting down
        ourselves"""
        self.fulltext.stop()
        self.eventloop.quit()


if __name__ == "__main__":
    def handle_shutdown(signum, frame):
        idx.stopped()
        print "shutdown cleanly"
        raise SystemExit("Shutting down on signal %s" % signum)

    signal.signal(signal.SIGHUP, handle_shutdown)
    signal.signal(signal.SIGTERM, handle_shutdown)

    idx = IndexService()
    #idx()
    # w/o ore.main

    import gobject 
    idx.eventloop = gobject.MainLoop()
    class options(object): pass
    o = options()
    o.fulltext_dir = 'fulltext'
    idx.options = o
    try:
        idx.main()
    except:
        # force logging this one
        logger.setLevel(logging.DEBUG)
        logger.debug("Problem in index service",
                     exc_info=sys.exc_info())
        idx.stopped()