~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shell.py

  • Committer: Aaron Bentley
  • Date: 2011-04-15 02:33:36 UTC
  • mto: This revision was merged to the branch mainline in revision 761.
  • Revision ID: aaron@aaronbentley.com-20110415023336-o00049lbrfpmt1ae
Update compatibility-checking code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2004, 2005 Aaron Bentley
2
 
# <aaron.bentley@utoronto.ca>
 
2
# <aaron@aaronbentley.com>
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
24
24
import string
25
25
import sys
26
26
 
 
27
from bzrlib import osutils
27
28
from bzrlib.branch import Branch
28
 
from bzrlib.commands import get_cmd_object, get_all_cmds, get_alias
 
29
from bzrlib.config import config_dir, ensure_config_dir_exists
 
30
from bzrlib.commands import get_cmd_object, all_command_names, get_alias
29
31
from bzrlib.errors import BzrError
30
32
from bzrlib.workingtree import WorkingTree
31
33
 
80
82
            else:
81
83
                iter = iter_file_completions(self.text)
82
84
                completions.extend(filter_completions(iter, self.text))
83
 
            return completions 
 
85
            return completions
84
86
 
85
87
 
86
88
class PromptCmd(cmd.Cmd):
 
89
 
87
90
    def __init__(self):
88
91
        cmd.Cmd.__init__(self)
89
92
        self.prompt = "bzr> "
94
97
        self.set_title()
95
98
        self.set_prompt()
96
99
        self.identchars += '-'
97
 
        self.history_file = os.path.expanduser("~/.bazaar/shell-history")
98
 
        readline.set_completer_delims(string.whitespace)
 
100
        ensure_config_dir_exists()
 
101
        self.history_file = osutils.pathjoin(config_dir(), 'shell-history')
 
102
        whitespace = ''.join(c for c in string.whitespace if c < chr(127))
 
103
        readline.set_completer_delims(whitespace)
99
104
        if os.access(self.history_file, os.R_OK) and \
100
105
            os.path.isfile(self.history_file):
101
106
            readline.read_history_file(self.history_file)
122
127
    def set_prompt(self):
123
128
        if self.tree is not None:
124
129
            try:
125
 
                prompt_data = (self.tree.branch.nick, self.tree.branch.revno(), 
 
130
                prompt_data = (self.tree.branch.nick, self.tree.branch.revno(),
126
131
                               self.tree.relpath('.'))
127
132
                prompt = " %s:%d/%s" % prompt_data
128
133
            except:
164
169
        self.default("help "+line)
165
170
 
166
171
    def default(self, line):
167
 
        args = shlex.split(line)
 
172
        try:
 
173
            args = shlex.split(line)
 
174
        except ValueError, e:
 
175
            print 'Parse error:', e
 
176
            return
 
177
 
168
178
        alias_args = get_alias(args[0])
169
179
        if alias_args is not None:
170
180
            args[0] = alias_args.pop(0)
171
 
            
 
181
 
172
182
        commandname = args.pop(0)
173
183
        for char in ('|', '<', '>'):
174
184
            commandname = commandname.split(char)[0]
182
192
            return os.system(line)
183
193
 
184
194
        try:
185
 
            if too_complicated(line):
 
195
            is_qbzr = cmd_obj.__module__.startswith('bzrlib.plugins.qbzr.')
 
196
            if too_complicated(line) or is_qbzr:
186
197
                return os.system("bzr "+line)
187
198
            else:
188
199
                return (cmd_obj.run_argv_aliases(args, alias_args) or 0)
200
211
 
201
212
    def completedefault(self, text, line, begidx, endidx):
202
213
        """Perform completion for native commands.
203
 
        
 
214
 
204
215
        :param text: The text to complete
205
216
        :type text: str
206
217
        :param line: The entire line to complete
216
227
        return CompletionContext(text, command=cmd).get_completions()
217
228
 
218
229
 
219
 
def run_shell():
 
230
def run_shell(directory=None):
220
231
    try:
 
232
        if not directory is None:
 
233
            os.chdir(directory)
221
234
        prompt = PromptCmd()
222
 
        try:
223
 
            prompt.cmdloop()
224
 
        finally:
225
 
            prompt.write_history()
 
235
        while True:
 
236
            try:
 
237
                try:
 
238
                    prompt.cmdloop()
 
239
                except KeyboardInterrupt:
 
240
                    print
 
241
            finally:
 
242
                prompt.write_history()
226
243
    except StopIteration:
227
244
        pass
228
245
 
276
293
 
277
294
 
278
295
def iter_command_names(hidden=False):
279
 
    for real_cmd_name, cmd_class in get_all_cmds():
280
 
        if not hidden and cmd_class.hidden:
 
296
    for real_cmd_name in all_command_names():
 
297
        cmd_obj = get_cmd_object(real_cmd_name)
 
298
        if not hidden and cmd_obj.hidden:
281
299
            continue
282
 
        for name in [real_cmd_name] + cmd_class.aliases:
 
300
        for name in [real_cmd_name] + cmd_obj.aliases:
283
301
            # Don't complete on aliases that are prefixes of the canonical name
284
302
            if name == real_cmd_name or not real_cmd_name.startswith(name):
285
303
                yield name