kernel-doc.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. from docutils import nodes, statemachine
  34. from docutils.statemachine import ViewList
  35. from docutils.parsers.rst import directives
  36. from sphinx.util.compat import Directive
  37. class KernelDocDirective(Directive):
  38. """Extract kernel-doc comments from the specified file"""
  39. required_argument = 1
  40. optional_arguments = 4
  41. option_spec = {
  42. 'doc': directives.unchanged_required,
  43. 'functions': directives.unchanged_required,
  44. 'export': directives.flag,
  45. 'internal': directives.flag,
  46. }
  47. has_content = False
  48. def run(self):
  49. env = self.state.document.settings.env
  50. cmd = [env.config.kerneldoc_bin, '-rst', '-enable-lineno']
  51. filename = env.config.kerneldoc_srctree + '/' + self.arguments[0]
  52. # Tell sphinx of the dependency
  53. env.note_dependency(os.path.abspath(filename))
  54. tab_width = self.options.get('tab-width', self.state.document.settings.tab_width)
  55. # FIXME: make this nicer and more robust against errors
  56. if 'export' in self.options:
  57. cmd += ['-export']
  58. elif 'internal' in self.options:
  59. cmd += ['-internal']
  60. elif 'doc' in self.options:
  61. cmd += ['-function', str(self.options.get('doc'))]
  62. elif 'functions' in self.options:
  63. for f in str(self.options.get('functions')).split(' '):
  64. cmd += ['-function', f]
  65. cmd += [filename]
  66. try:
  67. env.app.verbose('calling kernel-doc \'%s\'' % (" ".join(cmd)))
  68. p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
  69. out, err = p.communicate()
  70. # python2 needs conversion to unicode.
  71. # python3 with universal_newlines=True returns strings.
  72. if sys.version_info.major < 3:
  73. out, err = unicode(out, 'utf-8'), unicode(err, 'utf-8')
  74. if p.returncode != 0:
  75. sys.stderr.write(err)
  76. env.app.warn('kernel-doc \'%s\' failed with return code %d' % (" ".join(cmd), p.returncode))
  77. return [nodes.error(None, nodes.paragraph(text = "kernel-doc missing"))]
  78. elif env.config.kerneldoc_verbosity > 0:
  79. sys.stderr.write(err)
  80. lines = statemachine.string2lines(out, tab_width, convert_whitespace=True)
  81. result = ViewList()
  82. lineoffset = 0;
  83. line_regex = re.compile("^#define LINENO ([0-9]+)$")
  84. for line in lines:
  85. match = line_regex.search(line)
  86. if match:
  87. # sphinx counts lines from 0
  88. lineoffset = int(match.group(1)) - 1
  89. # we must eat our comments since the upset the markup
  90. else:
  91. result.append(line, filename, lineoffset)
  92. lineoffset += 1
  93. node = nodes.section()
  94. node.document = self.state.document
  95. self.state.nested_parse(result, self.content_offset, node)
  96. return node.children
  97. except Exception as e:
  98. env.app.warn('kernel-doc \'%s\' processing failed with: %s' %
  99. (" ".join(cmd), str(e)))
  100. return [nodes.error(None, nodes.paragraph(text = "kernel-doc missing"))]
  101. def setup(app):
  102. app.add_config_value('kerneldoc_bin', None, 'env')
  103. app.add_config_value('kerneldoc_srctree', None, 'env')
  104. app.add_config_value('kerneldoc_verbosity', 1, 'env')
  105. app.add_directive('kernel-doc', KernelDocDirective)