1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
# Copyright (C) 2004, 2005 Aaron Bentley
# <aaron@aaronbentley.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import cmd
from itertools import chain
import os
import readline
import shlex
import stat
import string
import sys
from bzrlib import osutils, trace
from bzrlib.branch import Branch
from bzrlib.config import config_dir, ensure_config_dir_exists
from bzrlib.commands import get_cmd_object, all_command_names, get_alias
from bzrlib.errors import BzrError
from bzrlib.workingtree import WorkingTree
import terminal
SHELL_BLACKLIST = set(['rm', 'ls'])
COMPLETION_BLACKLIST = set(['shell'])
class BlackListedCommand(BzrError):
def __init__(self, command):
BzrError.__init__(self, "The command %s is blacklisted for shell use" %
command)
class CompletionContext(object):
def __init__(self, text, command=None, prev_opt=None, arg_pos=None):
self.text = text
self.command = command
self.prev_opt = prev_opt
self.arg_pos = None
def get_completions(self):
try:
return self.get_completions_or_raise()
except Exception, e:
print e, type(e)
return []
def get_option_completions(self):
try:
command_obj = get_cmd_object(self.command)
except BzrError:
return []
opts = [o+" " for o in iter_opt_completions(command_obj)]
return list(filter_completions(opts, self.text))
def get_completions_or_raise(self):
if self.command is None:
if '/' in self.text:
iter = iter_executables(self.text)
else:
iter = (c+" " for c in iter_command_names() if
c not in COMPLETION_BLACKLIST)
return list(filter_completions(iter, self.text))
if self.prev_opt is None:
completions = self.get_option_completions()
if self.command == "cd":
iter = iter_dir_completions(self.text)
completions.extend(list(filter_completions(iter, self.text)))
else:
iter = iter_file_completions(self.text)
completions.extend(filter_completions(iter, self.text))
return completions
class PromptCmd(cmd.Cmd):
def __init__(self):
cmd.Cmd.__init__(self)
self.prompt = "bzr> "
try:
self.tree = WorkingTree.open_containing('.')[0]
except:
self.tree = None
self.set_title()
self.set_prompt()
self.identchars += '-'
ensure_config_dir_exists()
self.history_file = osutils.pathjoin(config_dir(), 'shell-history')
whitespace = ''.join(c for c in string.whitespace if c < chr(127))
readline.set_completer_delims(whitespace)
if os.access(self.history_file, os.R_OK) and \
os.path.isfile(self.history_file):
readline.read_history_file(self.history_file)
self.cwd = os.getcwd()
def write_history(self):
readline.write_history_file(self.history_file)
def do_quit(self, args):
self.write_history()
raise StopIteration
def do_exit(self, args):
self.do_quit(args)
def do_EOF(self, args):
print
self.do_quit(args)
def postcmd(self, line, bar):
self.set_title()
self.set_prompt()
def set_prompt(self):
if self.tree is not None:
try:
prompt_data = (self.tree.branch.nick, self.tree.branch.revno(),
self.tree.relpath('.'))
prompt = " %s:%d/%s" % prompt_data
except:
prompt = ""
else:
prompt = ""
self.prompt = "bzr%s> " % prompt
def set_title(self, command=None):
try:
b = Branch.open_containing('.')[0]
version = "%s:%d" % (b.nick, b.revno())
except:
version = "[no version]"
if command is None:
command = ""
sys.stdout.write(terminal.term_title("bzr %s %s" % (command, version)))
def do_cd(self, line):
if line == "":
line = "~"
line = os.path.expanduser(line)
if os.path.isabs(line):
newcwd = line
else:
newcwd = self.cwd+'/'+line
newcwd = os.path.normpath(newcwd)
try:
os.chdir(newcwd)
self.cwd = newcwd
except Exception, e:
print e
try:
self.tree = WorkingTree.open_containing(".")[0]
except:
self.tree = None
def do_help(self, line):
self.default("help "+line)
def default(self, line):
try:
args = shlex.split(line)
except ValueError, e:
print 'Parse error:', e
return
alias_args = get_alias(args[0])
if alias_args is not None:
args[0] = alias_args.pop(0)
commandname = args.pop(0)
for char in ('|', '<', '>'):
commandname = commandname.split(char)[0]
if commandname[-1] in ('|', '<', '>'):
commandname = commandname[:-1]
try:
if commandname in SHELL_BLACKLIST:
raise BlackListedCommand(commandname)
cmd_obj = get_cmd_object(commandname)
except (BlackListedCommand, BzrError):
return os.system(line)
try:
is_qbzr = cmd_obj.__module__.startswith('bzrlib.plugins.qbzr.')
if too_complicated(line) or is_qbzr:
return os.system("bzr "+line)
else:
return (cmd_obj.run_argv_aliases(args, alias_args) or 0)
except BzrError, e:
trace.log_exception_quietly()
print e
except KeyboardInterrupt, e:
print "Interrupted"
except Exception, e:
trace.log_exception_quietly()
print "Unhandled error:\n%s" % (e)
def completenames(self, text, line, begidx, endidx):
return CompletionContext(text).get_completions()
def completedefault(self, text, line, begidx, endidx):
"""Perform completion for native commands.
:param text: The text to complete
:type text: str
:param line: The entire line to complete
:type line: str
:param begidx: The start of the text in the line
:type begidx: int
:param endidx: The end of the text in the line
:type endidx: int
"""
(cmd, args, foo) = self.parseline(line)
if cmd == "bzr":
cmd = None
return CompletionContext(text, command=cmd).get_completions()
def run_shell(directory=None):
try:
if not directory is None:
os.chdir(directory)
prompt = PromptCmd()
while True:
try:
try:
prompt.cmdloop()
except KeyboardInterrupt:
print
finally:
prompt.write_history()
except StopIteration:
pass
def iter_opt_completions(command_obj):
for option_name, option in command_obj.options().items():
yield "--" + option_name
short_name = option.short_name()
if short_name:
yield "-" + short_name
def iter_file_completions(arg, only_dirs = False):
"""Generate an iterator that iterates through filename completions.
:param arg: The filename fragment to match
:type arg: str
:param only_dirs: If true, match only directories
:type only_dirs: bool
"""
cwd = os.getcwd()
if cwd != "/":
extras = [".", ".."]
else:
extras = []
(dir, file) = os.path.split(arg)
if dir != "":
listingdir = os.path.expanduser(dir)
else:
listingdir = cwd
for file in chain(os.listdir(listingdir), extras):
if dir != "":
userfile = dir+'/'+file
else:
userfile = file
if userfile.startswith(arg):
if os.path.isdir(listingdir+'/'+file):
userfile+='/'
yield userfile
elif not only_dirs:
yield userfile + ' '
def iter_dir_completions(arg):
"""Generate an iterator that iterates through directory name completions.
:param arg: The directory name fragment to match
:type arg: str
"""
return iter_file_completions(arg, True)
def iter_command_names(hidden=False):
for real_cmd_name in all_command_names():
cmd_obj = get_cmd_object(real_cmd_name)
if not hidden and cmd_obj.hidden:
continue
for name in [real_cmd_name] + cmd_obj.aliases:
# Don't complete on aliases that are prefixes of the canonical name
if name == real_cmd_name or not real_cmd_name.startswith(name):
yield name
def iter_executables(path):
dirname, partial = os.path.split(path)
for filename in os.listdir(dirname):
if not filename.startswith(partial):
continue
fullpath = os.path.join(dirname, filename)
mode=os.lstat(fullpath)[stat.ST_MODE]
if stat.S_ISREG(mode) and 0111 & mode:
yield fullpath + ' '
def filter_completions(iter, arg):
return (c for c in iter if c.startswith(arg))
def iter_munged_completions(iter, arg, text):
for completion in iter:
completion = str(completion)
if completion.startswith(arg):
yield completion[len(arg)-len(text):]+" "
def too_complicated(line):
for char in '|<>*?':
if char in line:
return True
return False
|