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