~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shell.py

  • Committer: Robert Collins
  • Date: 2006-04-24 01:35:08 UTC
  • mto: (364.1.3 bzrtools)
  • mto: This revision was merged to the branch mainline in revision 366.
  • Revision ID: robertc@robertcollins.net-20060424013508-2d37e47814d9493e
Support importing into empty branches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
#    You should have received a copy of the GNU General Public License
15
15
#    along with this program; if not, write to the Free Software
16
16
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 
 
18
17
import cmd
19
 
from itertools import chain
 
18
import sys
20
19
import os
 
20
import terminal
21
21
import readline
22
 
import shlex
23
 
import stat
24
22
import string
25
 
import sys
26
 
 
 
23
from itertools import chain
 
24
from bzrlib.errors import BzrError
 
25
from bzrlib.commands import get_cmd_object, get_all_cmds, get_alias
27
26
from bzrlib.branch import Branch
28
 
from bzrlib.commands import get_cmd_object, get_all_cmds, get_alias
29
 
from bzrlib.errors import BzrError
30
 
from bzrlib.workingtree import WorkingTree
31
 
 
32
 
import terminal
33
 
 
34
27
 
35
28
SHELL_BLACKLIST = set(['rm', 'ls'])
36
29
COMPLETION_BLACKLIST = set(['shell'])
37
30
 
38
 
 
39
31
class BlackListedCommand(BzrError):
40
32
    def __init__(self, command):
41
33
        BzrError.__init__(self, "The command %s is blacklisted for shell use" %
42
34
                          command)
43
35
 
44
 
 
45
36
class CompletionContext(object):
46
37
    def __init__(self, text, command=None, prev_opt=None, arg_pos=None):
47
38
        self.text = text
66
57
 
67
58
    def get_completions_or_raise(self):
68
59
        if self.command is None:
69
 
            if '/' in self.text:
70
 
                iter = iter_executables(self.text)
71
 
            else:
72
 
                iter = (c+" " for c in iter_command_names() if
73
 
                        c not in COMPLETION_BLACKLIST)
 
60
            iter = (c+" " for c in iter_command_names() if
 
61
                    c not in COMPLETION_BLACKLIST)
74
62
            return list(filter_completions(iter, self.text))
75
63
        if self.prev_opt is None:
76
64
            completions = self.get_option_completions()
79
67
                completions.extend(list(filter_completions(iter, self.text)))
80
68
            else:
81
69
                iter = iter_file_completions(self.text)
82
 
                completions.extend(filter_completions(iter, self.text))
83
 
            return completions
 
70
                completions.extend([f+" " for f in 
 
71
                                    filter_completions(iter, self.text)])
 
72
            return completions 
84
73
 
85
74
 
86
75
class PromptCmd(cmd.Cmd):
122
111
    def set_prompt(self):
123
112
        if self.tree is not None:
124
113
            try:
125
 
                prompt_data = (self.tree.branch.nick, self.tree.branch.revno(),
126
 
                               self.tree.relpath('.'))
 
114
                prompt_data = (self.tree.branch.nick, self.tree.branch.revno(), 
 
115
                               self.tree.branch.relpath('.'))
127
116
                prompt = " %s:%d/%s" % prompt_data
128
117
            except:
129
118
                prompt = ""
164
153
        self.default("help "+line)
165
154
 
166
155
    def default(self, line):
167
 
        args = shlex.split(line)
 
156
        args = line.split()
168
157
        alias_args = get_alias(args[0])
169
158
        if alias_args is not None:
170
159
            args[0] = alias_args.pop(0)
171
 
 
 
160
            
172
161
        commandname = args.pop(0)
173
162
        for char in ('|', '<', '>'):
174
163
            commandname = commandname.split(char)[0]
200
189
 
201
190
    def completedefault(self, text, line, begidx, endidx):
202
191
        """Perform completion for native commands.
203
 
 
 
192
        
204
193
        :param text: The text to complete
205
194
        :type text: str
206
195
        :param line: The entire line to complete
215
204
            cmd = None
216
205
        return CompletionContext(text, command=cmd).get_completions()
217
206
 
218
 
 
219
207
def run_shell():
220
208
    try:
221
209
        prompt = PromptCmd()
226
214
    except StopIteration:
227
215
        pass
228
216
 
229
 
 
230
217
def iter_opt_completions(command_obj):
231
218
    for option_name, option in command_obj.options().items():
232
219
        yield "--" + option_name
234
221
        if short_name:
235
222
            yield "-" + short_name
236
223
 
237
 
 
238
224
def iter_file_completions(arg, only_dirs = False):
239
225
    """Generate an iterator that iterates through filename completions.
240
226
 
263
249
                userfile+='/'
264
250
                yield userfile
265
251
            elif not only_dirs:
266
 
                yield userfile + ' '
 
252
                yield userfile
267
253
 
268
254
 
269
255
def iter_dir_completions(arg):
274
260
    """
275
261
    return iter_file_completions(arg, True)
276
262
 
277
 
 
278
263
def iter_command_names(hidden=False):
279
264
    for real_cmd_name, cmd_class in get_all_cmds():
280
265
        if not hidden and cmd_class.hidden:
284
269
            if name == real_cmd_name or not real_cmd_name.startswith(name):
285
270
                yield name
286
271
 
287
 
 
288
 
def iter_executables(path):
289
 
    dirname, partial = os.path.split(path)
290
 
    for filename in os.listdir(dirname):
291
 
        if not filename.startswith(partial):
292
 
            continue
293
 
        fullpath = os.path.join(dirname, filename)
294
 
        mode=os.lstat(fullpath)[stat.ST_MODE]
295
 
        if stat.S_ISREG(mode) and 0111 & mode:
296
 
            yield fullpath + ' '
297
 
 
298
 
 
299
272
def filter_completions(iter, arg):
300
273
    return (c for c in iter if c.startswith(arg))
301
274
 
302
 
 
303
275
def iter_munged_completions(iter, arg, text):
304
276
    for completion in iter:
305
277
        completion = str(completion)
306
278
        if completion.startswith(arg):
307
279
            yield completion[len(arg)-len(text):]+" "
308
280
 
309
 
 
310
281
def too_complicated(line):
311
 
    for char in '|<>*?':
 
282
    for char in '|<>"\"*?':
312
283
        if char in line:
313
284
            return True
314
285
    return False