/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.27.16 by Martin von Gagern
Added distutils setup script and plugin meta data.
1
#!/usr/bin/env python
0.27.2 by Martin von Gagern
First programmatic generation of completions.
2
0.27.14 by Martin von Gagern
Added GPL 2 license document and copyright notice.
3
# Copyright (C) 2009, 2010  Martin von Gagern
4
#
5
# This file is part of bzr-bash-completion
6
#
7
# bzr-bash-completion free software: you can redistribute it and/or
8
# modify it under the terms of the GNU General Public License as
9
# published by the Free Software Foundation, either version 2 of the
10
# License, or (at your option) any later version.
11
#
12
# bzr-bash-completion is distributed in the hope that it will be
13
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
# General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
0.27.17 by Martin von Gagern
Mention version and homepage in generated shell code comment.
20
from meta import __version__
0.28.2 by Martin von Gagern
merge from trunk
21
from bzrlib import (
22
    commands,
23
    config,
24
    help_topics,
25
    option,
26
    plugin,
27
)
0.27.36 by Martin von Gagern
List versions of bzr and plugins.
28
import bzrlib
0.27.18 by Martin von Gagern
Add global options in all cases
29
import re
0.27.2 by Martin von Gagern
First programmatic generation of completions.
30
0.27.7 by Martin von Gagern
Introduce --function-only option
31
head="""\
0.30.3 by Martin von Gagern
Lazy initialization
32
# Programmable completion for the Bazaar-NG bzr command under bash.
33
# Known to work with bash 2.05a as well as bash 4.1.2, and probably
34
# all versions in between as well.
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
35
36
# Based originally on the svn bash completition script.
37
# Customized by Sven Wilhelm/Icecrash.com
0.27.2 by Martin von Gagern
First programmatic generation of completions.
38
# Adjusted for automatic generation by Martin von Gagern
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
39
0.27.17 by Martin von Gagern
Mention version and homepage in generated shell code comment.
40
# Generated using the bzr-bash-completion plugin version %(version)s.
41
# See https://launchpad.net/bzr-bash-completion for details.
42
0.27.36 by Martin von Gagern
List versions of bzr and plugins.
43
# Commands and options of bzr %(bzr_version)s
44
0.27.20 by Martin von Gagern
More flexible handling of fixed word lists.
45
shopt -s progcomp
0.27.7 by Martin von Gagern
Introduce --function-only option
46
"""
47
fun="""\
0.27.4 by Martin von Gagern
Introduce --function-name switch. (LP #439829)
48
%(function_name)s ()
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
49
{
0.27.20 by Martin von Gagern
More flexible handling of fixed word lists.
50
	local cur cmds cmdIdx cmd cmdOpts fixedWords i globalOpts
0.27.30 by Martin von Gagern
Complete values for enum_switch of RegistryOptions.
51
	local curOpt optEnums
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
52
53
	COMPREPLY=()
54
	cur=${COMP_WORDS[COMP_CWORD]}
55
0.27.2 by Martin von Gagern
First programmatic generation of completions.
56
	cmds='%(cmds)s'
0.27.19 by Martin von Gagern
Take global options preceding the command into account.
57
	globalOpts='%(global_options)s'
58
59
	# do ordinary expansion if we are anywhere after a -- argument
60
	for ((i = 1; i < COMP_CWORD; ++i)); do
61
		[[ ${COMP_WORDS[i]} == "--" ]] && return 0
62
	done
63
64
	# find the command; it's the first word not starting in -
65
	cmd=
66
	for ((cmdIdx = 1; cmdIdx < ${#COMP_WORDS[@]}; ++cmdIdx)); do
67
		if [[ ${COMP_WORDS[cmdIdx]} != -* ]]; then
68
			cmd=${COMP_WORDS[cmdIdx]}
69
			break
70
		fi
71
	done
72
73
	# complete command name if we are not already past the command
0.27.30 by Martin von Gagern
Complete values for enum_switch of RegistryOptions.
74
	if [[ $COMP_CWORD -le cmdIdx ]]; then
0.27.18 by Martin von Gagern
Add global options in all cases
75
		COMPREPLY=( $( compgen -W "$cmds $globalOpts" -- $cur ) )
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
76
		return 0
77
	fi
78
0.27.30 by Martin von Gagern
Complete values for enum_switch of RegistryOptions.
79
	# find the option for which we want to complete a value
80
	curOpt=
81
	if [[ $cur != -* ]] && [[ $COMP_CWORD -gt 1 ]]; then
82
		curOpt=${COMP_WORDS[COMP_CWORD - 1]}
83
		if [[ $curOpt == = ]]; then
84
			curOpt=${COMP_WORDS[COMP_CWORD - 2]}
85
		fi
86
	fi
0.27.31 by Martin von Gagern
Introduce --debug switch to enable some debugging code.
87
%(debug)s
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
88
	cmdOpts=
0.27.30 by Martin von Gagern
Complete values for enum_switch of RegistryOptions.
89
	optEnums=
0.27.20 by Martin von Gagern
More flexible handling of fixed word lists.
90
	fixedWords=
0.27.19 by Martin von Gagern
Take global options preceding the command into account.
91
	case $cmd in
0.27.2 by Martin von Gagern
First programmatic generation of completions.
92
%(cases)s\
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
93
	*)
0.27.2 by Martin von Gagern
First programmatic generation of completions.
94
		cmdOpts='--help -h'
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
95
		;;
96
	esac
97
0.27.20 by Martin von Gagern
More flexible handling of fixed word lists.
98
	# if not typing an option, and if we don't know all the
99
	# possible non-option arguments for the current command,
100
	# then fallback on ordinary filename expansion
0.27.30 by Martin von Gagern
Complete values for enum_switch of RegistryOptions.
101
	if [[ -z $fixedWords ]] && [[ -z $optEnums ]] && [[ $cur != -* ]]; then
0.27.20 by Martin von Gagern
More flexible handling of fixed word lists.
102
		return 0
103
	fi
104
0.27.30 by Martin von Gagern
Complete values for enum_switch of RegistryOptions.
105
	if [[ $cur == = ]] && [[ -n $optEnums ]]; then
106
		# complete directly after "--option=", list all enum values
107
		COMPREPLY=( $optEnums )
108
	else
109
		fixedWords="$cmdOpts $globalOpts $optEnums $fixedWords"
110
		COMPREPLY=( $( compgen -W "$fixedWords" -- $cur ) )
111
	fi
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
112
113
	return 0
114
}
0.27.7 by Martin von Gagern
Introduce --function-only option
115
"""
116
tail="""\
0.27.4 by Martin von Gagern
Introduce --function-name switch. (LP #439829)
117
complete -F %(function_name)s -o default bzr
0.27.2 by Martin von Gagern
First programmatic generation of completions.
118
"""
0.27.31 by Martin von Gagern
Introduce --debug switch to enable some debugging code.
119
debug_output=r"""
120
	# Debugging code enabled using the --debug command line switch.
121
	# Will dump some variables to the top portion of the terminal.
122
	echo -ne '\e[s\e[H'
123
	for (( i=0; i < ${#COMP_WORDS[@]}; ++i)); do
124
		echo "\$COMP_WORDS[$i]='${COMP_WORDS[i]}'"$'\e[K'
125
	done
126
	for i in COMP_CWORD COMP_LINE COMP_POINT COMP_TYPE COMP_KEY cur curOpt; do
127
		echo "\$${i}=\"${!i}\""$'\e[K'
128
	done
129
	echo -ne '---\e[K\e[u'
130
"""
0.27.2 by Martin von Gagern
First programmatic generation of completions.
131
0.28.1 by Martin von Gagern
Include negated switches as well, using a modified options parser.
132
def wrap_container(list, parser):
133
    def tweaked_add_option(*opts, **attrs):
134
        list.extend(opts)
135
    parser.add_option = tweaked_add_option
136
    return parser
137
138
def wrap_parser(list, parser):
139
    orig_add_option_group = parser.add_option_group
140
    def tweaked_add_option_group(*opts, **attrs):
141
        return wrap_container(list, orig_add_option_group(*opts, **attrs))
142
    parser.add_option_group = tweaked_add_option_group
143
    return wrap_container(list, parser)
144
0.27.31 by Martin von Gagern
Introduce --debug switch to enable some debugging code.
145
def bash_completion_function(out, function_name="_bzr", function_only=False,
0.27.38 by Martin von Gagern
Add --plugin switch to selectively include plugins.
146
                             debug=False,
147
                             no_plugins=False, selected_plugins=None):
0.27.12 by Martin von Gagern
Take user-configured aliases into account.
148
    cmds = []
0.27.2 by Martin von Gagern
First programmatic generation of completions.
149
    cases = ""
150
    reqarg = {}
0.27.36 by Martin von Gagern
List versions of bzr and plugins.
151
    plugins = set()
0.27.38 by Martin von Gagern
Add --plugin switch to selectively include plugins.
152
    if selected_plugins:
153
        selected_plugins = set([x.replace('-', '_') for x in selected_plugins])
154
    else:
155
        selected_plugins = None
0.27.12 by Martin von Gagern
Take user-configured aliases into account.
156
0.27.18 by Martin von Gagern
Add global options in all cases
157
    re_switch = re.compile(r'\n(--[A-Za-z0-9-_]+)(?:, (-\S))?\s')
158
    help_text = help_topics.topic_registry.get_detail('global-options')
159
    global_options = set()
160
    for long, short in re_switch.findall(help_text):
161
        global_options.add(long)
162
        if short:
163
            global_options.add(short)
164
    global_options = " ".join(sorted(global_options))
165
0.27.12 by Martin von Gagern
Take user-configured aliases into account.
166
    user_aliases = {} # dict from cmd name to set of user-defined alias names
167
    for alias, expansion in config.GlobalConfig().get_aliases().iteritems():
168
        for token in commands.shlex_split_unicode(expansion):
169
            if not token.startswith("-"):
170
                user_aliases.setdefault(token, set()).add(alias)
171
                break
172
0.27.10 by Martin von Gagern
Complete help topics.
173
    all_cmds = sorted(commands.all_command_names())
174
    for cmdname in all_cmds:
175
        cmd = commands.get_cmd_object(cmdname)
0.27.12 by Martin von Gagern
Take user-configured aliases into account.
176
177
        # Find all aliases to the command; both cmd-defined and user-defined.
178
        # We assume a user won't override one command with a different one,
179
        # but will choose completely new names or add options to existing
180
        # ones while maintaining the actual command name unchanged.
181
        aliases = [cmdname]
182
        aliases.extend(cmd.aliases)
183
        aliases.extend(sorted([alias
184
                               for name in aliases
185
                               if name in user_aliases
186
                               for alias in user_aliases[name]
187
                               if alias not in aliases]))
188
        cases += "\t%s)\n" % "|".join(aliases)
189
        cmds.extend(aliases)
0.27.2 by Martin von Gagern
First programmatic generation of completions.
190
        plugin = cmd.plugin_name()
191
        if plugin is not None:
0.27.38 by Martin von Gagern
Add --plugin switch to selectively include plugins.
192
            if selected_plugins is not None and plugin not in selected_plugins:
193
                continue
0.27.36 by Martin von Gagern
List versions of bzr and plugins.
194
            plugins.add(plugin)
0.27.2 by Martin von Gagern
First programmatic generation of completions.
195
            cases += "\t\t# plugin \"%s\"\n" % plugin
196
        opts = cmd.options()
0.27.9 by Martin von Gagern
Remove suppression of alternate switches for the same option.
197
        switches = []
0.27.30 by Martin von Gagern
Complete values for enum_switch of RegistryOptions.
198
        enums = []
0.27.21 by Martin von Gagern
Allow help topic completion for aliases as well.
199
        fixedWords = None
0.27.2 by Martin von Gagern
First programmatic generation of completions.
200
        for optname in sorted(cmd.options()):
201
            opt = opts[optname]
0.28.1 by Martin von Gagern
Include negated switches as well, using a modified options parser.
202
            optswitches = []
203
            parser = option.get_optparser({optname: opt})
204
            parser = wrap_parser(optswitches, parser)
205
            optswitches[:] = []
206
            opt.add_option(parser, opt.short_name())
0.27.30 by Martin von Gagern
Complete values for enum_switch of RegistryOptions.
207
            if isinstance(opt, option.RegistryOption) and opt.enum_switch:
208
                enum_switch = '--%s' % optname
209
                keys = opt.registry.keys()
210
                if enum_switch in optswitches and keys:
211
                    optswitches.remove(enum_switch)
212
                    for key in keys:
213
                        optswitches.append('%s=%s' % (enum_switch, key))
214
                    enums.append("%s) optEnums='%s' ;;"
215
                                 % (enum_switch, ' '.join(keys)))
0.28.1 by Martin von Gagern
Include negated switches as well, using a modified options parser.
216
            switches.extend(optswitches)
0.27.10 by Martin von Gagern
Complete help topics.
217
        if 'help' == cmdname or 'help' in cmd.aliases:
0.27.21 by Martin von Gagern
Allow help topic completion for aliases as well.
218
            fixedWords = " ".join(sorted(help_topics.topic_registry.keys()))
219
            fixedWords = '"$cmds %s"' % fixedWords
220
0.27.9 by Martin von Gagern
Remove suppression of alternate switches for the same option.
221
        cases += "\t\tcmdOpts='" + " ".join(switches) + "'\n"
0.27.20 by Martin von Gagern
More flexible handling of fixed word lists.
222
        if fixedWords:
0.27.21 by Martin von Gagern
Allow help topic completion for aliases as well.
223
            if isinstance(fixedWords, list):
0.27.30 by Martin von Gagern
Complete values for enum_switch of RegistryOptions.
224
                fixedWords = "'" + join(fixedWords) + "'"
0.27.21 by Martin von Gagern
Allow help topic completion for aliases as well.
225
            cases += "\t\tfixedWords=" + fixedWords + "\n"
0.27.30 by Martin von Gagern
Complete values for enum_switch of RegistryOptions.
226
        if enums:
227
            cases += "\t\tcase $curOpt in\n\t\t\t"
228
            cases += "\n\t\t\t".join(enums)
229
            cases += "\n\t\tesac\n"
0.27.9 by Martin von Gagern
Remove suppression of alternate switches for the same option.
230
        cases += "\t\t;;\n"
0.27.36 by Martin von Gagern
List versions of bzr and plugins.
231
232
    bzr_version = bzrlib.version_string
233
    if not plugins:
234
        bzr_version += "."
235
    else:
236
        bzr_version += " and the following plugins:"
237
        for plugin in sorted(plugins):
238
            pv = bzrlib.plugin.plugins()[plugin].__version__
239
            if pv == 'unknown':
240
                pv = ''
241
            else:
242
                pv = ' ' + pv
243
                bzr_version += "\n# %s%s" % (plugin, pv)
244
0.27.7 by Martin von Gagern
Introduce --function-only option
245
    if function_only:
246
        template = fun
247
    else:
248
        template = head + fun + tail
0.27.12 by Martin von Gagern
Take user-configured aliases into account.
249
    out.write(template % {"cmds": " ".join(cmds),
0.27.2 by Martin von Gagern
First programmatic generation of completions.
250
                          "cases": cases,
0.27.4 by Martin von Gagern
Introduce --function-name switch. (LP #439829)
251
                          "function_name": function_name,
0.27.18 by Martin von Gagern
Add global options in all cases
252
                          "version": __version__,
253
                          "global_options": global_options,
0.27.31 by Martin von Gagern
Introduce --debug switch to enable some debugging code.
254
                          "debug": debug_output if debug else "",
0.27.36 by Martin von Gagern
List versions of bzr and plugins.
255
                          "bzr_version": bzr_version,
0.27.4 by Martin von Gagern
Introduce --function-name switch. (LP #439829)
256
                          })
0.27.2 by Martin von Gagern
First programmatic generation of completions.
257
258
if __name__ == '__main__':
259
260
    import sys
261
    import locale
0.27.32 by Martin von Gagern
Added OptionParser for script usage.
262
    import optparse
263
    from meta import __version__
264
0.27.38 by Martin von Gagern
Add --plugin switch to selectively include plugins.
265
    def plugin_callback(option, opt, value, parser):
266
        values = parser.values.selected_plugins
267
        if value == '-':
268
            del values[:]
269
        else:
270
            values.append(value)
271
0.27.32 by Martin von Gagern
Added OptionParser for script usage.
272
    parser = optparse.OptionParser(usage="%prog [-f NAME] [-o]",
273
                                   version="%%prog %s" % __version__)
274
    parser.add_option("--function-name", "-f", metavar="NAME",
275
                      help="Name of the generated function (default: _bzr)")
276
    parser.add_option("--function-only", "-o", action="store_true",
277
                      help="Generate only the shell function, don't enable it")
278
    parser.add_option("--debug", action="store_true",
279
                      help=optparse.SUPPRESS_HELP)
0.27.37 by Martin von Gagern
Introduce --no-plugins option to script execution mode.
280
    parser.add_option("--no-plugins", action="store_true",
281
                      help="Don't load any bzr plugins")
0.27.38 by Martin von Gagern
Add --plugin switch to selectively include plugins.
282
    parser.add_option("--plugin", metavar="NAME", type="string",
283
                      dest="selected_plugins", default=[],
284
                      action="callback", callback=plugin_callback,
285
                      help="Enable completions for the selected plugin"
286
                      + " (default: all plugins)")
0.27.32 by Martin von Gagern
Added OptionParser for script usage.
287
    (opts, args) = parser.parse_args()
288
    if args:
289
        parser.error("script does not take positional arguments")
290
    kwargs = dict()
291
    for name, value in opts.__dict__.iteritems():
292
        if value is not None:
293
            kwargs[name] = value
0.27.2 by Martin von Gagern
First programmatic generation of completions.
294
295
    locale.setlocale(locale.LC_ALL, '')
0.27.37 by Martin von Gagern
Introduce --no-plugins option to script execution mode.
296
    if not kwargs.get('no_plugins', False):
297
        plugin.load_plugins()
0.27.2 by Martin von Gagern
First programmatic generation of completions.
298
    commands.install_bzr_command_hooks()
0.27.32 by Martin von Gagern
Added OptionParser for script usage.
299
    bash_completion_function(sys.stdout, **kwargs)