From 6faf450a35601ceb8bafeb9e4f2fdd1a7c0304f9 Mon Sep 17 00:00:00 2001 From: Hans Ulrich Niedermann Date: Thu, 10 Feb 2011 16:39:03 +0100 Subject: Move location/name of man_page module Move location/name of man_page module (out of the pyfedpkg module/namespace) in order to avoid importing pyfedpkg for man page generation. --- src/fedpkg.py | 8 +-- src/fedpkg_man_page.py | 146 +++++++++++++++++++++++++++++++++++++++++++++++ src/pyfedpkg/man_page.py | 146 ----------------------------------------------- 3 files changed, 148 insertions(+), 152 deletions(-) create mode 100644 src/fedpkg_man_page.py delete mode 100644 src/pyfedpkg/man_page.py (limited to 'src') diff --git a/src/fedpkg.py b/src/fedpkg.py index 4275f5f..91a7b5d 100755 --- a/src/fedpkg.py +++ b/src/fedpkg.py @@ -1428,12 +1428,8 @@ defined, packages will be built sequentially.""") 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) + import fedpkg_man_page + fedpkg_man_page.generate(parser, subparsers) sys.exit(0) # no return possible diff --git a/src/fedpkg_man_page.py b/src/fedpkg_man_page.py new file mode 100644 index 0000000..117ad62 --- /dev/null +++ b/src/fedpkg_man_page.py @@ -0,0 +1,146 @@ +# 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) + + choices = subparsers.choices + k = choices.keys() + k.sort() + + man_file.write(man_header % man_constants()) + + helptext = parser.format_help() + helptext = strip_usage(helptext) + helptextsplit = helptext.split('\n') + helptextsplit = [ line for line in helptextsplit + if not line.startswith(' -h, --help') ] + + man_file.write('.SS "%s"\n' % ("Global Options",)) + + outflag = False + for line in helptextsplit: + if line == "optional arguments:": + outflag = True + elif line == "": + outflag = False + elif outflag: + man_file.write("%s\n" % line) + + help_texts = {} + for pa in subparsers._choices_actions: + help_texts[pa.dest] = getattr(pa, 'help', None) + + man_file.write('.SH "COMMAND OVERVIEW"\n') + + for command in k: + cmdparser = choices[command] + if not cmdparser.add_help: + continue + usage = cmdparser.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: + cmdparser = choices[command] + if not cmdparser.add_help: + continue + + man_file.write('.SS "%s"\n' % cmdparser.prog) + + help = help_texts[command] + if help and not cmdparser.description: + if not help.endswith('.'): help = "%s." % help + cmdparser.description = help + + formatter = cmdparser.formatter_class(cmdparser.prog) + h = cmdparser.format_help() + mf.write(h) + + man_file.write(man_footer) diff --git a/src/pyfedpkg/man_page.py b/src/pyfedpkg/man_page.py deleted file mode 100644 index 117ad62..0000000 --- a/src/pyfedpkg/man_page.py +++ /dev/null @@ -1,146 +0,0 @@ -# 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) - - choices = subparsers.choices - k = choices.keys() - k.sort() - - man_file.write(man_header % man_constants()) - - helptext = parser.format_help() - helptext = strip_usage(helptext) - helptextsplit = helptext.split('\n') - helptextsplit = [ line for line in helptextsplit - if not line.startswith(' -h, --help') ] - - man_file.write('.SS "%s"\n' % ("Global Options",)) - - outflag = False - for line in helptextsplit: - if line == "optional arguments:": - outflag = True - elif line == "": - outflag = False - elif outflag: - man_file.write("%s\n" % line) - - help_texts = {} - for pa in subparsers._choices_actions: - help_texts[pa.dest] = getattr(pa, 'help', None) - - man_file.write('.SH "COMMAND OVERVIEW"\n') - - for command in k: - cmdparser = choices[command] - if not cmdparser.add_help: - continue - usage = cmdparser.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: - cmdparser = choices[command] - if not cmdparser.add_help: - continue - - man_file.write('.SS "%s"\n' % cmdparser.prog) - - help = help_texts[command] - if help and not cmdparser.description: - if not help.endswith('.'): help = "%s." % help - cmdparser.description = help - - formatter = cmdparser.formatter_class(cmdparser.prog) - h = cmdparser.format_help() - mf.write(h) - - man_file.write(man_footer) -- cgit