source: trunk/moap/util/deps.py @ 204

Revision 204, 2.8 KB checked in by thomas, 6 years ago (diff)
  • moap/main.py (main): Handle import errors.
  • moap/util/deps.py (Dependency, Dependency.install, RDF, RDF.fedora_install, genshi, genshi.fedora_install, handleImportError): Flesh out dependency checking code more, using the new distro code.
  • moap/util/Makefile.am:
  • moap/util/distro.py (Distro, Distro.init, getSysName, getMachine, getDistroFromRelease, _fedora_getVersionFromRelease): Add a distro module to figure out what distro we're on, inspired by codeina.
Line 
1# -*- Mode: Python -*-
2# vi:si:et:sw=4:sts=4:ts=4
3
4# code to handle import errors and tell us more information about the
5# missing dependency
6
7from moap.util import distro
8
9import sys
10
11class Dependency:
12    module = None
13    name = None
14    homepage = None
15
16    def install(self, distro):
17        """
18        Return an explanation on how to install the given dependency
19        for the given distro/version/arch.
20
21        @rtype:   str or None
22        @returns: an explanation on how to install the dependency, or None.
23        """
24        name = distro.tag + '_install'
25        m = getattr(self, name, None)
26        if m:
27            return m(distro)
28
29
30class RDF(Dependency):
31    module = 'RDF'
32    name = "Redland RDF Python Bindings"
33    homepage = "http://librdf.org/docs/python.html"
34
35    def fedora_install(self, distro):
36        if distro.version >= '5':
37            return "You can install RDF on Fedora with:\n" \
38                "su -c \"yum install python-redland\" -"
39
40        return "Your version of Fedora does not have python-redland available."
41
42
43class genshi(Dependency):
44    module = 'genshii'
45    name = "Genshi templating language"
46    homepage = "http://genshi.edgewall.com/"
47
48    def fedora_install(self, distro):
49        return "genshi is not yet available in Fedora Extras.\n"
50
51def handleImportError(exception):
52    """
53    Handle dependency import errors by displaying more information about
54    the dependency.
55    """
56    first = exception.args[0]
57    if first.find('No module named ') < 0:
58        raise
59    module = first[len('No module named '):]
60    module = module.split('.')[0]
61    deps = {}
62    for dep in [RDF(), genshi()]:
63        deps[dep.module] = dep
64
65    if module in deps.keys():
66        dep = deps[module]
67        sys.stderr.write("Could not import python module '%s'\n" % module)
68        sys.stderr.write('This module is part of %s.\n' % dep.name)
69        if dep.homepage:
70            sys.stderr.write('See %s for more information.\n\n' % dep.homepage)
71
72        d = distro.getDistroFromRelease()
73        if d:
74            howto = dep.install(d)
75            if howto:
76                sys.stderr.write(howto)
77            else:
78                sys.stderr.write(
79                    "On %s, MOAP does not know how to install %s.\n" 
80                    "Please file a bug so we can add this dependency.\n" % (
81                        d.name, dep.module))
82        else:
83            sys.stderr.write("""MOAP does not know your distribution.
84Please file a bug so we can add your distribution.
85""")
86
87        sys.stderr.write('\n')
88
89        sys.stderr.write('Please install %s and try again.\n' % module)
90        sys.stderr.write(
91            'You can confirm it is installed by starting Python and running:\n')
92        sys.stderr.write('import %s\n' % module)
93
94
95        return
96
97    raise
Note: See TracBrowser for help on using the repository browser.