diff --git a/doc/Makefile b/doc/Makefile new file mode 100644 index 0000000..5e85499 --- /dev/null +++ b/doc/Makefile @@ -0,0 +1,231 @@ +# Makefile for Sphinx documentation +# + +# You can set these variables from the command line. +SPHINXOPTS = +SPHINXBUILD = sphinx-build +PAPER = +BUILDDIR = _build + +# Internal variables. +PAPEROPT_a4 = -D latex_paper_size=a4 +PAPEROPT_letter = -D latex_paper_size=letter +ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . +# the i18n builder cannot share the environment and doctrees with the others +I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . + +.PHONY: help +help: + @echo "Please use \`make ' where is one of" + @echo " html to make standalone HTML files" + @echo " dirhtml to make HTML files named index.html in directories" + @echo " singlehtml to make a single large HTML file" + @echo " pickle to make pickle files" + @echo " json to make JSON files" + @echo " htmlhelp to make HTML files and a HTML help project" + @echo " qthelp to make HTML files and a qthelp project" + @echo " applehelp to make an Apple Help Book" + @echo " devhelp to make HTML files and a Devhelp project" + @echo " epub to make an epub" + @echo " epub3 to make an epub3" + @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" + @echo " latexpdf to make LaTeX files and run them through pdflatex" + @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" + @echo " text to make text files" + @echo " man to make manual pages" + @echo " texinfo to make Texinfo files" + @echo " info to make Texinfo files and run them through makeinfo" + @echo " gettext to make PO message catalogs" + @echo " changes to make an overview of all changed/added/deprecated items" + @echo " xml to make Docutils-native XML files" + @echo " pseudoxml to make pseudoxml-XML files for display purposes" + @echo " linkcheck to check all external links for integrity" + @echo " doctest to run all doctests embedded in the documentation (if enabled)" + @echo " coverage to run coverage check of the documentation (if enabled)" + @echo " dummy to check syntax errors of document sources" + +.PHONY: clean +clean: + rm -rf $(BUILDDIR)/* + +.PHONY: html +html: + $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." + +.PHONY: dirhtml +dirhtml: + $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." + +.PHONY: singlehtml +singlehtml: + $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml + @echo + @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." + +.PHONY: pickle +pickle: + $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle + @echo + @echo "Build finished; now you can process the pickle files." + +.PHONY: json +json: + $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json + @echo + @echo "Build finished; now you can process the JSON files." + +.PHONY: htmlhelp +htmlhelp: + $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp + @echo + @echo "Build finished; now you can run HTML Help Workshop with the" \ + ".hhp project file in $(BUILDDIR)/htmlhelp." + +.PHONY: qthelp +qthelp: + $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp + @echo + @echo "Build finished; now you can run "qcollectiongenerator" with the" \ + ".qhcp project file in $(BUILDDIR)/qthelp, like this:" + @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/QubesAdminclient.qhcp" + @echo "To view the help file:" + @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/QubesAdminclient.qhc" + +.PHONY: applehelp +applehelp: + $(SPHINXBUILD) -b applehelp $(ALLSPHINXOPTS) $(BUILDDIR)/applehelp + @echo + @echo "Build finished. The help book is in $(BUILDDIR)/applehelp." + @echo "N.B. You won't be able to view it unless you put it in" \ + "~/Library/Documentation/Help or install it in your application" \ + "bundle." + +.PHONY: devhelp +devhelp: + $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp + @echo + @echo "Build finished." + @echo "To view the help file:" + @echo "# mkdir -p $$HOME/.local/share/devhelp/QubesAdminclient" + @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/QubesAdminclient" + @echo "# devhelp" + +.PHONY: epub +epub: + $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub + @echo + @echo "Build finished. The epub file is in $(BUILDDIR)/epub." + +.PHONY: epub3 +epub3: + $(SPHINXBUILD) -b epub3 $(ALLSPHINXOPTS) $(BUILDDIR)/epub3 + @echo + @echo "Build finished. The epub3 file is in $(BUILDDIR)/epub3." + +.PHONY: latex +latex: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo + @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." + @echo "Run \`make' in that directory to run these through (pdf)latex" \ + "(use \`make latexpdf' here to do that automatically)." + +.PHONY: latexpdf +latexpdf: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo "Running LaTeX files through pdflatex..." + $(MAKE) -C $(BUILDDIR)/latex all-pdf + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +.PHONY: latexpdfja +latexpdfja: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo "Running LaTeX files through platex and dvipdfmx..." + $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +.PHONY: text +text: + $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text + @echo + @echo "Build finished. The text files are in $(BUILDDIR)/text." + +.PHONY: man +man: + $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man + @echo + @echo "Build finished. The manual pages are in $(BUILDDIR)/man." + +.PHONY: texinfo +texinfo: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo + @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." + @echo "Run \`make' in that directory to run these through makeinfo" \ + "(use \`make info' here to do that automatically)." + +.PHONY: info +info: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo "Running Texinfo files through makeinfo..." + make -C $(BUILDDIR)/texinfo info + @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." + +.PHONY: gettext +gettext: + $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale + @echo + @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." + +.PHONY: changes +changes: + $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes + @echo + @echo "The overview file is in $(BUILDDIR)/changes." + +.PHONY: linkcheck +linkcheck: + $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck + @echo + @echo "Link check complete; look for any errors in the above output " \ + "or in $(BUILDDIR)/linkcheck/output.txt." + +.PHONY: doctest +doctest: + $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest + @echo "Testing of doctests in the sources finished, look at the " \ + "results in $(BUILDDIR)/doctest/output.txt." + +.PHONY: coverage +coverage: + $(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage + @echo "Testing of coverage in the sources finished, look at the " \ + "results in $(BUILDDIR)/coverage/python.txt." + +.PHONY: xml +xml: + $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml + @echo + @echo "Build finished. The XML files are in $(BUILDDIR)/xml." + +.PHONY: pseudoxml +pseudoxml: + $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml + @echo + @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." + +.PHONY: dummy +dummy: + $(SPHINXBUILD) -b dummy $(ALLSPHINXOPTS) $(BUILDDIR)/dummy + @echo + @echo "Build finished. Dummy builder generates no files." + +.PHONY: install +install: man + mkdir -p $(DESTDIR)/usr/share/man/man1 + cp $(BUILDDIR)/man/* $(DESTDIR)/usr/share/man/man1/ + diff --git a/doc/conf.py b/doc/conf.py new file mode 100644 index 0000000..ca9cf00 --- /dev/null +++ b/doc/conf.py @@ -0,0 +1,360 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# Qubes Admin client documentation build configuration file, created by +# sphinx-quickstart on Thu May 11 19:00:43 2017. +# +# This file is execfile()d with the current directory set to its +# containing dir. +# +# Note that not all possible configuration values are present in this +# autogenerated file. +# +# All configuration values have a default; values that are commented out +# serve to show the default. + +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +# +import os +import sys +import subprocess +sys.path.insert(0, os.path.abspath('..')) + +# -- General configuration ------------------------------------------------ + +# If your documentation needs a minimal Sphinx version, state it here. +# +# needs_sphinx = '1.0' + +# Add any Sphinx extension module names here, as strings. They can be +# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom +# ones. +extensions = [ + 'sphinx.ext.autodoc', + 'sphinx.ext.doctest', + 'sphinx.ext.intersphinx', + 'sphinx.ext.todo', + 'sphinx.ext.coverage', + 'sphinx.ext.viewcode', +] + +try: + import qubesmgmt.tools.dochelpers + extensions.append('qubesmgmt.tools.dochelpers') +except ImportError: + pass + +# Add any paths that contain templates here, relative to this directory. +templates_path = ['_templates'] + +# The suffix(es) of source filenames. +# You can specify multiple suffix as a list of string: +# +# source_suffix = ['.rst', '.md'] +source_suffix = '.rst' + +# The encoding of source files. +# +# source_encoding = 'utf-8-sig' + +# The master toctree document. +master_doc = 'index' + +# General information about the project. +project = 'Qubes Admin client' +copyright = '2017, Invisible Things Lab' +author = 'Invisible Things Lab' + +# The version info for the project you're documenting, acts as replacement for +# |version| and |release|, also used in various other places throughout the +# built documents. +# +# The short X.Y version. +version = open('../version').read().strip() +# The full version, including alpha/beta/rc tags. +release = subprocess.check_output(['git', 'describe', '--long', '--dirty']).strip().decode() + +# The language for content autogenerated by Sphinx. Refer to documentation +# for a list of supported languages. +# +# This is also used if you do content translation via gettext catalogs. +# Usually you set "language" from the command line for these cases. +language = None + +# There are two options for replacing |today|: either, you set today to some +# non-false value, then it is used: +# +# today = '' +# +# Else, today_fmt is used as the format for a strftime call. +# +# today_fmt = '%B %d, %Y' + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +# This patterns also effect to html_static_path and html_extra_path +exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] + +# The reST default role (used for this markup: `text`) to use for all +# documents. +# +# default_role = None + +# If true, '()' will be appended to :func: etc. cross-reference text. +# +add_function_parentheses = True + +# If true, the current module name will be prepended to all description +# unit titles (such as .. function::). +# +# add_module_names = True + +# If true, sectionauthor and moduleauthor directives will be shown in the +# output. They are ignored by default. +# +# show_authors = False + +# The name of the Pygments (syntax highlighting) style to use. +pygments_style = 'sphinx' + +# A list of ignored prefixes for module index sorting. +# modindex_common_prefix = [] + +# If true, keep warnings as "system message" paragraphs in the built documents. +# keep_warnings = False + +# If true, `todo` and `todoList` produce output, else they produce nothing. +todo_include_todos = True + + +# -- Options for HTML output ---------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. +# +#html_theme = 'alabaster' +html_theme = 'nature' + +# Theme options are theme-specific and customize the look and feel of a theme +# further. For a list of options available for each theme, see the +# documentation. +# +# html_theme_options = {} + +# Add any paths that contain custom themes here, relative to this directory. +# html_theme_path = [] + +# The name for this set of Sphinx documents. +# " v documentation" by default. +# +# html_title = 'Qubes Admin client v4.0.0' + +# A shorter title for the navigation bar. Default is the same as html_title. +# +# html_short_title = None + +# The name of an image file (relative to this directory) to place at the top +# of the sidebar. +# +# html_logo = None + +# The name of an image file (relative to this directory) to use as a favicon of +# the docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 +# pixels large. +# +# html_favicon = None + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ['_static'] + +# Add any extra paths that contain custom files (such as robots.txt or +# .htaccess) here, relative to this directory. These files are copied +# directly to the root of the documentation. +# +# html_extra_path = [] + +# If not None, a 'Last updated on:' timestamp is inserted at every page +# bottom, using the given strftime format. +# The empty string is equivalent to '%b %d, %Y'. +# +# html_last_updated_fmt = None + +# If true, SmartyPants will be used to convert quotes and dashes to +# typographically correct entities. +# +# html_use_smartypants = True + +# Custom sidebar templates, maps document names to template names. +# +# html_sidebars = {} + +# Additional templates that should be rendered to pages, maps page names to +# template names. +# +# html_additional_pages = {} + +# If false, no module index is generated. +# +# html_domain_indices = True + +# If false, no index is generated. +# +# html_use_index = True + +# If true, the index is split into individual pages for each letter. +# +# html_split_index = False + +# If true, links to the reST sources are added to the pages. +# +# html_show_sourcelink = True + +# If true, "Created using Sphinx" is shown in the HTML footer. Default is True. +# +# html_show_sphinx = True + +# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. +# +# html_show_copyright = True + +# If true, an OpenSearch description file will be output, and all pages will +# contain a tag referring to it. The value of this option must be the +# base URL from which the finished HTML is served. +# +# html_use_opensearch = '' + +# This is the file name suffix for HTML files (e.g. ".xhtml"). +# html_file_suffix = None + +# Language to be used for generating the HTML full-text search index. +# Sphinx supports the following languages: +# 'da', 'de', 'en', 'es', 'fi', 'fr', 'h', 'it', 'ja' +# 'nl', 'no', 'pt', 'ro', 'r', 'sv', 'tr', 'zh' +# +# html_search_language = 'en' + +# A dictionary with options for the search language support, empty by default. +# 'ja' uses this config value. +# 'zh' user can custom change `jieba` dictionary path. +# +# html_search_options = {'type': 'default'} + +# The name of a javascript file (relative to the configuration directory) that +# implements a search results scorer. If empty, the default will be used. +# +# html_search_scorer = 'scorer.js' + +# Output file base name for HTML help builder. +htmlhelp_basename = 'QubesAdminclientdoc' + +# -- Options for LaTeX output --------------------------------------------- + +latex_elements = { + # The paper size ('letterpaper' or 'a4paper'). + # + # 'papersize': 'letterpaper', + + # The font size ('10pt', '11pt' or '12pt'). + # + # 'pointsize': '10pt', + + # Additional stuff for the LaTeX preamble. + # + # 'preamble': '', + + # Latex figure (float) alignment + # + # 'figure_align': 'htbp', +} + +# Grouping the document tree into LaTeX files. List of tuples +# (source start file, target name, title, +# author, documentclass [howto, manual, or own class]). +latex_documents = [ + (master_doc, 'QubesAdminclient.tex', 'Qubes Admin client Documentation', + 'Invisible Things Lab', 'manual'), +] + +# The name of an image file (relative to this directory) to place at the top of +# the title page. +# +# latex_logo = None + +# For "manual" documents, if this is true, then toplevel headings are parts, +# not chapters. +# +# latex_use_parts = False + +# If true, show page references after internal links. +# +# latex_show_pagerefs = False + +# If true, show URL addresses after external links. +# +# latex_show_urls = False + +# Documents to append as an appendix to all manuals. +# +# latex_appendices = [] + +# It false, will not define \strong, \code, itleref, \crossref ... but only +# \sphinxstrong, ..., \sphinxtitleref, ... To help avoid clash with user added +# packages. +# +# latex_keep_old_macro_names = True + +# If false, no module index is generated. +# +# latex_domain_indices = True + + +# -- Options for manual page output --------------------------------------- + +# authors should be empty and authors should be specified in each man page, +# because html builder will omit them +_man_pages_author = [] + +# One entry per manual page. List of tuples +# (source start file, name, description, authors, manual section). +man_pages = [ +] + +# If true, show URL addresses after external links. +# +# man_show_urls = False + + +# -- Options for Texinfo output ------------------------------------------- + +# Grouping the document tree into Texinfo files. List of tuples +# (source start file, target name, title, author, +# dir menu entry, description, category) +texinfo_documents = [ + (master_doc, 'QubesAdminclient', 'Qubes Admin client Documentation', + author, 'QubesAdminclient', 'One line description of project.', + 'Miscellaneous'), +] + +# Documents to append as an appendix to all manuals. +# +# texinfo_appendices = [] + +# If false, no module index is generated. +# +# texinfo_domain_indices = True + +# How to display URL addresses: 'footnote', 'no', or 'inline'. +# +# texinfo_show_urls = 'footnote' + +# If true, do not generate a @detailmenu in the "Top" node's menu. +# +# texinfo_no_detailmenu = False + + +# Example configuration for intersphinx: refer to the Python standard library. +intersphinx_mapping = {'https://docs.python.org/': None} diff --git a/doc/index.rst b/doc/index.rst new file mode 100644 index 0000000..78001b0 --- /dev/null +++ b/doc/index.rst @@ -0,0 +1,22 @@ +.. Qubes Admin client documentation master file, created by + sphinx-quickstart on Thu May 11 19:00:43 2017. + You can adapt this file completely to your liking, but it should at least + contain the root `toctree` directive. + +Welcome to Qubes Admin client's documentation! +============================================== + +Contents: + +.. toctree:: + :maxdepth: 2 + + + +Indices and tables +================== + +* :ref:`genindex` +* :ref:`modindex` +* :ref:`search` + diff --git a/doc/skel-manpage.py b/doc/skel-manpage.py new file mode 100755 index 0000000..37d8d85 --- /dev/null +++ b/doc/skel-manpage.py @@ -0,0 +1,20 @@ +#!/usr/bin/python3 + +import os +import sys +sys.path.insert(0, os.path.abspath('../')) + +import argparse +import qubesmgmt.dochelpers + +parser = argparse.ArgumentParser(description='prepare new manpage for command') +parser.add_argument('command', metavar='COMMAND', + help='program\'s command name; this should translate to ' + 'qubesmgmt.tools.') + +def main(): + args = parser.parse_args() + sys.stdout.write(qubesmgmt.dochelpers.prepare_manpage(args.command)) + +if __name__ == '__main__': + main() diff --git a/qubesmgmt/tools/dochelpers.py b/qubesmgmt/tools/dochelpers.py new file mode 100644 index 0000000..8e012fa --- /dev/null +++ b/qubesmgmt/tools/dochelpers.py @@ -0,0 +1,298 @@ +# +# The Qubes OS Project, https://www.qubes-os.org/ +# +# Copyright (C) 2010-2015 Joanna Rutkowska +# Copyright (C) 2014-2015 Wojtek Porczyk +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation; either version 2.1 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# + +'''Documentation helpers. + +This module contains classes and functions which help to maintain documentation, +particularly our custom Sphinx extension. +''' + +import argparse +import io +import os +import re + +import docutils +import docutils.nodes +import docutils.parsers.rst +import docutils.parsers.rst.roles +import docutils.statemachine +import sphinx +import sphinx.errors +import sphinx.locale +import sphinx.util.docfields + +import qubesmgmt.tools + +SUBCOMMANDS_TITLE = 'COMMANDS' +OPTIONS_TITLE = 'OPTIONS' + + +def make_rst_section(heading, char): + return '{}\n{}\n\n'.format(heading, char[0] * len(heading)) + + +def prepare_manpage(command): + parser = qubesmgmt.tools.get_parser_for_command(command) + stream = io.StringIO() + stream.write('.. program:: {}\n\n'.format(command)) + stream.write(make_rst_section( + ':program:`{}` -- {}'.format(command, parser.description), '=')) + stream.write('''.. warning:: + + This page was autogenerated from command-line parser. It shouldn't be 1:1 + conversion, because it would add little value. Please revise it and add + more descriptive help, which normally won't fit in standard ``--help`` + option. + + After rewrite, please remove this admonition.\n\n''') + + stream.write(make_rst_section('Synopsis', '-')) + usage = ' '.join(parser.format_usage().strip().split()) + if usage.startswith('usage: '): + usage = usage[len('usage: '):] + + # replace METAVARS with *METAVARS* + usage = re.sub(r'\b([A-Z]{2,})\b', r'*\1*', usage) + + stream.write(':command:`{}` {}\n\n'.format(command, usage)) + + stream.write(make_rst_section('Options', '-')) + + for action in parser._actions: # pylint: disable=protected-access + stream.write('.. option:: ') + if action.metavar: + stream.write(', '.join('{}{}{}'.format( + option, + '=' if option.startswith('--') else ' ', + action.metavar) + for option in sorted(action.option_strings))) + else: + stream.write(', '.join(sorted(action.option_strings))) + stream.write('\n\n {}\n\n'.format(action.help)) + + stream.write(make_rst_section('Authors', '-')) + stream.write('''\ +| Joanna Rutkowska +| Rafal Wojtczuk +| Marek Marczykowski +| Wojtek Porczyk + +.. vim: ts=3 sw=3 et tw=80 +''') + + return stream.getvalue() + + +class OptionsCheckVisitor(docutils.nodes.SparseNodeVisitor): + ''' Checks if the visited option nodes and the specified args are in sync. + ''' + def __init__(self, command, args, document): + assert isinstance(args, set) + docutils.nodes.SparseNodeVisitor.__init__(self, document) + self.command = command + self.args = args + + def visit_desc(self, node): + ''' Skips all but 'option' elements ''' + # pylint: disable=no-self-use + if not node.get('desctype', None) == 'option': + raise docutils.nodes.SkipChildren + + + def visit_desc_name(self, node): + ''' Checks if the option is defined `self.args` ''' + if not isinstance(node[0], docutils.nodes.Text): + raise sphinx.errors.SphinxError('first child should be Text') + + arg = str(node[0]) + try: + self.args.remove(arg) + except KeyError: + raise sphinx.errors.SphinxError( + 'No such argument for {!r}: {!r}'.format(self.command, arg)) + + def check_undocumented_arguments(self, ignored_options=None): + ''' Call this to check if any undocumented arguments are left. + + While the documentation talks about a + 'SparseNodeVisitor.depart_document()' function, this function does + not exists. (For details see implementation of + :py:method:`NodeVisitor.dispatch_departure()`) So we need to + manually call this. + ''' + if ignored_options is None: + ignored_options = set() + left_over_args = self.args - ignored_options + if left_over_args: + raise sphinx.errors.SphinxError( + 'Undocumented arguments for command {!r}: {!r}'.format( + self.command, ', '.join(sorted(left_over_args)))) + + +class CommandCheckVisitor(docutils.nodes.SparseNodeVisitor): + ''' Checks if the visited sub command section nodes and the specified sub + command args are in sync. + ''' + + def __init__(self, command, sub_commands, document): + docutils.nodes.SparseNodeVisitor.__init__(self, document) + self.command = command + self.sub_commands = sub_commands + + def visit_section(self, node): + ''' Checks if the visited sub-command section nodes exists and it + options are in sync. + + Uses :py:class:`OptionsCheckVisitor` for checking + sub-commands options + ''' + # pylint: disable=no-self-use + title = str(node[0][0]) + if title.upper() == SUBCOMMANDS_TITLE: + return + + sub_cmd = self.command + ' ' + title + + try: + args = self.sub_commands[title] + options_visitor = OptionsCheckVisitor(sub_cmd, args, self.document) + node.walkabout(options_visitor) + options_visitor.check_undocumented_arguments( + {'--help', '--quiet', '--verbose', '-h', '-q', '-v'}) + del self.sub_commands[title] + except KeyError: + raise sphinx.errors.SphinxError( + 'No such sub-command {!r}'.format(sub_cmd)) + + def visit_Text(self, node): + ''' If the visited text node starts with 'alias: ', all the provided + comma separted alias in this node, are removed from + `self.sub_commands` + ''' + # pylint: disable=invalid-name + text = str(node).strip() + if text.startswith('aliases:'): + aliases = {a.strip() for a in text.split('aliases:')[1].split(',')} + for alias in aliases: + assert alias in self.sub_commands + del self.sub_commands[alias] + + + def check_undocumented_sub_commands(self): + ''' Call this to check if any undocumented sub_commands are left. + + While the documentation talks about a + 'SparseNodeVisitor.depart_document()' function, this function does + not exists. (For details see implementation of + :py:method:`NodeVisitor.dispatch_departure()`) So we need to + manually call this. + ''' + if self.sub_commands: + raise sphinx.errors.SphinxError( + 'Undocumented commands for {!r}: {!r}'.format( + self.command, ', '.join(sorted(self.sub_commands.keys())))) + + +class ManpageCheckVisitor(docutils.nodes.SparseNodeVisitor): + ''' Checks if the sub-commands and options specified in the 'COMMAND' and + 'OPTIONS' (case insensitve) sections in sync the command parser. + ''' + def __init__(self, app, command, document): + docutils.nodes.SparseNodeVisitor.__init__(self, document) + try: + parser = qubesmgmt.tools.get_parser_for_command(command) + except ImportError: + app.warn('cannot import module for command') + self.parser = None + return + except AttributeError: + raise sphinx.errors.SphinxError('cannot find parser in module') + + self.command = command + self.parser = parser + self.options = set() + self.sub_commands = {} + self.app = app + + # pylint: disable=protected-access + for action in parser._actions: + if action.help == argparse.SUPPRESS: + continue + + if issubclass(action.__class__, + qubesmgmt.tools.AliasedSubParsersAction): + for cmd, cmd_parser in action._name_parser_map.items(): + self.sub_commands[cmd] = set() + for sub_action in cmd_parser._actions: + if sub_action.help != argparse.SUPPRESS: + self.sub_commands[cmd].update( + sub_action.option_strings) + else: + self.options.update(action.option_strings) + + def visit_section(self, node): + ''' If section title is OPTIONS or COMMANDS dispatch the apropriate + `NodeVisitor`. + ''' + if self.parser is None: + return + + section_title = str(node[0][0]).upper() + if section_title == OPTIONS_TITLE: + options_visitor = OptionsCheckVisitor(self.command, self.options, + self.document) + node.walkabout(options_visitor) + options_visitor.check_undocumented_arguments() + elif section_title == SUBCOMMANDS_TITLE: + sub_cmd_visitor = CommandCheckVisitor( + self.command, self.sub_commands, self.document) + node.walkabout(sub_cmd_visitor) + sub_cmd_visitor.check_undocumented_sub_commands() + +def check_man_args(app, doctree, docname): + ''' Checks the manpage for undocumented or obsolete sub-commands and + options. + ''' + dirname, command = os.path.split(docname) + if os.path.basename(dirname) != 'manpages': + return + + app.info('Checking arguments for {!r}'.format(command)) + doctree.walk(ManpageCheckVisitor(app, command, doctree)) + + + +def break_to_pdb(app, *dummy): + if not app.config.break_to_pdb: + return + import pdb + pdb.set_trace() + + +def setup(app): + app.add_config_value('break_to_pdb', False, 'env') + + app.connect('doctree-resolved', break_to_pdb) + app.connect('doctree-resolved', check_man_args) + + +# vim: ts=4 sw=4 et