pamac-classic/pamac/pamac-daemon.py

326 lines
9.1 KiB
Python
Raw Normal View History

#! /usr/bin/python
# -*-coding:utf-8-*-
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
2013-01-20 13:38:33 -03:00
from gi.repository import GObject
import pyalpm
import traceback
2013-01-16 15:09:52 -03:00
from pamac import config, common
2013-01-20 13:38:33 -03:00
class PamacDBusService(dbus.service.Object):
def __init__(self):
bus=dbus.SystemBus()
bus_name = dbus.service.BusName('org.manjaro.pamac', bus)
dbus.service.Object.__init__(self, bus_name, '/org/manjaro/pamac')
self.t = None
self.error = ''
self.warning = ''
2013-01-23 11:12:11 -03:00
self.action = 'Preparing...'
self.icon = '/usr/share/pamac/icons/24x24/status/setup.png'
2013-01-20 13:38:33 -03:00
self.target = ''
self.percent = 0
self.total_size = 0
self.already_transferred = 0
2013-01-23 11:12:11 -03:00
config.handle.dlcb = self.cb_dl
config.handle.totaldlcb = self.totaldlcb
config.handle.eventcb = self.cb_event
config.handle.questioncb = self.cb_conv
config.handle.progresscb = self.cb_progress
config.handle.logcb = self.cb_log
2013-01-16 15:09:52 -03:00
2013-01-20 13:38:33 -03:00
@dbus.service.signal('org.manjaro.pamac')
def EmitAction(self, action):
pass
2013-01-16 15:09:52 -03:00
2013-01-20 13:38:33 -03:00
@dbus.service.signal('org.manjaro.pamac')
def EmitIcon(self, icon):
pass
2013-01-16 15:09:52 -03:00
2013-01-20 13:38:33 -03:00
@dbus.service.signal('org.manjaro.pamac')
def EmitTarget(self, target):
pass
2013-01-16 15:09:52 -03:00
2013-01-20 13:38:33 -03:00
@dbus.service.signal('org.manjaro.pamac')
def EmitPercent(self, percent):
pass
2013-01-16 15:09:52 -03:00
2013-01-20 13:38:33 -03:00
def cb_event(self, ID, event, tupel):
if ID is 1:
2013-01-26 12:10:11 -03:00
self.action = 'Checking dependencies...'
2013-01-20 13:38:33 -03:00
self.icon = '/usr/share/pamac/icons/24x24/status/package-search.png'
elif ID is 3:
2013-01-26 12:10:11 -03:00
self.action = 'Checking file conflicts...'
2013-01-20 13:38:33 -03:00
self.icon = '/usr/share/pamac/icons/24x24/status/package-search.png'
elif ID is 5:
2013-01-26 12:10:11 -03:00
self.action = 'Resolving dependencies...'
2013-01-20 13:38:33 -03:00
self.icon = '/usr/share/pamac/icons/24x24/status/setup.png'
elif ID is 7:
2013-01-26 12:10:11 -03:00
self.action = 'Checking inter conflicts...'
2013-01-20 13:38:33 -03:00
self.icon = '/usr/share/pamac/icons/24x24/status/package-search.png'
elif ID is 9:
2013-01-26 12:10:11 -03:00
self.action = 'Installing...'
2013-01-20 13:38:33 -03:00
self.icon = '/usr/share/pamac/icons/24x24/status/package-add.png'
elif ID is 11:
2013-01-26 12:10:11 -03:00
self.action = 'Removing...'
2013-01-20 13:38:33 -03:00
self.icon = '/usr/share/pamac/icons/24x24/status/package-delete.png'
elif ID is 13:
2013-01-26 12:10:11 -03:00
self.action = 'Upgrading...'
2013-01-20 13:38:33 -03:00
self.icon = '/usr/share/pamac/icons/24x24/status/package-update.png'
elif ID is 15:
2013-01-26 12:10:11 -03:00
self.action = 'Checking integrity...'
2013-01-20 13:38:33 -03:00
self.icon = '/usr/share/pamac/icons/24x24/status/package-search.png'
self.already_transferred = 0
elif ID is 17:
2013-02-10 11:59:53 -03:00
self.action = 'Loading packages files...'
2013-01-20 13:38:33 -03:00
self.icon = '/usr/share/pamac/icons/24x24/status/package-search.png'
2013-02-10 11:59:53 -03:00
print('Loading packages files')
2013-01-26 12:10:11 -03:00
elif ID is 26:
self.action = 'Configuring...'
self.icon = '/usr/share/pamac/icons/24x24/status/setup.png'
print('Configuring a package')
2013-01-20 13:38:33 -03:00
elif ID is 27:
print('Downloading a file')
else :
self.action = ''
self.EmitTarget('')
self.EmitPercent(str(0))
self.EmitAction(self.action)
self.EmitIcon(self.icon)
print(ID,event)
2013-01-16 15:09:52 -03:00
2013-01-20 13:38:33 -03:00
def cb_conv(self, *args):
print("conversation", args)
2013-01-16 15:09:52 -03:00
2013-01-20 13:38:33 -03:00
def cb_log(self, level, line):
#global t
_logmask = pyalpm.LOG_ERROR | pyalpm.LOG_WARNING
if not (level & _logmask):
return
if level & pyalpm.LOG_ERROR:
self.error = "ERROR: "+line
#t.release()
elif level & pyalpm.LOG_WARNING:
self.warning = "WARNING: "+line
elif level & pyalpm.LOG_DEBUG:
line = "DEBUG: " + line
print(line)
elif level & pyalpm.LOG_FUNCTION:
line = "FUNC: " + line
print(line)
2013-01-16 15:09:52 -03:00
2013-01-20 13:38:33 -03:00
def totaldlcb(self, _total_size):
self.total_size = _total_size
2013-01-16 15:09:52 -03:00
2013-01-20 13:38:33 -03:00
def cb_dl(self, _target, _transferred, total):
if self.total_size > 0:
fraction = (_transferred+self.already_transferred)/self.total_size
size = 0
if (t.to_remove or t.to_add):
for pkg in t.to_remove+t.to_add:
if pkg.name+'-'+pkg.version in _target:
size = pkg.size
if _transferred == size:
self.already_transferred += size
self.action = 'Downloading '+common.format_size(self.total_size)
self.target = _target
2013-01-23 11:12:11 -03:00
if fraction > 1:
self.percent = 0
else:
self.percent = fraction
2013-01-20 13:38:33 -03:00
self.icon = '/usr/share/pamac/icons/24x24/status/package-download.png'
else:
self.action = 'Refreshing...'
self.target = _target
self.percent = 0
self.icon = '/usr/share/pamac/icons/24x24/status/refresh-cache.png'
self.EmitAction(self.action)
self.EmitIcon(self.icon)
self.EmitTarget(self.target)
self.EmitPercent(str(self.percent))
2013-01-16 15:09:52 -03:00
2013-01-20 13:38:33 -03:00
def cb_progress(self, _target, _percent, n, i):
self.target = _target+' ('+str(i)+'/'+str(n)+')'
self.percent = _percent/100
self.EmitTarget(self.target)
self.EmitPercent(str(self.percent))
2013-01-20 13:38:33 -03:00
def policykit_test(self, sender,connexion, action):
bus = dbus.SystemBus()
proxy_dbus = connexion.get_object('org.freedesktop.DBus','/org/freedesktop/DBus/Bus', False)
dbus_info = dbus.Interface(proxy_dbus,'org.freedesktop.DBus')
sender_pid = dbus_info.GetConnectionUnixProcessID(sender)
proxy_policykit = bus.get_object('org.freedesktop.PolicyKit1','/org/freedesktop/PolicyKit1/Authority',False)
policykit_authority = dbus.Interface(proxy_policykit,'org.freedesktop.PolicyKit1.Authority')
Subject = ('unix-process', {'pid': dbus.UInt32(sender_pid, variant_level=1),
'start-time': dbus.UInt64(0, variant_level=1)})
(is_authorized,is_challenge,details) = policykit_authority.CheckAuthorization(Subject, action, {'': ''}, dbus.UInt32(1), '')
return is_authorized
2013-01-20 13:38:33 -03:00
@dbus.service.method('org.manjaro.pamac', '', 's')
def Refresh(self):
global t
global error
error = ''
for db in config.handle.get_syncdbs():
try:
t = config.handle.init_transaction()
2013-01-23 11:12:11 -03:00
db.update(force=False)
2013-01-20 13:38:33 -03:00
t.release()
except pyalpm.error:
error = traceback.format_exc()
break
return error
2013-02-11 11:45:24 -03:00
@dbus.service.signal('org.manjaro.pamac')
def EmitAvailableUpdates(self, available_updates):
pass
@dbus.service.method('org.manjaro.pamac', '', '')
def CheckUpdates(self):
for pkg in config.handle.get_localdb().pkgcache:
candidate = pyalpm.sync_newversion(pkg, config.handle.get_syncdbs())
if candidate:
2013-02-16 13:27:46 -03:00
self.EmitAvailableUpdates(True)
2013-02-11 11:45:24 -03:00
return
@dbus.service.method('org.manjaro.pamac', 'a{sb}', 's', sender_keyword='sender', connection_keyword='connexion')
def Init(self, options, sender=None, connexion=None):
global t
global error
if self.policykit_test(sender,connexion,'org.manjaro.pamac.init_release'):
error = ''
try:
2013-01-16 15:09:52 -03:00
t = config.handle.init_transaction(**options)
print('Init:',t.flags)
except pyalpm.error:
error = traceback.format_exc()
finally:
return error
else :
return 'You are not authorized'
2013-01-20 13:38:33 -03:00
@dbus.service.method('org.manjaro.pamac', '', 's')
def Sysupgrade(self):
global t
global error
error = ''
try:
t.sysupgrade(downgrade=False)
2013-02-06 12:09:43 -03:00
print('to_upgrade:',t.to_add)
2013-01-20 13:38:33 -03:00
except pyalpm.error:
error = traceback.format_exc()
finally:
return error
2013-01-13 12:37:17 -03:00
@dbus.service.method('org.manjaro.pamac', 's', 's')
def Remove(self, pkgname):
global t
global error
error = ''
try:
2013-01-16 15:09:52 -03:00
pkg = config.handle.get_localdb().get_pkg(pkgname)
if pkg is not None:
t.remove_pkg(pkg)
except pyalpm.error:
error = traceback.format_exc()
finally:
return error
2013-01-13 12:37:17 -03:00
@dbus.service.method('org.manjaro.pamac', 's', 's')
def Add(self, pkgname):
global t
global error
error = ''
try:
2013-01-16 15:09:52 -03:00
for repo in config.handle.get_syncdbs():
pkg = repo.get_pkg(pkgname)
if pkg:
t.add_pkg(pkg)
break
except pyalpm.error:
error = traceback.format_exc()
finally:
return error
2013-01-13 12:37:17 -03:00
@dbus.service.method('org.manjaro.pamac', '', 's')
def Prepare(self):
global t
global error
error = ''
try:
t.prepare()
2013-01-16 15:09:52 -03:00
print('to_add:',t.to_add)
print('to_remove:',t.to_remove)
except pyalpm.error:
error = traceback.format_exc()
finally:
return error
2013-01-13 12:37:17 -03:00
@dbus.service.method('org.manjaro.pamac', '', 'as')
def To_Remove(self):
global t
liste = []
for pkg in t.to_remove:
liste.append(pkg.name)
return liste
2013-01-13 12:37:17 -03:00
@dbus.service.method('org.manjaro.pamac', '', 'as')
def To_Add(self):
global t
liste = []
for pkg in t.to_add:
liste.append(pkg.name)
return liste
2013-01-20 13:38:33 -03:00
@dbus.service.method('org.manjaro.pamac', '', 's', sender_keyword='sender', connection_keyword='connexion')#, async_callbacks=('success', 'nosuccess'))
def Commit(self, sender=None, connexion=None):#success, nosuccess, sender=None, connexion=None):
global t
global error
2013-01-20 13:38:33 -03:00
error = ''
2013-01-16 15:09:52 -03:00
if self.policykit_test(sender,connexion,'org.manjaro.pamac.commit'):
try:
t.commit()
2013-01-20 13:38:33 -03:00
#success('')
except pyalpm.error:
2013-01-20 13:38:33 -03:00
error = traceback.format_exc()
#nosuccess(error)
except dbus.exceptions.DBusException:
pass
finally:
return error
else :
return 'You are not authorized'
@dbus.service.method('org.manjaro.pamac', '', 's', sender_keyword='sender', connection_keyword='connexion')
def Release(self, sender=None, connexion=None):
global t
global error
if self.policykit_test(sender,connexion,'org.manjaro.pamac.init_release'):
error = ''
try:
t.release()
except pyalpm.error:
error = traceback.format_exc()
finally:
return error
else :
return 'You are not authorized'
2013-01-13 12:37:17 -03:00
@dbus.service.method('org.manjaro.pamac')
def StopDaemon(self):
2013-02-11 11:45:24 -03:00
global t
try:
t.release()
except:
pass
2013-01-20 13:38:33 -03:00
mainloop.quit()
DBusGMainLoop(set_as_default=True)
myservice = PamacDBusService()
2013-01-20 13:38:33 -03:00
mainloop = GObject.MainLoop()
mainloop.run()