~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shell.py

  • Committer: Aaron Bentley
  • Date: 2005-10-27 14:26:09 UTC
  • Revision ID: abentley@panoramicfeedback.com-20051027142609-c6fdf66ac5b86dc4
Prevented bzr's rm and ls from being invoked in the shell

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()
106
54
 
107
55
    def do_quit(self, args):
108
56
        self.write_history()
109
 
        raise StopIteration
 
57
        sys.exit(0)
110
58
 
111
59
    def do_exit(self, args):
112
60
        self.do_quit(args)
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
 
        for char in ('|', '<', '>'):
174
 
            commandname = commandname.split(char)[0]
175
 
        if commandname[-1] in ('|', '<', '>'):
176
 
            commandname = commandname[:-1]
177
119
        try:
178
120
            if commandname in SHELL_BLACKLIST:
179
121
                raise BlackListedCommand(commandname)
181
123
        except (BlackListedCommand, BzrError):
182
124
            return os.system(line)
183
125
 
 
126
 
184
127
        try:
185
 
            if too_complicated(line):
186
 
                return os.system("bzr "+line)
187
 
            else:
188
 
                return (cmd_obj.run_argv_aliases(args, alias_args) or 0)
 
128
            return (cmd_obj.run_argv(args) or 0)
189
129
        except BzrError, e:
190
130
            print e
191
131
        except KeyboardInterrupt, e:
196
136
 
197
137
 
198
138
    def completenames(self, text, line, begidx, endidx):
199
 
        return CompletionContext(text).get_completions()
 
139
        completions = []
 
140
        iter = iter_command_names()
 
141
        try:
 
142
            if len(line) > 0:
 
143
                arg = line.split()[-1]
 
144
            else:
 
145
                arg = ""
 
146
            iter = list(iter_munged_completions(iter, arg, text))
 
147
        except Exception, e:
 
148
            print e, type(e)
 
149
        return list(iter)
200
150
 
201
151
    def completedefault(self, text, line, begidx, endidx):
202
152
        """Perform completion for native commands.
211
161
        :type endidx: int
212
162
        """
213
163
        (cmd, args, foo) = self.parseline(line)
214
 
        if cmd == "bzr":
215
 
            cmd = None
216
 
        return CompletionContext(text, command=cmd).get_completions()
217
 
 
 
164
        try:
 
165
            command_obj = get_cmd_object(cmd)
 
166
        except BzrError:
 
167
            command_obj = None
 
168
        try:
 
169
            if command_obj is not None:
 
170
                opts = []
 
171
                for option_name, option in command_obj.options().items():
 
172
                    opts.append("--" + option_name)
 
173
                    short_name = option.short_name()
 
174
                    if short_name:
 
175
                        opts.append("-" + short_name)
 
176
                q = list(iter_munged_completions(opts, args, text))
 
177
                return list(iter_munged_completions(opts, args, text))
 
178
            elif cmd == "cd":
 
179
                if len(args) > 0:
 
180
                    arg = args.split()[-1]
 
181
                else:
 
182
                    arg = ""
 
183
                iter = iter_dir_completions(arg)
 
184
                iter = iter_munged_completions(iter, arg, text)
 
185
                return list(iter)
 
186
            elif len(args)>0:
 
187
                arg = args.split()[-1]
 
188
                iter = iter_file_completions(arg)
 
189
                return list(iter_munged_completions(iter, arg, text))
 
190
            else:
 
191
                return self.completenames(text, line, begidx, endidx)
 
192
        except Exception, e:
 
193
            print e
218
194
 
219
195
def run_shell():
 
196
    prompt = PromptCmd()
220
197
    try:
221
 
        prompt = PromptCmd()
222
 
        try:
223
 
            prompt.cmdloop()
224
 
        finally:
225
 
            prompt.write_history()
226
 
    except StopIteration:
227
 
        pass
228
 
 
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
 
 
 
198
        prompt.cmdloop()
 
199
    finally:
 
200
        prompt.write_history()
237
201
 
238
202
def iter_file_completions(arg, only_dirs = False):
239
203
    """Generate an iterator that iterates through filename completions.
263
227
                userfile+='/'
264
228
                yield userfile
265
229
            elif not only_dirs:
266
 
                yield userfile + ' '
 
230
                yield userfile
267
231
 
268
232
 
269
233
def iter_dir_completions(arg):
274
238
    """
275
239
    return iter_file_completions(arg, True)
276
240
 
277
 
 
278
241
def iter_command_names(hidden=False):
279
242
    for real_cmd_name, cmd_class in get_all_cmds():
280
243
        if not hidden and cmd_class.hidden:
281
244
            continue
282
245
        for name in [real_cmd_name] + cmd_class.aliases:
283
 
            # Don't complete on aliases that are prefixes of the canonical name
284
 
            if name == real_cmd_name or not real_cmd_name.startswith(name):
285
 
                yield name
286
 
 
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
 
 
 
246
            yield name
302
247
 
303
248
def iter_munged_completions(iter, arg, text):
304
249
    for completion in iter:
305
250
        completion = str(completion)
306
251
        if completion.startswith(arg):
307
 
            yield completion[len(arg)-len(text):]+" "
308
 
 
309
 
 
310
 
def too_complicated(line):
311
 
    for char in '|<>*?':
312
 
        if char in line:
313
 
            return True
314
 
    return False
 
252
            yield completion[len(arg)-len(text):]
 
253