[meld/flatpak-ci] bin/meld: Rewrite dependency checking logic
- From: Kai Willadsen <kaiw src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [meld/flatpak-ci] bin/meld: Rewrite dependency checking logic
- Date: Fri, 20 Dec 2019 20:52:29 +0000 (UTC)
commit b0d03b3b8b72d3c9175744fda8ef58d1a20562aa
Author: Kai Willadsen <kai willadsen gmail com>
Date: Sat Dec 21 06:49:30 2019 +1000
bin/meld: Rewrite dependency checking logic
This isn't any shorter, but hopefully the single list of dependencies
makes it much more obvious what's going on.
bin/meld | 101 ++++++++++++++++++++++++++++++++-------------------------------
1 file changed, 52 insertions(+), 49 deletions(-)
---
diff --git a/bin/meld b/bin/meld
index 6627d176..f5da8f68 100755
--- a/bin/meld
+++ b/bin/meld
@@ -201,57 +201,60 @@ def show_error_and_exit(error_text):
def check_requirements():
- cairo_requirement = (1, 15)
- gtk_requirement = (3, 20)
- glib_requirement = (2, 48)
- gtksourceview_requirement = (4, 0, 0)
- pygobject_requirement = (3, 30, 0)
-
- def missing_reqs(mod, ver, exc=None):
- if isinstance(exc, ImportError):
- show_error_and_exit(_("Cannot import: ") + mod + "\n" + str(exc))
- else:
- modver = mod + " " + ".".join(map(str, ver))
- show_error_and_exit(_("Meld requires %s or higher.") % modver)
-
- if sys.version_info[:2] < meld.conf.PYTHON_REQUIREMENT_TUPLE:
- missing_reqs("Python", meld.conf.PYTHON_REQUIREMENT_TUPLE)
-
- try:
- import gi
- assert gi.version_info >= pygobject_requirement
- except (ImportError, AssertionError, ValueError) as e:
- missing_reqs("pygobject", pygobject_requirement, e)
-
- # gtk+ and related imports
- try:
- # FIXME: Extra clause for gi
- import gi
- gi.require_version("Gtk", "3.0")
- from gi.repository import Gtk
- version = (Gtk.get_major_version(), Gtk.get_minor_version())
- assert version >= gtk_requirement
- except (ImportError, AssertionError, ValueError) as e:
- missing_reqs("GTK+", gtk_requirement, e)
-
- try:
- from gi.repository import GLib
- assert (GLib.MAJOR_VERSION, GLib.MINOR_VERSION) >= glib_requirement
- except (ImportError, AssertionError, ValueError) as e:
- missing_reqs("GLib", glib_requirement, e)
-
- try:
- gi.require_version('GtkSource', '4')
- from gi.repository import GtkSource
- GtkSource.init()
- except (ImportError, AssertionError, ValueError) as e:
- missing_reqs("GtkSourceView 4", gtksourceview_requirement, e)
+ import importlib
+ from typing import NamedTuple, Optional
+
+ class Requirement(NamedTuple):
+ name: str
+ module: Optional[str]
+ gi_version: Optional[str]
+ major_version: int
+ minor_version: int
+
+ def check(self):
+ if self.gi_version:
+ import gi
+ gi.require_version(self.module, self.gi_version)
+ module = 'gi.repository.{}'.format(self.module)
+ else:
+ module = self.module
+
+ mod = importlib.import_module(module)
+ if mod.__name__ == 'gi.repository.Gtk':
+ version = (mod.get_major_version(), mod.get_minor_version())
+ elif mod.__name__ == 'gi.repository.GLib':
+ version = (mod.MAJOR_VERSION, mod.MINOR_VERSION)
+ elif mod.__name__ == 'gi.repository.GtkSource':
+ # There's no accessors for GtkSource, so we try a 4.0 API
+ # call and just set the version so it will pass
+ mod.init()
+ version = (self.major_version, self.minor_version)
+ elif mod.__name__ in ('cairo', 'gi', 'sys'):
+ version = mod.version_info
+
+ if version < (self.major_version, self.minor_version):
+ raise RuntimeError('Unsupported version')
+
+ def __str__(self):
+ return '{} {}.{}'.format(
+ self.name, self.major_version, self.minor_version)
+
+ # These requirements are kept in both `bin/meld` and `meson.build`. If you
+ # update one, update the other.
+ requirements = (
+ Requirement('Python', 'sys', None, 3, 6),
+ Requirement('Gtk+', 'Gtk', '3.0', 3, 20),
+ Requirement('GLib', 'GLib', '2.0', 2, 48),
+ Requirement('GtkSourceView', 'GtkSource', '4', 4, 0),
+ Requirement('pygobject', 'gi', None, 3, 30),
+ Requirement('pycairo', 'cairo', None, 1, 15),
+ )
try:
- import cairo
- assert cairo.version_info >= cairo_requirement
- except (ImportError, AssertionError) as e:
- missing_reqs("cairo", cairo_requirement, e)
+ for r in requirements:
+ r.check()
+ except (AttributeError, ImportError, RuntimeError, ValueError):
+ show_error_and_exit(_('Meld requires {} or higher.'.format(r)))
def setup_resources():
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]