~aaronp/software-center/lp790450-for-4.0

« back to all changes in this revision

Viewing changes to utils/update-app-install

  • Committer: Michael Vogt
  • Date: 2009-07-27 14:49:44 UTC
  • Revision ID: michael.vogt@ubuntu.com-20090727144944-178z479wps3m907e
initial version of update-app-install based on xapian

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
import logging
 
4
import os
 
5
import xapian
 
6
 
 
7
from ConfigParser import RawConfigParser, NoOptionError
 
8
from glob import glob
 
9
 
 
10
XAPIAN_DATA_ICON = 0
 
11
 
 
12
class DesktopConfigParser(RawConfigParser):
 
13
    " thin wrapper that is tailored for xdg Desktop files "
 
14
    def get_desktop(self, key):
 
15
        " get generic option under 'Desktop Entry'"
 
16
        return self.get("Desktop Entry", key)
 
17
    def has_option_desktop(self, key):
 
18
        " test if there is the option under 'Desktop Entry'"
 
19
        return self.has_option("Desktop Entry", key)
 
20
    def get_desktop_categories(self):
 
21
        " get the list of categories for the desktop file "
 
22
        categories = []
 
23
        try:
 
24
            categories_str = self.get_desktop("Categories")
 
25
            for item in categories_str.split(";"):
 
26
                if item:
 
27
                    categories.append(item)
 
28
        except NoOptionError:
 
29
            pass
 
30
        return categories
 
31
 
 
32
def update(db, datadir="/usr/share/app-install/"):
 
33
    " index the desktop files in $datadir/desktop/*.desktop "
 
34
    term_generator = xapian.TermGenerator()
 
35
    for desktopf in glob(datadir+"/desktop/*.desktop"):
 
36
        logging.debug("processing %s" % desktopf)
 
37
        parser = DesktopConfigParser()
 
38
        doc = xapian.Document()
 
39
        term_generator.set_document(doc)
 
40
        try:
 
41
            parser.read(desktopf)
 
42
            # special terms
 
43
            name = parser.get_desktop("Name")
 
44
            doc.set_data(name)
 
45
            doc.add_term("XA"+name)
 
46
            pkgname = parser.get_desktop("X-AppInstall-Package")
 
47
            doc.add_term("XP"+pkgname)
 
48
            if parser.has_option_desktop("Icon"):
 
49
                icon = parser.get_desktop("Icon")
 
50
                doc.add_value(XAPIAN_DATA_ICON, icon)
 
51
            for cat in parser.get_desktop_categories():
 
52
                doc.add_term("XC"+cat.lower())
 
53
            # now add search data from the desktop file
 
54
            for key in ["Name","Generic Name","Comment"]:
 
55
                if not parser.has_option_desktop(key):
 
56
                    continue
 
57
                s = parser.get_desktop(key)
 
58
                term_generator.index_text_without_positions(s)
 
59
            # FIXME: now do the same for the localizations in the
 
60
            #        desktop file
 
61
        except Exception, e:
 
62
            logging.warn("error processing: %s %s" % (desktopf, e))
 
63
            continue
 
64
        # now add it
 
65
        db.add_document(doc)
 
66
    return True
 
67
 
 
68
if __name__ == "__main__":
 
69
    #logging.basicConfig(level=logging.DEBUG)
 
70
    desktop_base_path = "/usr/share/app-install"
 
71
    xapian_base_path = "/var/cache/app-install"
 
72
    
 
73
    pathname = os.path.join(xapian_base_path, "xapian")
 
74
    db = xapian.WritableDatabase(pathname, xapian.DB_CREATE_OR_OVERWRITE)
 
75
    update(db, desktop_base_path)