diff options
author | Hans Ulrich Niedermann <hun@n-dimensional.de> | 2011-01-21 11:05:31 +0100 |
---|---|---|
committer | Hans Ulrich Niedermann <hun@n-dimensional.de> | 2011-01-21 11:05:31 +0100 |
commit | a730ad030b7d37b4e50a0cb8072beb1d99cf5158 (patch) | |
tree | 1ace146a43875620bf084e3ef65628b4684a3729 /src | |
parent | 7ee64a029b22cb4a4cdafd5a827b7e06be519022 (diff) | |
download | fedora-packager-a730ad030b7d37b4e50a0cb8072beb1d99cf5158.tar.gz fedora-packager-a730ad030b7d37b4e50a0cb8072beb1d99cf5158.tar.xz fedora-packager-a730ad030b7d37b4e50a0cb8072beb1d99cf5158.zip |
Add build time man page generator4/man-page
Add man page generator to generate a man page at build time.
Diffstat (limited to 'src')
-rwxr-xr-x | src/fedpkg.py | 15 | ||||
-rw-r--r-- | src/pyfedpkg/man_page.py | 134 |
2 files changed, 147 insertions, 2 deletions
diff --git a/src/fedpkg.py b/src/fedpkg.py index 4bd565f..eff2842 100755 --- a/src/fedpkg.py +++ b/src/fedpkg.py @@ -1220,8 +1220,19 @@ packages will be built sequentially. ' name-version-release') parser_verrel.set_defaults(command = verrel) - # Parse the args - return parser.parse_args() + if not generate_manpage: + # Parse the args + return parser.parse_args() + else: + # Generate the man page + + # Use the "as man_page" part to avoid overwriting the pyfedpkg + # namespace, which would break all usage of pyfedpkg.* outside + # of this else branch. + import pyfedpkg.man_page as man_page + man_page.generate(parser, subparsers) + sys.exit(0) + # no return possible # The main code goes here diff --git a/src/pyfedpkg/man_page.py b/src/pyfedpkg/man_page.py new file mode 100644 index 0000000..a9e16f0 --- /dev/null +++ b/src/pyfedpkg/man_page.py @@ -0,0 +1,134 @@ +# Print a man page from the help texts. + + +import argparse +import sys +import datetime + + +# We could substitute the "" in .TH with the fedpkg version if we knew it +man_header = """\ +.\" man page for fedpkg +.TH fedpkg 1 "%(today)s" "" "fedora\-packager" +.SH "NAME" +fedpkg \- Fedora Packaging utility +.SH "SYNOPSIS" +.B "fedpkg" +[ +.I global_options +] +.I "command" +[ +.I command_options +] +[ +.I command_arguments +] +.br +.B "fedpkg" +.B "help" +.br +.B "fedpkg" +.I "command" +.B "\-\-help" +.SH "DESCRIPTION" +.B "fedpkg" +is a script to interact with the Fedora Packaging system. +""" + +man_footer = """\ +.SH "SEE ALSO" +.UR "https://fedorahosted.org/fedora\-packager/" +.BR "https://fedorahosted.org/fedora\-packager/" +""" + +class ManFormatter(object): + + def __init__(self, man): + self.man = man + + def write(self, data): + #print "MF:", repr(data) + for line in data.split('\n'): + #print 'MFL:', line + self.man.write(' %s\n' % line) + + +def strip_usage(s): + """Strip "usage: " string from beginning of string if present""" + if s.startswith('usage: '): + return s.replace('usage: ', '', 1) + else: + return s + + +def man_constants(): + """Global constants for man file templates""" + today = datetime.date.today() + today_manstr = today.strftime('%Y\-%m\-%d') + return {'today': today_manstr} + + +def generate(parser, subparsers): + """\ + Generate the man page on stdout + + Given the argparse based parser and subparsers arguments, generate + the corresponding man page and write it to stdout. + """ + + # Not nice, but works: Redirect any print statement output to + # stderr to avoid clobbering the man page output on stdout. + man_file = sys.stdout + sys.stdout = sys.stderr + + mf = ManFormatter(man_file) + + #print dir(subparsers) + # subparsers.print_help() + + choices = subparsers.choices + k = choices.keys() + k.sort() + + man_file.write(man_header % man_constants()) + + man_file.write('.SH "COMMAND OVERVIEW"\n') + + help_texts = {} + for pa in subparsers._choices_actions: + help_texts[pa.dest] = getattr(pa, 'help', None) + + for command in k: + parser = choices[command] + if not parser.add_help: + continue + usage = parser.format_usage() + usage = strip_usage(usage) + usage = ''.join(usage.split('\n')) + usage = ' '.join(usage.split()) + if help_texts[command]: + man_file.write('.TP\n.B "%s"\n%s\n' % (usage, help_texts[command])) + else: + man_file.write('.TP\n.B "%s"\n' % (usage)) + + man_file.write('.SH "COMMAND REFERENCE"\n') + for command in k: + parser = choices[command] + if not parser.add_help: + continue + + #print + #print "####", command, "####" + man_file.write('.SS "%s"\n' % parser.prog) + + help = help_texts[command] + if help and not parser.description: + if not help.endswith('.'): help = "%s." % help + parser.description = help + #print parser + formatter = parser.formatter_class(parser.prog) + h = parser.format_help() + mf.write(h) + + man_file.write(man_footer) |