kerneldoc.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # coding=utf-8
  2. #
  3. # Copyright © 2016 Intel Corporation
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice (including the next
  13. # paragraph) shall be included in all copies or substantial portions of the
  14. # Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. # IN THE SOFTWARE.
  23. #
  24. # Authors:
  25. # Jani Nikula <jani.nikula@intel.com>
  26. #
  27. # Please make sure this works on both python2 and python3.
  28. #
  29. import codecs
  30. import os
  31. import subprocess
  32. import sys
  33. import re
  34. import glob
  35. from docutils import nodes, statemachine
  36. from docutils.statemachine import ViewList
  37. from docutils.parsers.rst import directives, Directive
  38. from sphinx.ext.autodoc import AutodocReporter
  39. __version__ = '1.0'
  40. class KernelDocDirective(Directive):
  41. """Extract kernel-doc comments from the specified file"""
  42. required_argument = 1
  43. optional_arguments = 4
  44. option_spec = {
  45. 'doc': directives.unchanged_required,
  46. 'functions': directives.unchanged_required,
  47. 'export': directives.unchanged,
  48. 'internal': directives.unchanged,
  49. }
  50. has_content = False
  51. def run(self):
  52. env = self.state.document.settings.env
  53. cmd = [env.config.kerneldoc_bin, '-rst', '-enable-lineno']
  54. filename = env.config.kerneldoc_srctree + '/' + self.arguments[0]
  55. export_file_patterns = []
  56. # Tell sphinx of the dependency
  57. env.note_dependency(os.path.abspath(filename))
  58. tab_width = self.options.get('tab-width', self.state.document.settings.tab_width)
  59. # FIXME: make this nicer and more robust against errors
  60. if 'export' in self.options:
  61. cmd += ['-export']
  62. export_file_patterns = str(self.options.get('export')).split()
  63. elif 'internal' in self.options:
  64. cmd += ['-internal']
  65. export_file_patterns = str(self.options.get('internal')).split()
  66. elif 'doc' in self.options:
  67. cmd += ['-function', str(self.options.get('doc'))]
  68. elif 'functions' in self.options:
  69. for f in str(self.options.get('functions')).split():
  70. cmd += ['-function', f]
  71. for pattern in export_file_patterns:
  72. for f in glob.glob(env.config.kerneldoc_srctree + '/' + pattern):
  73. env.note_dependency(os.path.abspath(f))
  74. cmd += ['-export-file', f]
  75. cmd += [filename]
  76. try:
  77. env.app.verbose('calling kernel-doc \'%s\'' % (" ".join(cmd)))
  78. p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  79. out, err = p.communicate()
  80. out, err = codecs.decode(out, 'utf-8'), codecs.decode(err, 'utf-8')
  81. if p.returncode != 0:
  82. sys.stderr.write(err)
  83. env.app.warn('kernel-doc \'%s\' failed with return code %d' % (" ".join(cmd), p.returncode))
  84. return [nodes.error(None, nodes.paragraph(text = "kernel-doc missing"))]
  85. elif env.config.kerneldoc_verbosity > 0:
  86. sys.stderr.write(err)
  87. lines = statemachine.string2lines(out, tab_width, convert_whitespace=True)
  88. result = ViewList()
  89. lineoffset = 0;
  90. line_regex = re.compile("^#define LINENO ([0-9]+)$")
  91. for line in lines:
  92. match = line_regex.search(line)
  93. if match:
  94. # sphinx counts lines from 0
  95. lineoffset = int(match.group(1)) - 1
  96. # we must eat our comments since the upset the markup
  97. else:
  98. result.append(line, filename, lineoffset)
  99. lineoffset += 1
  100. node = nodes.section()
  101. buf = self.state.memo.title_styles, self.state.memo.section_level, self.state.memo.reporter
  102. self.state.memo.reporter = AutodocReporter(result, self.state.memo.reporter)
  103. self.state.memo.title_styles, self.state.memo.section_level = [], 0
  104. try:
  105. self.state.nested_parse(result, 0, node, match_titles=1)
  106. finally:
  107. self.state.memo.title_styles, self.state.memo.section_level, self.state.memo.reporter = buf
  108. return node.children
  109. except Exception as e: # pylint: disable=W0703
  110. env.app.warn('kernel-doc \'%s\' processing failed with: %s' %
  111. (" ".join(cmd), str(e)))
  112. return [nodes.error(None, nodes.paragraph(text = "kernel-doc missing"))]
  113. def setup(app):
  114. app.add_config_value('kerneldoc_bin', None, 'env')
  115. app.add_config_value('kerneldoc_srctree', None, 'env')
  116. app.add_config_value('kerneldoc_verbosity', 1, 'env')
  117. app.add_directive('kernel-doc', KernelDocDirective)
  118. return dict(
  119. version = __version__,
  120. parallel_read_safe = True,
  121. parallel_write_safe = True
  122. )