kernel-doc.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 os
  30. import subprocess
  31. import sys
  32. import re
  33. import glob
  34. from docutils import nodes, statemachine
  35. from docutils.statemachine import ViewList
  36. from docutils.parsers.rst import directives
  37. from sphinx.util.compat import 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, universal_newlines=True)
  79. out, err = p.communicate()
  80. # python2 needs conversion to unicode.
  81. # python3 with universal_newlines=True returns strings.
  82. if sys.version_info.major < 3:
  83. out, err = unicode(out, 'utf-8'), unicode(err, 'utf-8')
  84. if p.returncode != 0:
  85. sys.stderr.write(err)
  86. env.app.warn('kernel-doc \'%s\' failed with return code %d' % (" ".join(cmd), p.returncode))
  87. return [nodes.error(None, nodes.paragraph(text = "kernel-doc missing"))]
  88. elif env.config.kerneldoc_verbosity > 0:
  89. sys.stderr.write(err)
  90. lines = statemachine.string2lines(out, tab_width, convert_whitespace=True)
  91. result = ViewList()
  92. lineoffset = 0;
  93. line_regex = re.compile("^#define LINENO ([0-9]+)$")
  94. for line in lines:
  95. match = line_regex.search(line)
  96. if match:
  97. # sphinx counts lines from 0
  98. lineoffset = int(match.group(1)) - 1
  99. # we must eat our comments since the upset the markup
  100. else:
  101. result.append(line, filename, lineoffset)
  102. lineoffset += 1
  103. node = nodes.section()
  104. buf = self.state.memo.title_styles, self.state.memo.section_level, self.state.memo.reporter
  105. self.state.memo.reporter = AutodocReporter(result, self.state.memo.reporter)
  106. self.state.memo.title_styles, self.state.memo.section_level = [], 0
  107. try:
  108. self.state.nested_parse(result, 0, node, match_titles=1)
  109. finally:
  110. self.state.memo.title_styles, self.state.memo.section_level, self.state.memo.reporter = buf
  111. return node.children
  112. except Exception as e: # pylint: disable=W0703
  113. env.app.warn('kernel-doc \'%s\' processing failed with: %s' %
  114. (" ".join(cmd), str(e)))
  115. return [nodes.error(None, nodes.paragraph(text = "kernel-doc missing"))]
  116. def setup(app):
  117. app.add_config_value('kerneldoc_bin', None, 'env')
  118. app.add_config_value('kerneldoc_srctree', None, 'env')
  119. app.add_config_value('kerneldoc_verbosity', 1, 'env')
  120. app.add_directive('kernel-doc', KernelDocDirective)
  121. return dict(
  122. version = __version__,
  123. parallel_read_safe = True,
  124. parallel_write_safe = True
  125. )