/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.18 by Martin von Gagern
Add global options in all cases
28
import re
0.27.2 by Martin von Gagern
First programmatic generation of completions.
29
0.27.7 by Martin von Gagern
Introduce --function-only option
30
head="""\
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
31
# Programmable completion for the Bazaar-NG bzr command under bash. Source
32
# this file (or on some systems add it to ~/.bash_completion and start a new
33
# shell) and bash's completion mechanism will know all about bzr's options!
34
35
# Known to work with bash 2.05a with programmable completion and extended
36
# pattern matching enabled (use 'shopt -s extglob progcomp' to enable
37
# these if they are not already enabled).
38
39
# Based originally on the svn bash completition script.
40
# Customized by Sven Wilhelm/Icecrash.com
0.27.2 by Martin von Gagern
First programmatic generation of completions.
41
# Adjusted for automatic generation by Martin von Gagern
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
42
0.27.17 by Martin von Gagern
Mention version and homepage in generated shell code comment.
43
# Generated using the bzr-bash-completion plugin version %(version)s.
44
# See https://launchpad.net/bzr-bash-completion for details.
45
0.27.20 by Martin von Gagern
More flexible handling of fixed word lists.
46
shopt -s progcomp
0.27.7 by Martin von Gagern
Introduce --function-only option
47
"""
48
fun="""\
0.27.4 by Martin von Gagern
Introduce --function-name switch. (LP #439829)
49
%(function_name)s ()
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
50
{
0.27.20 by Martin von Gagern
More flexible handling of fixed word lists.
51
	local cur cmds cmdIdx cmd cmdOpts fixedWords i globalOpts
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
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
79
	cmdOpts=
0.27.20 by Martin von Gagern
More flexible handling of fixed word lists.
80
	fixedWords=
0.27.19 by Martin von Gagern
Take global options preceding the command into account.
81
	case $cmd in
0.27.2 by Martin von Gagern
First programmatic generation of completions.
82
%(cases)s\
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
83
	*)
0.27.2 by Martin von Gagern
First programmatic generation of completions.
84
		cmdOpts='--help -h'
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
85
		;;
86
	esac
87
0.27.20 by Martin von Gagern
More flexible handling of fixed word lists.
88
	# if not typing an option, and if we don't know all the
89
	# possible non-option arguments for the current command,
90
	# then fallback on ordinary filename expansion
91
	if [[ -z $fixedWords ]] && [[ $cur != -* ]] ; then
92
		return 0
93
	fi
94
95
	COMPREPLY=( $( compgen -W "$cmdOpts $globalOpts $fixedWords" -- $cur ) )
0.27.1 by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr
96
97
	return 0
98
}
0.27.7 by Martin von Gagern
Introduce --function-only option
99
"""
100
tail="""\
0.27.4 by Martin von Gagern
Introduce --function-name switch. (LP #439829)
101
complete -F %(function_name)s -o default bzr
0.27.2 by Martin von Gagern
First programmatic generation of completions.
102
"""
103
0.28.1 by Martin von Gagern
Include negated switches as well, using a modified options parser.
104
def wrap_container(list, parser):
105
    def tweaked_add_option(*opts, **attrs):
106
        list.extend(opts)
107
    parser.add_option = tweaked_add_option
108
    return parser
109
110
def wrap_parser(list, parser):
111
    orig_add_option_group = parser.add_option_group
112
    def tweaked_add_option_group(*opts, **attrs):
113
        return wrap_container(list, orig_add_option_group(*opts, **attrs))
114
    parser.add_option_group = tweaked_add_option_group
115
    return wrap_container(list, parser)
116
0.27.7 by Martin von Gagern
Introduce --function-only option
117
def bash_completion_function(out, function_name="_bzr", function_only=False):
0.27.12 by Martin von Gagern
Take user-configured aliases into account.
118
    cmds = []
0.27.2 by Martin von Gagern
First programmatic generation of completions.
119
    cases = ""
120
    reqarg = {}
0.27.12 by Martin von Gagern
Take user-configured aliases into account.
121
0.27.18 by Martin von Gagern
Add global options in all cases
122
    re_switch = re.compile(r'\n(--[A-Za-z0-9-_]+)(?:, (-\S))?\s')
123
    help_text = help_topics.topic_registry.get_detail('global-options')
124
    global_options = set()
125
    for long, short in re_switch.findall(help_text):
126
        global_options.add(long)
127
        if short:
128
            global_options.add(short)
129
    global_options = " ".join(sorted(global_options))
130
0.27.12 by Martin von Gagern
Take user-configured aliases into account.
131
    user_aliases = {} # dict from cmd name to set of user-defined alias names
132
    for alias, expansion in config.GlobalConfig().get_aliases().iteritems():
133
        for token in commands.shlex_split_unicode(expansion):
134
            if not token.startswith("-"):
135
                user_aliases.setdefault(token, set()).add(alias)
136
                break
137
0.27.10 by Martin von Gagern
Complete help topics.
138
    all_cmds = sorted(commands.all_command_names())
139
    for cmdname in all_cmds:
140
        cmd = commands.get_cmd_object(cmdname)
0.27.12 by Martin von Gagern
Take user-configured aliases into account.
141
142
        # Find all aliases to the command; both cmd-defined and user-defined.
143
        # We assume a user won't override one command with a different one,
144
        # but will choose completely new names or add options to existing
145
        # ones while maintaining the actual command name unchanged.
146
        aliases = [cmdname]
147
        aliases.extend(cmd.aliases)
148
        aliases.extend(sorted([alias
149
                               for name in aliases
150
                               if name in user_aliases
151
                               for alias in user_aliases[name]
152
                               if alias not in aliases]))
153
        cases += "\t%s)\n" % "|".join(aliases)
154
        cmds.extend(aliases)
0.27.2 by Martin von Gagern
First programmatic generation of completions.
155
        plugin = cmd.plugin_name()
156
        if plugin is not None:
157
            cases += "\t\t# plugin \"%s\"\n" % plugin
158
        opts = cmd.options()
0.27.9 by Martin von Gagern
Remove suppression of alternate switches for the same option.
159
        switches = []
0.27.21 by Martin von Gagern
Allow help topic completion for aliases as well.
160
        fixedWords = None
0.27.2 by Martin von Gagern
First programmatic generation of completions.
161
        for optname in sorted(cmd.options()):
162
            opt = opts[optname]
0.28.1 by Martin von Gagern
Include negated switches as well, using a modified options parser.
163
            optswitches = []
164
            parser = option.get_optparser({optname: opt})
165
            parser = wrap_parser(optswitches, parser)
166
            optswitches[:] = []
167
            opt.add_option(parser, opt.short_name())
168
            switches.extend(optswitches)
0.27.10 by Martin von Gagern
Complete help topics.
169
        if 'help' == cmdname or 'help' in cmd.aliases:
0.27.21 by Martin von Gagern
Allow help topic completion for aliases as well.
170
            fixedWords = " ".join(sorted(help_topics.topic_registry.keys()))
171
            fixedWords = '"$cmds %s"' % fixedWords
172
0.27.9 by Martin von Gagern
Remove suppression of alternate switches for the same option.
173
        cases += "\t\tcmdOpts='" + " ".join(switches) + "'\n"
0.27.20 by Martin von Gagern
More flexible handling of fixed word lists.
174
        if fixedWords:
0.27.21 by Martin von Gagern
Allow help topic completion for aliases as well.
175
            if isinstance(fixedWords, list):
176
                fixedWords = "'" + join(fixedWords) + "'";
177
            cases += "\t\tfixedWords=" + fixedWords + "\n"
0.27.9 by Martin von Gagern
Remove suppression of alternate switches for the same option.
178
        cases += "\t\t;;\n"
0.27.7 by Martin von Gagern
Introduce --function-only option
179
    if function_only:
180
        template = fun
181
    else:
182
        template = head + fun + tail
0.27.12 by Martin von Gagern
Take user-configured aliases into account.
183
    out.write(template % {"cmds": " ".join(cmds),
0.27.2 by Martin von Gagern
First programmatic generation of completions.
184
                          "cases": cases,
0.27.4 by Martin von Gagern
Introduce --function-name switch. (LP #439829)
185
                          "function_name": function_name,
0.27.18 by Martin von Gagern
Add global options in all cases
186
                          "version": __version__,
187
                          "global_options": global_options,
0.27.4 by Martin von Gagern
Introduce --function-name switch. (LP #439829)
188
                          })
0.27.2 by Martin von Gagern
First programmatic generation of completions.
189
190
if __name__ == '__main__':
191
192
    import sys
193
    import locale
194
195
    locale.setlocale(locale.LC_ALL, '')
196
    plugin.load_plugins()
197
    commands.install_bzr_command_hooks()
198
    bash_completion_function(sys.stdout)