~bzr-pqm/bzr/bzr.dev

4988.10.3 by John Arbash Meinel
Merge bzr.dev 5007, resolve conflict, update NEWS
1
# Copyright (C) 2005-2010 Canonical Ltd
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
2
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
16
1551.3.11 by Aaron Bentley
Merge from Robert
17
"""man.py - create man page from built-in bzr help and static text
18
19
TODO:
20
  * use usage information instead of simple "bzr foo" in COMMAND OVERVIEW
21
  * add command aliases
22
"""
23
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
24
from __future__ import absolute_import
25
6498.4.1 by Jelmer Vernooij
Document launchpad plugin commands in manual page.
26
PLUGINS_TO_DOCUMENT = ["launchpad"]
27
1551.3.11 by Aaron Bentley
Merge from Robert
28
import textwrap
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
29
import time
1551.3.11 by Aaron Bentley
Merge from Robert
30
31
import bzrlib
32
import bzrlib.help
2666.1.3 by Ian Clatworthy
Fix man page to show plain text, not reStructuredText
33
import bzrlib.help_topics
1551.3.11 by Aaron Bentley
Merge from Robert
34
import bzrlib.commands
35
6498.4.1 by Jelmer Vernooij
Document launchpad plugin commands in manual page.
36
from bzrlib.plugin import load_plugins
37
load_plugins()
38
1551.3.11 by Aaron Bentley
Merge from Robert
39
40
def get_filename(options):
41
    """Provides name of manpage"""
42
    return "%s.1" % (options.bzr_name)
43
44
45
def infogen(options, outfile):
46
    """Assembles a man page"""
47
    t = time.time()
48
    tt = time.gmtime(t)
49
    params = \
50
           { "bzrcmd": options.bzr_name,
51
             "datestamp": time.strftime("%Y-%m-%d",tt),
52
             "timestamp": time.strftime("%Y-%m-%d %H:%M:%S +0000",tt),
53
             "version": bzrlib.__version__,
54
             }
55
    outfile.write(man_preamble % params)
56
    outfile.write(man_escape(man_head % params))
57
    outfile.write(man_escape(getcommand_list(params)))
58
    outfile.write(man_escape(getcommand_help(params)))
59
    outfile.write(man_escape(man_foot % params))
1185.74.4 by James Blackwell
Give long help with command on man page
60
1185.75.1 by Hans Ulrich Niedermann
Start implementing general doc generation plan:
61
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
62
def man_escape(string):
1551.3.11 by Aaron Bentley
Merge from Robert
63
    """Escapes strings for man page compatibility"""
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
64
    result = string.replace("\\","\\\\")
6498.2.2 by Jelmer Vernooij
Fix accent grave generation.
65
    result = result.replace("`","\\'")
6498.2.1 by Jelmer Vernooij
Properly format apostrophes in manual page.
66
    result = result.replace("'","\\*(Aq")
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
67
    result = result.replace("-","\\-")
68
    return result
1185.75.1 by Hans Ulrich Niedermann
Start implementing general doc generation plan:
69
70
1551.3.11 by Aaron Bentley
Merge from Robert
71
def command_name_list():
72
    """Builds a list of command names from bzrlib"""
73
    command_names = bzrlib.commands.builtin_command_names()
6498.4.1 by Jelmer Vernooij
Document launchpad plugin commands in manual page.
74
    for cmdname in bzrlib.commands.plugin_command_names():
75
        cmd_object = bzrlib.commands.get_cmd_object(cmdname)
76
        if (PLUGINS_TO_DOCUMENT is None or
6498.4.3 by Jelmer Vernooij
Use Command.plugin_name().
77
            cmd_object.plugin_name() in PLUGINS_TO_DOCUMENT):
6498.4.1 by Jelmer Vernooij
Document launchpad plugin commands in manual page.
78
            command_names.append(cmdname)
1551.3.11 by Aaron Bentley
Merge from Robert
79
    command_names.sort()
80
    return command_names
81
82
83
def getcommand_list (params):
84
    """Builds summary help for command names in manpage format"""
85
    bzrcmd = params["bzrcmd"]
86
    output = '.SH "COMMAND OVERVIEW"\n'
87
    for cmd_name in command_name_list():
88
        cmd_object = bzrlib.commands.get_cmd_object(cmd_name)
89
        if cmd_object.hidden:
90
            continue
91
        cmd_help = cmd_object.help()
92
        if cmd_help:
93
            firstline = cmd_help.split('\n', 1)[0]
2432.1.33 by Robert Collins
Fix tests broken by deprecation of help.command_usage.
94
            usage = cmd_object._usage()
1551.3.11 by Aaron Bentley
Merge from Robert
95
            tmp = '.TP\n.B "%s"\n%s\n' % (usage, firstline)
96
            output = output + tmp
1185.75.1 by Hans Ulrich Niedermann
Start implementing general doc generation plan:
97
        else:
1551.3.11 by Aaron Bentley
Merge from Robert
98
            raise RuntimeError, "Command '%s' has no help text" % (cmd_name)
99
    return output
100
101
102
def getcommand_help(params):
103
    """Shows individual options for a bzr command"""
104
    output='.SH "COMMAND REFERENCE"\n'
4056.4.1 by James Westby
Explicitly list aliases in the manpage as commands that can be executed.
105
    formatted = {}
1551.3.11 by Aaron Bentley
Merge from Robert
106
    for cmd_name in command_name_list():
107
        cmd_object = bzrlib.commands.get_cmd_object(cmd_name)
108
        if cmd_object.hidden:
109
            continue
4056.4.1 by James Westby
Explicitly list aliases in the manpage as commands that can be executed.
110
        formatted[cmd_name] = format_command(params, cmd_object)
111
        for alias in cmd_object.aliases:
112
            formatted[alias] = format_alias(params, alias, cmd_name)
4056.4.2 by James Westby
Tweaks from review by Ian.
113
    for cmd_name in sorted(formatted):
114
        output += formatted[cmd_name]
1551.3.11 by Aaron Bentley
Merge from Robert
115
    return output
116
117
6498.4.3 by Jelmer Vernooij
Use Command.plugin_name().
118
def format_command(params, cmd):
1551.3.11 by Aaron Bentley
Merge from Robert
119
    """Provides long help for each public command"""
2432.1.33 by Robert Collins
Fix tests broken by deprecation of help.command_usage.
120
    subsection_header = '.SS "%s"\n' % (cmd._usage())
1551.3.11 by Aaron Bentley
Merge from Robert
121
    doc = "%s\n" % (cmd.__doc__)
2666.1.3 by Ian Clatworthy
Fix man page to show plain text, not reStructuredText
122
    doc = bzrlib.help_topics.help_as_plain_text(cmd.help())
1861.3.2 by Alexander Belchenko
bzr.1 man page: show aliases for commands; fixed description of bazaar.conf
123
6503.1.1 by Jelmer Vernooij
Prevent lines starting with a dot to accidentally be interpreted as a roff macro.
124
    # A dot at the beginning of a line is interpreted as a macro.
125
    # Simply join lines that begin with a dot with the previous
126
    # line to work around this.
127
    doc = doc.replace("\n.", ".")
128
1551.3.11 by Aaron Bentley
Merge from Robert
129
    option_str = ""
130
    options = cmd.options()
131
    if options:
132
        option_str = "\nOptions:\n"
133
        for option_name, option in sorted(options.items()):
1857.1.14 by Aaron Bentley
Fix man page generation
134
            for name, short_name, argname, help in option.iter_switches():
1551.18.12 by Aaron Bentley
Man page doesn't list hidden options (#131667)
135
                if option.is_hidden(name):
136
                    continue
1857.1.14 by Aaron Bentley
Fix man page generation
137
                l = '    --' + name
138
                if argname is not None:
139
                    l += ' ' + argname
140
                if short_name:
141
                    l += ', -' + short_name
2309.3.1 by bialix at ukr
Fix for selftest of man generator with instaled plugin 'htmllog' (no help for plugin's log formatter)
142
                l += (30 - len(l)) * ' ' + (help or '')
1857.1.14 by Aaron Bentley
Fix man page generation
143
                wrapped = textwrap.fill(l, initial_indent='',
4070.11.2 by Martin Pool
Ask textwrap not to break long words or on hyphens
144
                    subsequent_indent=30*' ',
145
                    break_long_words=False,
4070.11.5 by Martin Pool
textwrap break_on_hyphens option is not available in python2.5
146
                    )
6498.4.3 by Jelmer Vernooij
Use Command.plugin_name().
147
                option_str += wrapped + '\n'
1861.3.2 by Alexander Belchenko
bzr.1 man page: show aliases for commands; fixed description of bazaar.conf
148
149
    aliases_str = ""
150
    if cmd.aliases:
151
        if len(cmd.aliases) > 1:
152
            aliases_str += '\nAliases: '
153
        else:
154
            aliases_str += '\nAlias: '
155
        aliases_str += ', '.join(cmd.aliases)
156
        aliases_str += '\n'
157
2425.2.4 by Robert Collins
Update the doc_generate logic to include see-also help topics.
158
    see_also_str = ""
159
    see_also = cmd.get_see_also()
160
    if see_also:
161
        see_also_str += '\nSee also: '
162
        see_also_str += ', '.join(see_also)
163
        see_also_str += '\n'
164
165
    return subsection_header + option_str + aliases_str + see_also_str + "\n" + doc + "\n"
1185.75.1 by Hans Ulrich Niedermann
Start implementing general doc generation plan:
166
167
4056.4.1 by James Westby
Explicitly list aliases in the manpage as commands that can be executed.
168
def format_alias(params, alias, cmd_name):
169
    help = '.SS "bzr %s"\n' % alias
170
    help += 'Alias for "%s", see "bzr %s".\n' % (cmd_name, cmd_name)
171
    return help
172
173
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
174
man_preamble = """\
3709.2.1 by David Futcher
Make the first line of the manpage preamble a comment again (#242106)
175
.\\\"Man page for Bazaar (%(bzrcmd)s)
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
176
.\\\"
177
.\\\" Large parts of this file are autogenerated from the output of
178
.\\\"     \"%(bzrcmd)s help commands\"
179
.\\\"     \"%(bzrcmd)s help <cmd>\"
180
.\\\"
181
.\\\" Generation time: %(timestamp)s
182
.\\\"
6498.2.1 by Jelmer Vernooij
Properly format apostrophes in manual page.
183
184
.ie \\n(.g .ds Aq \\(aq
185
.el .ds Aq '
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
186
"""
187
1551.3.11 by Aaron Bentley
Merge from Robert
188
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
189
man_head = """\
1861.2.8 by Alexander Belchenko
More branding: bazaar-ng -> Bazaar; bazaar-ng.org -> bazaar-vcs.org
190
.TH bzr 1 "%(datestamp)s" "%(version)s" "Bazaar"
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
191
.SH "NAME"
4785.1.1 by John Ferlito
lexgrog doesn't like having "Bazaar (bzr)" as the nae of the command page
192
%(bzrcmd)s - Bazaar next-generation distributed version control
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
193
.SH "SYNOPSIS"
194
.B "%(bzrcmd)s"
195
.I "command"
196
[
197
.I "command_options"
198
]
199
.br
200
.B "%(bzrcmd)s"
201
.B "help"
202
.br
203
.B "%(bzrcmd)s"
204
.B "help"
205
.I "command"
206
.SH "DESCRIPTION"
6015.8.1 by Martin Pool
Mention GNU and improve phrasing of the start of the man page.
207
208
Bazaar (or %(bzrcmd)s) is a distributed version control system that is powerful, 
209
friendly, and scalable.  Bazaar is a project of Canonical Ltd and part of 
210
the GNU Project to develop a free operating system.
211
212
Bazaar keeps track of changes to software source code (or similar information);
213
lets you explore who changed it, when, and why; merges concurrent changes; and
214
helps people work together in a team.
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
215
"""
216
217
man_foot = """\
218
.SH "ENVIRONMENT"
219
.TP
220
.I "BZRPATH"
221
Path where
222
.B "%(bzrcmd)s"
2374.1.1 by Ian Clatworthy
Help and man page fixes
223
is to look for shell plugin external commands.
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
224
.TP
1861.4.1 by Matthieu Moy
BZREMAIL renamed to BZR_EMAIL.
225
.I "BZR_EMAIL"
1551.3.11 by Aaron Bentley
Merge from Robert
226
E-Mail address of the user. Overrides default user config.
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
227
.TP
228
.I "EMAIL"
2221.3.1 by Alexander Belchenko
Bugfix #76603: fix typo in bzr manpage text
229
E-Mail address of the user. Overrides default user config.
1861.3.4 by Alexander Belchenko
more environment variables for man page
230
.TP
231
.I "BZR_EDITOR"
232
Editor for editing commit messages
233
.TP
234
.I "EDITOR"
235
Editor for editing commit messages
236
.TP
237
.I "BZR_PLUGIN_PATH"
238
Paths where bzr should look for plugins
239
.TP
240
.I "BZR_HOME"
241
Home directory for bzr
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
242
.SH "FILES"
243
.TP
1861.3.2 by Alexander Belchenko
bzr.1 man page: show aliases for commands; fixed description of bazaar.conf
244
.I "~/.bazaar/bazaar.conf"
2374.1.1 by Ian Clatworthy
Help and man page fixes
245
Contains the user's default configuration. The section
1551.3.11 by Aaron Bentley
Merge from Robert
246
.B [DEFAULT]
1861.2.14 by Alexander Belchenko
generate_docs (man/rstx): fixed description of configuration file
247
is used to define general configuration that will be applied everywhere.
248
The section
249
.B [ALIASES]
250
can be used to create command aliases for
251
commonly used options.
252
253
A typical config file might look something like:
254
255
.br
256
[DEFAULT]
257
.br
258
email=John Doe <jdoe@isp.com>
259
.br
260
[ALIASES]
261
.br
262
commit = commit --strict
263
.br
264
log10 = log --short -r -10..-1
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
265
.SH "SEE ALSO"
5050.47.1 by Martin Pool
Backport fix for bad url in bzr help
266
.UR http://bazaar.canonical.com/
267
.BR http://bazaar.canonical.com/
635 by Martin Pool
- manpage generator by Hans Ulrich Niedermann
268
"""
269