[Pkg-bazaar-commits] ./bzr/unstable r635: - manpage generator by Hans Ulrich Niedermann

Martin Pool mbp at sourcefrog.net
Fri Apr 10 08:18:48 UTC 2009


------------------------------------------------------------
revno: 635
committer: Martin Pool <mbp at sourcefrog.net>
timestamp: Mon 2005-06-06 23:24:18 +1000
message:
  - manpage generator by Hans Ulrich Niedermann
added:
  bzr-man.py
modified:
  .bzrignore
  bzrlib/help.py
  contrib/add-bzr-to-baz
-------------- next part --------------
=== modified file '.bzrignore'
--- a/.bzrignore	2005-06-06 12:31:16 +0000
+++ b/.bzrignore	2005-06-06 13:24:18 +0000
@@ -8,6 +8,7 @@
 {arch}
 CHANGELOG
 bzr-test.log
+bzr.1
 ,,*
 testbzr.log
 api

=== added file 'bzr-man.py'
--- a/bzr-man.py	1970-01-01 00:00:00 +0000
+++ b/bzr-man.py	2005-06-06 13:24:18 +0000
@@ -0,0 +1,269 @@
+#!/usr/bin/python
+
+# Copyright (C) 2005 by Hans Ulrich Niedermann
+# Portions Copyright (C) 2005 by Canonical Ltd
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 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 General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+#<<< code taken from bzr (C) Canonical
+
+import os, sys
+
+try:
+    version_info = sys.version_info
+except AttributeError:
+    version_info = 1, 5 # 1.5 or older
+
+
+REINVOKE = "__BZR_REINVOKE"    
+NEED_VERS = (2, 3)
+
+if version_info < NEED_VERS:
+    if not os.environ.has_key(REINVOKE):
+        # mutating os.environ doesn't work in old Pythons
+        os.putenv(REINVOKE, "1")
+        for python in 'python2.4', 'python2.3':
+            try:
+                os.execvp(python, [python] + sys.argv)
+            except OSError:
+                pass
+    print >>sys.stderr, "bzr-man.py: error: cannot find a suitable python interpreter"
+    print >>sys.stderr, "  (need %d.%d or later)" % NEED_VERS
+    sys.exit(1)
+os.unsetenv(REINVOKE)
+
+import bzrlib, bzrlib.help
+
+#>>> code taken from bzr (C) Canonical
+
+#<<< code by HUN
+
+import time
+import re
+
+
+def man_escape(string):
+    result = string.replace("\\","\\\\")
+    result = result.replace("`","\\`")
+    result = result.replace("'","\\'")
+    result = result.replace("-","\\-")
+    return result
+
+
+class Parser:
+
+    def parse_line(self, line):
+        pass
+
+
+class CommandListParser(Parser):
+
+    """Parser for output of "bzr help commands".
+
+    The parsed content can then be used to
+    - write a "COMMAND OVERVIEW" section into a man page
+    - provide a list of all commands
+    """
+
+    def __init__(self,params):
+        self.params = params
+        self.command_usage = []
+        self.all_commands = []
+        self.usage_exp = re.compile("([a-z0-9-]+).*")
+        self.descr_exp = re.compile("    ([A-Z].*)\s*")
+        self.state = 0
+        self.command = None
+        self.usage = None
+        self.descr = None
+
+    def parse_line(self, line):
+        m = self.usage_exp.match(line)
+        if m:
+            if self.state == 0:
+                if self.usage:
+                    self.command_usage.append((self.command,self.usage,self.descr))
+                    self.all_commands.append(self.command)
+                self.usage = line
+                self.command = m.groups()[0]
+            else:
+                raise Error, "matching usage line in state %d" % state
+            self.state = 1
+            return
+        m = self.descr_exp.match(line)
+        if m:
+            if self.state == 1:
+                self.descr = m.groups()[0]
+            else:
+                raise Error, "matching descr line in state %d" % state
+            self.state = 0
+            return
+        raise Error, "Cannot parse this line"
+
+    def end_parse(self):
+        if self.state == 0:
+            if self.usage:
+                self.command_usage.append((self.command,self.usage,self.descr))
+                self.all_commands.append(self.command)
+        else:
+            raise Error, "ending parse in state %d" % state
+
+    def write_to_manpage(self, outfile):
+        bzrcmd = self.params["bzrcmd"]
+        outfile.write('.SH "COMMAND OVERVIEW"\n')
+        for (command,usage,descr) in self.command_usage:
+            outfile.write('.TP\n.B "%s %s"\n%s\n\n' % (bzrcmd, usage, descr))
+
+
+class HelpReader:
+
+    def __init__(self, parser):
+        self.parser = parser
+
+    def write(self, data):
+        if data[-1] == '\n':
+            data = data[:-1]
+        for line in data.split('\n'):
+            self.parser.parse_line(line)
+
+
+def write_command_details(params, command, usage, descr, outfile):
+    x = ('.SS "%s %s"\n.B "%s"\n.PP\n.B "Usage:"\n%s %s\n\n' %
+         (params["bzrcmd"],
+          command,
+          descr,
+          params["bzrcmd"],
+          usage))
+    outfile.write(man_escape(x))
+
+
+man_preamble = """\
+.\\\" Man page for %(bzrcmd)s (bazaar-ng)
+.\\\"
+.\\\" Large parts of this file are autogenerated from the output of
+.\\\"     \"%(bzrcmd)s help commands\"
+.\\\"     \"%(bzrcmd)s help <cmd>\"
+.\\\"
+.\\\" Generation time: %(timestamp)s
+.\\\"
+"""
+
+# The DESCRIPTION text was taken from http://www.bazaar-ng.org/
+# and is thus (C) Canonical
+man_head = """\
+.TH bzr 1 "%(datestamp)s" "%(version)s" "bazaar-ng"
+.SH "NAME"
+%(bzrcmd)s - bazaar-ng next-generation distributed version control
+.SH "SYNOPSIS"
+.B "%(bzrcmd)s"
+.I "command"
+[
+.I "command_options"
+]
+.br
+.B "%(bzrcmd)s"
+.B "help"
+.br
+.B "%(bzrcmd)s"
+.B "help"
+.I "command"
+.SH "DESCRIPTION"
+bazaar-ng (or
+.B "%(bzrcmd)s"
+) is a project of Canonical to develop an open source distributed version control system that is powerful, friendly, and scalable. Version control means a system that keeps track of previous revisions of software source code or similar information and helps people work on it in teams.
+.SS "Warning"
+bazaar-ng is at an early stage of development, and the design is still changing from week to week. This man page here may be inconsistent with itself, with other documentation or with the code, and sometimes refer to features that are planned but not yet written. Comments are still very welcome; please send them to bazaar-ng at lists.canonical.com.
+"""
+
+man_foot = """\
+.SH "ENVIRONMENT"
+.TP
+.I "BZRPATH"
+Path where
+.B "%(bzrcmd)s"
+is to look for external command.
+
+.TP
+.I "BZREMAIL"
+E-Mail address of the user. Overrides
+.I "~/.bzr.conf/email" and
+.IR "EMAIL" .
+Example content:
+.I "John Doe <john at example.com>"
+
+.TP
+.I "EMAIL"
+E-Mail address of the user. Overridden by the content of the file
+.I "~/.bzr.conf/email"
+and of the environment variable
+.IR "BZREMAIL" .
+
+.SH "FILES"
+.TP
+.I "~/.bzr.conf/"
+Directory where all the user\'s settings are stored.
+.TP
+.I "~/.bzr.conf/email"
+Stores name and email address of the user. Overrides content of
+.I "EMAIL"
+environment variable. Example content:
+.I "John Doe <john at example.com>"
+
+.SH "SEE ALSO"
+.UR http://www.bazaar-ng.org/
+.BR http://www.bazaar-ng.org/,
+.UR http://www.bazaar-ng.org/doc/
+.BR http://www.bazaar-ng.org/doc/
+"""
+
+def main():
+    t = time.time()
+    tt = time.gmtime(t)
+    params = \
+           { "bzrcmd": "bzr",
+             "datestamp": time.strftime("%Y-%m-%d",tt),
+             "timestamp": time.strftime("%Y-%m-%d %H:%M:%S +0000",tt),
+             "version": bzrlib.__version__,
+             }
+
+    clp = CommandListParser(params)
+    bzrlib.help.help("commands", outfile=HelpReader(clp))
+    clp.end_parse()
+
+    filename = "bzr.1"
+    if len(sys.argv) == 2:
+        filename = sys.argv[1]
+    if filename == "-":
+        outfile = sys.stdout
+    else:
+        outfile = open(filename,"w")
+
+    outfile.write(man_preamble % params)
+    outfile.write(man_escape(man_head % params))
+    clp.write_to_manpage(outfile)
+
+    # FIXME:
+    #   This doesn't do more than the summary so far.
+    #outfile.write('.SH "DETAILED COMMAND DESCRIPTION"\n')
+    #for (command,usage,descr) in clp.command_usage:
+    #    write_command_details(params, command, usage, descr, outfile = outfile)
+
+    outfile.write(man_escape(man_foot % params))
+
+
+if __name__ == '__main__':
+    main()
+
+
+#>>> code by HUN

=== modified file 'bzrlib/help.py'
--- a/bzrlib/help.py	2005-05-11 06:20:05 +0000
+++ b/bzrlib/help.py	2005-06-06 13:24:18 +0000
@@ -56,14 +56,18 @@
 """
 
 
-
-def help(topic=None):
+import sys
+
+
+def help(topic=None, outfile = None):
+    if outfile == None:
+        outfile = sys.stdout
     if topic == None:
-        print global_help
+        outfile.write(global_help)
     elif topic == 'commands':
-        help_commands()
+        help_commands(outfile = outfile)
     else:
-        help_on_command(topic)
+        help_on_command(topic, outfile = outfile)
 
 
 def command_usage(cmdname, cmdclass):
@@ -88,9 +92,12 @@
     return s
 
 
-def help_on_command(cmdname):
+def help_on_command(cmdname, outfile = None):
     cmdname = str(cmdname)
 
+    if outfile == None:
+        outfile = sys.stdout
+
     from inspect import getdoc
     import commands
     topic, cmdclass = commands.get_cmd_class(cmdname)
@@ -99,37 +106,42 @@
     if doc == None:
         raise NotImplementedError("sorry, no detailed help yet for %r" % cmdname)
 
-    print 'usage:', command_usage(topic, cmdclass)
+    outfile.write('usage: ' + command_usage(topic, cmdclass) + '\n')
 
     if cmdclass.aliases:
-        print 'aliases: ' + ', '.join(cmdclass.aliases)
-    
-    print doc
-    
-    help_on_option(cmdclass.takes_options)
-
-
-def help_on_option(options):
+        outfile.write('aliases: ' + ', '.join(cmdclass.aliases) + '\n')
+    
+    outfile.write(doc)
+    
+    help_on_option(cmdclass.takes_options, outfile = None)
+
+
+def help_on_option(options, outfile = None):
     import commands
     
     if not options:
         return
     
-    print
-    print 'options:'
+    if outfile == None:
+        outfile = sys.stdout
+
+    outfile.write('\noptions:\n')
     for on in options:
         l = '    --' + on
         for shortname, longname in commands.SHORT_OPTIONS.items():
             if longname == on:
                 l += ', -' + shortname
                 break
-        print l
-
-
-def help_commands():
+        outfile.write(l + '\n')
+
+
+def help_commands(outfile = None):
     """List all commands"""
     import inspect
     import commands
+
+    if outfile == None:
+        outfile = sys.stdout
     
     accu = []
     for cmdname, cmdclass in commands.get_all_cmds():
@@ -138,9 +150,10 @@
     for cmdname, cmdclass in accu:
         if cmdclass.hidden:
             continue
-        print command_usage(cmdname, cmdclass)
+        outfile.write(command_usage(cmdname, cmdclass) + '\n')
         help = inspect.getdoc(cmdclass)
         if help:
-            print "    " + help.split('\n', 1)[0]
+            outfile.write("    " + help.split('\n', 1)[0] + '\n')
+
             
 

=== modified file 'contrib/add-bzr-to-baz'
--- a/contrib/add-bzr-to-baz	2005-05-05 02:43:47 +0000
+++ b/contrib/add-bzr-to-baz	2005-06-06 13:24:18 +0000
@@ -1,6 +1,7 @@
 #! /bin/sh -e
 
-# Add a file that's version by bzr to baz with the same file-id
+# Take a file that is versioned by bzr and
+# add it to baz with the same file-id.
 
 if [ $# -ne 1 ]
 then
@@ -8,5 +9,5 @@
     exit 1
 fi
 
-baz add -i $( bzr file-id $1 ) $1
+baz add -i "$( bzr file-id "$1" )" "$1"
 



More information about the Pkg-bazaar-commits mailing list