~aaronp/software-center/enhance-usefulness

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
#!/usr/bin/python
# Copyright (C) 2009 Canonical
#
# Authors:
#  Michael Vogt
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; version 3.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA


import apt
import locale
import gettext
import logging
import os
import os.path
import sys
import time
import xapian

from optparse import OptionParser

from softwarecenter.enums import *
from softwarecenter.paths import XAPIAN_BASE_PATH
from softwarecenter.db.update import rebuild_database

# dbus may not be available during a upgrade so we 
# only set it up if its there
try:
    import dbus
    import dbus.service
    import glib
    import gobject # without gobject it will crash
    from dbus.mainloop.glib import DBusGMainLoop
except ImportError:
    pass

# add a bit of extra time between sending the "we-rebuild-the-db-now"
# signal and the actual rebuilding to help the applications to shutdown
# the DB access
APP_CATCHUP_DELAY = 2

# the language pack directory that we need for the triggers checking
LANGPACKDIR = "/usr/share/locale-langpack"

LOG = logging.getLogger(__name__)

class UpdateSoftwarecenterDbus(dbus.service.Object):
    """ 
    This is a helper to provide the UpdateSoftwarecenterIFace
    """
    def __init__(self, bus_name,
                 object_path='/com/ubuntu/Softwarecenter'):
        dbus.service.Object.__init__(self, bus_name, object_path)

    @dbus.service.method('com.ubuntu.Softwarecenter')
    def IsRebuilding(self):
        return True
    @dbus.service.signal(dbus_interface='com.ubuntu.Softwarecenter',
                         signature='b')
    def DatabaseRebuilding(self, isRebuilding):
        LOG.debug("Sending DatabaseRebuilding signal '%s'" % isRebuilding)


if __name__ == "__main__":
    try:
        locale.setlocale(locale.LC_ALL, "")
    except Exception, e:
        LOG.warn("setlocale failed with '%s'" % e) 

    # parser
    parser = OptionParser()
    parser.add_option("--triggered", "", default="",
                      help="triggered from dpkg")
    parser.add_option("--debug", "", action="store_true", default=False,
                      help="show debug output")
    (options, args) = parser.parse_args()

    #logging.basicConfig(level=logging.INFO)
    if options.debug:
        LOG.setLevel(logging.DEBUG)

    # check if we are dpkg triggered because of langpack change
    # and avoid unneeded database rebuilds by checking the timestamp
    # of the app-install-data mo file
    if options.triggered and options.triggered == LANGPACKDIR:
        LOG.debug("triggered with '%s'" % options.triggered)
        mofile = gettext.find("app-install-data")
        if not mofile:
            LOG.info("no translation information in database needed")
            sys.exit(0)
        mo_time = os.path.getctime(mofile)
        pathname = os.path.join(XAPIAN_BASE_PATH, "xapian")
        if os.path.exists(pathname):
            db = xapian.Database(pathname)
            mo_db_time = db.get_metadata("app-install-mo-time")
            LOG.debug("mo_time: %s db_mo_time: %s" % (mo_time, mo_db_time))
            if str(mo_time) == mo_db_time:
                LOG.info("translation information in database is up-to-date")
                sys.exit(0)

    # setup dbus
    dbus_controller = None
    try:
        DBusGMainLoop(set_as_default=True)
        bus = dbus.SystemBus()
        bus_name = dbus.service.BusName('com.ubuntu.Softwarecenter', bus)
        dbus_controller = UpdateSoftwarecenterDbus(bus_name)
        dbus_controller.DatabaseRebuilding(True)
        time.sleep(APP_CATCHUP_DELAY)
    except:
        LOG.warn("Failed to setup dbus (ignoring)")

    # rebuild and send signal when done
    try:
        # setup path
        pathname = os.path.join(XAPIAN_BASE_PATH, "xapian")
        if not os.path.exists(pathname):
            os.makedirs(pathname)
        # rebuild the database, the default context is run to ensure
        # dbus querries are processed
        print "Updating software catalog...this may take a moment."
        if rebuild_database(pathname):
            print "Software catalog update was successful."
        else:
            print "There was a problem updating the software catalog. Please try again or check the log."
    finally:
        # signal that the xapian db is valid again
        if dbus_controller:
            time.sleep(APP_CATCHUP_DELAY)
            dbus_controller.DatabaseRebuilding(False)
            context = glib.main_context_default()
            while context.pending():
                context.iteration()