Fix Sphinx 2 new API for Fedora 31+

QubesOS/qubes-issues#5289
This commit is contained in:
Frédéric Pierret (fepitre) 2019-09-05 22:55:20 +02:00
parent 769f8a5ee8
commit ab8f487b50
No known key found for this signature in database
GPG Key ID: 484010B5CDC576E2
2 changed files with 19 additions and 15 deletions

View File

@ -2,6 +2,7 @@
import os import os
import sys import sys
sys.path.insert(0, os.path.abspath('../')) sys.path.insert(0, os.path.abspath('../'))
import argparse import argparse
@ -12,9 +13,11 @@ parser.add_argument('command', metavar='COMMAND',
help='program\'s command name; this should translate to ' help='program\'s command name; this should translate to '
'qubesadmin.tools.<command_name>') 'qubesadmin.tools.<command_name>')
def main(): def main():
args = parser.parse_args() args = parser.parse_args()
sys.stdout.write(qubesadmin.tools.dochelpers.prepare_manpage(args.command)) sys.stdout.write(qubesadmin.tools.dochelpers.prepare_manpage(args.command))
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -39,9 +39,12 @@ import sphinx
import sphinx.errors import sphinx.errors
import sphinx.locale import sphinx.locale
import sphinx.util.docfields import sphinx.util.docfields
from sphinx.util import logging
import qubesadmin.tools import qubesadmin.tools
log = logging.getLogger(__name__)
SUBCOMMANDS_TITLE = 'COMMANDS' SUBCOMMANDS_TITLE = 'COMMANDS'
OPTIONS_TITLE = 'OPTIONS' OPTIONS_TITLE = 'OPTIONS'
@ -82,11 +85,10 @@ def prepare_manpage(command):
for action in parser._actions: # pylint: disable=protected-access for action in parser._actions: # pylint: disable=protected-access
stream.write('.. option:: ') stream.write('.. option:: ')
if action.metavar: if action.metavar:
stream.write(', '.join('{}{}{}'.format( stream.write(', '.join(
option, '{}{}{}'.format(option, '=' if option.startswith('--') else ' ',
'=' if option.startswith('--') else ' ', action.metavar) for option in
action.metavar) sorted(action.option_strings)))
for option in sorted(action.option_strings)))
else: else:
stream.write(', '.join(sorted(action.option_strings))) stream.write(', '.join(sorted(action.option_strings)))
stream.write('\n\n {}\n\n'.format(action.help)) stream.write('\n\n {}\n\n'.format(action.help))
@ -107,6 +109,7 @@ def prepare_manpage(command):
class OptionsCheckVisitor(docutils.nodes.SparseNodeVisitor): class OptionsCheckVisitor(docutils.nodes.SparseNodeVisitor):
''' Checks if the visited option nodes and the specified args are in sync. ''' Checks if the visited option nodes and the specified args are in sync.
''' '''
def __init__(self, command, args, document): def __init__(self, command, args, document):
assert isinstance(args, set) assert isinstance(args, set)
docutils.nodes.SparseNodeVisitor.__init__(self, document) docutils.nodes.SparseNodeVisitor.__init__(self, document)
@ -119,7 +122,6 @@ class OptionsCheckVisitor(docutils.nodes.SparseNodeVisitor):
if not node.get('desctype', None) == 'option': if not node.get('desctype', None) == 'option':
raise docutils.nodes.SkipChildren raise docutils.nodes.SkipChildren
def visit_desc_name(self, node): def visit_desc_name(self, node):
''' Checks if the option is defined `self.args` ''' ''' Checks if the option is defined `self.args` '''
if not isinstance(node[0], docutils.nodes.Text): if not isinstance(node[0], docutils.nodes.Text):
@ -198,7 +200,6 @@ class CommandCheckVisitor(docutils.nodes.SparseNodeVisitor):
assert alias in self.sub_commands assert alias in self.sub_commands
del self.sub_commands[alias] del self.sub_commands[alias]
def check_undocumented_sub_commands(self): def check_undocumented_sub_commands(self):
''' Call this to check if any undocumented sub_commands are left. ''' Call this to check if any undocumented sub_commands are left.
@ -218,12 +219,13 @@ class ManpageCheckVisitor(docutils.nodes.SparseNodeVisitor):
''' Checks if the sub-commands and options specified in the 'COMMAND' and ''' Checks if the sub-commands and options specified in the 'COMMAND' and
'OPTIONS' (case insensitve) sections in sync the command parser. 'OPTIONS' (case insensitve) sections in sync the command parser.
''' '''
def __init__(self, app, command, document): def __init__(self, app, command, document):
docutils.nodes.SparseNodeVisitor.__init__(self, document) docutils.nodes.SparseNodeVisitor.__init__(self, document)
try: try:
parser = qubesadmin.tools.get_parser_for_command(command) parser = qubesadmin.tools.get_parser_for_command(command)
except ImportError: except ImportError:
app.warn('cannot import module for command') log.warning('cannot import module for command')
self.parser = None self.parser = None
return return
except AttributeError: except AttributeError:
@ -270,6 +272,7 @@ class ManpageCheckVisitor(docutils.nodes.SparseNodeVisitor):
node.walkabout(sub_cmd_visitor) node.walkabout(sub_cmd_visitor)
sub_cmd_visitor.check_undocumented_sub_commands() sub_cmd_visitor.check_undocumented_sub_commands()
def check_man_args(app, doctree, docname): def check_man_args(app, doctree, docname):
''' Checks the manpage for undocumented or obsolete sub-commands and ''' Checks the manpage for undocumented or obsolete sub-commands and
options. options.
@ -278,11 +281,10 @@ def check_man_args(app, doctree, docname):
if os.path.basename(dirname) != 'manpages': if os.path.basename(dirname) != 'manpages':
return return
app.info('Checking arguments for {!r}'.format(command)) log.info('Checking arguments for {!r}'.format(command))
doctree.walk(ManpageCheckVisitor(app, command, doctree)) doctree.walk(ManpageCheckVisitor(app, command, doctree))
def break_to_pdb(app, *_dummy): def break_to_pdb(app, *_dummy):
'''DEBUG''' '''DEBUG'''
if not app.config.break_to_pdb: if not app.config.break_to_pdb:
@ -298,5 +300,4 @@ def setup(app):
app.connect('doctree-resolved', break_to_pdb) app.connect('doctree-resolved', break_to_pdb)
app.connect('doctree-resolved', check_man_args) app.connect('doctree-resolved', check_man_args)
# vim: ts=4 sw=4 et # vim: ts=4 sw=4 et