kernel_include.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. # ==============================================================================
  32. def setup(app):
  33. # ==============================================================================
  34. app.add_directive("kernel-include", KernelInclude)
  35. # ==============================================================================
  36. class KernelInclude(Include):
  37. # ==============================================================================
  38. u"""KernelInclude (``kernel-include``) directive"""
  39. def run(self):
  40. path = os.path.realpath(
  41. os.path.expandvars(self.arguments[0]))
  42. # to get a bit security back, prohibit /etc:
  43. if path.startswith(os.sep + "etc"):
  44. raise self.severe(
  45. 'Problems with "%s" directive, prohibited path: %s'
  46. % (self.name, path))
  47. self.arguments[0] = path
  48. #return super(KernelInclude, self).run() # won't work, see HINTs in _run()
  49. return self._run()
  50. def _run(self):
  51. """Include a file as part of the content of this reST file."""
  52. # HINT: I had to copy&paste the whole Include.run method. I'am not happy
  53. # with this, but due to security reasons, the Include.run method does
  54. # not allow absolute or relative pathnames pointing to locations *above*
  55. # the filesystem tree where the reST document is placed.
  56. if not self.state.document.settings.file_insertion_enabled:
  57. raise self.warning('"%s" directive disabled.' % self.name)
  58. source = self.state_machine.input_lines.source(
  59. self.lineno - self.state_machine.input_offset - 1)
  60. source_dir = os.path.dirname(os.path.abspath(source))
  61. path = directives.path(self.arguments[0])
  62. if path.startswith('<') and path.endswith('>'):
  63. path = os.path.join(self.standard_include_path, path[1:-1])
  64. path = os.path.normpath(os.path.join(source_dir, path))
  65. # HINT: this is the only line I had to change / commented out:
  66. #path = utils.relative_path(None, path)
  67. path = nodes.reprunicode(path)
  68. encoding = self.options.get(
  69. 'encoding', self.state.document.settings.input_encoding)
  70. e_handler=self.state.document.settings.input_encoding_error_handler
  71. tab_width = self.options.get(
  72. 'tab-width', self.state.document.settings.tab_width)
  73. try:
  74. self.state.document.settings.record_dependencies.add(path)
  75. include_file = io.FileInput(source_path=path,
  76. encoding=encoding,
  77. error_handler=e_handler)
  78. except UnicodeEncodeError as error:
  79. raise self.severe('Problems with "%s" directive path:\n'
  80. 'Cannot encode input file path "%s" '
  81. '(wrong locale?).' %
  82. (self.name, SafeString(path)))
  83. except IOError as error:
  84. raise self.severe('Problems with "%s" directive path:\n%s.' %
  85. (self.name, ErrorString(error)))
  86. startline = self.options.get('start-line', None)
  87. endline = self.options.get('end-line', None)
  88. try:
  89. if startline or (endline is not None):
  90. lines = include_file.readlines()
  91. rawtext = ''.join(lines[startline:endline])
  92. else:
  93. rawtext = include_file.read()
  94. except UnicodeError as error:
  95. raise self.severe('Problem with "%s" directive:\n%s' %
  96. (self.name, ErrorString(error)))
  97. # start-after/end-before: no restrictions on newlines in match-text,
  98. # and no restrictions on matching inside lines vs. line boundaries
  99. after_text = self.options.get('start-after', None)
  100. if after_text:
  101. # skip content in rawtext before *and incl.* a matching text
  102. after_index = rawtext.find(after_text)
  103. if after_index < 0:
  104. raise self.severe('Problem with "start-after" option of "%s" '
  105. 'directive:\nText not found.' % self.name)
  106. rawtext = rawtext[after_index + len(after_text):]
  107. before_text = self.options.get('end-before', None)
  108. if before_text:
  109. # skip content in rawtext after *and incl.* a matching text
  110. before_index = rawtext.find(before_text)
  111. if before_index < 0:
  112. raise self.severe('Problem with "end-before" option of "%s" '
  113. 'directive:\nText not found.' % self.name)
  114. rawtext = rawtext[:before_index]
  115. include_lines = statemachine.string2lines(rawtext, tab_width,
  116. convert_whitespace=True)
  117. if 'literal' in self.options:
  118. # Convert tabs to spaces, if `tab_width` is positive.
  119. if tab_width >= 0:
  120. text = rawtext.expandtabs(tab_width)
  121. else:
  122. text = rawtext
  123. literal_block = nodes.literal_block(rawtext, source=path,
  124. classes=self.options.get('class', []))
  125. literal_block.line = 1
  126. self.add_name(literal_block)
  127. if 'number-lines' in self.options:
  128. try:
  129. startline = int(self.options['number-lines'] or 1)
  130. except ValueError:
  131. raise self.error(':number-lines: with non-integer '
  132. 'start value')
  133. endline = startline + len(include_lines)
  134. if text.endswith('\n'):
  135. text = text[:-1]
  136. tokens = NumberLines([([], text)], startline, endline)
  137. for classes, value in tokens:
  138. if classes:
  139. literal_block += nodes.inline(value, value,
  140. classes=classes)
  141. else:
  142. literal_block += nodes.Text(value, value)
  143. else:
  144. literal_block += nodes.Text(text, text)
  145. return [literal_block]
  146. if 'code' in self.options:
  147. self.options['source'] = path
  148. codeblock = CodeBlock(self.name,
  149. [self.options.pop('code')], # arguments
  150. self.options,
  151. include_lines, # content
  152. self.lineno,
  153. self.content_offset,
  154. self.block_text,
  155. self.state,
  156. self.state_machine)
  157. return codeblock.run()
  158. self.state_machine.insert_input(include_lines, path)
  159. return []