~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shell.py

  • Committer: Alexander Belchenko
  • Date: 2006-07-18 20:37:53 UTC
  • mto: This revision was merged to the branch mainline in revision 421.
  • Revision ID: bialix@ukr.net-20060718203753-fa30c2f3cc59316b
don't use curses on win32

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2004, 2005 Aaron Bentley
2
 
# <aaron@aaronbentley.com>
 
2
# <aaron.bentley@utoronto.ca>
3
3
#
4
4
#    This program is free software; you can redistribute it and/or modify
5
5
#    it under the terms of the GNU General Public License as published by
18
18
import cmd
19
19
from itertools import chain
20
20
import os
21
 
try:
22
 
    import readline
23
 
except ImportError:
24
 
    _has_readline = False
25
 
else:
26
 
    _has_readline = True
 
21
import readline
27
22
import shlex
28
 
import stat
29
23
import string
30
24
import sys
31
25
 
32
 
from bzrlib import osutils, trace
33
26
from bzrlib.branch import Branch
34
 
from bzrlib.config import config_dir, ensure_config_dir_exists
35
 
from bzrlib.commands import get_cmd_object, all_command_names, get_alias
 
27
from bzrlib.commands import get_cmd_object, get_all_cmds, get_alias
36
28
from bzrlib.errors import BzrError
37
 
from bzrlib.workingtree import WorkingTree
38
29
 
39
30
import terminal
40
31
 
73
64
 
74
65
    def get_completions_or_raise(self):
75
66
        if self.command is None:
76
 
            if '/' in self.text:
77
 
                iter = iter_executables(self.text)
78
 
            else:
79
 
                iter = (c+" " for c in iter_command_names() if
80
 
                        c not in COMPLETION_BLACKLIST)
 
67
            iter = (c+" " for c in iter_command_names() if
 
68
                    c not in COMPLETION_BLACKLIST)
81
69
            return list(filter_completions(iter, self.text))
82
70
        if self.prev_opt is None:
83
71
            completions = self.get_option_completions()
87
75
            else:
88
76
                iter = iter_file_completions(self.text)
89
77
                completions.extend(filter_completions(iter, self.text))
90
 
            return completions
 
78
            return completions 
91
79
 
92
80
 
93
81
class PromptCmd(cmd.Cmd):
94
 
 
95
82
    def __init__(self):
96
83
        cmd.Cmd.__init__(self)
97
84
        self.prompt = "bzr> "
102
89
        self.set_title()
103
90
        self.set_prompt()
104
91
        self.identchars += '-'
105
 
        ensure_config_dir_exists()
106
 
        self.history_file = osutils.pathjoin(config_dir(), 'shell-history')
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)
 
92
        self.history_file = os.path.expanduser("~/.bazaar/shell-history")
 
93
        readline.set_completer_delims(string.whitespace)
 
94
        if os.access(self.history_file, os.R_OK) and \
 
95
            os.path.isfile(self.history_file):
 
96
            readline.read_history_file(self.history_file)
113
97
        self.cwd = os.getcwd()
114
98
 
115
99
    def write_history(self):
116
 
        if _has_readline:
117
 
            readline.write_history_file(self.history_file)
 
100
        readline.write_history_file(self.history_file)
118
101
 
119
102
    def do_quit(self, args):
120
103
        self.write_history()
134
117
    def set_prompt(self):
135
118
        if self.tree is not None:
136
119
            try:
137
 
                prompt_data = (self.tree.branch.nick, self.tree.branch.revno(),
138
 
                               self.tree.relpath('.'))
 
120
                prompt_data = (self.tree.branch.nick, self.tree.branch.revno(), 
 
121
                               self.tree.branch.relpath('.'))
139
122
                prompt = " %s:%d/%s" % prompt_data
140
123
            except:
141
124
                prompt = ""
176
159
        self.default("help "+line)
177
160
 
178
161
    def default(self, line):
179
 
        try:
180
 
            args = shlex.split(line)
181
 
        except ValueError, e:
182
 
            print 'Parse error:', e
183
 
            return
184
 
 
 
162
        args = shlex.split(line)
185
163
        alias_args = get_alias(args[0])
186
164
        if alias_args is not None:
187
165
            args[0] = alias_args.pop(0)
188
 
 
 
166
            
189
167
        commandname = args.pop(0)
190
168
        for char in ('|', '<', '>'):
191
169
            commandname = commandname.split(char)[0]
199
177
            return os.system(line)
200
178
 
201
179
        try:
202
 
            is_qbzr = cmd_obj.__module__.startswith('bzrlib.plugins.qbzr.')
203
 
            if too_complicated(line) or is_qbzr:
 
180
            if too_complicated(line):
204
181
                return os.system("bzr "+line)
205
182
            else:
206
183
                return (cmd_obj.run_argv_aliases(args, alias_args) or 0)
207
184
        except BzrError, e:
208
 
            trace.log_exception_quietly()
209
185
            print e
210
186
        except KeyboardInterrupt, e:
211
187
            print "Interrupted"
212
188
        except Exception, e:
213
 
            trace.log_exception_quietly()
 
189
#            print "Unhandled error:\n%s" % errors.exception_str(e)
214
190
            print "Unhandled error:\n%s" % (e)
215
191
 
216
192
 
219
195
 
220
196
    def completedefault(self, text, line, begidx, endidx):
221
197
        """Perform completion for native commands.
222
 
 
 
198
        
223
199
        :param text: The text to complete
224
200
        :type text: str
225
201
        :param line: The entire line to complete
235
211
        return CompletionContext(text, command=cmd).get_completions()
236
212
 
237
213
 
238
 
def run_shell(directory=None):
 
214
def run_shell():
239
215
    try:
240
 
        if not directory is None:
241
 
            os.chdir(directory)
242
216
        prompt = PromptCmd()
243
 
        while True:
244
 
            try:
245
 
                try:
246
 
                    prompt.cmdloop()
247
 
                except KeyboardInterrupt:
248
 
                    print
249
 
            finally:
250
 
                prompt.write_history()
 
217
        try:
 
218
            prompt.cmdloop()
 
219
        finally:
 
220
            prompt.write_history()
251
221
    except StopIteration:
252
222
        pass
253
223
 
301
271
 
302
272
 
303
273
def iter_command_names(hidden=False):
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:
 
274
    for real_cmd_name, cmd_class in get_all_cmds():
 
275
        if not hidden and cmd_class.hidden:
307
276
            continue
308
 
        for name in [real_cmd_name] + cmd_obj.aliases:
 
277
        for name in [real_cmd_name] + cmd_class.aliases:
309
278
            # Don't complete on aliases that are prefixes of the canonical name
310
279
            if name == real_cmd_name or not real_cmd_name.startswith(name):
311
280
                yield name
312
281
 
313
282
 
314
 
def iter_executables(path):
315
 
    dirname, partial = os.path.split(path)
316
 
    for filename in os.listdir(dirname):
317
 
        if not filename.startswith(partial):
318
 
            continue
319
 
        fullpath = os.path.join(dirname, filename)
320
 
        mode=os.lstat(fullpath)[stat.ST_MODE]
321
 
        if stat.S_ISREG(mode) and 0111 & mode:
322
 
            yield fullpath + ' '
323
 
 
324
 
 
325
283
def filter_completions(iter, arg):
326
284
    return (c for c in iter if c.startswith(arg))
327
285