~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shell.py

  • Committer: Aaron Bentley
  • Date: 2005-10-27 04:45:06 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20051027044506-45f616c07537a1da
Got the shell basics working properly

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2004, 2005 Aaron Bentley
 
2
# <aaron.bentley@utoronto.ca>
 
3
#
 
4
#    This program is free software; you can redistribute it and/or modify
 
5
#    it under the terms of the GNU General Public License as published by
 
6
#    the Free Software Foundation; either version 2 of the License, or
 
7
#    (at your option) any later version.
 
8
#
 
9
#    This program is distributed in the hope that it will be useful,
 
10
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
#    GNU General Public License for more details.
 
13
#
 
14
#    You should have received a copy of the GNU General Public License
 
15
#    along with this program; if not, write to the Free Software
 
16
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
import cmd
 
18
import sys
 
19
import os
 
20
import terminal
 
21
import readline
 
22
import string
 
23
from bzrlib.errors import BzrError
 
24
from bzrlib.commands import get_cmd_object
 
25
 
1
26
class PromptCmd(cmd.Cmd):
2
27
    def __init__(self):
3
28
        cmd.Cmd.__init__(self)
4
 
        self.prompt = "Fai> "
 
29
        self.prompt = "bzr> "
5
30
        try:
6
31
            self.tree = arch.tree_root(".")
7
32
        except:
8
33
            self.tree = None
9
34
        self.set_title()
10
35
        self.set_prompt()
11
 
        self.fake_aba = abacmds.AbaCmds()
12
36
        self.identchars += '-'
13
 
        self.history_file = os.path.expanduser("~/.fai-history")
 
37
        self.history_file = os.path.expanduser("~/.bazaar/shell-history")
14
38
        readline.set_completer_delims(string.whitespace)
15
39
        if os.access(self.history_file, os.R_OK) and \
16
40
            os.path.isfile(self.history_file):
47
71
                prompt = ""
48
72
        else:
49
73
            prompt = ""
50
 
        self.prompt = "Fai%s> " % prompt
 
74
        self.prompt = "bzr%s> " % prompt
51
75
 
52
76
    def set_title(self, command=None):
53
77
        try:
57
81
            version = "[no version]"
58
82
        if command is None:
59
83
            command = ""
60
 
        sys.stdout.write(terminal.term_title("Fai %s %s" % (command, version)))
 
84
        sys.stdout.write(terminal.term_title("bzr %s %s" % (command, version)))
61
85
 
62
86
    def do_cd(self, line):
63
87
        if line == "":
83
107
 
84
108
    def default(self, line):
85
109
        args = line.split()
86
 
        if find_command(args[0]):
87
 
            try:
88
 
                find_command(args[0]).do_command(args[1:])
89
 
            except cmdutil.BadCommandOption, e:
90
 
                print e
91
 
            except cmdutil.GetHelp, e:
92
 
                find_command(args[0]).help()
93
 
            except CommandFailed, e:
94
 
                print e
95
 
            except arch.errors.ArchiveNotRegistered, e:
96
 
                print e
97
 
            except KeyboardInterrupt, e:
98
 
                print "Interrupted"
99
 
            except arch.util.ExecProblem, e:
100
 
                print e.proc.error.rstrip('\n')
101
 
            except cmdutil.CantDetermineVersion, e:
102
 
                print e
103
 
            except cmdutil.CantDetermineRevision, e:
104
 
                print e
105
 
            except Exception, e:
106
 
                print "Unhandled error:\n%s" % errors.exception_str(e)
107
 
 
108
 
        elif suggestions.has_key(args[0]):
109
 
            print suggestions[args[0]]
110
 
 
111
 
        elif self.fake_aba.is_command(args[0]):
112
 
            tree = None
113
 
            try:
114
 
                tree = arch.tree_root(".")
115
 
            except arch.errors.TreeRootError:
116
 
                pass
117
 
            cmd = self.fake_aba.is_command(args[0])
118
 
            try:
119
 
                args = cmdutil.expand_prefix_alias(args[1:], tree)
120
 
            except Exception, e:
121
 
                print e
122
 
                return
123
 
            try:
124
 
                cmd.run(args)
125
 
            except KeyboardInterrupt, e:
126
 
                print "Interrupted"
127
 
 
128
 
        elif options.tla_fallthrough and args[0] != "rm" and \
129
 
            cmdutil.is_tla_command(args[0]):
130
 
            try:
131
 
                tree = None
132
 
                try:
133
 
                    tree = arch.tree_root(".")
134
 
                except arch.errors.TreeRootError:
135
 
                    pass
136
 
                try:
137
 
                    args = cmdutil.expand_prefix_alias(args, tree)
138
 
                except Exception, e:
139
 
                    print e
140
 
                    return
141
 
                arch.util.exec_safe('tla', args, stderr=sys.stderr,
142
 
                                    stdout=sys.stdout, expected=(0, 1))
143
 
            except arch.util.ExecProblem, e:
144
 
                pass
145
 
            except KeyboardInterrupt, e:
146
 
                print "Interrupted"
147
 
        else:
148
 
            try:
149
 
                try:
150
 
                    tree = arch.tree_root(".")
151
 
                except arch.errors.TreeRootError:
152
 
                    tree = None
153
 
                args=line.split()
154
 
                try:
155
 
                    args = cmdutil.expand_prefix_alias(args, tree)
156
 
                except Exception, e:
157
 
                    print e
158
 
                    return
159
 
                os.system(" ".join(args))
160
 
            except KeyboardInterrupt, e:
161
 
                print "Interrupted"
 
110
        commandname = args.pop(0)
 
111
        try:
 
112
            cmd_obj = get_cmd_object(commandname)
 
113
        except BzrError:
 
114
            return os.system(line)
 
115
 
 
116
 
 
117
        try:
 
118
            return (cmd_obj.run_argv(args) or 0)
 
119
        except BzrError, e:
 
120
            print e
 
121
        except KeyboardInterrupt, e:
 
122
            print "Interrupted"
 
123
        except Exception, e:
 
124
#            print "Unhandled error:\n%s" % errors.exception_str(e)
 
125
            print "Unhandled error:\n%s" % (e)
 
126
 
162
127
 
163
128
    def completenames(self, text, line, begidx, endidx):
164
129
        completions = []
223
188
            print e
224
189
 
225
190
def run_shell():
226
 
    if len(cmdargs)!=0:
227
 
        raise cmdutil.GetHelp
228
191
    prompt = PromptCmd()
229
192
    try:
230
193
        prompt.cmdloop()