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

Revision 252, 4.6 KB checked in by thomas, 6 years ago (diff)
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
7import os
8import sys
9import urllib
10
11from moap.util import distro
12
13class Dependency:
14    module = None
15    name = None
16    homepage = None
17
18    def install(self, distro):
19        """
20        Return an explanation on how to install the given dependency
21        for the given distro/version/arch.
22
23        @type  distro: L{distro.Distro}
24
25        @rtype:   str or None
26        @returns: an explanation on how to install the dependency, or None.
27        """
28        name = distro.distributor + '_install'
29        m = getattr(self, name, None)
30        if m:
31            return m(distro)
32
33    def fedora_yum(self, packageName):
34        """
35        Returns a string explaining how to install the given package.
36        """
37        return "On Fedora, you can install %s with:\n" \
38                "su -c \"yum install %s\"" % (self.module, packageName)
39
40class RDF(Dependency):
41    module = 'RDF'
42    name = "Redland RDF Python Bindings"
43    homepage = "http://librdf.org/docs/python.html"
44
45    def FedoraCore_install(self, distro):
46        return "python-redland is not yet available in Fedora Extras.\n"
47
48class Cheetah(Dependency):
49    module = 'Cheetah'
50    name = "Cheetah templating language"
51    homepage = "http://cheetahtemplate.org/"
52
53    def FedoraCore_install(self, distro):
54        if distro.atLeast('4'):
55            return self.fedora_yum('python-cheetah')
56
57        return "python-cheetah is only available in Fedora 4 or newer.\n"
58
59class genshi(Dependency):
60    module = 'genshi'
61    name = "Genshi templating language"
62    homepage = "http://genshi.edgewall.com/"
63
64    def FedoraCore_install(self, distro):
65        return "genshi is not yet available in Fedora Extras.\n"
66
67class pygoogle(Dependency):
68    module = 'pygoogle'
69    name = "A Python Interface to the Google API"
70    homepage = "http://pygoogle.sourceforge.net/"
71
72    def FedoraCore_install(self, distro):
73        return "pygoogle is not yet available in Fedora Extras.\n"
74
75class yahoo(Dependency):
76    module = 'yahoo'
77    name = "A Python Interface to the Yahoo Web API"
78    homepage = "http://developer.yahoo.com/python/"
79
80    def FedoraCore_install(self, distro):
81        return "python-yahoo is not yet available in Fedora Extras.\n"
82
83# non-python dependencies
84class ctags(Dependency):
85    module = 'ctags'
86    name = "Exuberant ctags"
87    homepage = "http://ctags.sourceforge.net/"
88
89    def FedoraCore_install(self, distro):
90        return self.fedora_yum('ctags')
91
92def handleImportError(exception):
93    """
94    Handle dependency import errors by displaying more information about
95    the dependency.
96    """
97    first = exception.args[0]
98    if first.find('No module named ') < 0:
99        raise
100    module = first[len('No module named '):]
101    module = module.split('.')[0]
102    deps = {}
103    for dep in [RDF(), Cheetah(), genshi(), pygoogle(), yahoo()]:
104        deps[dep.module] = dep
105
106    if module in deps.keys():
107        dep = deps[module]
108        sys.stderr.write("Could not import python module '%s'\n" % module)
109        sys.stderr.write('This module is part of %s.\n' % dep.name)
110
111        handleMissingDependency(dep)
112
113        # how to confirm the python module got installed
114        sys.stderr.write(
115            'You can confirm it is installed by starting Python and running:\n')
116        sys.stderr.write('import %s\n' % module)
117
118        return
119
120    # re-raise if we didn't have it
121    raise
122
123def getTicketURL(summary):
124    reporter = os.environ.get('EMAIL_ADDRESS', None)
125    get = "summary=%s" % urllib.quote(summary)
126    if reporter:
127        get += "&reporter=%s" % urllib.quote(reporter)
128    return 'https://thomas.apestaart.org/moap/trac/newticket?' + get
129 
130def handleMissingDependency(dep):
131    if dep.homepage:
132        sys.stderr.write('See %s for more information.\n\n' % dep.homepage)
133
134    d = distro.getDistroFromRelease()
135    if d:
136        howto = dep.install(d)
137        if howto:
138            sys.stderr.write(howto)
139        else:
140            url = getTicketURL('DEP: %s, %s' % (dep.module, d.description))
141            sys.stderr.write("""On %s, MOAP does not know how to install %s.
142Please file a bug at:
143%s
144with instructions on how to install the dependency so we can add it.
145""" % (d.description, dep.module, url))
146    else:
147        url = getTicketURL('DISTRO: Unknown')
148        sys.stderr.write("""MOAP does not know your distribution.
149Please file a bug at:
150%s
151with instructions on how to recognize your distribution so we can add it.
152""" % url)
153
154    sys.stderr.write('\n')
155
156    sys.stderr.write('Please install %s and try again.\n' % dep.module)
Note: See TracBrowser for help on using the repository browser.