~launchpad-pqm/launchpad/devel

10637.3.1 by Guilherme Salgado
Use the default python version instead of a hard-coded version
1
#! /usr/bin/python -S
8687.15.4 by Karl Fogel
Add the copyright header block to more files; tweak format in a few files.
2
#
3
# Copyright 2009 Canonical Ltd.  This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
3851.3.3 by Francis J. Lacoste
Add check-content-interfaces.py utility script.
5
6
"""
7
check-content-interfaces.py - Check Content Interfaces.
8
9
= Launchpad Content Interfaces =
10
11
XXX flacoste 2007/02/21 Ideally this should be a unit test.
12
Unfortunately, it would currently fail. See bug 87199.
13
14
Launchpad is complex piece of software composed of multiple
15
applications. Many components use other components, so it is important
16
for each of them to clearly define the interface it supports and make
17
sure that it respects its contract.
18
3851.3.4 by Francis J. Lacoste
Typos.
19
This is especially important as newcomers joining the team will often look
3851.3.3 by Francis J. Lacoste
Add check-content-interfaces.py utility script.
20
at the interface of a component in another part of Launchpad to know
3851.3.4 by Francis J. Lacoste
Typos.
21
what properties/methods are available on the object.
3851.3.3 by Francis J. Lacoste
Add check-content-interfaces.py utility script.
22
3851.3.4 by Francis J. Lacoste
Typos.
23
Ideally, all components should have a test of this form as part of their
3851.3.3 by Francis J. Lacoste
Add check-content-interfaces.py utility script.
24
system documentation.
25
26
    > > > verifyObject(IContentInterface, object)
27
    True
28
29
This is a fall back test that makes sure that all content classes
3851.3.4 by Francis J. Lacoste
Typos.
30
really do implement the interfaces it declares to.
3851.3.3 by Francis J. Lacoste
Add check-content-interfaces.py utility script.
31
32
It's not because a class implements correctly an interface that
33
verifyObject on its instances would also pass. verifyClass only checks
34
the methods of the interface (since it is possible that some attributes
35
will be provided at construction time). Also additional constraints will
3851.3.4 by Francis J. Lacoste
Typos.
36
be checked on instance attributes that are part of a schema.
3851.3.3 by Francis J. Lacoste
Add check-content-interfaces.py utility script.
37
38
"""
39
40
import _pythonpath
41
42
from zope.interface import implementedBy
43
from zope.interface.exceptions import (
44
    BrokenImplementation, BrokenMethodImplementation)
45
from zope.interface.verify import verifyClass
46
47
import canonical.launchpad.database
48
49
def check_content_classes():
50
    classes_checked = 0
51
    classes_with_failures = 1
52
    for class_name in dir(canonical.launchpad.database):
53
        klass = getattr(canonical.launchpad.database, class_name)
54
        # Skip names that don't implement anything.
55
        if getattr(klass, '__implemented__', None) is None:
56
            continue
57
        for interface in implementedBy(klass):
58
            interface_name = interface.__name__.split('.')[-1]
59
            try:
60
                classes_checked += 1
61
                result = verifyClass(interface, klass)
62
            except BrokenImplementation, e:
63
                classes_with_failures += 1
64
                print "%s fails to implement %s: missing attribute %s" % (
65
                    class_name, interface_name, e.name)
66
            except BrokenMethodImplementation, e:
67
                classes_with_failures += 1
68
                print "%s fails to implement %s: invalid method %s: %s" % (
69
                    class_name, interface_name, e.method, e.mess)
3851.3.4 by Francis J. Lacoste
Typos.
70
    print "** Checked %d content classes. Found %d with broken implementation." % (
3851.3.3 by Francis J. Lacoste
Add check-content-interfaces.py utility script.
71
        classes_checked, classes_with_failures)
72
73
74
if __name__ == '__main__':
3944.1.1 by Francis J. Lacoste
Use system version python2.4 for scripts.
75
    check_content_classes()