PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/documentor/libraries/Sphinx-1.1.3-py3.2/sphinx/ext/coverage.py

https://github.com/tictactatic/Superdesk
Python | 265 lines | 207 code | 36 blank | 22 comment | 76 complexity | a7b1bdc61e55b3f156fbf04bff538855 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0, GPL-2.0
  1. # -*- coding: utf-8 -*-
  2. """
  3. sphinx.ext.coverage
  4. ~~~~~~~~~~~~~~~~~~~
  5. Check Python modules and C API for coverage. Mostly written by Josip
  6. Dzolonga for the Google Highly Open Participation contest.
  7. :copyright: Copyright 2007-2011 by the Sphinx team, see AUTHORS.
  8. :license: BSD, see LICENSE for details.
  9. """
  10. import re
  11. import glob
  12. import inspect
  13. import pickle as pickle
  14. from os import path
  15. from sphinx.builders import Builder
  16. # utility
  17. def write_header(f, text, char='-'):
  18. f.write(text + '\n')
  19. f.write(char * len(text) + '\n')
  20. def compile_regex_list(name, exps, warnfunc):
  21. lst = []
  22. for exp in exps:
  23. try:
  24. lst.append(re.compile(exp))
  25. except Exception:
  26. warnfunc('invalid regex %r in %s' % (exp, name))
  27. return lst
  28. class CoverageBuilder(Builder):
  29. name = 'coverage'
  30. def init(self):
  31. self.c_sourcefiles = []
  32. for pattern in self.config.coverage_c_path:
  33. pattern = path.join(self.srcdir, pattern)
  34. self.c_sourcefiles.extend(glob.glob(pattern))
  35. self.c_regexes = []
  36. for (name, exp) in list(self.config.coverage_c_regexes.items()):
  37. try:
  38. self.c_regexes.append((name, re.compile(exp)))
  39. except Exception:
  40. self.warn('invalid regex %r in coverage_c_regexes' % exp)
  41. self.c_ignorexps = {}
  42. for (name, exps) in self.config.coverage_ignore_c_items.items():
  43. self.c_ignorexps[name] = compile_regex_list(
  44. 'coverage_ignore_c_items', exps, self.warn)
  45. self.mod_ignorexps = compile_regex_list(
  46. 'coverage_ignore_modules', self.config.coverage_ignore_modules,
  47. self.warn)
  48. self.cls_ignorexps = compile_regex_list(
  49. 'coverage_ignore_classes', self.config.coverage_ignore_classes,
  50. self.warn)
  51. self.fun_ignorexps = compile_regex_list(
  52. 'coverage_ignore_functions', self.config.coverage_ignore_functions,
  53. self.warn)
  54. def get_outdated_docs(self):
  55. return 'coverage overview'
  56. def write(self, *ignored):
  57. self.py_undoc = {}
  58. self.build_py_coverage()
  59. self.write_py_coverage()
  60. self.c_undoc = {}
  61. self.build_c_coverage()
  62. self.write_c_coverage()
  63. def build_c_coverage(self):
  64. # Fetch all the info from the header files
  65. c_objects = self.env.domaindata['c']['objects']
  66. for filename in self.c_sourcefiles:
  67. undoc = []
  68. f = open(filename, 'r')
  69. try:
  70. for line in f:
  71. for key, regex in self.c_regexes:
  72. match = regex.match(line)
  73. if match:
  74. name = match.groups()[0]
  75. if name not in c_objects:
  76. for exp in self.c_ignorexps.get(key, ()):
  77. if exp.match(name):
  78. break
  79. else:
  80. undoc.append((key, name))
  81. continue
  82. finally:
  83. f.close()
  84. if undoc:
  85. self.c_undoc[filename] = undoc
  86. def write_c_coverage(self):
  87. output_file = path.join(self.outdir, 'c.txt')
  88. op = open(output_file, 'w')
  89. try:
  90. if self.config.coverage_write_headline:
  91. write_header(op, 'Undocumented C API elements', '=')
  92. op.write('\n')
  93. for filename, undoc in self.c_undoc.items():
  94. write_header(op, filename)
  95. for typ, name in undoc:
  96. op.write(' * %-50s [%9s]\n' % (name, typ))
  97. op.write('\n')
  98. finally:
  99. op.close()
  100. def build_py_coverage(self):
  101. objects = self.env.domaindata['py']['objects']
  102. modules = self.env.domaindata['py']['modules']
  103. skip_undoc = self.config.coverage_skip_undoc_in_source
  104. for mod_name in modules:
  105. ignore = False
  106. for exp in self.mod_ignorexps:
  107. if exp.match(mod_name):
  108. ignore = True
  109. break
  110. if ignore:
  111. continue
  112. try:
  113. mod = __import__(mod_name, fromlist=['foo'])
  114. except ImportError as err:
  115. self.warn('module %s could not be imported: %s' %
  116. (mod_name, err))
  117. self.py_undoc[mod_name] = {'error': err}
  118. continue
  119. funcs = []
  120. classes = {}
  121. for name, obj in inspect.getmembers(mod):
  122. # diverse module attributes are ignored:
  123. if name[0] == '_':
  124. # begins in an underscore
  125. continue
  126. if not hasattr(obj, '__module__'):
  127. # cannot be attributed to a module
  128. continue
  129. if obj.__module__ != mod_name:
  130. # is not defined in this module
  131. continue
  132. full_name = '%s.%s' % (mod_name, name)
  133. if inspect.isfunction(obj):
  134. if full_name not in objects:
  135. for exp in self.fun_ignorexps:
  136. if exp.match(name):
  137. break
  138. else:
  139. if skip_undoc and not obj.__doc__:
  140. continue
  141. funcs.append(name)
  142. elif inspect.isclass(obj):
  143. for exp in self.cls_ignorexps:
  144. if exp.match(name):
  145. break
  146. else:
  147. if full_name not in objects:
  148. if skip_undoc and not obj.__doc__:
  149. continue
  150. # not documented at all
  151. classes[name] = []
  152. continue
  153. attrs = []
  154. for attr_name in dir(obj):
  155. if attr_name not in obj.__dict__:
  156. continue
  157. attr = getattr(obj, attr_name)
  158. if not (inspect.ismethod(attr) or
  159. inspect.isfunction(attr)):
  160. continue
  161. if attr_name[0] == '_':
  162. # starts with an underscore, ignore it
  163. continue
  164. if skip_undoc and not attr.__doc__:
  165. # skip methods without docstring if wished
  166. continue
  167. full_attr_name = '%s.%s' % (full_name, attr_name)
  168. if full_attr_name not in objects:
  169. attrs.append(attr_name)
  170. if attrs:
  171. # some attributes are undocumented
  172. classes[name] = attrs
  173. self.py_undoc[mod_name] = {'funcs': funcs, 'classes': classes}
  174. def write_py_coverage(self):
  175. output_file = path.join(self.outdir, 'python.txt')
  176. op = open(output_file, 'w')
  177. failed = []
  178. try:
  179. if self.config.coverage_write_headline:
  180. write_header(op, 'Undocumented Python objects', '=')
  181. keys = list(self.py_undoc.keys())
  182. keys.sort()
  183. for name in keys:
  184. undoc = self.py_undoc[name]
  185. if 'error' in undoc:
  186. failed.append((name, undoc['error']))
  187. else:
  188. if not undoc['classes'] and not undoc['funcs']:
  189. continue
  190. write_header(op, name)
  191. if undoc['funcs']:
  192. op.write('Functions:\n')
  193. op.writelines(' * %s\n' % x for x in undoc['funcs'])
  194. op.write('\n')
  195. if undoc['classes']:
  196. op.write('Classes:\n')
  197. for name, methods in sorted(
  198. undoc['classes'].items()):
  199. if not methods:
  200. op.write(' * %s\n' % name)
  201. else:
  202. op.write(' * %s -- missing methods:\n\n' % name)
  203. op.writelines(' - %s\n' % x for x in methods)
  204. op.write('\n')
  205. if failed:
  206. write_header(op, 'Modules that failed to import')
  207. op.writelines(' * %s -- %s\n' % x for x in failed)
  208. finally:
  209. op.close()
  210. def finish(self):
  211. # dump the coverage data to a pickle file too
  212. picklepath = path.join(self.outdir, 'undoc.pickle')
  213. dumpfile = open(picklepath, 'wb')
  214. try:
  215. pickle.dump((self.py_undoc, self.c_undoc), dumpfile)
  216. finally:
  217. dumpfile.close()
  218. def setup(app):
  219. app.add_builder(CoverageBuilder)
  220. app.add_config_value('coverage_ignore_modules', [], False)
  221. app.add_config_value('coverage_ignore_functions', [], False)
  222. app.add_config_value('coverage_ignore_classes', [], False)
  223. app.add_config_value('coverage_c_path', [], False)
  224. app.add_config_value('coverage_c_regexes', {}, False)
  225. app.add_config_value('coverage_ignore_c_items', {}, False)
  226. app.add_config_value('coverage_write_headline', True, False)
  227. app.add_config_value('coverage_skip_undoc_in_source', False, False)