1
# Copyright (C) 2004, 2005 Aaron Bentley
2
# <aaron.bentley@utoronto.ca>
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
from itertools import chain
24
from bzrlib.errors import BzrError
25
from bzrlib.commands import get_cmd_object, get_all_cmds
27
SHELL_BLACKLIST = set(['rm', 'ls'])
28
COMPLETION_BLACKLIST = set(['shell'])
30
class BlackListedCommand(BzrError):
31
def __init__(self, command):
32
BzrError.__init__(self, "The command %s is blacklisted for shell use" %
35
class CompletionContext(object):
36
def __init__(self, text, command=None, prev_opt=None, arg_pos=None):
38
self.command = command
39
self.prev_opt = prev_opt
42
def get_completions(self):
44
iter = (c for c in iter_command_names() if
45
c not in COMPLETION_BLACKLIST)
48
arg = line.split()[-1]
51
iter = list(iter_munged_completions(iter, arg, text))
57
class PromptCmd(cmd.Cmd):
59
cmd.Cmd.__init__(self)
62
self.tree = arch.tree_root(".")
67
self.identchars += '-'
68
self.history_file = os.path.expanduser("~/.bazaar/shell-history")
69
readline.set_completer_delims(string.whitespace)
70
if os.access(self.history_file, os.R_OK) and \
71
os.path.isfile(self.history_file):
72
readline.read_history_file(self.history_file)
73
self.cwd = os.getcwd()
75
def write_history(self):
76
readline.write_history_file(self.history_file)
78
def do_quit(self, args):
82
def do_exit(self, args):
85
def do_EOF(self, args):
89
def postcmd(self, line, bar):
94
if self.tree is not None:
96
prompt = pylon.alias_or_version(self.tree.tree_version,
99
if prompt is not None:
100
prompt = " " + prompt +":"+ pylon.tree_cwd(self.tree)
105
self.prompt = "bzr%s> " % prompt
107
def set_title(self, command=None):
109
version = pylon.alias_or_version(self.tree.tree_version, self.tree,
112
version = "[no version]"
115
sys.stdout.write(terminal.term_title("bzr %s %s" % (command, version)))
117
def do_cd(self, line):
120
line = os.path.expanduser(line)
121
if os.path.isabs(line):
124
newcwd = self.cwd+'/'+line
125
newcwd = os.path.normpath(newcwd)
132
self.tree = arch.tree_root(".")
136
def do_help(self, line):
137
self.default("help "+line)
139
def default(self, line):
141
commandname = args.pop(0)
142
for char in ('|', '<', '>'):
143
commandname = commandname.split(char)[0]
144
if commandname[-1] in ('|', '<', '>'):
145
commandname = commandname[:-1]
147
if commandname in SHELL_BLACKLIST:
148
raise BlackListedCommand(commandname)
149
cmd_obj = get_cmd_object(commandname)
150
except (BlackListedCommand, BzrError):
151
return os.system(line)
154
if too_complicated(line):
155
return os.system("bzr "+line)
157
return (cmd_obj.run_argv(args) or 0)
160
except KeyboardInterrupt, e:
163
# print "Unhandled error:\n%s" % errors.exception_str(e)
164
print "Unhandled error:\n%s" % (e)
167
def completenames(self, text, line, begidx, endidx):
169
iter = (c for c in iter_command_names() if
170
c not in COMPLETION_BLACKLIST)
173
arg = line.split()[-1]
176
iter = list(iter_munged_completions(iter, arg, text))
181
def completedefault(self, text, line, begidx, endidx):
182
"""Perform completion for native commands.
184
:param text: The text to complete
186
:param line: The entire line to complete
188
:param begidx: The start of the text in the line
190
:param endidx: The end of the text in the line
193
(cmd, args, foo) = self.parseline(line)
196
return self.completenames(text, line, begidx, endidx)
200
command_obj = get_cmd_object(cmd)
204
if command_obj is not None:
206
for option_name, option in command_obj.options().items():
207
opts.append("--" + option_name)
208
short_name = option.short_name()
210
opts.append("-" + short_name)
211
q = list(iter_munged_completions(opts, args, text))
212
return list(iter_munged_completions(opts, args, text))
215
arg = args.split()[-1]
218
iter = iter_dir_completions(arg)
219
iter = iter_munged_completions(iter, arg, text)
222
arg = args.split()[-1]
223
iter = iter_file_completions(arg)
224
return list(iter_munged_completions(iter, arg, text))
226
return self.completenames(text, line, begidx, endidx)
236
prompt.write_history()
237
except StopIteration:
240
def iter_file_completions(arg, only_dirs = False):
241
"""Generate an iterator that iterates through filename completions.
243
:param arg: The filename fragment to match
245
:param only_dirs: If true, match only directories
246
:type only_dirs: bool
253
(dir, file) = os.path.split(arg)
255
listingdir = os.path.expanduser(dir)
258
for file in chain(os.listdir(listingdir), extras):
260
userfile = dir+'/'+file
263
if userfile.startswith(arg):
264
if os.path.isdir(listingdir+'/'+file):
271
def iter_dir_completions(arg):
272
"""Generate an iterator that iterates through directory name completions.
274
:param arg: The directory name fragment to match
277
return iter_file_completions(arg, True)
279
def iter_command_names(hidden=False):
280
for real_cmd_name, cmd_class in get_all_cmds():
281
if not hidden and cmd_class.hidden:
283
for name in [real_cmd_name] + cmd_class.aliases:
284
# Don't complete on aliases that are prefixes of the canonical name
285
if name == real_cmd_name or not real_cmd_name.startswith(name):
288
def iter_munged_completions(iter, arg, text):
289
for completion in iter:
290
completion = str(completion)
291
if completion.startswith(arg):
292
yield completion[len(arg)-len(text):]+" "
294
def too_complicated(line):
295
for char in '|<>"\"*?':