~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shell.py

  • Committer: Aaron Bentley
  • Date: 2012-01-20 02:07:15 UTC
  • Revision ID: aaron@aaronbentley.com-20120120020715-ar6jbqnrjcuebggz
Tags: release-2.5
Update for 2.5 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
import cmd
19
19
from itertools import chain
20
20
import os
21
 
import readline
 
21
try:
 
22
    import readline
 
23
except ImportError:
 
24
    _has_readline = False
 
25
else:
 
26
    _has_readline = True
22
27
import shlex
23
28
import stat
24
29
import string
25
30
import sys
26
31
 
27
 
from bzrlib import osutils
 
32
from bzrlib import osutils, trace
28
33
from bzrlib.branch import Branch
29
34
from bzrlib.config import config_dir, ensure_config_dir_exists
30
 
from bzrlib.commands import get_cmd_object, get_all_cmds, get_alias
 
35
from bzrlib.commands import get_cmd_object, all_command_names, get_alias
31
36
from bzrlib.errors import BzrError
32
37
from bzrlib.workingtree import WorkingTree
33
38
 
99
104
        self.identchars += '-'
100
105
        ensure_config_dir_exists()
101
106
        self.history_file = osutils.pathjoin(config_dir(), 'shell-history')
102
 
        readline.set_completer_delims(string.whitespace)
103
 
        if os.access(self.history_file, os.R_OK) and \
104
 
            os.path.isfile(self.history_file):
105
 
            readline.read_history_file(self.history_file)
 
107
        whitespace = ''.join(c for c in string.whitespace if c < chr(127))
 
108
        if _has_readline:
 
109
            readline.set_completer_delims(whitespace)
 
110
            if os.access(self.history_file, os.R_OK) and \
 
111
                os.path.isfile(self.history_file):
 
112
                readline.read_history_file(self.history_file)
106
113
        self.cwd = os.getcwd()
107
114
 
108
115
    def write_history(self):
109
 
        readline.write_history_file(self.history_file)
 
116
        if _has_readline:
 
117
            readline.write_history_file(self.history_file)
110
118
 
111
119
    def do_quit(self, args):
112
120
        self.write_history()
168
176
        self.default("help "+line)
169
177
 
170
178
    def default(self, line):
171
 
        args = shlex.split(line)
 
179
        try:
 
180
            args = shlex.split(line)
 
181
        except ValueError, e:
 
182
            print 'Parse error:', e
 
183
            return
 
184
 
172
185
        alias_args = get_alias(args[0])
173
186
        if alias_args is not None:
174
187
            args[0] = alias_args.pop(0)
186
199
            return os.system(line)
187
200
 
188
201
        try:
189
 
            if too_complicated(line):
 
202
            is_qbzr = cmd_obj.__module__.startswith('bzrlib.plugins.qbzr.')
 
203
            if too_complicated(line) or is_qbzr:
190
204
                return os.system("bzr "+line)
191
205
            else:
192
206
                return (cmd_obj.run_argv_aliases(args, alias_args) or 0)
193
207
        except BzrError, e:
 
208
            trace.log_exception_quietly()
194
209
            print e
195
210
        except KeyboardInterrupt, e:
196
211
            print "Interrupted"
197
212
        except Exception, e:
198
 
#            print "Unhandled error:\n%s" % errors.exception_str(e)
 
213
            trace.log_exception_quietly()
199
214
            print "Unhandled error:\n%s" % (e)
200
215
 
201
216
 
220
235
        return CompletionContext(text, command=cmd).get_completions()
221
236
 
222
237
 
223
 
def run_shell():
 
238
def run_shell(directory=None):
224
239
    try:
 
240
        if not directory is None:
 
241
            os.chdir(directory)
225
242
        prompt = PromptCmd()
226
 
        try:
227
 
            prompt.cmdloop()
228
 
        finally:
229
 
            prompt.write_history()
 
243
        while True:
 
244
            try:
 
245
                try:
 
246
                    prompt.cmdloop()
 
247
                except KeyboardInterrupt:
 
248
                    print
 
249
            finally:
 
250
                prompt.write_history()
230
251
    except StopIteration:
231
252
        pass
232
253
 
280
301
 
281
302
 
282
303
def iter_command_names(hidden=False):
283
 
    for real_cmd_name, cmd_class in get_all_cmds():
284
 
        if not hidden and cmd_class.hidden:
 
304
    for real_cmd_name in all_command_names():
 
305
        cmd_obj = get_cmd_object(real_cmd_name)
 
306
        if not hidden and cmd_obj.hidden:
285
307
            continue
286
 
        for name in [real_cmd_name] + cmd_class.aliases:
 
308
        for name in [real_cmd_name] + cmd_obj.aliases:
287
309
            # Don't complete on aliases that are prefixes of the canonical name
288
310
            if name == real_cmd_name or not real_cmd_name.startswith(name):
289
311
                yield name