dochelpers.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. #
  4. # The Qubes OS Project, https://www.qubes-os.org/
  5. #
  6. # Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. #
  23. '''Documentation helpers.
  24. This module contains classes and functions which help to maintain documentation,
  25. particularly our custom Sphinx extension.
  26. '''
  27. import argparse
  28. import csv
  29. import os
  30. import posixpath
  31. import re
  32. import StringIO
  33. import urllib2
  34. import docutils
  35. import docutils.nodes
  36. import docutils.parsers.rst
  37. import docutils.parsers.rst.roles
  38. import docutils.statemachine
  39. import qubes.tools
  40. import sphinx
  41. import sphinx.errors
  42. import sphinx.locale
  43. import sphinx.util.docfields
  44. SUBCOMMANDS_TITLE = 'COMMANDS'
  45. OPTIONS_TITLE = 'OPTIONS'
  46. def fetch_ticket_info(uri):
  47. '''Fetch info about particular trac ticket given
  48. :param str uri: URI at which ticket resides
  49. :rtype: mapping
  50. :raises: urllib2.HTTPError
  51. '''
  52. data = urllib2.urlopen(uri + '?format=csv').read()
  53. reader = csv.reader((line + '\n' for line in data.split('\r\n')),
  54. quoting=csv.QUOTE_MINIMAL, quotechar='"')
  55. return dict(zip(*((cell.decode('utf-8') for cell in row)
  56. for row in list(reader)[:2])))
  57. def ticket(name, rawtext, text, lineno, inliner, options=None, content=None):
  58. '''Link to qubes ticket
  59. :param str name: The role name used in the document
  60. :param str rawtext: The entire markup snippet, with role
  61. :param str text: The text marked with the role
  62. :param int lineno: The line number where rawtext appears in the input
  63. :param docutils.parsers.rst.states.Inliner inliner: The inliner instance \
  64. that called this function
  65. :param options: Directive options for customisation
  66. :param content: The directive content for customisation
  67. ''' # pylint: disable=unused-argument
  68. if options is None:
  69. options = {}
  70. ticketno = text.lstrip('#')
  71. if not ticketno.isdigit():
  72. msg = inliner.reporter.error(
  73. 'Invalid ticket identificator: {!r}'.format(text), line=lineno)
  74. prb = inliner.problematic(rawtext, rawtext, msg)
  75. return [prb], [msg]
  76. app = inliner.document.settings.env.app
  77. uri = posixpath.join(app.config.ticket_base_uri, ticketno)
  78. try:
  79. info = fetch_ticket_info(uri)
  80. except urllib2.HTTPError, e:
  81. msg = inliner.reporter.error(
  82. 'Error while fetching ticket info: {!s}'.format(e), line=lineno)
  83. prb = inliner.problematic(rawtext, rawtext, msg)
  84. return [prb], [msg]
  85. docutils.parsers.rst.roles.set_classes(options)
  86. node = docutils.nodes.reference(
  87. rawtext,
  88. '#{} ({})'.format(ticketno, info['summary']),
  89. refuri=uri,
  90. **options)
  91. return [node], []
  92. class versioncheck(docutils.nodes.warning):
  93. # pylint: disable=invalid-name
  94. pass
  95. def visit(self, node):
  96. self.visit_admonition(node, 'version')
  97. def depart(self, node):
  98. self.depart_admonition(node)
  99. sphinx.locale.admonitionlabels['version'] = 'Version mismatch'
  100. class VersionCheck(docutils.parsers.rst.Directive):
  101. '''Directive versioncheck
  102. Check if current version (from ``conf.py``) equals version specified as
  103. argument. If not, generate warning.'''
  104. has_content = True
  105. required_arguments = 1
  106. optional_arguments = 0
  107. final_argument_whitespace = True
  108. option_spec = {}
  109. def run(self):
  110. current = self.state.document.settings.env.app.config.version
  111. version = self.arguments[0]
  112. if current == version:
  113. return []
  114. text = ' '.join('''This manual page was written for version **{}**, but
  115. current version at the time when this page was generated is **{}**.
  116. This may or may not mean that page is outdated or has
  117. inconsistencies.'''.format(version, current).split())
  118. node = versioncheck(text)
  119. node['classes'] = ['admonition', 'warning']
  120. self.state.nested_parse(docutils.statemachine.StringList([text]),
  121. self.content_offset, node)
  122. return [node]
  123. def make_rst_section(heading, char):
  124. return '{}\n{}\n\n'.format(heading, char[0] * len(heading))
  125. def prepare_manpage(command):
  126. parser = qubes.tools.get_parser_for_command(command)
  127. stream = StringIO.StringIO()
  128. stream.write('.. program:: {}\n\n'.format(command))
  129. stream.write(make_rst_section(
  130. ':program:`{}` -- {}'.format(command, parser.description), '='))
  131. stream.write('''.. warning::
  132. This page was autogenerated from command-line parser. It shouldn't be 1:1
  133. conversion, because it would add little value. Please revise it and add
  134. more descriptive help, which normally won't fit in standard ``--help``
  135. option.
  136. After rewrite, please remove this admonition.\n\n''')
  137. stream.write(make_rst_section('Synopsis', '-'))
  138. usage = ' '.join(parser.format_usage().strip().split())
  139. if usage.startswith('usage: '):
  140. usage = usage[len('usage: '):]
  141. # replace METAVARS with *METAVARS*
  142. usage = re.sub(r'\b([A-Z]{2,})\b', r'*\1*', usage)
  143. stream.write(':command:`{}` {}\n\n'.format(command, usage))
  144. stream.write(make_rst_section('Options', '-'))
  145. for action in parser._actions: # pylint: disable=protected-access
  146. stream.write('.. option:: ')
  147. if action.metavar:
  148. stream.write(', '.join('{}{}{}'.format(
  149. option,
  150. '=' if option.startswith('--') else ' ',
  151. action.metavar)
  152. for option in sorted(action.option_strings)))
  153. else:
  154. stream.write(', '.join(sorted(action.option_strings)))
  155. stream.write('\n\n {}\n\n'.format(action.help))
  156. stream.write(make_rst_section('Authors', '-'))
  157. stream.write('''\
  158. | Joanna Rutkowska <joanna at invisiblethingslab dot com>
  159. | Rafal Wojtczuk <rafal at invisiblethingslab dot com>
  160. | Marek Marczykowski <marmarek at invisiblethingslab dot com>
  161. | Wojtek Porczyk <woju at invisiblethingslab dot com>
  162. .. vim: ts=3 sw=3 et tw=80
  163. ''')
  164. return stream.getvalue()
  165. class OptionsCheckVisitor(docutils.nodes.SparseNodeVisitor):
  166. ''' Checks if the visited option nodes and the specified args are in sync.
  167. '''
  168. def __init__(self, command, args, document):
  169. assert isinstance(args, set)
  170. docutils.nodes.SparseNodeVisitor.__init__(self, document)
  171. self.command = command
  172. self.args = args
  173. def visit_desc(self, node):
  174. ''' Skips all but 'option' elements '''
  175. # pylint: disable=no-self-use
  176. if not node.get('desctype', None) == 'option':
  177. raise docutils.nodes.SkipChildren
  178. def visit_desc_name(self, node):
  179. ''' Checks if the option is defined `self.args` '''
  180. if not isinstance(node[0], docutils.nodes.Text):
  181. raise sphinx.errors.SphinxError('first child should be Text')
  182. arg = str(node[0])
  183. try:
  184. self.args.remove(arg)
  185. except KeyError:
  186. raise sphinx.errors.SphinxError(
  187. 'No such argument for {!r}: {!r}'.format(self.command, arg))
  188. def check_undocumented_arguments(self, ignored_options=set()):
  189. ''' Call this to check if any undocumented arguments are left.
  190. While the documentation talks about a
  191. 'SparseNodeVisitor.depart_document()' function, this function does
  192. not exists. (For details see implementation of
  193. :py:method:`NodeVisitor.dispatch_departure()`) So we need to
  194. manually call this.
  195. '''
  196. left_over_args = self.args - ignored_options
  197. if left_over_args:
  198. raise sphinx.errors.SphinxError(
  199. 'Undocumented arguments for command {!r}: {!r}'.format(
  200. self.command, ', '.join(sorted(left_over_args))))
  201. class CommandCheckVisitor(docutils.nodes.SparseNodeVisitor):
  202. ''' Checks if the visited sub command section nodes and the specified sub
  203. command args are in sync.
  204. '''
  205. def __init__(self, command, sub_commands, document):
  206. docutils.nodes.SparseNodeVisitor.__init__(self, document)
  207. self.command = command
  208. self.sub_commands = sub_commands
  209. def visit_section(self, node):
  210. ''' Checks if the visited sub-command section nodes exists and it
  211. options are in sync.
  212. Uses :py:class:`OptionsCheckVisitor` for checking
  213. sub-commands options
  214. '''
  215. # pylint: disable=no-self-use
  216. title = str(node[0][0])
  217. if title.upper() == SUBCOMMANDS_TITLE:
  218. return
  219. sub_cmd = self.command + ' ' + title
  220. try:
  221. args = self.sub_commands[title]
  222. options_visitor = OptionsCheckVisitor(sub_cmd, args, self.document)
  223. node.walkabout(options_visitor)
  224. options_visitor.check_undocumented_arguments(
  225. {'--help', '--quiet', '--verbose', '-h', '-q', '-v'})
  226. del self.sub_commands[title]
  227. except KeyError:
  228. raise sphinx.errors.SphinxError(
  229. 'No such sub-command {!r}'.format(sub_cmd))
  230. def visit_Text(self, node):
  231. ''' If the visited text node starts with 'alias: ', all the provided
  232. comma separted alias in this node, are removed from
  233. `self.sub_commands`
  234. '''
  235. # pylint: disable=invalid-name
  236. text = str(node).strip()
  237. if text.startswith('aliases:'):
  238. aliases = {a.strip() for a in text.split('aliases:')[1].split(',')}
  239. for alias in aliases:
  240. assert alias in self.sub_commands
  241. del self.sub_commands[alias]
  242. def check_undocumented_sub_commands(self):
  243. ''' Call this to check if any undocumented sub_commands are left.
  244. While the documentation talks about a
  245. 'SparseNodeVisitor.depart_document()' function, this function does
  246. not exists. (For details see implementation of
  247. :py:method:`NodeVisitor.dispatch_departure()`) So we need to
  248. manually call this.
  249. '''
  250. if self.sub_commands:
  251. raise sphinx.errors.SphinxError(
  252. 'Undocumented commands for {!r}: {!r}'.format(
  253. self.command, ', '.join(sorted(self.sub_commands.keys()))))
  254. class ManpageCheckVisitor(docutils.nodes.SparseNodeVisitor):
  255. ''' Checks if the sub-commands and options specified in the 'COMMAND' and
  256. 'OPTIONS' (case insensitve) sections in sync the command parser.
  257. '''
  258. def __init__(self, app, command, document):
  259. docutils.nodes.SparseNodeVisitor.__init__(self, document)
  260. try:
  261. parser = qubes.tools.get_parser_for_command(command)
  262. except ImportError:
  263. app.warn('cannot import module for command')
  264. self.parser = None
  265. return
  266. except AttributeError:
  267. raise sphinx.errors.SphinxError('cannot find parser in module')
  268. self.command = command
  269. self.parser = parser
  270. self.options = set()
  271. self.sub_commands = {}
  272. self.app = app
  273. # pylint: disable=protected-access
  274. for action in parser._actions:
  275. if action.help == argparse.SUPPRESS:
  276. continue
  277. if issubclass(action.__class__,
  278. qubes.tools.AliasedSubParsersAction):
  279. for cmd, cmd_parser in action._name_parser_map.items():
  280. self.sub_commands[cmd] = set()
  281. for sub_action in cmd_parser._actions:
  282. if sub_action.help != argparse.SUPPRESS:
  283. self.sub_commands[cmd].update(
  284. sub_action.option_strings)
  285. else:
  286. self.options.update(action.option_strings)
  287. def visit_section(self, node):
  288. ''' If section title is OPTIONS or COMMANDS dispatch the apropriate
  289. `NodeVisitor`.
  290. '''
  291. if self.parser is None:
  292. return
  293. section_title = str(node[0][0]).upper()
  294. if section_title == OPTIONS_TITLE:
  295. options_visitor = OptionsCheckVisitor(self.command, self.options,
  296. self.document)
  297. node.walkabout(options_visitor)
  298. options_visitor.check_undocumented_arguments()
  299. elif section_title == SUBCOMMANDS_TITLE:
  300. sub_cmd_visitor = CommandCheckVisitor(
  301. self.command, self.sub_commands, self.document)
  302. node.walkabout(sub_cmd_visitor)
  303. sub_cmd_visitor.check_undocumented_sub_commands()
  304. def check_man_args(app, doctree, docname):
  305. ''' Checks the manpage for undocumented or obsolete sub-commands and
  306. options.
  307. '''
  308. command = os.path.split(docname)[1]
  309. app.info('Checking arguments for {!r}'.format(command))
  310. doctree.walk(ManpageCheckVisitor(app, command, doctree))
  311. #
  312. # this is lifted from sphinx' own conf.py
  313. #
  314. event_sig_re = re.compile(r'([a-zA-Z-:<>]+)\s*\((.*)\)')
  315. def parse_event(env, sig, signode):
  316. # pylint: disable=unused-argument
  317. m = event_sig_re.match(sig)
  318. if not m:
  319. signode += sphinx.addnodes.desc_name(sig, sig)
  320. return sig
  321. name, args = m.groups()
  322. signode += sphinx.addnodes.desc_name(name, name)
  323. plist = sphinx.addnodes.desc_parameterlist()
  324. for arg in args.split(','):
  325. arg = arg.strip()
  326. plist += sphinx.addnodes.desc_parameter(arg, arg)
  327. signode += plist
  328. return name
  329. #
  330. # end of codelifting
  331. #
  332. def break_to_pdb(app, *dummy):
  333. if not app.config.break_to_pdb:
  334. return
  335. import pdb
  336. pdb.set_trace()
  337. def setup(app):
  338. app.add_role('ticket', ticket)
  339. app.add_config_value('ticket_base_uri',
  340. 'https://wiki.qubes-os.org/ticket/', 'env')
  341. app.add_config_value('break_to_pdb', False, 'env')
  342. app.add_node(versioncheck,
  343. html=(visit, depart),
  344. man=(visit, depart))
  345. app.add_directive('versioncheck', VersionCheck)
  346. fdesc = sphinx.util.docfields.GroupedField('parameter', label='Parameters',
  347. names=['param'], can_collapse=True)
  348. app.add_object_type('event', 'event', 'pair: %s; event', parse_event,
  349. doc_field_types=[fdesc])
  350. app.connect('doctree-resolved', break_to_pdb)
  351. app.connect('doctree-resolved', check_man_args)
  352. # vim: ts=4 sw=4 et