summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHans Ulrich Niedermann <hun@n-dimensional.de>2010-08-05 14:46:07 +0200
committerHans Ulrich Niedermann <hun@n-dimensional.de>2010-08-05 15:31:25 +0200
commit642b1a2ca4492c3dafccd974eac9e729731fa0e7 (patch)
treea2c6412f4eb665c4668e6887325462f18cd50c4f
parent6217e6fb371888ea8b19cedefa936beff6f50850 (diff)
downloadfedora-packager-642b1a2ca4492c3dafccd974eac9e729731fa0e7.tar.gz
fedora-packager-642b1a2ca4492c3dafccd974eac9e729731fa0e7.tar.xz
fedora-packager-642b1a2ca4492c3dafccd974eac9e729731fa0e7.zip
Add basic man page generator
-rwxr-xr-xsrc/fedpkg.py5
-rw-r--r--src/pyfedpkg/__init__.py2
-rw-r--r--src/pyfedpkg/man_page.py149
3 files changed, 156 insertions, 0 deletions
diff --git a/src/fedpkg.py b/src/fedpkg.py
index 005b152..392f5c4 100755
--- a/src/fedpkg.py
+++ b/src/fedpkg.py
@@ -934,6 +934,9 @@ packages will be built sequentially.
' name-version-release')
parser_verrel.set_defaults(command = verrel)
+ # man page
+ pyfedpkg.man_page.add_parser(subparsers)
+
# Parse the args
args = parser.parse_args()
@@ -951,4 +954,6 @@ packages will be built sequentially.
log.addHandler(streamhandler)
# Run the necessary command
+ args.parser = parser # the man page generator needs this
+ args.subparsers = subparsers # the man page generator needs this
args.command(args)
diff --git a/src/pyfedpkg/__init__.py b/src/pyfedpkg/__init__.py
index fe4f3a0..48bb019 100644
--- a/src/pyfedpkg/__init__.py
+++ b/src/pyfedpkg/__init__.py
@@ -28,6 +28,8 @@ import stat
import StringIO
import OpenSSL
+from . import man_page
+
# Define some global variables, put them here to make it easy to change
LOOKASIDE = 'http://pkgs.fedoraproject.org/repo/pkgs'
diff --git a/src/pyfedpkg/man_page.py b/src/pyfedpkg/man_page.py
new file mode 100644
index 0000000..bd8cb90
--- /dev/null
+++ b/src/pyfedpkg/man_page.py
@@ -0,0 +1,149 @@
+# Print a man page from the help texts.
+
+
+import argparse
+import sys
+import datetime
+
+
+__all__ = ['get_parser']
+
+
+# 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 usage_clean(s):
+ 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 fedpkg_command(args):
+ man_file = args.outfile
+
+ # Not nice, but redirects any print statement output to stderr
+ sys.stdout = sys.stderr
+
+ mf = ManFormatter(man_file)
+
+ subparsers = args.subparsers
+
+ #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 = usage_clean(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)
+
+
+def add_parser(subparsers):
+
+ descr = """\
+Generate a basic fedpkg(1) man page from the command line argument
+help text and descriptions.
+"""
+
+ sp = subparsers.add_parser('man-page',
+ help = 'Generate a fedpkg(1) man page',
+ description = descr)
+ sp.add_argument('-o', '--output',
+ dest='outfile', nargs='?',
+ type=argparse.FileType('w'), default=sys.stdout,
+ help="File to write the man page to. Default is stdout.")
+ sp.set_defaults(command = fedpkg_command)
+ return sp