]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - misc/coding_tools/check-interfaces.py
Merge pull request #236 from daira/2725.timezone-test.0
[tahoe-lafs/tahoe-lafs.git] / misc / coding_tools / check-interfaces.py
1
2 # To check a particular Tahoe source distribution, this should be invoked from
3 # the root directory of that distribution as
4 #
5 #   bin/tahoe @misc/coding_tools/check-interfaces.py
6
7 import os, sys, re, platform
8
9 import zope.interface as zi
10 # We use the forked version of verifyClass below.
11 #from zope.interface.verify import verifyClass
12 from zope.interface.advice import addClassAdvisor
13
14
15 interesting_modules = re.compile(r'(allmydata)|(foolscap)\..*')
16 excluded_classnames = re.compile(r'(_)|(Mock)|(Fake)|(Dummy).*')
17 excluded_file_basenames = re.compile(r'(check)|(bench)_.*')
18
19
20 _other_modules_with_violations = set()
21 _err = sys.stderr
22 _report_argname_mismatch = False  # very noisy and usually not important
23
24
25 # deep magic
26 def strictly_implements(*interfaces):
27     frame = sys._getframe(1)
28     f_locals = frame.f_locals
29
30     # Try to make sure we were called from a class def. Assumes Python > 2.2.
31     if f_locals is frame.f_globals or '__module__' not in f_locals:
32         raise TypeError("implements can be used only from a class definition.")
33
34     if '__implements_advice_data__' in f_locals:
35         raise TypeError("implements can be used only once in a class definition.")
36
37     def _implements_advice(cls):
38         interfaces, classImplements = cls.__dict__['__implements_advice_data__']
39         del cls.__implements_advice_data__
40         classImplements(cls, *interfaces)
41
42         if interesting_modules.match(cls.__module__):
43             if not excluded_classnames.match(cls.__name__):
44                 for interface in interfaces:
45                     try:
46                         verifyClass(interface, cls)
47                     except Exception, e:
48                         print >>_err, ("%s.%s does not correctly implement %s.%s:\n%s"
49                                        % (cls.__module__, cls.__name__,
50                                           interface.__module__, interface.__name__, e))
51         else:
52             _other_modules_with_violations.add(cls.__module__)
53         return cls
54
55     f_locals['__implements_advice_data__'] = interfaces, zi.classImplements
56     addClassAdvisor(_implements_advice, depth=2)
57
58
59 def check():
60     # patchee-monkey
61     zi.implements = strictly_implements
62
63     if len(sys.argv) >= 2:
64         if sys.argv[1] == '--help' or len(sys.argv) > 2:
65             print >>_err, "Usage: check-miscaptures.py [SOURCEDIR]"
66             return
67         srcdir = sys.argv[1]
68     else:
69         # import modules under src/ by default
70         srcdir = 'src'
71
72     # attempt to avoid side-effects from importing command scripts
73     sys.argv = ['', '--help']
74
75     syslow = platform.system().lower()
76     is_windows = 'cygwin' in syslow or 'windows' in syslow
77
78     for (dirpath, dirnames, filenames) in os.walk(srcdir):
79         for fn in filenames:
80             (basename, ext) = os.path.splitext(fn)
81             if ext in ('.pyc', '.pyo') and not os.path.exists(os.path.join(dirpath, basename+'.py')):
82                 print >>_err, ("Warning: no .py source file for %r.\n"
83                                % (os.path.join(dirpath, fn),))
84
85             if ext == '.py' and not excluded_file_basenames.match(basename):
86                 relpath = os.path.join(dirpath[len(srcdir)+1:], basename)
87                 module = relpath.replace(os.sep, '/').replace('/', '.')
88                 try:
89                     __import__(module)
90                 except ImportError, e:
91                     if not is_windows and (' _win' in str(e) or 'win32' in str(e)):
92                         print >>_err, ("Warning: %r imports a Windows-specific module, so we cannot check it (%s).\n"
93                                        % (module, str(e)))
94                     else:
95                         import traceback
96                         traceback.print_exc(file=_err)
97                         print >>_err
98
99     others = list(_other_modules_with_violations)
100     others.sort()
101     print >>_err, "There were also interface violations in:\n", ", ".join(others), "\n"
102
103
104 # Forked from
105 # http://svn.zope.org/*checkout*/Zope3/trunk/src/zope/interface/verify.py?content-type=text%2Fplain&rev=27687
106 # but modified to report all interface violations rather than just the first.
107
108 ##############################################################################
109 #
110 # Copyright (c) 2001, 2002 Zope Corporation and Contributors.
111 # All Rights Reserved.
112 #
113 # This software is subject to the provisions of the Zope Public License,
114 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
115 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
116 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
117 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
118 # FOR A PARTICULAR PURPOSE.
119 #
120 ##############################################################################
121 """Verify interface implementations
122
123 $Id$
124 """
125 from zope.interface.exceptions import DoesNotImplement
126 from zope.interface.exceptions import BrokenMethodImplementation
127 from types import FunctionType, MethodType
128 from zope.interface.interface import fromMethod, fromFunction, Method
129
130 # This will be monkey-patched when running under Zope 2, so leave this
131 # here:
132 MethodTypes = (MethodType, )
133
134
135 def _verify(iface, candidate, tentative=0, vtype=None):
136     """Verify that 'candidate' might correctly implements 'iface'.
137
138     This involves:
139
140       o Making sure the candidate defines all the necessary methods
141
142       o Making sure the methods have the correct signature
143
144       o Making sure the candidate asserts that it implements the interface
145
146     Note that this isn't the same as verifying that the class does
147     implement the interface.
148
149     If optional tentative is true, suppress the "is implemented by" test.
150     """
151
152     if vtype == 'c':
153         tester = iface.implementedBy
154     else:
155         tester = iface.providedBy
156
157     violations = []
158     def format(e):
159         return "    " + str(e).strip() + "\n"
160
161     if not tentative and not tester(candidate):
162         violations.append(format(DoesNotImplement(iface)))
163
164     # Here the `desc` is either an `Attribute` or `Method` instance
165     for name, desc in iface.namesAndDescriptions(1):
166         if not hasattr(candidate, name):
167             if (not isinstance(desc, Method)) and vtype == 'c':
168                 # We can't verify non-methods on classes, since the
169                 # class may provide attrs in it's __init__.
170                 continue
171
172             if isinstance(desc, Method):
173                 violations.append("    The %r method was not provided.\n" % (name,))
174             else:
175                 violations.append("    The %r attribute was not provided.\n" % (name,))
176             continue
177
178         attr = getattr(candidate, name)
179         if not isinstance(desc, Method):
180             # If it's not a method, there's nothing else we can test
181             continue
182
183         if isinstance(attr, FunctionType):
184             # should never get here, since classes should not provide functions
185             meth = fromFunction(attr, iface, name=name)
186         elif (isinstance(attr, MethodTypes)
187               and type(attr.im_func) is FunctionType):
188             meth = fromMethod(attr, iface, name)
189         else:
190             if not callable(attr):
191                 violations.append(format(BrokenMethodImplementation(name, "Not a method")))
192             # sigh, it's callable, but we don't know how to intrspect it, so
193             # we have to give it a pass.
194             continue
195
196         # Make sure that the required and implemented method signatures are
197         # the same.
198         desc = desc.getSignatureInfo()
199         meth = meth.getSignatureInfo()
200
201         mess = _incompat(desc, meth)
202         if mess:
203             violations.append(format(BrokenMethodImplementation(name, mess)))
204
205     if violations:
206         raise Exception("".join(violations))
207     return True
208
209 def verifyClass(iface, candidate, tentative=0):
210     return _verify(iface, candidate, tentative, vtype='c')
211
212 def verifyObject(iface, candidate, tentative=0):
213     return _verify(iface, candidate, tentative, vtype='o')
214
215 def _incompat(required, implemented):
216     if len(implemented['required']) > len(required['required']):
217         return 'implementation requires too many arguments'
218     if ((len(implemented['positional']) < len(required['positional']))
219         and not implemented['varargs']):
220         return "implementation doesn't allow enough arguments"
221     if required['kwargs'] and not implemented['kwargs']:
222         return "implementation doesn't support keyword arguments"
223     if required['varargs'] and not implemented['varargs']:
224         return "implementation doesn't support variable arguments"
225     if (_report_argname_mismatch and required['positional'] !=
226         implemented['positional'][:len(required['positional'])]
227         and implemented['kwargs'] is None):
228         return 'implementation has different argument names'
229
230
231 if __name__ == "__main__":
232     check()
233     # Avoid spurious warnings about ignored exceptions during shutdown by doing a hard exit.
234     os._exit(0)