2963
|
1 |
#!/usr/bin/env python
|
3617
|
2 |
#Copyright ReportLab Europe Ltd. 2000-2012
|
2963
|
3 |
#see license.txt for license details
|
|
4 |
|
|
5 |
"""This is a test on a package level that find all modules,
|
|
6 |
classes, methods and functions that do not have a doc string
|
|
7 |
and lists them in individual log files.
|
|
8 |
|
|
9 |
Currently, methods with leading and trailing double underscores
|
|
10 |
are skipped.
|
|
11 |
"""
|
3075
|
12 |
from reportlab.lib.testutils import setOutDir,SecureTestCase, GlobDirectoryWalker, outputfile, printLocation
|
2984
|
13 |
setOutDir(__name__)
|
3794
|
14 |
import os, sys, glob, re, unittest
|
2963
|
15 |
from types import ModuleType, ClassType, MethodType, FunctionType
|
|
16 |
import reportlab
|
|
17 |
|
|
18 |
def getModuleObjects(folder, rootName, typ, pattern='*.py'):
|
|
19 |
"Get a list of all objects defined *somewhere* in a package."
|
|
20 |
objects = []
|
|
21 |
lookup = {}
|
|
22 |
for file in GlobDirectoryWalker(folder, pattern):
|
|
23 |
folder = os.path.dirname(file)
|
|
24 |
|
|
25 |
if os.path.basename(file) == '__init__.py':
|
|
26 |
continue
|
|
27 |
|
|
28 |
## if os.path.exists(os.path.join(folder, '__init__.py')):
|
|
29 |
#### print 'skipping', os.path.join(folder, '__init__.py')
|
|
30 |
## continue
|
|
31 |
|
|
32 |
sys.path.insert(0, folder)
|
|
33 |
cwd = os.getcwd()
|
|
34 |
os.chdir(folder)
|
|
35 |
|
|
36 |
modName = os.path.splitext(os.path.basename(file))[0]
|
3794
|
37 |
prefix = folder[folder.find(rootName):]
|
|
38 |
prefix = prefix.replace(os.sep,'.')
|
2963
|
39 |
mName = prefix + '.' + modName
|
|
40 |
|
|
41 |
try:
|
|
42 |
module = __import__(mName)
|
|
43 |
except ImportError:
|
|
44 |
# Restore sys.path and working directory.
|
|
45 |
os.chdir(cwd)
|
|
46 |
del sys.path[0]
|
|
47 |
continue
|
|
48 |
|
|
49 |
# Get the 'real' (leaf) module
|
|
50 |
# (__import__ loads only the top-level one).
|
3794
|
51 |
if mName.find('.') != -1:
|
|
52 |
for part in mName.split('.')[1:]:
|
2963
|
53 |
module = getattr(module, part)
|
|
54 |
|
|
55 |
# Find the objects in the module's content.
|
|
56 |
modContentNames = dir(module)
|
|
57 |
|
|
58 |
# Handle modules.
|
|
59 |
if typ == ModuleType:
|
3794
|
60 |
if module.__name__.find('reportlab') > -1:
|
2963
|
61 |
objects.append((mName, module))
|
|
62 |
continue
|
|
63 |
|
|
64 |
for n in modContentNames:
|
|
65 |
obj = eval(mName + '.' + n)
|
|
66 |
# Handle functions and classes.
|
|
67 |
if typ in (FunctionType, ClassType):
|
3326
|
68 |
if type(obj) == typ and obj not in lookup:
|
2963
|
69 |
if typ == ClassType:
|
3794
|
70 |
if obj.__module__.find(rootName) != 0:
|
2963
|
71 |
continue
|
|
72 |
objects.append((mName, obj))
|
|
73 |
lookup[obj] = 1
|
|
74 |
# Handle methods.
|
|
75 |
elif typ == MethodType:
|
|
76 |
if type(obj) == ClassType:
|
|
77 |
for m in dir(obj):
|
|
78 |
a = getattr(obj, m)
|
3326
|
79 |
if type(a) == typ and a not in lookup:
|
3794
|
80 |
if a.__self__.__class__.__module__.find(rootName) != 0:
|
2963
|
81 |
continue
|
|
82 |
cName = obj.__name__
|
|
83 |
objects.append((mName, a))
|
|
84 |
lookup[a] = 1
|
|
85 |
|
|
86 |
# Restore sys.path and working directory.
|
|
87 |
os.chdir(cwd)
|
|
88 |
del sys.path[0]
|
|
89 |
return objects
|
|
90 |
|
|
91 |
class DocstringTestCase(SecureTestCase):
|
|
92 |
"Testing if objects in the ReportLab package have docstrings."
|
|
93 |
|
|
94 |
def _writeLogFile(self, objType):
|
|
95 |
"Write log file for different kind of documentable objects."
|
|
96 |
|
|
97 |
cwd = os.getcwd()
|
3075
|
98 |
from reportlab.lib.testutils import RL_HOME
|
2963
|
99 |
objects = getModuleObjects(RL_HOME, 'reportlab', objType)
|
|
100 |
objects.sort()
|
|
101 |
os.chdir(cwd)
|
|
102 |
|
|
103 |
expl = {FunctionType:'functions',
|
|
104 |
ClassType:'classes',
|
|
105 |
MethodType:'methods',
|
|
106 |
ModuleType:'modules'}[objType]
|
|
107 |
|
|
108 |
path = outputfile("test_docstrings-%s.log" % expl)
|
|
109 |
file = open(path, 'w')
|
|
110 |
file.write('No doc strings found for the following %s below.\n\n' % expl)
|
|
111 |
p = re.compile('__.+__')
|
|
112 |
|
|
113 |
lines = []
|
|
114 |
for name, obj in objects:
|
|
115 |
if objType == MethodType:
|
|
116 |
n = obj.__name__
|
|
117 |
# Skip names with leading and trailing double underscores.
|
|
118 |
if p.match(n):
|
|
119 |
continue
|
|
120 |
|
|
121 |
if objType == FunctionType:
|
|
122 |
if not obj.__doc__ or len(obj.__doc__) == 0:
|
|
123 |
lines.append("%s.%s\n" % (name, obj.__name__))
|
|
124 |
else:
|
|
125 |
if not obj.__doc__ or len(obj.__doc__) == 0:
|
|
126 |
if objType == ClassType:
|
|
127 |
lines.append("%s.%s\n" % (obj.__module__, obj.__name__))
|
|
128 |
elif objType == MethodType:
|
3721
|
129 |
lines.append("%s.%s\n" % (obj.__self__.__class__, obj.__name__))
|
2963
|
130 |
else:
|
|
131 |
lines.append("%s\n" % (obj.__name__))
|
|
132 |
|
|
133 |
lines.sort()
|
|
134 |
for line in lines:
|
|
135 |
file.write(line)
|
|
136 |
|
|
137 |
file.close()
|
|
138 |
|
|
139 |
def test0(self):
|
|
140 |
"Test if functions have a doc string."
|
|
141 |
self._writeLogFile(FunctionType)
|
|
142 |
|
|
143 |
def test1(self):
|
|
144 |
"Test if classes have a doc string."
|
|
145 |
self._writeLogFile(ClassType)
|
|
146 |
|
|
147 |
def test2(self):
|
|
148 |
"Test if methods have a doc string."
|
|
149 |
self._writeLogFile(MethodType)
|
|
150 |
|
|
151 |
def test3(self):
|
|
152 |
"Test if modules have a doc string."
|
|
153 |
self._writeLogFile(ModuleType)
|
|
154 |
|
|
155 |
def makeSuite():
|
|
156 |
suite = unittest.TestSuite()
|
|
157 |
loader = unittest.TestLoader()
|
|
158 |
if sys.platform[:4] != 'java': suite.addTest(loader.loadTestsFromTestCase(DocstringTestCase))
|
|
159 |
return suite
|
|
160 |
|
|
161 |
#noruntests
|
|
162 |
if __name__ == "__main__":
|
|
163 |
unittest.TextTestRunner().run(makeSuite())
|
|
164 |
printLocation()
|