diff options
Diffstat (limited to 'AptUrl')
-rw-r--r-- | AptUrl/AptUrl.py | 193 | ||||
-rw-r--r-- | AptUrl/Helpers.py | 66 | ||||
-rw-r--r-- | AptUrl/Parser.py | 153 | ||||
-rw-r--r-- | AptUrl/UI.py | 23 | ||||
-rw-r--r-- | AptUrl/Version.py | 0 | ||||
-rw-r--r-- | AptUrl/__init__.py | 0 | ||||
-rw-r--r-- | AptUrl/gtk/GtkUI.py | 158 | ||||
-rw-r--r-- | AptUrl/gtk/__init__.py | 0 |
8 files changed, 593 insertions, 0 deletions
diff --git a/AptUrl/AptUrl.py b/AptUrl/AptUrl.py new file mode 100644 index 0000000..e712e5b --- /dev/null +++ b/AptUrl/AptUrl.py @@ -0,0 +1,193 @@ +# Copyright (c) 2007-2008 Canonical +# +# AUTHOR: +# Michael Vogt <mvo@ubuntu.com> +# With contributions by Siegfried-A. Gevatter <rainct@ubuntu.com> +# +# This file is part of AptUrl +# +# AptUrl is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as published +# by the Free Software Foundation; either version 2 of the License, or (at +# your option) any later version. +# +# AptUrl is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with AptUrl; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +import apt +import apt_pkg + +from . import Parser +from . import Helpers +from .Helpers import _ + +from optparse import OptionParser + +import os +import os.path + +# adding new repositories is currently disabled because some people have +# security concerns about this feature +allow_new_repositories = False + +# return codes +(RESULT_OK, + RESULT_CANCELT, + RESULT_ERROR, + RESULT_BADARGS) = list(range(4)) + +class AptUrlController(object): + + def __init__(self, ui): + self.ui = ui + + def openCache(self): + try: + self.cache = apt.Cache() + except SystemError as strerr: + if not '/etc/apt/sources.list' in str(strerr): + raise + self.ui.error(_("Invalid /etc/apt/sources.list file"), strerr) + return False + if self.cache._depcache.broken_count > 0: + err_header = _("Software index is broken") + err_body = _("This is a major failure of your software " + "management system. Please check for broken packages " + "with synaptic, check the file permissions and " + "correctness of the file '/etc/apt/sources.list' and " + "reload the software information with: " + "'sudo apt-get update' and 'sudo apt-get install -f'." + ) + self.ui.error(err_header, err_body) + return False + return True + + def parseArgs(self): + parser = OptionParser() + parser.add_option("-p", "--http-proxy", dest="http_proxy", + default=None, help="use http proxy") + (options, args) = parser.parse_args() + + # eval and add proxy + if options.http_proxy is not None: + proxy = options.http_proxy + if not ":" in proxy: + proxy += ":3128" + os.environ["http_proxy"] = "http://%s" % proxy + + # parse + try: + apturl_list = Parser.parse(args[0]) + except IndexError as e: + self.ui.error(_("Need a url to continue, exiting")) + return [] + except Parser.InvalidUrlException as e: + self.ui.error(_("Invalid url: '%s' given, exiting") % e.url, + str(e)) + return [] + return (apturl_list) + + def verifyInstall(self, apturl): + " verify that the install package actually is installed " + # check if the package got actually installed + self.openCache() + pkg = self.cache[apturl.package] + if (not pkg.is_installed or + pkg._pkg.current_state != apt_pkg.CURSTATE_INSTALLED or + self.cache._depcache.broken_count > 0): + return False + return True + + def main(self): + # global return code + ret = RESULT_OK + ui = self.ui + + # parse arguments + apturl_list = self.parseArgs() + if not apturl_list: + return RESULT_BADARGS + + # open cache + if not self.openCache(): + return RESULT_ERROR + + # now go over the url list + for apturl in apturl_list: + # FIXME: move this code block into a func like + # evalAptUrl() + + if not apturl.schema in ("apt", "apt+http"): + self.ui.error(_("Can not deal with protocol '%s' ") + % apturl.schema) + continue + + if apturl.refresh is not None: + ui.doUpdate() + if not self.openCache(): + return RESULT_ERROR + + # now check the package + if apturl.package not in self.cache: + try: + package_in_cache = bool(self.cache._cache[apturl.package]) + except KeyError: + package_in_cache = False + if package_in_cache: + ui.error(_("Package '%s' is virtual.") % apturl.package) + continue + else: + ui.error(_("Could not find package '%s'.") + % apturl.package) + continue + + if (self.cache[apturl.package].is_installed and + apturl.minver is None): + ui.message(_("Package '%s' is already installed") + % apturl.package) + continue + + # ask the user + pkg = self.cache[apturl.package] + (sum, desc, homepage) = Helpers.parse_pkg(pkg) + if not ui.askInstallPackage(apturl.package, sum, desc, homepage): + ret = RESULT_CANCELT + continue + + # try to install it + try: + self.cache[apturl.package].mark_install() + except SystemError as e: + ui.error(_("Can not install '%s' (%s) ") % (apturl.package, e)) + continue + if apturl.minver is not None: + verStr = self.cache[apturl.package].candidate.version + if apt_pkg.version_compare(verStr, apturl.minver) < 1: + ui.error(_( + "Package '%s' requests minimal version '%s', but " + "only '%s' is available") % (apturl.package, + apturl.minver, + verStr)) + continue + + changes = self.cache.get_changes() + additional_pkgs = [] + + for pkg in changes: + if pkg.marked_install: + additional_pkgs.append(pkg.name) + + # install it + ui.doInstall(apturl, extra_pkg_names=additional_pkgs) + + if not self.verifyInstall(apturl): + ret = RESULT_ERROR + + # return values + return ret diff --git a/AptUrl/Helpers.py b/AptUrl/Helpers.py new file mode 100644 index 0000000..d00b862 --- /dev/null +++ b/AptUrl/Helpers.py @@ -0,0 +1,66 @@ +# Copyright (c) 2008 Canonical +# +# AUTHOR: +# Siegfried-A. Gevatter <rainct@ubuntu.com> +# +# This file is part of AptUrl +# +# AptUrl is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as published +# by the Free Software Foundation; either version 2 of the License, or (at +# your option) any later version. +# +# AptUrl is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with AptUrl; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +import gettext +import subprocess + +def _(str): + return utf8(gettext.gettext(str)) + +def _n(singular, plural, n): + return utf8(gettext.ngettext(singular, plural, n)) + +def utf8(str): + if str is bytes: + try: + return str.decode('UTF-8') + except UnicodeDecodeError: + # assume latin1 as fallback + return str.decode('latin1') + else: + return str + +def get_dist(): + return subprocess.check_output( + 'lsb_release -c -s'.split(), + universal_newlines=True).strip() + + +def parse_pkg(pkgobj): + summary = "" + description = "" + pkg_description = pkgobj.candidate.description + if pkg_description.count("\n") > 0: + summary, description = pkg_description.split('\n', 1) + else: + summary = pkg_description + lines = description.rstrip('\n').split('\n') + if len(lines) > 1 and lines[-1].startswith('Homepage: '): + homepage = lines[-1].split(' ', 1)[1] + description = '\n'.join(lines[:-1]) + else: + homepage = pkgobj.candidate.homepage + return (summary, description, homepage) + +def format_description(description): + const = 'APTURL_DOUBLE_EMPTY_LINE_PLACEHOLDER' + return description.replace('\n\n', const).replace('\n', ' ').replace( + const, '\n\n') diff --git a/AptUrl/Parser.py b/AptUrl/Parser.py new file mode 100644 index 0000000..0543d07 --- /dev/null +++ b/AptUrl/Parser.py @@ -0,0 +1,153 @@ +# Copyright (c) 2007-2008 Canonical +# +# AUTHOR: +# Michael Vogt <mvo@ubuntu.com> +# With contributions by Siegfried-A. Gevatter <rainct@ubuntu.com> +# +# This file is part of AptUrl +# +# AptUrl is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as published +# by the Free Software Foundation; either version 2 of the License, or (at +# your option) any later version. +# +# AptUrl is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with AptUrl; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +import os +import string +from string import Template +from .Helpers import get_dist +from .Helpers import _ + +class InvalidUrlException(Exception): + def __init__(self, url, msg=""): + self.url = url + self.message = msg + def __str__(self): + return self.message + +MAX_URL_LEN=255 + +# substituion mapping +apturl_substitution_mapping = { + "distro" : get_dist(), + "kernel" : os.uname()[2] +} + +# whitelist for the uri +whitelist = [] +whitelist.extend(string.ascii_letters) +whitelist.extend(string.digits) +whitelist.extend(['_',':','?','/','+','.','~','=','<','>','-',',','$','&']) + + +class AptUrl(object): + " a class that contains the parsed data from an apt url " + def __init__(self): + self.package = None + self.schema = None + self.minver = None + self.refresh = None + # for added repos + self.keyfile = None + self.repo_url = None + self.dist = '/' + # for known sections + self.section = [] + # for known channels + self.channel = None + +def is_format_package_name(string): + " return True if string would be an acceptable name for a Debian package " + + return (string.replace("+", "").replace("-", "").replace(".", "").replace(":", "").isalnum() + and string.islower() and string[0].isalnum() and len(string) > 1) + +def do_apt_url_substitution(apt_url, mapping): + " substitute known templates against the field package and channel " + for field in ["package","channel"]: + if getattr(apt_url, field): + s=Template(getattr(apt_url, field)) + setattr(apt_url, field, s.substitute(mapping)) + +def match_against_whitelist(raw_url): + " test if the url matches the internal whitelist " + for char in raw_url: + if not char in whitelist: + raise InvalidUrlException( + raw_url, _("Non whitelist char in the uri")) + return True + +def set_value(apt_url, s): + " set a key,value pair from string s to AptUrl object " + (key, value) = s.split("=") + try: + if ' ' in value: + raise InvalidUrlException(apt_url, _("Whitespace in key=value")) + if type(getattr(apt_url, key)) == type([]): + getattr(apt_url, key).append(value) + else: + setattr(apt_url, key, value) + except Exception as e: + raise InvalidUrlException(apt_url, _("Exception '%s'") % e) + + +def parse(full_url, mapping=apturl_substitution_mapping): + " parse an apt url and return a list of AptUrl objects " + # apt:pkg1?k11=v11?k12=v12,pkg2?k21=v21?k22=v22,... + res = [] + + if len(full_url) > MAX_URL_LEN: + url = "%s ..." % full_url[0:(MAX_URL_LEN // 10)] + raise InvalidUrlException(url, _("Url string '%s' too long") % url) + + # check against whitelist + match_against_whitelist(full_url) + for url in full_url.split(";"): + if not ":" in url: + raise InvalidUrlException(url, _("No ':' in the uri")) + + # now parse it + + (schema, packages) = url.split(":", 1) + packages = packages.split(",") + + for package in packages: + apt_url = AptUrl() + apt_url.schema = schema + # check for schemas of the form: apt+http:// + if schema.startswith("apt+"): + apt_url.repo_url = schema[len("apt+"):] + ":" + package.split("?",1)[0] + else: + if "?" in package: + apt_url.package = package.split("?")[0].lstrip("/") + else: + apt_url.package = package.lstrip("/") + + # now parse the ?... bits + if "?" in package: + key_value_pairs = package.split("?")[1:] + for s in key_value_pairs: + if "&" in s: + and_key_value_pairs = s.split("&") + for s in and_key_value_pairs: + set_value(apt_url, s) + else: + set_value(apt_url, s) + + # do substitution (if needed) + do_apt_url_substitution(apt_url, mapping) + + # check if the package name is valid + if not is_format_package_name(apt_url.package): + raise InvalidUrlException(url, "Invalid package name '%s'" % apt_url.package) + + res.append(apt_url) + return res diff --git a/AptUrl/UI.py b/AptUrl/UI.py new file mode 100644 index 0000000..0fd502c --- /dev/null +++ b/AptUrl/UI.py @@ -0,0 +1,23 @@ + +from .Helpers import _, _n + +class AbstractUI(object): + # generic dialogs + def error(self, summary, msg): + return False + def yesNoQuestion(self, summary, msg, title, default='no'): + pass + def message(self, summary, msg): + return True + + def askInstallPackage(self): + pass + + # install/update progress + def doUpdate(self): + pass + def doInstall(self, apturl, extra_pkg_names=None): + pass + + # UI specific actions for enabling stuff + diff --git a/AptUrl/Version.py b/AptUrl/Version.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/AptUrl/Version.py diff --git a/AptUrl/__init__.py b/AptUrl/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/AptUrl/__init__.py diff --git a/AptUrl/gtk/GtkUI.py b/AptUrl/gtk/GtkUI.py new file mode 100644 index 0000000..dcc905f --- /dev/null +++ b/AptUrl/gtk/GtkUI.py @@ -0,0 +1,158 @@ +import gi +gi.require_version('Gtk', '3.0') +gi.require_version('XApp', '1.0') +from gi.repository import Gtk +from gi.repository import Gdk +from gi.repository import GObject +from gi.repository import XApp +GObject.threads_init() + +import os +import sys +import apt_pkg +import subprocess +import tempfile + +from AptUrl.UI import AbstractUI +from AptUrl import Helpers +from AptUrl.Helpers import _ + +import mintcommon.aptdaemon + +APTURL_UI_FILE = os.environ.get( + # Set this envar to use a test .ui file. + 'APTURL_UI_FILE', + # System file to use if the envar is not set. + '/usr/share/apturl/apturl-gtk.ui' + ) + + +class GtkUI(AbstractUI): + def __init__(self): + Gtk.init_check(sys.argv) + # create empty dialog + self.dia_xml = Gtk.Builder() + self.dia_xml.set_translation_domain("apturl") + self.dia_xml.add_from_file(APTURL_UI_FILE) + self.dia = self.dia_xml.get_object('confirmation_dialog') + self.dia.start_available = lambda: Gtk.main_quit() + self.dia.start_error = lambda: Gtk.main_quit() + self.dia.exit = lambda: Gtk.main_quit() + self.dia.realize() + self.require_update = False + + # generic dialogs + def _get_dialog(self, dialog_type, summary, msg="", + buttons=Gtk.ButtonsType.CLOSE): + " internal helper for dialog construction " + d = Gtk.MessageDialog(parent=self.dia, + flags=Gtk.DialogFlags.MODAL, + type=dialog_type, + buttons=buttons) + d.set_title("") + d.set_markup("<big><b>%s</b></big>\n\n%s" % (summary, msg)) + XApp.set_window_icon_name(d, "package-x-generic") + d.set_keep_above(True) + d.realize() + d.get_window().set_functions(Gdk.WMFunction.MOVE) + return d + + def error(self, summary, msg=""): + d = self._get_dialog(Gtk.MessageType.ERROR, summary, msg) + d.run() + d.destroy() + return False + + def message(self, summary, msg="", title=""): + d = self._get_dialog(Gtk.MessageType.INFO, summary, msg) + d.set_title(title) + d.run() + d.destroy() + return True + + def yesNoQuestion(self, summary, msg, title="", default='no'): + d = self._get_dialog(Gtk.MessageType.QUESTION, summary, msg, + buttons=Gtk.ButtonsType.YES_NO) + d.set_title(title) + res = d.run() + d.destroy() + if res != Gtk.ResponseType.YES: + return False + return True + + def askInstallPackage(self, package, summary, description, homepage): + # populate the dialog + dia = self.dia + dia_xml = self.dia_xml + header = _("Install additional software?") + body = _("Do you want to install package '%s'?") % package + dia.set_title(package) + header_label = dia_xml.get_object('header_label') + header_label.set_markup("<b><big>%s</big></b>" % header) + body_label = dia_xml.get_object('body_label') + body_label.set_label(body) + description_text_view = dia_xml.get_object('description_text_view') + tbuf = Gtk.TextBuffer() + desc = "%s\n\n%s" % (summary, Helpers.format_description(description)) + tbuf.set_text(desc) + description_text_view.set_buffer(tbuf) + XApp.set_window_icon_name(dia, "package-x-generic") + + # check if another package manager is already running + # FIXME: just checking for the existance of the file is + # not sufficient, it need to be tested if it can + # be locked via apt_pkg.get_lock() + # - but that needs to run as root + # - a dbus helper might be the best answer here + #args = (update_button_status, dia_xml.get_object("yes_button"), + # dia_xml.get_object("infolabel")) + #args[0](*args[1:]) + #timer_id = GObject.timeout_add(750, *args ) + + # show the dialog + res = dia.run() + #GObject.source_remove(timer_id) + if res != Gtk.ResponseType.YES: + dia.hide() + return False + + return True + + # progress etc + def doUpdate(self): + self.require_update = True + + def doInstall(self, apturl, extra_pkg_names=[]): + self.dia.hide() + packages = [] + packages.append(apturl.package) + packages += extra_pkg_names + self.install_packages(packages) + + def install_packages(self, package_names): + self.apt = mintcommon.aptdaemon.APT(None) + self.package_names = package_names + self.busy = True + if self.require_update: + self.apt.set_finished_callback(self.on_update_before_install_finished) + self.apt.update_cache() + else: + self.on_update_before_install_finished() + while self.busy: + while Gtk.events_pending(): + Gtk.main_iteration() + + def on_update_before_install_finished(self, transaction=None, exit_state=None): + self.apt.set_finished_callback(self.on_install_finished) + self.apt.set_cancelled_callback(self.on_install_finished) + self.apt.install_packages(self.package_names) + + def on_install_finished(self, transaction=None, exit_state=None): + del self.package_names + del self.apt + self.busy = False + self.dia.exit() + +if __name__ == "__main__": + ui = GtkUI() + ui.error("foo","bar") diff --git a/AptUrl/gtk/__init__.py b/AptUrl/gtk/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/AptUrl/gtk/__init__.py |