pamac-classic/pamac-daemon.py

479 lines
15 KiB
Python
Raw Normal View History

#! /usr/bin/python3
2013-03-31 14:31:33 -03:00
# -*- coding:utf-8 -*-
2013-02-27 11:15:51 -03:00
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
from gi.repository import GObject
import pyalpm
2013-03-15 13:15:34 -03:00
from multiprocessing import Process
2013-02-27 11:15:51 -03:00
from pamac import config, common
2013-03-31 14:31:33 -03:00
# i18n
import gettext
gettext.bindtextdomain('pamac', '/usr/share/locale')
gettext.textdomain('pamac')
_ = gettext.gettext
2013-02-27 11:15:51 -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
2013-03-15 13:15:34 -03:00
self.task = None
2013-02-27 11:15:51 -03:00
self.error = ''
self.warning = ''
self.previous_action = ''
2013-03-31 14:31:33 -03:00
self.action = _('Preparing')+'...'
self.previous_icon = ''
2013-02-27 11:15:51 -03:00
self.icon = '/usr/share/pamac/icons/24x24/status/setup.png'
self.previous_target = ''
2013-02-27 11:15:51 -03:00
self.target = ''
self.previous_percent = 0
2013-02-27 11:15:51 -03:00
self.percent = 0
self.total_size = 0
self.already_transferred = 0
2013-03-18 11:29:20 -03:00
self.handle = config.handle()
def get_handle(self):
print('daemon get handle')
self.handle = config.handle()
self.handle.dlcb = self.cb_dl
self.handle.totaldlcb = self.totaldlcb
self.handle.eventcb = self.cb_event
self.handle.questioncb = self.cb_conv
self.handle.progresscb = self.cb_progress
self.handle.logcb = self.cb_log
2013-02-27 11:15:51 -03:00
@dbus.service.signal('org.manjaro.pamac')
def EmitAction(self, action):
pass
@dbus.service.signal('org.manjaro.pamac')
def EmitIcon(self, icon):
pass
@dbus.service.signal('org.manjaro.pamac')
def EmitTarget(self, target):
pass
@dbus.service.signal('org.manjaro.pamac')
def EmitPercent(self, percent):
pass
def cb_event(self, ID, event, tupel):
if ID is 1:
2013-03-31 14:31:33 -03:00
self.action = _('Checking dependencies')+'...'
2013-02-27 11:15:51 -03:00
self.icon = '/usr/share/pamac/icons/24x24/status/package-search.png'
elif ID is 3:
2013-03-31 14:31:33 -03:00
self.action = _('Checking file conflicts')+'...'
2013-02-27 11:15:51 -03:00
self.icon = '/usr/share/pamac/icons/24x24/status/package-search.png'
elif ID is 5:
2013-03-31 14:31:33 -03:00
self.action = _('Resolving dependencies')+'...'
2013-02-27 11:15:51 -03:00
self.icon = '/usr/share/pamac/icons/24x24/status/setup.png'
elif ID is 7:
2013-03-31 14:31:33 -03:00
self.action = _('Checking inter conflicts')+'...'
2013-02-27 11:15:51 -03:00
self.icon = '/usr/share/pamac/icons/24x24/status/package-search.png'
elif ID is 9:
2013-03-31 14:31:33 -03:00
self.action = _('Installing')+'...'
2013-02-27 11:15:51 -03:00
self.icon = '/usr/share/pamac/icons/24x24/status/package-add.png'
elif ID is 10:
2013-03-31 14:31:33 -03:00
formatted_event = 'Installed {pkgname} ({pkgversion})'.format(pkgname = tupel[0].name, pkgversion = tupel[0].version)
2013-02-27 11:15:51 -03:00
common.write_log_file(formatted_event)
print(formatted_event)
elif ID is 11:
2013-03-31 14:31:33 -03:00
self.action = _('Removing')+'...'
2013-02-27 11:15:51 -03:00
self.icon = '/usr/share/pamac/icons/24x24/status/package-delete.png'
elif ID is 12:
2013-03-31 14:31:33 -03:00
formatted_event = 'Removed {pkgname} ({pkgversion})'.format(pkgname = tupel[0].name, pkgversion = tupel[0].version)
2013-02-27 11:15:51 -03:00
common.write_log_file(formatted_event)
print(formatted_event)
elif ID is 13:
2013-03-31 14:31:33 -03:00
self.action = _('Upgrading')+'...'
2013-02-27 11:15:51 -03:00
self.icon = '/usr/share/pamac/icons/24x24/status/package-update.png'
elif ID is 14:
2013-03-31 14:31:33 -03:00
formatted_event = 'Upgraded {pkgname} ({oldversion} -> {newversion})'.format(pkgname = tupel[1].name, oldversion = tupel[1].version, newversion = tupel[0].version)
2013-02-27 11:15:51 -03:00
common.write_log_file(formatted_event)
print(formatted_event)
elif ID is 15:
self.action = _('Downgrading')+'...'
self.icon = '/usr/share/pamac/icons/24x24/status/rollback.png'
print('Downgrading a package')
#elif ID is 16:
#formatted_event = 'Downgraded {pkgname} ({oldversion} -> {newversion})'.format(pkgname = tupel[1].name, oldversion = tupel[1].version, newversion = tupel[0].version)
#common.write_log_file(formatted_event)
#print(formatted_event)
elif ID is 17:
self.action = _('Reinstalling')+'...'
self.icon = '/usr/share/pamac/icons/24x24/status/package-add.png'
print('Reinstalling a package')
#elif ID is 18:
#formatted_event = 'Reinstalled {pkgname} ({pkgversion})'.format(pkgname = tupel[0].name, pkgversion = tupel[0].version)
#common.write_log_file(formatted_event)
#print(formatted_event)
2013-04-10 11:20:54 -03:00
elif ID is 19:
2013-03-31 14:31:33 -03:00
self.action = _('Checking integrity')+'...'
2013-02-27 11:15:51 -03:00
self.icon = '/usr/share/pamac/icons/24x24/status/package-search.png'
self.already_transferred = 0
2013-04-10 11:20:54 -03:00
elif ID is 21:
2013-03-31 14:31:33 -03:00
self.action = _('Loading packages files')+'...'
2013-02-27 11:15:51 -03:00
self.icon = '/usr/share/pamac/icons/24x24/status/package-search.png'
print('Loading packages files')
2013-04-10 11:20:54 -03:00
elif ID is 30:
2013-03-31 14:31:33 -03:00
self.action = _('Configuring')+'...'
2013-02-27 11:15:51 -03:00
self.icon = '/usr/share/pamac/icons/24x24/status/setup.png'
self.EmitPercent(2)
2013-02-27 11:15:51 -03:00
print('Configuring a package')
2013-04-10 11:20:54 -03:00
elif ID is 31:
2013-02-27 11:15:51 -03:00
print('Downloading a file')
2013-04-10 11:20:54 -03:00
elif ID is 36:
self.action = _('Checking keys in keyring')+'...'
self.icon = '/usr/share/pamac/icons/24x24/status/package-search.png'
print('Checking keys in keyring')
2013-02-27 11:15:51 -03:00
else :
self.action = ''
2013-03-02 11:27:52 -03:00
#self.EmitTarget('')
#self.EmitPercent(0)
if self.action != self.previous_action:
self.previous_action = self.action
self.EmitAction(self.action)
if self.icon != self.previous_icon:
self.previous_icon = self.icon
self.EmitIcon(self.icon)
2013-02-27 11:15:51 -03:00
print(ID,event)
def cb_conv(self, *args):
print("conversation", args)
def cb_log(self, level, line):
_logmask = pyalpm.LOG_ERROR | pyalpm.LOG_WARNING
if not (level & _logmask):
return
if level & pyalpm.LOG_ERROR:
#self.error += "ERROR: "+line
self.EmitLogError(line)
#print(self.error)
2013-03-15 13:15:34 -03:00
#self.t.release()
2013-02-27 11:15:51 -03:00
elif level & pyalpm.LOG_WARNING:
#self.warning += "WARNING: "+line
self.EmitLogWarning(line)
2013-02-27 11:15:51 -03:00
elif level & pyalpm.LOG_DEBUG:
line = "DEBUG: " + line
print(line)
elif level & pyalpm.LOG_FUNCTION:
line = "FUNC: " + line
print(line)
@dbus.service.signal('org.manjaro.pamac')
def EmitLogError(self, message):
pass
@dbus.service.signal('org.manjaro.pamac')
def EmitLogWarning(self, message):
pass
2013-02-27 11:15:51 -03:00
def totaldlcb(self, _total_size):
self.total_size = _total_size
def cb_dl(self, _target, _transferred, total):
if self.total_size > 0:
fraction = (_transferred+self.already_transferred)/self.total_size
size = 0
2013-03-15 13:15:34 -03:00
try:
if (self.t.to_remove or self.t.to_add):
for pkg in self.t.to_remove+self.t.to_add:
if pkg.filename == _target:
2013-03-15 13:15:34 -03:00
size = pkg.size
if _transferred == size:
self.already_transferred += size
2013-04-01 05:08:03 -03:00
self.action = _('Downloading {size}').format(size = common.format_size(self.total_size))
2013-03-15 13:15:34 -03:00
self.target = _target
self.percent = round(fraction, 2)
2013-03-15 13:15:34 -03:00
self.icon = '/usr/share/pamac/icons/24x24/status/package-download.png'
else:
2013-03-31 14:31:33 -03:00
self.action = _('Refreshing')+'...'
2013-03-15 13:15:34 -03:00
self.target = _target
self.percent = 2
self.icon = '/usr/share/pamac/icons/24x24/status/refresh-cache.png'
if self.action != self.previous_action:
self.previous_action = self.action
self.EmitAction(self.action)
if self.icon != self.previous_icon:
self.previous_icon = self.icon
self.EmitIcon(self.icon)
if self.target != self.previous_target:
self.previous_target = self.target
self.EmitTarget(self.target)
if self.percent != self.previous_percent:
self.previous_percent = self.percent
self.EmitPercent(self.percent)
2013-03-15 13:15:34 -03:00
except pyalpm.error:
pass
2013-02-27 11:15:51 -03:00
def cb_progress(self, _target, _percent, n, i):
self.target = _target+' ('+str(i)+'/'+str(n)+')'
#self.percent = round(_percent/100, 2)
self.percent = round(i/n, 2)
if self.target != self.previous_target:
self.previous_target = self.target
self.EmitTarget(self.target)
if self.percent != self.previous_percent:
self.previous_percent = self.percent
self.EmitPercent(self.percent)
2013-02-27 11:15:51 -03:00
2013-03-15 13:15:34 -03:00
def policykit_test(self, sender, connexion, action):
2013-02-27 11:15:51 -03:00
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
@dbus.service.signal('org.manjaro.pamac')
def EmitAvailableUpdates(self, updates_nb):
pass
def CheckUpdates(self):
2013-03-18 07:07:58 -03:00
updates = 0
2013-04-10 11:20:54 -03:00
_ignorepkgs = []
for group in self.handle.ignoregrps:
db = self.handle.get_localdb()
grp = db.read_grp(group)
if grp:
name, pkg_list = grp
for pkg in pkg_list:
if not pkg.name in _ignorepkgs:
_ignorepkgs.append(pkg.name)
2013-04-12 10:46:43 -03:00
for name in self.handle.ignorepkgs:
pkg = self.handle.get_localdb().get_pkg(name)
if pkg:
if not pkg.name in _ignorepkgs:
_ignorepkgs.append(pkg.name)
2013-03-18 11:29:20 -03:00
if config.syncfirst:
for name in config.syncfirst:
pkg = self.handle.get_localdb().get_pkg(name)
if pkg:
candidate = pyalpm.sync_newversion(pkg, self.handle.get_syncdbs())
if candidate:
updates += 1
if not updates:
for pkg in self.handle.get_localdb().pkgcache:
candidate = pyalpm.sync_newversion(pkg, self.handle.get_syncdbs())
if candidate:
2013-04-10 11:20:54 -03:00
if not candidate.name in _ignorepkgs:
updates += 1
2013-02-27 11:15:51 -03:00
self.EmitAvailableUpdates(updates)
2013-03-15 13:15:34 -03:00
@dbus.service.method('org.manjaro.pamac', '', 's', async_callbacks=('success', 'nosuccess'))
def Refresh(self, success, nosuccess):
def refresh():
self.target = ''
self.percent = 0
self.error = ''
2013-04-30 12:06:47 -04:00
self.get_handle()
2013-03-18 11:29:20 -03:00
for db in self.handle.get_syncdbs():
2013-03-15 13:15:34 -03:00
try:
2013-03-18 11:29:20 -03:00
self.t = self.handle.init_transaction()
db.update(force = False)
2013-03-15 13:15:34 -03:00
except pyalpm.error as e:
self.error += ' --> '+str(e)+'\n'
2013-04-10 11:20:54 -03:00
break
2013-03-15 13:15:34 -03:00
finally:
2013-03-18 11:29:20 -03:00
try:
self.t.release()
except:
pass
2013-03-15 13:15:34 -03:00
if self.error:
self.EmitTransactionError(self.error)
2013-03-18 07:07:58 -03:00
else:
self.EmitTransactionDone('')
2013-03-18 11:29:20 -03:00
self.CheckUpdates()
2013-03-15 13:15:34 -03:00
self.task = Process(target=refresh)
self.task.start()
success('')
2013-02-27 11:15:51 -03:00
2013-04-30 12:06:47 -04:00
@dbus.service.method('org.manjaro.pamac', 'a{sb}', 's')#, sender_keyword='sender', connection_keyword='connexion')
def Init(self, options):#, sender=None, connexion=None):
2013-02-27 11:15:51 -03:00
self.error = ''
2013-04-30 12:06:47 -04:00
#if self.policykit_test(sender,connexion,'org.manjaro.pamac.init_release'):
try:
self.get_handle()
self.t = self.handle.init_transaction(**options)
print('Init:',self.t.flags)
except pyalpm.error as e:
self.error += ' --> '+str(e)+'\n'
finally:
if self.error:
self.EmitTransactionError(self.error)
return self.error
#else:
# return _('Authentication failed')
2013-02-27 11:15:51 -03:00
@dbus.service.method('org.manjaro.pamac', '', 's')
def Sysupgrade(self):
self.error = ''
try:
2013-03-15 13:15:34 -03:00
self.t.sysupgrade(downgrade=False)
print('to_upgrade:',self.t.to_add)
2013-02-27 11:15:51 -03:00
except pyalpm.error as e:
self.error += ' --> '+str(e)+'\n'
finally:
return self.error
@dbus.service.method('org.manjaro.pamac', 's', 's')
def Remove(self, pkgname):
self.error = ''
try:
2013-03-18 11:29:20 -03:00
pkg = self.handle.get_localdb().get_pkg(pkgname)
2013-02-27 11:15:51 -03:00
if pkg is not None:
2013-03-15 13:15:34 -03:00
self.t.remove_pkg(pkg)
2013-02-27 11:15:51 -03:00
except pyalpm.error as e:
self.error += ' --> '+str(e)+'\n'
finally:
return self.error
@dbus.service.method('org.manjaro.pamac', 's', 's')
def Add(self, pkgname):
self.error = ''
try:
2013-03-18 11:29:20 -03:00
for repo in self.handle.get_syncdbs():
2013-02-27 11:15:51 -03:00
pkg = repo.get_pkg(pkgname)
if pkg:
2013-03-15 13:15:34 -03:00
self.t.add_pkg(pkg)
2013-02-27 11:15:51 -03:00
break
except pyalpm.error as e:
self.error += ' --> '+str(e)+'\n'
finally:
return self.error
@dbus.service.method('org.manjaro.pamac', 's', 's')
def Load(self, tarball_path):
self.error = ''
try:
pkg = self.handle.load_pkg(tarball_path)
if pkg:
self.t.add_pkg(pkg)
except pyalpm.error:
self.error += _('{pkgname} is not a valid path or package name').format(pkgname = tarball_path)
2013-02-27 11:15:51 -03:00
finally:
return self.error
@dbus.service.method('org.manjaro.pamac', '', 's')
def Prepare(self):
self.error = ''
try:
2013-03-15 13:15:34 -03:00
self.t.prepare()
2013-02-27 11:15:51 -03:00
except pyalpm.error as e:
print(e)
self.error += ' --> '+str(e)+'\n'
finally:
2013-04-30 12:06:47 -04:00
return self.error
2013-02-27 11:15:51 -03:00
@dbus.service.method('org.manjaro.pamac', '', 'a(ss)')
2013-02-27 11:15:51 -03:00
def To_Remove(self):
liste = []
2013-03-15 13:15:34 -03:00
for pkg in self.t.to_remove:
liste.append((pkg.name, pkg.version))
2013-03-15 13:15:34 -03:00
return liste
2013-02-27 11:15:51 -03:00
@dbus.service.method('org.manjaro.pamac', '', 'a(ssi)')
2013-02-27 11:15:51 -03:00
def To_Add(self):
liste = []
2013-03-15 13:15:34 -03:00
for pkg in self.t.to_add:
liste.append((pkg.name, pkg.version, pkg.download_size))
2013-03-15 13:15:34 -03:00
return liste
2013-02-27 11:15:51 -03:00
2013-03-15 13:15:34 -03:00
@dbus.service.method('org.manjaro.pamac', '', 's', async_callbacks=('success', 'nosuccess'))
def Interrupt(self, success, nosuccess):
def interrupt():
self.error = ''
#try:
# self.t.interrupt()
#except pyalpm.error as e:
# self.error += ' --> '+str(e)+'\n'
2013-02-27 11:15:51 -03:00
try:
2013-03-15 13:15:34 -03:00
self.t.release()
#except pyalpm.error as e:
#self.error += ' --> '+str(e)+'\n'
except:
pass
#finally:
2013-03-15 13:15:34 -03:00
#if self.error:
#self.EmitTransactionError(self.error)
self.task.terminate()
interrupt()
success('')
@dbus.service.method('org.manjaro.pamac', '', 's', sender_keyword='sender', connection_keyword='connexion', async_callbacks=('success', 'nosuccess'))
def Commit(self, success, nosuccess, sender=None, connexion=None):
def commit():
self.error = ''
try:
self.t.commit()
2013-02-27 11:15:51 -03:00
except pyalpm.error as e:
#error = traceback.format_exc()
self.error += ' --> '+str(e)+'\n'
#except dbus.exceptions.DBusException:
#pass
finally:
self.CheckUpdates()
2013-03-15 13:15:34 -03:00
if self.error:
self.EmitTransactionError(self.error)
else:
2013-03-31 14:31:33 -03:00
self.EmitTransactionDone(_('Transaction successfully finished'))
try:
authorized = self.policykit_test(sender,connexion,'org.manjaro.pamac.commit')
except dbus.exceptions.DBusException as e:
2013-03-31 14:31:33 -03:00
self.EmitTransactionError(_('Authentication failed'))
success('')
else:
if authorized:
self.task = Process(target=commit)
self.task.start()
else :
self.t.release()
2013-03-31 14:31:33 -03:00
self.EmitTransactionError(_('Authentication failed'))
success('')
2013-03-15 13:15:34 -03:00
@dbus.service.signal('org.manjaro.pamac')
def EmitTransactionDone(self, message):
pass
@dbus.service.signal('org.manjaro.pamac')
def EmitTransactionError(self, message):
pass
2013-02-27 11:15:51 -03:00
2013-04-30 12:06:47 -04:00
@dbus.service.method('org.manjaro.pamac', '', 's')#, sender_keyword='sender', connection_keyword='connexion')
def Release(self):#, sender=None, connexion=None):
2013-02-27 11:15:51 -03:00
self.error = ''
2013-04-30 12:06:47 -04:00
#if self.policykit_test(sender,connexion,'org.manjaro.pamac.init_release'):
try:
self.t.release()
except pyalpm.error as e:
self.error += ' --> '+str(e)+'\n'
finally:
return self.error
#else :
# return _('Authentication failed')
2013-02-27 11:15:51 -03:00
@dbus.service.method('org.manjaro.pamac')
def StopDaemon(self):
try:
2013-03-15 13:15:34 -03:00
self.t.release()
2013-02-27 11:15:51 -03:00
except:
pass
mainloop.quit()
2013-03-15 13:15:34 -03:00
GObject.threads_init()
2013-02-27 11:15:51 -03:00
DBusGMainLoop(set_as_default=True)
myservice = PamacDBusService()
mainloop = GObject.MainLoop()
mainloop.run()