~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shell.py

  • Committer: Aaron Bentley
  • Date: 2005-10-28 03:01:02 UTC
  • mfrom: (147.4.13)
  • mto: (147.4.24 trunk)
  • mto: This revision was merged to the branch mainline in revision 324.
  • Revision ID: aaron.bentley@utoronto.ca-20051028030102-eead1e484007de74
Merged lifeless

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
 
 
27
 
from bzrlib.branch import Branch
28
 
from bzrlib.commands import get_cmd_object, get_all_cmds, get_alias
 
23
from itertools import chain
29
24
from bzrlib.errors import BzrError
30
 
from bzrlib.workingtree import WorkingTree
31
 
 
32
 
import terminal
33
 
 
 
25
from bzrlib.commands import get_cmd_object, get_all_cmds
34
26
 
35
27
SHELL_BLACKLIST = set(['rm', 'ls'])
36
 
COMPLETION_BLACKLIST = set(['shell'])
37
 
 
38
28
 
39
29
class BlackListedCommand(BzrError):
40
30
    def __init__(self, command):
41
31
        BzrError.__init__(self, "The command %s is blacklisted for shell use" %
42
32
                          command)
43
33
 
44
 
 
45
 
class CompletionContext(object):
46
 
    def __init__(self, text, command=None, prev_opt=None, arg_pos=None):
47
 
        self.text = text
48
 
        self.command = command
49
 
        self.prev_opt = prev_opt
50
 
        self.arg_pos = None
51
 
 
52
 
    def get_completions(self):
53
 
        try:
54
 
            return self.get_completions_or_raise()
55
 
        except Exception, e:
56
 
            print e, type(e)
57
 
            return []
58
 
 
59
 
    def get_option_completions(self):
60
 
        try:
61
 
            command_obj = get_cmd_object(self.command)
62
 
        except BzrError:
63
 
            return []
64
 
        opts = [o+" " for o in iter_opt_completions(command_obj)]
65
 
        return list(filter_completions(opts, self.text))
66
 
 
67
 
    def get_completions_or_raise(self):
68
 
        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)
74
 
            return list(filter_completions(iter, self.text))
75
 
        if self.prev_opt is None:
76
 
            completions = self.get_option_completions()
77
 
            if self.command == "cd":
78
 
                iter = iter_dir_completions(self.text)
79
 
                completions.extend(list(filter_completions(iter, self.text)))
80
 
            else:
81
 
                iter = iter_file_completions(self.text)
82
 
                completions.extend(filter_completions(iter, self.text))
83
 
            return completions 
84
 
 
85
 
 
86
34
class PromptCmd(cmd.Cmd):
87
35
    def __init__(self):
88
36
        cmd.Cmd.__init__(self)
89
37
        self.prompt = "bzr> "
90
38
        try:
91
 
            self.tree = WorkingTree.open_containing('.')[0]
 
39
            self.tree = arch.tree_root(".")
92
40
        except:
93
41
            self.tree = None
94
42
        self.set_title()
122
70
    def set_prompt(self):
123
71
        if self.tree is not None:
124
72
            try:
125
 
                prompt_data = (self.tree.branch.nick, self.tree.branch.revno(), 
126
 
                               self.tree.relpath('.'))
127
 
                prompt = " %s:%d/%s" % prompt_data
 
73
                prompt = pylon.alias_or_version(self.tree.tree_version, 
 
74
                                                self.tree, 
 
75
                                                full=False)
 
76
                if prompt is not None:
 
77
                    prompt = " " + prompt +":"+ pylon.tree_cwd(self.tree)
128
78
            except:
129
79
                prompt = ""
130
80
        else:
133
83
 
134
84
    def set_title(self, command=None):
135
85
        try:
136
 
            b = Branch.open_containing('.')[0]
137
 
            version = "%s:%d" % (b.nick, b.revno())
 
86
            version = pylon.alias_or_version(self.tree.tree_version, self.tree, 
 
87
                                             full=False)
138
88
        except:
139
89
            version = "[no version]"
140
90
        if command is None:
156
106
        except Exception, e:
157
107
            print e
158
108
        try:
159
 
            self.tree = WorkingTree.open_containing(".")[0]
 
109
            self.tree = arch.tree_root(".")
160
110
        except:
161
111
            self.tree = None
162
112
 
164
114
        self.default("help "+line)
165
115
 
166
116
    def default(self, line):
167
 
        args = shlex.split(line)
168
 
        alias_args = get_alias(args[0])
169
 
        if alias_args is not None:
170
 
            args[0] = alias_args.pop(0)
171
 
            
 
117
        args = line.split()
172
118
        commandname = args.pop(0)
173
119
        for char in ('|', '<', '>'):
174
120
            commandname = commandname.split(char)[0]
185
131
            if too_complicated(line):
186
132
                return os.system("bzr "+line)
187
133
            else:
188
 
                return (cmd_obj.run_argv_aliases(args, alias_args) or 0)
 
134
                return (cmd_obj.run_argv(args) or 0)
189
135
        except BzrError, e:
190
136
            print e
191
137
        except KeyboardInterrupt, e:
196
142
 
197
143
 
198
144
    def completenames(self, text, line, begidx, endidx):
199
 
        return CompletionContext(text).get_completions()
 
145
        completions = []
 
146
        iter = iter_command_names()
 
147
        try:
 
148
            if len(line) > 0:
 
149
                arg = line.split()[-1]
 
150
            else:
 
151
                arg = ""
 
152
            iter = list(iter_munged_completions(iter, arg, text))
 
153
        except Exception, e:
 
154
            print e, type(e)
 
155
        return list(iter)
200
156
 
201
157
    def completedefault(self, text, line, begidx, endidx):
202
158
        """Perform completion for native commands.
212
168
        """
213
169
        (cmd, args, foo) = self.parseline(line)
214
170
        if cmd == "bzr":
215
 
            cmd = None
216
 
        return CompletionContext(text, command=cmd).get_completions()
217
 
 
 
171
            try:
 
172
                return self.completenames(text, line, begidx, endidx)
 
173
            except Exception, e:
 
174
                print e
 
175
        try:
 
176
            command_obj = get_cmd_object(cmd)
 
177
        except BzrError:
 
178
            command_obj = None
 
179
        try:
 
180
            if command_obj is not None:
 
181
                opts = []
 
182
                for option_name, option in command_obj.options().items():
 
183
                    opts.append("--" + option_name)
 
184
                    short_name = option.short_name()
 
185
                    if short_name:
 
186
                        opts.append("-" + short_name)
 
187
                q = list(iter_munged_completions(opts, args, text))
 
188
                return list(iter_munged_completions(opts, args, text))
 
189
            elif cmd == "cd":
 
190
                if len(args) > 0:
 
191
                    arg = args.split()[-1]
 
192
                else:
 
193
                    arg = ""
 
194
                iter = iter_dir_completions(arg)
 
195
                iter = iter_munged_completions(iter, arg, text)
 
196
                return list(iter)
 
197
            elif len(args)>0:
 
198
                arg = args.split()[-1]
 
199
                iter = iter_file_completions(arg)
 
200
                return list(iter_munged_completions(iter, arg, text))
 
201
            else:
 
202
                return self.completenames(text, line, begidx, endidx)
 
203
        except Exception, e:
 
204
            print e
218
205
 
219
206
def run_shell():
220
207
    try:
226
213
    except StopIteration:
227
214
        pass
228
215
 
229
 
 
230
 
def iter_opt_completions(command_obj):
231
 
    for option_name, option in command_obj.options().items():
232
 
        yield "--" + option_name
233
 
        short_name = option.short_name()
234
 
        if short_name:
235
 
            yield "-" + short_name
236
 
 
237
 
 
238
216
def iter_file_completions(arg, only_dirs = False):
239
217
    """Generate an iterator that iterates through filename completions.
240
218
 
263
241
                userfile+='/'
264
242
                yield userfile
265
243
            elif not only_dirs:
266
 
                yield userfile + ' '
 
244
                yield userfile
267
245
 
268
246
 
269
247
def iter_dir_completions(arg):
274
252
    """
275
253
    return iter_file_completions(arg, True)
276
254
 
277
 
 
278
255
def iter_command_names(hidden=False):
279
256
    for real_cmd_name, cmd_class in get_all_cmds():
280
257
        if not hidden and cmd_class.hidden:
284
261
            if name == real_cmd_name or not real_cmd_name.startswith(name):
285
262
                yield name
286
263
 
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
 
def filter_completions(iter, arg):
300
 
    return (c for c in iter if c.startswith(arg))
301
 
 
302
 
 
303
264
def iter_munged_completions(iter, arg, text):
304
265
    for completion in iter:
305
266
        completion = str(completion)
306
267
        if completion.startswith(arg):
307
268
            yield completion[len(arg)-len(text):]+" "
308
269
 
309
 
 
310
270
def too_complicated(line):
311
 
    for char in '|<>*?':
 
271
    for char in '|<>"\"':
312
272
        if char in line:
313
273
            return True
314
274
    return False