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'])
29
class BlackListedCommand(BzrError):
30
def __init__(self, command):
31
BzrError.__init__(self, "The command %s is blacklisted for shell use" %
34
class PromptCmd(cmd.Cmd):
36
cmd.Cmd.__init__(self)
39
self.tree = arch.tree_root(".")
44
self.identchars += '-'
45
self.history_file = os.path.expanduser("~/.bazaar/shell-history")
46
readline.set_completer_delims(string.whitespace)
47
if os.access(self.history_file, os.R_OK) and \
48
os.path.isfile(self.history_file):
49
readline.read_history_file(self.history_file)
50
self.cwd = os.getcwd()
52
def write_history(self):
53
readline.write_history_file(self.history_file)
55
def do_quit(self, args):
59
def do_exit(self, args):
62
def do_EOF(self, args):
66
def postcmd(self, line, bar):
71
if self.tree is not None:
73
prompt = pylon.alias_or_version(self.tree.tree_version,
76
if prompt is not None:
77
prompt = " " + prompt +":"+ pylon.tree_cwd(self.tree)
82
self.prompt = "bzr%s> " % prompt
84
def set_title(self, command=None):
86
version = pylon.alias_or_version(self.tree.tree_version, self.tree,
89
version = "[no version]"
92
sys.stdout.write(terminal.term_title("bzr %s %s" % (command, version)))
94
def do_cd(self, line):
97
line = os.path.expanduser(line)
98
if os.path.isabs(line):
101
newcwd = self.cwd+'/'+line
102
newcwd = os.path.normpath(newcwd)
109
self.tree = arch.tree_root(".")
113
def do_help(self, line):
114
self.default("help "+line)
116
def default(self, line):
118
commandname = args.pop(0)
119
for char in ('|', '<', '>'):
120
commandname = commandname.split(char)[0]
121
if commandname[-1] in ('|', '<', '>'):
122
commandname = commandname[:-1]
124
if commandname in SHELL_BLACKLIST:
125
raise BlackListedCommand(commandname)
126
cmd_obj = get_cmd_object(commandname)
127
except (BlackListedCommand, BzrError):
128
return os.system(line)
131
if too_complicated(line):
132
return os.system("bzr "+line)
134
return (cmd_obj.run_argv(args) or 0)
137
except KeyboardInterrupt, e:
140
# print "Unhandled error:\n%s" % errors.exception_str(e)
141
print "Unhandled error:\n%s" % (e)
144
def completenames(self, text, line, begidx, endidx):
146
iter = iter_command_names()
149
arg = line.split()[-1]
152
iter = list(iter_munged_completions(iter, arg, text))
157
def completedefault(self, text, line, begidx, endidx):
158
"""Perform completion for native commands.
160
:param text: The text to complete
162
:param line: The entire line to complete
164
:param begidx: The start of the text in the line
166
:param endidx: The end of the text in the line
169
(cmd, args, foo) = self.parseline(line)
172
return self.completenames(text, line, begidx, endidx)
176
command_obj = get_cmd_object(cmd)
180
if command_obj is not None:
182
for option_name, option in command_obj.options().items():
183
opts.append("--" + option_name)
184
short_name = option.short_name()
186
opts.append("-" + short_name)
187
q = list(iter_munged_completions(opts, args, text))
188
return list(iter_munged_completions(opts, args, text))
191
arg = args.split()[-1]
194
iter = iter_dir_completions(arg)
195
iter = iter_munged_completions(iter, arg, text)
198
arg = args.split()[-1]
199
iter = iter_file_completions(arg)
200
return list(iter_munged_completions(iter, arg, text))
202
return self.completenames(text, line, begidx, endidx)
212
prompt.write_history()
213
except StopIteration:
216
def iter_file_completions(arg, only_dirs = False):
217
"""Generate an iterator that iterates through filename completions.
219
:param arg: The filename fragment to match
221
:param only_dirs: If true, match only directories
222
:type only_dirs: bool
229
(dir, file) = os.path.split(arg)
231
listingdir = os.path.expanduser(dir)
234
for file in chain(os.listdir(listingdir), extras):
236
userfile = dir+'/'+file
239
if userfile.startswith(arg):
240
if os.path.isdir(listingdir+'/'+file):
247
def iter_dir_completions(arg):
248
"""Generate an iterator that iterates through directory name completions.
250
:param arg: The directory name fragment to match
253
return iter_file_completions(arg, True)
255
def iter_command_names(hidden=False):
256
for real_cmd_name, cmd_class in get_all_cmds():
257
if not hidden and cmd_class.hidden:
259
for name in [real_cmd_name] + cmd_class.aliases:
260
# Don't complete on aliases that are prefixes of the canonical name
261
if name == real_cmd_name or not real_cmd_name.startswith(name):
264
def iter_munged_completions(iter, arg, text):
265
for completion in iter:
266
completion = str(completion)
267
if completion.startswith(arg):
268
yield completion[len(arg)-len(text):]+" "
270
def too_complicated(line):
271
for char in '|<>"\"':