~lidaobing/ubuntu-tweak/ubuntu-ubuntu-tweak

« back to all changes in this revision

Viewing changes to ubuntutweak/backends/daemon.py

  • Committer: LI Daobing
  • Date: 2010-07-31 10:10:26 UTC
  • mto: This revision was merged to the branch mainline in revision 13.
  • Revision ID: lidaobing@gmail.com-20100731101026-9o3m59258q5perbo
Tags: upstream-0.5.5
ImportĀ upstreamĀ versionĀ 0.5.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python
2
2
 
3
 
# Ubuntu Tweak - PyGTK based desktop configure tool
4
 
#
5
 
# Copyright (C) 2007-2008 TualatriX <tualatrix@gmail.com>
6
 
#
7
 
# Ubuntu Tweak is free software; you can redistribute it and/or modify
8
 
# it under the terms of the GNU General Public License as published by
9
 
# the Free Software Foundation; either version 2 of the License, or
10
 
# (at your option) any later version.
11
 
#
12
 
# Ubuntu Tweak is distributed in the hope that it will be useful,
13
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 
# GNU General Public License for more details.
16
 
#
17
 
# You should have received a copy of the GNU General Public License
18
 
# along with Ubuntu Tweak; if not, write to the Free Software Foundation, Inc.,
19
 
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 
3
# Copyright (C) 2007-2010 TualatriX <tualatrix@gmail.com>
 
4
#
 
5
# The class AptAuth is modified from softwareproperty. Author: Michael Vogt <mvo@debian.org>
 
6
# The original file is: softwareproperties/AptAuth.py
 
7
# GPL v2+
 
8
# Copyright (c) 2004 Canonical
20
9
 
21
10
import sys
22
11
reload(sys)
23
12
sys.setdefaultencoding('utf8')
24
13
import os
 
14
import apt
 
15
import fcntl
25
16
import apt_pkg
26
17
import dbus
27
18
import dbus.glib
28
19
import dbus.service
29
20
import dbus.mainloop.glib
30
21
import gobject
 
22
import gettext
31
23
import subprocess
32
24
import tempfile
 
25
import logging
33
26
 
34
27
from subprocess import PIPE
35
28
from aptsources.sourceslist import SourceEntry, SourcesList
36
29
from ubuntutweak.backends import PolicyKitService
 
30
from ubuntutweak.common.systeminfo import module_check
37
31
 
38
32
apt_pkg.init()
39
33
 
40
 
#This class is modified from softwareproperty. Author: Michael Vogt <mvo@debian.org>
 
34
log = logging.getLogger('Daemon')
 
35
 
 
36
PPA_KEY = '''-----BEGIN PGP PUBLIC KEY BLOCK-----
 
37
Version: SKS 1.0.10
 
38
 
 
39
mI0ESXTUHwEEAMtdNPmcgQcoPN3JcUcRrmdm1chJSmX6gj28OamOgE3Nxp3XgkDdg/vLFPv6
 
40
Tk8zIMxQnvuSpuG1YGp3x8atcKlQAlEHncAo27Vlio6pk8jG+qipDBKq7X7FyXE6X9Peg/k7
 
41
t7eXMLwH6ZJFN6IEmvPRTsiiQEd/dXRRuIRhPHirABEBAAG0G0xhdW5jaHBhZCBQUEEgZm9y
 
42
IFR1YWxhdHJpWIi2BBMBAgAgBQJJdNQfAhsDBgsJCAcDAgQVAggDBBYCAwECHgECF4AACgkQ
 
43
avDhlAYkoiC8mAQAmaxr4Kw/R2WZKde7MfbTPy7O9YoL/NQeThYGwxX6ICVr0IZUj9nxFQ/v
 
44
tmhZ59p53bpdR8jpPXjdDwjZIIlxTf72Fky6Ri3/zsC4YRD6idS4c4L50dTy74W6IabCt8GQ
 
45
LtJy5YASlEp5OGwRNptRSFxVE59LuOPRo2kvLIAa0Dc=
 
46
=3itC
 
47
-----END PGP PUBLIC KEY BLOCK-----'''
 
48
 
41
49
class AptAuth:
42
50
    def __init__(self):
43
51
        self.gpg = ["/usr/bin/gpg"]
90
98
    #TODO use signal
91
99
    liststate = None
92
100
    list = SourcesList()
 
101
#    cache = apt.Cache()
 
102
    PPA_URL = 'ppa.launchpad.net'
 
103
    stable_url = 'http://ppa.launchpad.net/tualatrix/ppa/ubuntu'
 
104
    ppa_list = []
 
105
    p = None
93
106
 
94
 
    def __init__ (self, bus):
 
107
    def __init__ (self, bus, mainloop):
95
108
        bus_name = dbus.service.BusName(INTERFACE, bus=bus)
96
109
        PolicyKitService.__init__(self, bus_name, PATH)
 
110
        self.mainloop = mainloop
 
111
 
 
112
    def __setup_non_block_io(self, io):
 
113
        outfd = io.fileno()
 
114
        file_flags = fcntl.fcntl(outfd, fcntl.F_GETFL)
 
115
        fcntl.fcntl(outfd, fcntl.F_SETFL, file_flags | os.O_NDELAY)
 
116
 
 
117
    @dbus.service.method(INTERFACE,
 
118
                         in_signature='sb', out_signature='b',
 
119
                         sender_keyword='sender')
 
120
    def set_source_enable(self, url, enable, sender=None):
 
121
        self._check_permission(sender)
 
122
        self.list.refresh()
 
123
 
 
124
        for source in self.list:
 
125
            if url in source.str() and source.type == 'deb':
 
126
                source.disabled = not enable
 
127
 
 
128
        self.list.save()
 
129
 
 
130
        for source in self.list:
 
131
            if url in source.str() and source.type == 'deb':
 
132
                return not source.disabled
97
133
 
98
134
    @dbus.service.method(INTERFACE,
99
135
                         in_signature='ssssb', out_signature='s',
156
192
        self.list.save()
157
193
 
158
194
    @dbus.service.method(INTERFACE,
 
195
                         in_signature='', out_signature='')
 
196
    def disable_ppa(self):
 
197
        self.list.refresh()
 
198
        self.ppa_list = []
 
199
 
 
200
        for source in self.list:
 
201
            if self.PPA_URL in source.uri and not source.disabled:
 
202
                self.ppa_list.append(source.uri)
 
203
                source.set_enabled(False)
 
204
 
 
205
        self.list.save()
 
206
 
 
207
    @dbus.service.method(INTERFACE,
 
208
                         in_signature='', out_signature='')
 
209
    def enable_ppa(self):
 
210
        self.list.refresh()
 
211
 
 
212
        for source in self.list:
 
213
            url = source.uri
 
214
            if self.PPA_URL in url and url in self.ppa_list:
 
215
                source.set_enabled(True)
 
216
 
 
217
        self.list.save()
 
218
 
 
219
    @dbus.service.method(INTERFACE,
 
220
                         in_signature='sv', out_signature='')
 
221
    def upgrade_sources(self, check_string, source_dict):
 
222
        self.list.refresh()
 
223
 
 
224
        for source in self.list:
 
225
            if source.uri in source_dict:
 
226
                source.dist = source_dict[source.uri]
 
227
                source.comment = source.comment.split(check_string)[0]
 
228
                source.set_enabled(True)
 
229
 
 
230
        self.list.save()
 
231
 
 
232
    @dbus.service.method(INTERFACE,
 
233
                         in_signature='', out_signature='')
 
234
    def enable_stable_source(self):
 
235
        self.list.refresh()
 
236
 
 
237
        for source in self.list:
 
238
            if self.stable_url in source.str() and source.type == 'deb' and not source.disabled:
 
239
                return
 
240
 
 
241
        distro = module_check.get_codename()
 
242
 
 
243
        if distro:
 
244
            self.set_separated_entry(self.stable_url, distro, 'main',
 
245
                                     'Ubuntu Tweak Stable Source', True,
 
246
                                     'ubuntu-tweak-stable')
 
247
            self.add_apt_key_from_content(PPA_KEY)
 
248
 
 
249
    @dbus.service.method(INTERFACE,
 
250
                         in_signature='', out_signature='b')
 
251
    def get_stable_source_enabled(self):
 
252
        self.list.refresh()
 
253
 
 
254
        for source in self.list:
 
255
            if self.stable_url in source.str() and source.type == 'deb' and not source.disabled:
 
256
                return True
 
257
 
 
258
        return False
 
259
 
 
260
    @dbus.service.method(INTERFACE,
159
261
                         in_signature='', out_signature='s')
160
262
    def get_list_state(self):
161
263
        if self.liststate:
171
273
        os.system('apt-get clean')
172
274
 
173
275
        return 'done'
174
 
            
 
276
 
 
277
#TODO enable it in the future for UTURL
 
278
#    @dbus.service.method(INTERFACE,
 
279
#                         in_signature='s', out_signature='b')
 
280
#    def get_package_status(self, package):
 
281
#        try:
 
282
#            pkg = self.cache[package]
 
283
#            return pkg.isInstalled
 
284
#        except Exception, e:
 
285
#            print e
 
286
#        else:
 
287
#            return False
 
288
 
175
289
    @dbus.service.method(INTERFACE,
176
290
                         in_signature='s', out_signature='s',
177
291
                         sender_keyword='sender')
216
330
        file.close()
217
331
 
218
332
    @dbus.service.method(INTERFACE,
219
 
                         in_signature='s', out_signature='s',
220
 
                         sender_keyword='sender')
221
 
    def clean_config(self, pkg, sender=None):
222
 
        self._check_permission(sender)
223
 
        return str(os.system('sudo dpkg --purge %s' % pkg))
224
 
 
225
 
    @dbus.service.method(INTERFACE,
226
 
                         in_signature='s', out_signature='',
227
 
                         sender_keyword='sender')
228
 
    def add_apt_key(self, filename, sender=None):
229
 
        self._check_permission(sender)
230
 
        apt_key = AptAuth()
231
 
        apt_key.add(filename)
 
333
                         in_signature='as', out_signature='',
 
334
                         sender_keyword='sender')
 
335
    def clean_configs(self, pkgs, sender=None):
 
336
        self._check_permission(sender)
 
337
        cmd = ['sudo', 'dpkg', '--purge']
 
338
        cmd.extend(pkgs)
 
339
        self.p = subprocess.Popen(cmd, stdout=PIPE)
 
340
        self.__setup_non_block_io(self.p.stdout)
 
341
 
 
342
    @dbus.service.method(INTERFACE,
 
343
                         in_signature='as', out_signature='',
 
344
                         sender_keyword='sender')
 
345
    def install_select_pkgs(self, pkgs, sender=None):
 
346
        self._check_permission(sender)
 
347
        cmd = ['sudo', 'apt-get', '-y', '--force-yes', 'install']
 
348
        cmd.extend(pkgs)
 
349
        log.debug("The install command is %s" % ' '.join(cmd))
 
350
        self.p = subprocess.Popen(cmd, stdout=PIPE)
 
351
        self.__setup_non_block_io(self.p.stdout)
 
352
 
 
353
    @dbus.service.method(INTERFACE,
 
354
                         in_signature='', out_signature='v')
 
355
    def get_cmd_pipe(self):
 
356
        if self.p:
 
357
            terminaled = self.p.poll()
 
358
            if terminaled == None:
 
359
                try:
 
360
                    return self.p.stdout.readline(), str(terminaled)
 
361
                except:
 
362
                    return '', 'None'
 
363
            else:
 
364
                strings, returncode = ''.join(self.p.stdout.readlines()), str(terminaled)
 
365
                self.p = None
 
366
                return strings, returncode
 
367
        else:
 
368
            return '', 'None'
232
369
 
233
370
    @dbus.service.method(INTERFACE,
234
371
                         in_signature='s', out_signature='',
267
404
        return True
268
405
 
269
406
    @dbus.service.method(INTERFACE,
 
407
                         in_signature='s', out_signature='s',
 
408
                         sender_keyword='sender')
 
409
    def exec_command(self, command, sender=None):
 
410
        self._check_permission(sender)
 
411
        cmd = os.popen(command)
 
412
        return cmd.read().strip()
 
413
 
 
414
    @dbus.service.method(INTERFACE,
 
415
                         in_signature='s', out_signature='s')
 
416
    def get_as_tempfile(self, path):
 
417
        f = tempfile.NamedTemporaryFile()
 
418
        new_path = f.name
 
419
        f.close()
 
420
        os.popen('cp %s %s' % (path, new_path))
 
421
        return new_path
 
422
 
 
423
    @dbus.service.method(INTERFACE,
 
424
                         in_signature='ss', out_signature='s')
 
425
    def get_user_gconf(self, user, key):
 
426
        command = 'sudo -u %s gconftool-2 --get %s' % (user, key)
 
427
        cmd = os.popen(command)
 
428
        return cmd.read().strip()
 
429
 
 
430
    @dbus.service.method(INTERFACE,
 
431
                         in_signature='sssss', out_signature='s',
 
432
                         sender_keyword='sender')
 
433
    def set_user_gconf(self, user, key, value, type, list_type='', sender=None):
 
434
        self._check_permission(sender)
 
435
        command = 'sudo -u %s gconftool-2 --type %s' % (user, type)
 
436
        if list_type == '':
 
437
            command = '%s --set %s %s' % (command, key, value)
 
438
        else:
 
439
            command = '%s --type %s --list-type %s --set %s %s' % (command,
 
440
                                                                   list_type,
 
441
                                                                   key, value)
 
442
        cmd = os.popen(command)
 
443
        return cmd.read().strip()
 
444
 
 
445
    @dbus.service.method(INTERFACE,
 
446
                         in_signature='s', out_signature='s')
 
447
    def get_system_gconf(self, key):
 
448
        command = 'gconftool-2 --direct --config-source xml:readwrite:/etc/gconf/gconf.xml.defaults --get %s' % key
 
449
        cmd = os.popen(command)
 
450
        return cmd.read().strip()
 
451
 
 
452
    @dbus.service.method(INTERFACE,
 
453
                         in_signature='ssss', out_signature='s',
 
454
                         sender_keyword='sender')
 
455
    def set_system_gconf(self, key, value, type, list_type='', sender=None):
 
456
        self._check_permission(sender)
 
457
        if list_type == '':
 
458
            command = 'gconftool-2 --direct --config-source xml:readwrite:/etc/gconf/gconf.xml.defaults --type %s --set %s %s' % (type, key, value)
 
459
        else:
 
460
            command = 'gconftool-2 --direct --config-source xml:readwrite:/etc/gconf/gconf.xml.defaults --type %s --list-type %s --set %s %s' % (type, list_type, key, value)
 
461
        cmd = os.popen(command)
 
462
        return cmd.read().strip()
 
463
 
 
464
    @dbus.service.method(INTERFACE,
270
465
                         in_signature='', out_signature='')
271
466
    def exit(self):
272
 
        mainloop.quit()
 
467
        self.mainloop.quit()
273
468
 
274
469
if __name__ == '__main__':
275
470
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
276
 
    Daemon(dbus.SystemBus())
277
 
 
278
471
    mainloop = gobject.MainLoop()
 
472
    Daemon(dbus.SystemBus(), mainloop)
279
473
    mainloop.run()