~ubuntu-branches/ubuntu/raring/apt-xapian-index/raring-proposed

« back to all changes in this revision

Viewing changes to update-apt-xapian-index-dbus

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2010-03-30 14:46:08 UTC
  • Revision ID: james.westby@ubuntu.com-20100330144608-3q3s62uvzbs4uzac
Tags: 0.25ubuntu1
export update-apt-xapian-index as dbus service for integration with
tools like software-center or packagekit (part of the
software-center-non-applications spec)

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 string
 
6
import subprocess
 
7
 
 
8
try:
 
9
    import glib
 
10
    import gobject
 
11
    import dbus
 
12
    import dbus.service
 
13
    import dbus.mainloop.glib
 
14
except ImportError, e:
 
15
    sys.stderr.write("Failed to import '%s', can not use dbus" % e)
 
16
    sys.exit(1)
 
17
 
 
18
class PermissionDeniedError(dbus.DBusException):
 
19
    " permission denied by policy "
 
20
    pass
 
21
 
 
22
class AptXapianIndexDBusService(dbus.service.Object): 
 
23
    DBUS_INTERFACE_NAME = "org.debian.AptXapianIndex"
 
24
 
 
25
    def __init__(self):
 
26
        bus_name = dbus.service.BusName(self.DBUS_INTERFACE_NAME,
 
27
                                        bus=dbus.SystemBus())
 
28
        dbus.service.Object.__init__(self, bus_name, '/')
 
29
 
 
30
    def _authWithPolicyKit(self, sender, connection, priv):
 
31
        system_bus = dbus.SystemBus()
 
32
        obj = system_bus.get_object("org.freedesktop.PolicyKit1", 
 
33
                                    "/org/freedesktop/PolicyKit1/Authority", 
 
34
                                    "org.freedesktop.PolicyKit1.Authority")
 
35
        policykit = dbus.Interface(obj, "org.freedesktop.PolicyKit1.Authority")
 
36
        info = dbus.Interface(connection.get_object('org.freedesktop.DBus',
 
37
                                              '/org/freedesktop/DBus/Bus', 
 
38
                                              False), 
 
39
                              'org.freedesktop.DBus')
 
40
        pid = info.GetConnectionUnixProcessID(sender) 
 
41
        subject = ('unix-process', 
 
42
                   { 'pid' : dbus.UInt32(pid, variant_level=1),
 
43
                     'start-time' : 0,
 
44
                   }
 
45
                  )
 
46
        details = { '' : '' }
 
47
        flags = dbus.UInt32(1) #   AllowUserInteraction = 0x00000001
 
48
        cancel_id = ''
 
49
        (ok, notused, details) = policykit.CheckAuthorization(
 
50
            subject, priv, details, flags, cancel_id)
 
51
        return ok
 
52
 
 
53
    @dbus.service.signal(dbus_interface=DBUS_INTERFACE_NAME,
 
54
                         signature="b")
 
55
    def UpdateFinished(self, res):
 
56
        logging.debug("Emitting UpdateFinished: %s" % res)
 
57
 
 
58
    @dbus.service.signal(dbus_interface=DBUS_INTERFACE_NAME,
 
59
                         signature="i")
 
60
    def UpdateProgress(self, percent):
 
61
        logging.debug("Emitting UpdateProgress: %s" % percent)
 
62
 
 
63
    def _update_apt_xapian_index(self, cmd):
 
64
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
 
65
        while True:
 
66
            while gobject.main_context_default().pending():
 
67
                 gobject.main_context_default().iteration()
 
68
            res = p.poll()
 
69
            if res is not None:
 
70
                break
 
71
            line = p.stdout.readline().strip()
 
72
            if not line:
 
73
                continue
 
74
            try:
 
75
                (op, progress) = string.split(line, sep=":", maxsplit=1)
 
76
                if op == "progress":
 
77
                    percent = int(progress.split("/")[0])
 
78
                    self.UpdateProgress(percent)
 
79
            except ValueError:
 
80
                pass
 
81
        # emit finish signal
 
82
        self.UpdateFinished(res == 0)
 
83
 
 
84
    @dbus.service.method(DBUS_INTERFACE_NAME,
 
85
                         in_signature='bb', 
 
86
                         out_signature='',
 
87
                         sender_keyword='sender',
 
88
                         connection_keyword='conn')
 
89
    def update_async(self, force, update_only, sender=None, conn=None):
 
90
        if not self._authWithPolicyKit(sender, conn, 
 
91
                                       "org.debian.aptxapianindex.update"):
 
92
            raise PermissionDeniedError, "Permission denied by policy"
 
93
        cmd = ["/usr/sbin/update-apt-xapian-index", "--batch-mode"]
 
94
        if force:
 
95
            cmd.append("--force")
 
96
        if update_only:
 
97
            cmd.append("--update")
 
98
        glib.timeout_add(100, self._update_apt_xapian_index, cmd)
 
99
 
 
100
if __name__ == "__main__":
 
101
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) 
 
102
    server = AptXapianIndexDBusService()
 
103
    gobject.MainLoop().run()
 
104