~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
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@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@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@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