kernel_include.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8; mode: python -*-
  3. # pylint: disable=R0903, C0330, R0914, R0912, E0401
  4. u"""
  5. kernel-include
  6. ~~~~~~~~~~~~~~
  7. Implementation of the ``kernel-include`` reST-directive.
  8. :copyright: Copyright (C) 2016 Markus Heiser
  9. :license: GPL Version 2, June 1991 see linux/COPYING for details.
  10. The ``kernel-include`` reST-directive is a replacement for the ``include``
  11. directive. The ``kernel-include`` directive expand environment variables in
  12. the path name and allows to include files from arbitrary locations.
  13. .. hint::
  14. Including files from arbitrary locations (e.g. from ``/etc``) is a
  15. security risk for builders. This is why the ``include`` directive from
  16. docutils *prohibit* pathnames pointing to locations *above* the filesystem
  17. tree where the reST document with the include directive is placed.
  18. Substrings of the form $name or ${name} are replaced by the value of
  19. environment variable name. Malformed variable names and references to
  20. non-existing variables are left unchanged.
  21. """
  22. # ==============================================================================
  23. # imports
  24. # ==============================================================================
  25. import os.path
  26. from docutils import io, nodes, statemachine
  27. from docutils.utils.error_reporting import SafeString, ErrorString
  28. from docutils.parsers.rst import directives
  29. from docutils.parsers.rst.directives.body import CodeBlock, NumberLines
  30. from docutils.parsers.rst.directives.misc import Include
  31. __version__ = '1.0'
  32. # ==============================================================================
  33. def setup(app):
  34. # ==============================================================================
  35. app.add_directive("kernel-include", KernelInclude)
  36. return dict(
  37. version = __version__,
  38. parallel_read_safe = True,
  39. parallel_write_safe = True
  40. )
  41. # ==============================================================================
  42. class KernelInclude(Include):
  43. # ==============================================================================
  44. u"""KernelInclude (``kernel-include``) directive"""
  45. def run(self):
  46. env = self.state.document.settings.env
  47. path = os.path.realpath(
  48. os.path.expandvars(self.arguments[0]))
  49. # to get a bit security back, prohibit /etc:
  50. if path.startswith(os.sep + "etc"):
  51. raise self.severe(
  52. 'Problems with "%s" directive, prohibited path: %s'
  53. % (self.name, path))
  54. self.arguments[0] = path
  55. env.note_dependency(os.path.abspath(path))
  56. #return super(KernelInclude, self).run() # won't work, see HINTs in _run()
  57. return self._run()
  58. def _run(self):
  59. """Include a file as part of the content of this reST file."""
  60. # HINT: I had to copy&paste the whole Include.run method. I'am not happy
  61. # with this, but due to security reasons, the Include.run method does
  62. # not allow absolute or relative pathnames pointing to locations *above*
  63. # the filesystem tree where the reST document is placed.
  64. if not self.state.document.settings.file_insertion_enabled:
  65. raise self.warning('"%s" directive disabled.' % self.name)
  66. source = self.state_machine.input_lines.source(
  67. self.lineno - self.state_machine.input_offset - 1)
  68. source_dir = os.path.dirname(os.path.abspath(source))
  69. path = directives.path(self.arguments[0])
  70. if path.startswith('<') and path.endswith('>'):
  71. path = os.path.join(self.standard_include_path, path[1:-1])
  72. path = os.path.normpath(os.path.join(source_dir, path))
  73. # HINT: this is the only line I had to change / commented out:
  74. #path = utils.relative_path(None, path)
  75. path = nodes.reprunicode(path)
  76. encoding = self.options.get(
  77. 'encoding', self.state.document.settings.input_encoding)
  78. e_handler=self.state.document.settings.input_encoding_error_handler
  79. tab_width = self.options.get(
  80. 'tab-width', self.state.document.settings.tab_width)
  81. try:
  82. self.state.document.settings.record_dependencies.add(path)
  83. include_file = io.FileInput(source_path=path,
  84. encoding=encoding,
  85. error_handler=e_handler)
  86. except UnicodeEncodeError as error:
  87. raise self.severe('Problems with "%s" directive path:\n'
  88. 'Cannot encode input file path "%s" '
  89. '(wrong locale?).' %
  90. (self.name, SafeString(path)))
  91. except IOError as error:
  92. raise self.severe('Problems with "%s" directive path:\n%s.' %
  93. (self.name, ErrorString(error)))
  94. startline = self.options.get('start-line', None)
  95. endline = self.options.get('end-line', None)
  96. try:
  97. if startline or (endline is not None):
  98. lines = include_file.readlines()
  99. rawtext = ''.join(lines[startline:endline])
  100. else:
  101. rawtext = include_file.read()
  102. except UnicodeError as error:
  103. raise self.severe('Problem with "%s" directive:\n%s' %
  104. (self.name, ErrorString(error)))
  105. # start-after/end-before: no restrictions on newlines in match-text,
  106. # and no restrictions on matching inside lines vs. line boundaries
  107. after_text = self.options.get('start-after', None)
  108. if after_text:
  109. # skip content in rawtext before *and incl.* a matching text
  110. after_index = rawtext.find(after_text)
  111. if after_index < 0:
  112. raise self.severe('Problem with "start-after" option of "%s" '
  113. 'directive:\nText not found.' % self.name)
  114. rawtext = rawtext[after_index + len(after_text):]
  115. before_text = self.options.get('end-before', None)
  116. if before_text:
  117. # skip content in rawtext after *and incl.* a matching text
  118. before_index = rawtext.find(before_text)
  119. if before_index < 0:
  120. raise self.severe('Problem with "end-before" option of "%s" '
  121. 'directive:\nText not found.' % self.name)
  122. rawtext = rawtext[:before_index]
  123. include_lines = statemachine.string2lines(rawtext, tab_width,
  124. convert_whitespace=True)
  125. if 'literal' in self.options:
  126. # Convert tabs to spaces, if `tab_width` is positive.
  127. if tab_width >= 0:
  128. text = rawtext.expandtabs(tab_width)
  129. else:
  130. text = rawtext
  131. literal_block = nodes.literal_block(rawtext, source=path,
  132. classes=self.options.get('class', []))
  133. literal_block.line = 1
  134. self.add_name(literal_block)
  135. if 'number-lines' in self.options:
  136. try:
  137. startline = int(self.options['number-lines'] or 1)
  138. except ValueError:
  139. raise self.error(':number-lines: with non-integer '
  140. 'start value')
  141. endline = startline + len(include_lines)
  142. if text.endswith('\n'):
  143. text = text[:-1]
  144. tokens = NumberLines([([], text)], startline, endline)
  145. for classes, value in tokens:
  146. if classes:
  147. literal_block += nodes.inline(value, value,
  148. classes=classes)
  149. else:
  150. literal_block += nodes.Text(value, value)
  151. else:
  152. literal_block += nodes.Text(text, text)
  153. return [literal_block]
  154. if 'code' in self.options:
  155. self.options['source'] = path
  156. codeblock = CodeBlock(self.name,
  157. [self.options.pop('code')], # arguments
  158. self.options,
  159. include_lines, # content
  160. self.lineno,
  161. self.content_offset,
  162. self.block_text,
  163. self.state,
  164. self.state_machine)
  165. return codeblock.run()
  166. self.state_machine.insert_input(include_lines, path)
  167. return []