improve errors msg and add total siglevel support

This commit is contained in:
guinux 2013-12-13 15:37:01 +01:00
parent dd702da40a
commit 1085ec580f
2 changed files with 136 additions and 30 deletions

View File

@ -29,11 +29,23 @@ from multiprocessing import Process
from pamac import config, common, aur from pamac import config, common, aur
# i18n # i18n
import locale
locale.setlocale(locale.LC_ALL, '')
import gettext import gettext
gettext.bindtextdomain('pamac', '/usr/share/locale') gettext.bindtextdomain('pamac', '/usr/share/locale')
gettext.textdomain('pamac') gettext.textdomain('pamac')
_ = gettext.gettext _ = gettext.gettext
def format_error(data):
errstr = data[0].strip('\n')
errno = data[1]
detail = data[2]
if detail:
# detail is a list of '\n' terminated strings
return '{}:\n'.format(errstr) + ''.join(i for i in detail)
else:
return errstr
class PamacDBusService(dbus.service.Object): class PamacDBusService(dbus.service.Object):
def __init__(self): def __init__(self):
bus = dbus.SystemBus() bus = dbus.SystemBus()
@ -52,6 +64,8 @@ class PamacDBusService(dbus.service.Object):
self.total_size = 0 self.total_size = 0
self.already_transferred = 0 self.already_transferred = 0
self.local_packages = set() self.local_packages = set()
self.aur_updates_checked = False
self.aur_updates_pkgs = []
self.localdb = None self.localdb = None
self.syncdbs = None self.syncdbs = None
self.get_handle() self.get_handle()
@ -320,6 +334,7 @@ class PamacDBusService(dbus.service.Object):
if not (level & _logmask): if not (level & _logmask):
return return
if level & pyalpm.LOG_ERROR: if level & pyalpm.LOG_ERROR:
self.EmitLogError(line)
_error = "ERROR: "+line _error = "ERROR: "+line
self.EmitActionLong(_error) self.EmitActionLong(_error)
self.EmitNeedDetails(True) self.EmitNeedDetails(True)
@ -408,7 +423,7 @@ class PamacDBusService(dbus.service.Object):
if pkg: if pkg:
self.handle.set_pkgreason(pkg, reason) self.handle.set_pkgreason(pkg, reason)
except Exception as e: except Exception as e:
error = str(e) error = format_error(e.args)
return error return error
@dbus.service.method('org.manjaro.pamac', '', 's', async_callbacks=('success', 'nosuccess')) @dbus.service.method('org.manjaro.pamac', '', 's', async_callbacks=('success', 'nosuccess'))
@ -418,7 +433,6 @@ class PamacDBusService(dbus.service.Object):
updates = [] updates = []
_ignorepkgs = set() _ignorepkgs = set()
self.get_handle() self.get_handle()
self.get_local_packages()
for group in self.handle.ignoregrps: for group in self.handle.ignoregrps:
db = self.localdb db = self.localdb
grp = db.read_grp(group) grp = db.read_grp(group)
@ -439,33 +453,39 @@ class PamacDBusService(dbus.service.Object):
syncfirst = True syncfirst = True
updates.append((candidate.name, candidate.version, candidate.db.name, '', candidate.download_size)) updates.append((candidate.name, candidate.version, candidate.db.name, '', candidate.download_size))
if not updates: if not updates:
if not self.aur_updates_checked:
self.get_local_packages()
for pkg in self.localdb.pkgcache: for pkg in self.localdb.pkgcache:
if not pkg.name in _ignorepkgs: if not pkg.name in _ignorepkgs:
candidate = pyalpm.sync_newversion(pkg, self.syncdbs) candidate = pyalpm.sync_newversion(pkg, self.syncdbs)
if candidate: if candidate:
updates.append((candidate.name, candidate.version, candidate.db.name, '', candidate.download_size)) updates.append((candidate.name, candidate.version, candidate.db.name, '', candidate.download_size))
self.local_packages.discard(pkg.name) self.local_packages.discard(pkg.name)
if not self.aur_updates_checked:
if self.local_packages: if self.local_packages:
aur_pkgs = aur.multiinfo(self.local_packages) self.aur_updates_pkgs = aur.multiinfo(self.local_packages)
for aur_pkg in aur_pkgs: self.aur_updates_checked = True
for aur_pkg in self.aur_updates_pkgs:
comp = pyalpm.vercmp(aur_pkg.version, self.localdb.get_pkg(aur_pkg.name).version) comp = pyalpm.vercmp(aur_pkg.version, self.localdb.get_pkg(aur_pkg.name).version)
if comp == 1: if comp == 1:
updates.append((aur_pkg.name, aur_pkg.version, aur_pkg.db.name, aur_pkg.tarpath, aur_pkg.download_size)) updates.append((aur_pkg.name, aur_pkg.version, aur_pkg.db.name, aur_pkg.tarpath, aur_pkg.download_size))
self.EmitAvailableUpdates((syncfirst, updates)) self.EmitAvailableUpdates((syncfirst, updates))
@dbus.service.method('org.manjaro.pamac', 'b', 's', async_callbacks=('success', 'nosuccess')) @dbus.service.method('org.manjaro.pamac', 'b', '')
def Refresh(self, force_update, success, nosuccess): def Refresh(self, force_update):
def refresh(): def refresh():
self.target = '' self.target = ''
self.percent = 0 self.percent = 0
error = '' error = ''
for db in self.syncdbs: for db in self.syncdbs:
try: try:
print(db.name)
self.t = self.handle.init_transaction() self.t = self.handle.init_transaction()
db.update(force = bool(force_update)) db.update(force = bool(force_update))
self.t.release() self.t.release()
except pyalpm.error as e: except pyalpm.error as e:
error += str(e) print(e)
error += format_error(e.args)
break break
if error: if error:
self.EmitTransactionError(error) self.EmitTransactionError(error)
@ -474,7 +494,6 @@ class PamacDBusService(dbus.service.Object):
self.task = Process(target=refresh) self.task = Process(target=refresh)
self.task.start() self.task.start()
GObject.timeout_add(100, self.check_finished_commit) GObject.timeout_add(100, self.check_finished_commit)
success('')
@dbus.service.method('org.manjaro.pamac', 'a{sb}', 's') @dbus.service.method('org.manjaro.pamac', 'a{sb}', 's')
def Init(self, options): def Init(self, options):
@ -483,7 +502,7 @@ class PamacDBusService(dbus.service.Object):
self.t = self.handle.init_transaction(**options) self.t = self.handle.init_transaction(**options)
print('Init:',self.t.flags) print('Init:',self.t.flags)
except pyalpm.error as e: except pyalpm.error as e:
error = str(e) error = format_error(e.args)
finally: finally:
return error return error
@ -493,7 +512,7 @@ class PamacDBusService(dbus.service.Object):
try: try:
self.t.sysupgrade(downgrade=False) self.t.sysupgrade(downgrade=False)
except pyalpm.error as e: except pyalpm.error as e:
error = ' --> '+str(e)+'\n' error = format_error(e.args)
self.t.release() self.t.release()
finally: finally:
return error return error
@ -506,7 +525,7 @@ class PamacDBusService(dbus.service.Object):
if pkg is not None: if pkg is not None:
self.t.remove_pkg(pkg) self.t.remove_pkg(pkg)
except pyalpm.error as e: except pyalpm.error as e:
error = ' --> '+str(e)+'\n' error = format_error(e.args)
finally: finally:
return error return error
@ -520,7 +539,7 @@ class PamacDBusService(dbus.service.Object):
self.t.add_pkg(pkg) self.t.add_pkg(pkg)
break break
except pyalpm.error as e: except pyalpm.error as e:
error += ' --> '+str(e)+'\n' error = format_error(e.args)
finally: finally:
return error return error
@ -634,7 +653,7 @@ class PamacDBusService(dbus.service.Object):
try: try:
self.t.prepare() self.t.prepare()
except pyalpm.error as e: except pyalpm.error as e:
error = str(e) error = format_error(e.args)
self.t.release() self.t.release()
else: else:
for pkg in self.t.to_remove: for pkg in self.t.to_remove:
@ -646,7 +665,7 @@ class PamacDBusService(dbus.service.Object):
try: try:
summ = len(self.t.to_add) + len(self.t.to_remove) summ = len(self.t.to_add) + len(self.t.to_remove)
except pyalpm.error: except pyalpm.error:
return [((), '')] return [((), error)]
if summ == 0: if summ == 0:
self.t.release() self.t.release()
return [((), _('Nothing to do'))] return [((), _('Nothing to do'))]
@ -677,8 +696,8 @@ class PamacDBusService(dbus.service.Object):
pass pass
return _list return _list
@dbus.service.method('org.manjaro.pamac', '', 's', async_callbacks=('success', 'nosuccess')) @dbus.service.method('org.manjaro.pamac', '', '')
def Interrupt(self, success, nosuccess): def Interrupt(self):
def interrupt(): def interrupt():
try: try:
self.t.interrupt() self.t.interrupt()
@ -692,16 +711,15 @@ class PamacDBusService(dbus.service.Object):
common.rm_lock_file() common.rm_lock_file()
self.task.terminate() self.task.terminate()
interrupt() interrupt()
success('')
@dbus.service.method('org.manjaro.pamac', '', 's', sender_keyword='sender', connection_keyword='connexion', async_callbacks=('success', 'nosuccess')) @dbus.service.method('org.manjaro.pamac', '', '', sender_keyword='sender', connection_keyword='connexion', async_callbacks=('success', 'nosuccess'))
def Commit(self, success, nosuccess, sender=None, connexion=None): def Commit(self, success, nosuccess, sender=None, connexion=None):
def commit(): def commit():
error = '' error = ''
try: try:
self.t.commit() self.t.commit()
except pyalpm.error as e: except pyalpm.error as e:
error = str(e) error = format_error(e.args)
#except dbus.exceptions.DBusException: #except dbus.exceptions.DBusException:
#pass #pass
finally: finally:
@ -713,11 +731,11 @@ class PamacDBusService(dbus.service.Object):
self.EmitTransactionError(error) self.EmitTransactionError(error)
else: else:
self.EmitTransactionDone(_('Transaction successfully finished')) self.EmitTransactionDone(_('Transaction successfully finished'))
success('')
try: try:
authorized = self.policykit_test(sender,connexion,'org.manjaro.pamac.commit') authorized = self.policykit_test(sender,connexion,'org.manjaro.pamac.commit')
except dbus.exceptions.DBusException as e: except dbus.exceptions.DBusException as e:
self.EmitTransactionError(_('Authentication failed')) self.EmitTransactionError(_('Authentication failed'))
success('')
else: else:
if authorized: if authorized:
self.task = Process(target=commit) self.task = Process(target=commit)
@ -726,7 +744,6 @@ class PamacDBusService(dbus.service.Object):
else : else :
self.t.release() self.t.release()
self.EmitTransactionError(_('Authentication failed')) self.EmitTransactionError(_('Authentication failed'))
success('')
@dbus.service.method('org.manjaro.pamac', '', '') @dbus.service.method('org.manjaro.pamac', '', '')
def Release(self): def Release(self):

View File

@ -72,6 +72,68 @@ BOOLEAN_OPTIONS = (
'Color' 'Color'
) )
def define_siglevel(default_level, conf_string):
for directive in conf_string.split():
affect_package = False
affect_database = False
if 'Package' in directive:
affect_package = True
elif 'Database' in directive:
affect_database = True
else:
affect_package = True
affect_database = True
if 'Never' in directive:
if affect_package:
default_level &= ~pyalpm.SIG_PACKAGE
default_level |= pyalpm.SIG_PACKAGE_SET
if affect_database:
default_level &= ~pyalpm.SIG_DATABASE
elif 'Optional' in directive:
if affect_package:
default_level |= pyalpm.SIG_PACKAGE
default_level |= pyalpm.SIG_PACKAGE_OPTIONAL
default_level |= pyalpm.SIG_PACKAGE_SET
if affect_database:
default_level |= pyalpm.SIG_DATABASE
default_level |= pyalpm.SIG_DATABASE_OPTIONAL
elif 'Required' in directive:
if affect_package:
default_level |= pyalpm.SIG_PACKAGE
default_level &= ~pyalpm.SIG_PACKAGE_OPTIONAL
default_level |= pyalpm.SIG_PACKAGE_SET
if affect_database:
default_level |= pyalpm.SIG_DATABASE
default_level &= ~pyalpm.SIG_DATABASE_OPTIONAL
elif 'TrustedOnly' in directive:
if affect_package:
default_level &= ~pyalpm.SIG_PACKAGE_MARGINAL_OK
default_level &= ~pyalpm.SIG_PACKAGE_UNKNOWN_OK
default_level |= pyalpm.SIG_PACKAGE_TRUST_SET
if affect_database:
default_level &= ~pyalpm.SIG_DATABASE_MARGINAL_OK
default_level &= ~pyalpm.SIG_DATABASE_UNKNOWN_OK
elif 'TrustAll' in directive:
if affect_package:
default_level |= pyalpm.SIG_PACKAGE_MARGINAL_OK
default_level |= pyalpm.SIG_PACKAGE_UNKNOWN_OK
default_level |= pyalpm.SIG_PACKAGE_TRUST_SET
if affect_database:
default_level |= pyalpm.SIG_DATABASE_MARGINAL_OK
default_level |= pyalpm.SIG_DATABASE_UNKNOWN_OK
else:
print('unrecognized siglevel: {}'.format(conf_string))
return default_level
def merge_siglevel(base_level, over_level):
if not over_level & pyalpm.SIG_PACKAGE_SET:
over_level |= base_level & pyalpm.SIG_PACKAGE
over_level |= base_level & pyalpm.SIG_PACKAGE_OPTIONAL
if not over_level & pyalpm.SIG_PACKAGE_TRUST_SET:
over_level |= base_level & pyalpm.SIG_PACKAGE_MARGINAL_OK
over_level |= base_level & pyalpm.SIG_PACKAGE_UNKNOWN_OK
return over_level
def pacman_conf_enumerator(path): def pacman_conf_enumerator(path):
filestack = [] filestack = []
current_section = None current_section = None
@ -133,6 +195,7 @@ class PacmanConfig:
self.options["GPGDir"] = "/etc/pacman.d/gnupg/" self.options["GPGDir"] = "/etc/pacman.d/gnupg/"
self.options["LogFile"] = "/var/log/pacman.log" self.options["LogFile"] = "/var/log/pacman.log"
self.options["Architecture"] = os.uname()[-1] self.options["Architecture"] = os.uname()[-1]
self.default_siglevel = pyalpm.SIG_PACKAGE | pyalpm.SIG_PACKAGE_OPTIONAL | pyalpm.SIG_DATABASE | pyalpm.SIG_DATABASE_OPTIONAL
if conf: if conf:
self.load_from_file(conf) self.load_from_file(conf)
if options: if options:
@ -147,10 +210,18 @@ class PacmanConfig:
self.options.setdefault(key, []).append(value) self.options.setdefault(key, []).append(value)
else: else:
self.options[key] = value self.options[key] = value
# define here default_siglevel to make it usable for servers
if key == 'SigLevel':
self.default_siglevel = define_siglevel(self.default_siglevel, self.options["SigLevel"])
else: else:
servers = self.repos.setdefault(section, []) if not self.repos.get(section):
self.repos[section] = ([], self.default_siglevel)
if key == 'Server': if key == 'Server':
servers.append(value) self.repos[section][0].append(value)
elif key == 'SigLevel':
urls = self.repos[section][0].copy()
new_siglevel = define_siglevel(self.repos[section][1], value)
self.repos[section] = (urls, new_siglevel)
if not "CacheDir" in self.options: if not "CacheDir" in self.options:
self.options["CacheDir"]= ["/var/cache/pacman/pkg"] self.options["CacheDir"]= ["/var/cache/pacman/pkg"]
@ -189,11 +260,29 @@ class PacmanConfig:
h.usesyslog = self.options["UseSyslog"] h.usesyslog = self.options["UseSyslog"]
if "CheckSpace" in self.options: if "CheckSpace" in self.options:
h.checkspace = self.options["CheckSpace"] h.checkspace = self.options["CheckSpace"]
# register default siglevel, it should have been updated previously
h.siglevel = self.default_siglevel
# update localsiglevel
if "LocalFileSigLevel" in self.options:
localsiglevel = define_siglevel(self.default_siglevel, self.options["LocalFileSigLevel"])
localsiglevel = merge_siglevel(self.default_siglevel, localsiglevel)
else:
localsiglevel = self.default_siglevel
# define localsiglevel
h.localsiglevel = localsiglevel
# update remotesiglevel
if "RemoteFileSigLevel" in self.options:
remotesiglevel = define_siglevel(self.default_siglevel, self.options["RemoteFileSigLevel"])
remotesiglevel = merge_siglevel(self.default_siglevel, remotesiglevel)
else:
remotesiglevel = self.default_siglevel
# define remotesiglevel
h.remotesiglevel = remotesiglevel
# set sync databases # set sync databases
for repo, servers in self.repos.items(): for repo, servers in self.repos.items():
db = h.register_syncdb(repo, 0) db = h.register_syncdb(repo, servers[1])
db_servers = [] db_servers = []
for rawurl in servers: for rawurl in servers[0]:
url = rawurl.replace("$repo", repo) url = rawurl.replace("$repo", repo)
url = url.replace("$arch", self.options["Architecture"]) url = url.replace("$arch", self.options["Architecture"])
db_servers.append(url) db_servers.append(url)
@ -201,7 +290,7 @@ class PacmanConfig:
return h return h
def __str__(self): def __str__(self):
return("PacmanConfig(options=%s, repos=%s)" % (str(self.options), str(self.repos))) return("PacmanConfig(options={}, repos={})".format(self.options, self.repos))
pacman_conf = PacmanConfig(conf = "/etc/pacman.conf") pacman_conf = PacmanConfig(conf = "/etc/pacman.conf")
handle = pacman_conf.initialize_alpm handle = pacman_conf.initialize_alpm