use backend package

This commit is contained in:
guinux
2012-12-24 15:42:49 +01:00
parent faf0065468
commit 520648ac58
6 changed files with 12 additions and 19 deletions

0
backend/__init__.py Executable file
View File

194
backend/config.py Normal file
View File

@@ -0,0 +1,194 @@
#! /usr/bin/python
# -*-coding:utf-8 -*-
import io
import os
import glob
import sys
import argparse
import collections
import warnings
import pyalpm
class InvalidSyntax(Warning):
def __init__(self, filename, problem, arg):
self.filename = filename
self.problem = problem
self.arg = arg
def __str__(self):
return "unable to parse %s, %s: %r" % (self.filename, self.problem, self.arg)
# Options that may occur several times in a section. Their values should be
# accumulated in a list.
LIST_OPTIONS = (
'CacheDir',
'HoldPkg',
'SyncFirst',
'IgnoreGroup',
'IgnorePkg',
'NoExtract',
'NoUpgrade',
'Server'
)
SINGLE_OPTIONS = (
'RootDir',
'DBPath',
'GPGDir',
'LogFile',
'Architecture',
'XferCommand',
'CleanMethod',
'SigLevel'
)
BOOLEAN_OPTIONS = (
'UseSyslog',
'ShowSize',
'UseDelta',
'TotalDownload',
'CheckSpace',
'VerbosePkgLists',
'ILoveCandy'
)
def pacman_conf_enumerator(path):
filestack = []
current_section = None
filestack.append(open(path))
while len(filestack) > 0:
f = filestack[-1]
line = f.readline()
if len(line) == 0:
# end of file
filestack.pop()
continue
line = line.strip()
if len(line) == 0: continue
if line[0] == '#':
continue
if line[0] == '[' and line[-1] == ']':
current_section = line[1:-1]
continue
if current_section is None:
raise InvalidSyntax(f.name, 'statement outside of a section', line)
# read key, value
key, equal, value = [x.strip() for x in line.partition('=')]
# include files
if equal == '=' and key == 'Include':
filestack.extend(open(f) for f in glob.glob(value))
continue
if current_section != 'options':
# repos only have the Server option
if key == 'Server' and equal == '=':
yield (current_section, 'Server', value)
elif key == 'SigLevel' and equal == '=':
yield (current_section, 'SigLevel', value)
else:
raise InvalidSyntax(f.name, 'invalid key for repository configuration', line)
continue
if equal == '=':
if key in LIST_OPTIONS:
for val in value.split():
yield (current_section, key, val)
elif key in SINGLE_OPTIONS:
yield (current_section, key, value)
else:
warnings.warn(InvalidSyntax(f.name, 'unrecognized option', key))
else:
if key in BOOLEAN_OPTIONS:
yield (current_section, key, True)
else:
warnings.warn(InvalidSyntax(f.name, 'unrecognized option', key))
class PacmanConfig(object):
def __init__(self, conf = None, options = None):
self.options = {}
self.repos = collections.OrderedDict()
self.options["RootDir"] = "/"
self.options["DBPath"] = "/var/lib/pacman"
self.options["GPGDir"] = "/etc/pacman.d/gnupg/"
self.options["LogFile"] = "/var/log/pacman.log"
self.options["Architecture"] = os.uname()[-1]
if conf is not None:
self.load_from_file(conf)
if options is not None:
self.load_from_options(options)
def load_from_file(self, filename):
for section, key, value in pacman_conf_enumerator(filename):
if section == 'options':
if key == 'Architecture' and value == 'auto':
continue
if key in LIST_OPTIONS:
self.options.setdefault(key, []).append(value)
else:
self.options[key] = value
else:
servers = self.repos.setdefault(section, [])
if key == 'Server':
servers.append(value)
if "CacheDir" not in self.options:
self.options["CacheDir"]= ["/var/cache/pacman/pkg"]
def load_from_options(self, options):
global _logmask
if options.root is not None:
self.options["RootDir"] = options.root
if options.dbpath is not None:
self.options["DBPath"] = options.dbpath
if options.gpgdir is not None:
self.options["GPGDir"] = options.gpgdir
if options.arch is not None:
self.options["Architecture"] = options.arch
if options.logfile is not None:
self.options["LogFile"] = options.logfile
if options.cachedir is not None:
self.options["CacheDir"] = [option.cachedir]
if options.debug:
_logmask = 0xffff
def apply(self, h):
h.arch = self.options["Architecture"]
h.logfile = self.options["LogFile"]
h.gpgdir = self.options["GPGDir"]
h.cachedirs = self.options["CacheDir"]
if "IgnoreGroup" in self.options:
h.ignoregrps = self.options["IgnoreGroup"]
if "IgnorePkg" in self.options:
h.ignorepkgs = self.options["IgnorePkg"]
if "NoExtract" in self.options:
h.noextracts = self.options["NoExtract"]
if "NoUpgrade" in self.options:
h.noupgrades = self.options["NoUpgrade"]
# set sync databases
for repo, servers in self.repos.items():
db = h.register_syncdb(repo, 0)
db_servers = []
for rawurl in servers:
url = rawurl.replace("$repo", repo)
url = url.replace("$arch", self.options["Architecture"])
db_servers.append(url)
db.servers = db_servers
def initialize_alpm(self):
h = pyalpm.Handle(self.options["RootDir"], self.options["DBPath"])
self.apply(h)
return h
def __str__(self):
return("PacmanConfig(options=%s, repos=%s)" % (str(self.options), str(self.repos)))
pacman_conf = PacmanConfig(conf = "/etc/pacman.conf")
handle = pacman_conf.initialize_alpm()
holpkg = None
syncfirst = None
if 'HoldPkg' in pacman_conf.options:
holdpkg = pacman_conf.options['HoldPkg']
if 'SyncFirst' in pacman_conf.options:
syncfirst = pacman_conf.options['SyncFirst']

377
backend/transaction.py Executable file
View File

@@ -0,0 +1,377 @@
#! /usr/bin/python
# -*-coding:utf-8-*-
from gi.repository import Gtk
import pyalpm
import traceback
import sys
from backend import config
interface = Gtk.Builder()
interface.add_from_file('gui/dialogs.glade')
ProgressWindow = interface.get_object('ProgressWindow')
progress_bar = interface.get_object('progressbar2')
progress_label = interface.get_object('progresslabel2')
ErrorDialog = interface.get_object('ErrorDialog')
WarningDialog = interface.get_object('WarningDialog')
QuestionDialog = interface.get_object('QuestionDialog')
ConfDialog = interface.get_object('ConfDialog')
transaction_desc = interface.get_object('transaction_desc')
down_label = interface.get_object('down_label')
t = None
t_lock = False
conflict_to_remove = None
to_remove = None
to_add = None
to_update = None
do_syncfirst = False
list_first = []
def init_transaction(handle, **options):
"Transaction initialization"
global t_lock
handle.dlcb = cb_dl
handle.totaldlcb = totaldlcb
handle.eventcb = cb_event
handle.questioncb = cb_conv
handle.progresscb = cb_progress
handle.logcb = cb_log
try:
_t = handle.init_transaction(**options)
print(_t.flags)
t_lock = True
return _t
except pyalpm.error:
ErrorDialog.format_secondary_text(traceback.format_exc())
response = ErrorDialog.run()
if response:
ErrorDialog.hide()
return False
def check_conflicts():
global conflict_to_remove
conflict_to_remove = {}
installed_conflicts = {}
for pkg in config.handle.get_localdb().pkgcache:
if pkg.conflicts:
installed_conflicts[pkg.name] = pkg.conflicts
targets_conflicts = {}
targets_replaces = {}
for pkg in t.to_add:
if pkg.replaces:
targets_replaces[pkg.name] = pkg.replaces
if pkg.conflicts:
targets_conflicts[pkg.name] = pkg.conflicts
warning = ''
if targets_replaces:
for key, value in targets_replaces.items():
for name in value:
pkg = config.handle.get_localdb().get_pkg(name)
if pkg:
if not pkg.name in conflict_to_remove.keys():
conflict_to_remove[pkg.name] = pkg
if warning:
warning = warning+'\n'
warning = warning+pkg.name+' will be replaced by '+key
if targets_conflicts:
for key, value in targets_conflicts.items():
for name in value:
pkg = config.handle.get_localdb().get_pkg(name)
if pkg:
if not pkg.name in conflict_to_remove.keys():
conflict_to_remove[pkg.name] = pkg
if installed_conflicts:
for key, value in installed_conflicts.items():
for name in value:
for pkg in t.to_add:
if pkg.name == name:
if not pkg.name in conflict_to_remove.keys():
conflict_to_remove[pkg.name] = pkg
if conflict_to_remove:
WarningDialog.format_secondary_text(warning)
response = WarningDialog.run()
if response:
WarningDialog.hide()
def do_refresh():
"""Sync databases like pacman -Sy"""
global t
global t_lock
ProgressWindow.show_all()
for db in config.handle.get_syncdbs():
if t_lock is False:
t = init_transaction(config.handle)
try:
db.update(force=False)
t.release()
t_lock = False
except pyalpm.error:
ErrorDialog.format_secondary_text(traceback.format_exc())
response = ErrorDialog.run()
if response:
ErrorDialog.hide()
t_lock = False
break
ProgressWindow.hide()
progress_label.set_text('')
progress_bar.set_text('')
def do_sysupgrade():
"""Upgrade a system like pacman -Su"""
global t
global t_lock
global to_remove
global to_add
global to_update
if t_lock is False:
if do_syncfirst is True:
t = init_transaction(config.handle, recurse = True)
for pkg in list_first:
t.add_pkg(pkg)
to_remove = t.to_remove
to_add = t.to_add
set_transaction_desc('update')
response = ConfDialog.run()
if response == Gtk.ResponseType.OK:
t_finalize(t)
if response == Gtk.ResponseType.CANCEL or Gtk.ResponseType.CLOSE or Gtk.ResponseType.DELETE_EVENT:
ProgressWindow.hide()
ConfDialog.hide()
t.release()
t_lock = False
else:
try:
t = init_transaction(config.handle)
t.sysupgrade(downgrade=False)
except pyalpm.error:
ErrorDialog.format_secondary_text(traceback.format_exc())
response = ErrorDialog.run()
if response:
ErrorDialog.hide()
t.release()
t_lock = False
check_conflicts()
to_add = t.to_add
to_remove = []
for pkg in conflict_to_remove.values():
to_remove.append(pkg)
if len(to_add) + len(to_remove) == 0:
t.release()
print("Nothing to update")
else:
t.release()
t = init_transaction(config.handle, noconflicts = True, nodeps = True, nodepversion = True)
for pkg in to_add:
t.add_pkg(pkg)
for pkg in conflict_to_remove.values():
t.remove_pkg(pkg)
to_remove = t.to_remove
to_add = t.to_add
set_transaction_desc('update')
if len(transaction_desc) != 0:
response = ConfDialog.run()
if response == Gtk.ResponseType.OK:
t_finalize(t)
if response == Gtk.ResponseType.CANCEL or Gtk.ResponseType.CLOSE or Gtk.ResponseType.DELETE_EVENT:
ProgressWindow.hide()
ConfDialog.hide()
t.release()
t_lock = False
else:
t_finalize(t)
t.release()
t_lock = False
def t_finalize(t):
ConfDialog.hide()
try:
t.prepare()
except pyalpm.error:
ErrorDialog.format_secondary_text(traceback.format_exc())
response = ErrorDialog.run()
if response:
ErrorDialog.hide()
t.release()
t_lock = False
try:
t.commit()
except pyalpm.error:
ErrorDialog.format_secondary_text(traceback.format_exc())
response = ErrorDialog.run()
if response:
ErrorDialog.hide()
ProgressWindow.hide()
def get_updates():
"""Return a list of package objects in local db which can be updated"""
global do_syncfirst
global list_first
if config.syncfirst:
for name in config.syncfirst:
pkg = config.handle.get_localdb().get_pkg(name)
candidate = pyalpm.sync_newversion(pkg, config.handle.get_syncdbs())
if candidate:
list_first.append(candidate)
if list_first:
do_syncfirst = True
return list_first
result = []
installed_pkglist = config.handle.get_localdb().pkgcache
for pkg in installed_pkglist:
candidate = pyalpm.sync_newversion(pkg, config.handle.get_syncdbs())
if candidate:
result.append(candidate)
return result
def get_new_version_available(pkgname):
for repo in config.handle.get_syncdbs():
pkg = repo.get_pkg(pkgname)
if pkg is not None:
return pkg.version
break
def format_size(size):
KiB_size = size / 1024
if KiB_size < 1000:
size_string = '%.1f KiB' % (KiB_size)
return size_string
else:
size_string = '%.2f MiB' % (KiB_size / 1024)
return size_string
def set_transaction_desc(mode):
global transaction_desc
global down_label
global to_add
global to_remove
global to_update
transaction_desc.clear()
if to_remove:
transaction_desc.append(['To remove:', to_remove[0].name])
i = 1
while i < len(to_remove):
transaction_desc.append([' ', to_remove[i].name])
i += 1
down_label.set_markup('')
if to_add:
installed_name = []
for pkg_object in config.handle.get_localdb().pkgcache:
installed_name.append(pkg_object.name)
to_add_name = []
for pkg_object in to_add:
to_add_name.append(pkg_object.name)
to_update = sorted(set(installed_name).intersection(to_add_name))
to_remove_from_add_name = sorted(set(to_update).intersection(to_add_name))
for name in to_remove_from_add_name:
to_add_name.remove(name)
if to_add_name:
transaction_desc.append(['To install:', to_add_name[0]])
i = 1
while i < len(to_add_name):
transaction_desc.append([' ', to_add_name[i]])
i += 1
if mode == 'normal':
if to_update:
transaction_desc.append(['To update:', to_update[0]])
i = 1
while i < len(to_update):
transaction_desc.append([' ', to_update[i]])
i += 1
down_label.set_markup('')
# down_label.set_markup('<b>Total Download size: </b>'+format_size(totaldlcb))
# Callbacks
event_text = ' '
def cb_event(ID, event, tupel):
global event_text
ProgressWindow.show_all()
while Gtk.events_pending():
Gtk.main_iteration()
for i in [1,3,5,7,9,11,15]:
if ID is i:
progress_label.set_text(event)
break
else :
progress_label.set_text(' ')
if ID is 27:
progress_label.set_text('Downloading '+format_size(total_size))
print('Downloading a file')
if ID is 17:
progress_label.set_text('Checking signatures')
print('Checking signatures')
progress_bar.set_fraction(0.0)
progress_bar.set_text('')
print(ID,event)
def cb_conv(*args):
print("conversation", args)
_logmask = pyalpm.LOG_ERROR | pyalpm.LOG_WARNING
def cb_log(level, line):
#global t
#try:
# _line = str(_line, encoding='utf-8').strip("\n")
#except:
# _line = str(_line, encoding='latin-1').strip("\n")
if not (level & _logmask):
return
if level & pyalpm.LOG_ERROR:
ErrorDialog.format_secondary_text("ERROR: "+line)
response = ErrorDialog.run()
if response:
ErrorDialog.hide()
#t.release()
elif level & pyalpm.LOG_WARNING:
WarningDialog.format_secondary_text("WARNING: "+line)
response = WarningDialog.run()
if response:
WarningDialog.hide()
elif level & pyalpm.LOG_DEBUG:
line = "DEBUG: " + line
print(line)
elif level & pyalpm.LOG_FUNCTION:
line = "FUNC: " + line
print(line)
#sys.stderr.write(line)
total_size = 0
def totaldlcb(_total_size):
global total_size
total_size = _total_size
already_transferred = 0
def cb_dl(_target, _transferred, total):
global already_transferred
while Gtk.events_pending():
Gtk.main_iteration()
if total_size > 0:
fraction = (_transferred+already_transferred)/total_size
size = 0
if (to_remove or to_add):
for pkg in to_remove+to_add:
if pkg.name+'-'+pkg.version in _target:
size = pkg.size
if _transferred == size:
already_transferred += size
progress_label.set_text('Downloading '+format_size(total_size))
progress_bar.set_text(_target)
progress_bar.set_fraction(fraction)
else:
progress_label.set_text('Refreshing...')
progress_bar.set_text(_target)
progress_bar.pulse()
def cb_progress(_target, _percent, n, i):
while Gtk.events_pending():
Gtk.main_iteration()
target = _target+' ('+str(i)+'/'+str(n)+')'
progress_bar.set_fraction(_percent/100)
progress_bar.set_text(target)
if __name__ == "__main__":
True