1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
class PromptCmd(cmd.Cmd):
def __init__(self):
cmd.Cmd.__init__(self)
self.prompt = "Fai> "
try:
self.tree = arch.tree_root(".")
except:
self.tree = None
self.set_title()
self.set_prompt()
self.fake_aba = abacmds.AbaCmds()
self.identchars += '-'
self.history_file = os.path.expanduser("~/.fai-history")
readline.set_completer_delims(string.whitespace)
if os.access(self.history_file, os.R_OK) and \
os.path.isfile(self.history_file):
readline.read_history_file(self.history_file)
self.cwd = os.getcwd()
def write_history(self):
readline.write_history_file(self.history_file)
def do_quit(self, args):
self.write_history()
sys.exit(0)
def do_exit(self, args):
self.do_quit(args)
def do_EOF(self, args):
print
self.do_quit(args)
def postcmd(self, line, bar):
self.set_title()
self.set_prompt()
def set_prompt(self):
if self.tree is not None:
try:
prompt = pylon.alias_or_version(self.tree.tree_version,
self.tree,
full=False)
if prompt is not None:
prompt = " " + prompt +":"+ pylon.tree_cwd(self.tree)
except:
prompt = ""
else:
prompt = ""
self.prompt = "Fai%s> " % prompt
def set_title(self, command=None):
try:
version = pylon.alias_or_version(self.tree.tree_version, self.tree,
full=False)
except:
version = "[no version]"
if command is None:
command = ""
sys.stdout.write(terminal.term_title("Fai %s %s" % (command, version)))
def do_cd(self, line):
if line == "":
line = "~"
line = os.path.expanduser(line)
if os.path.isabs(line):
newcwd = line
else:
newcwd = self.cwd+'/'+line
newcwd = os.path.normpath(newcwd)
try:
os.chdir(newcwd)
self.cwd = newcwd
except Exception, e:
print e
try:
self.tree = arch.tree_root(".")
except:
self.tree = None
def do_help(self, line):
Help()(line)
def default(self, line):
args = line.split()
if find_command(args[0]):
try:
find_command(args[0]).do_command(args[1:])
except cmdutil.BadCommandOption, e:
print e
except cmdutil.GetHelp, e:
find_command(args[0]).help()
except CommandFailed, e:
print e
except arch.errors.ArchiveNotRegistered, e:
print e
except KeyboardInterrupt, e:
print "Interrupted"
except arch.util.ExecProblem, e:
print e.proc.error.rstrip('\n')
except cmdutil.CantDetermineVersion, e:
print e
except cmdutil.CantDetermineRevision, e:
print e
except Exception, e:
print "Unhandled error:\n%s" % errors.exception_str(e)
elif suggestions.has_key(args[0]):
print suggestions[args[0]]
elif self.fake_aba.is_command(args[0]):
tree = None
try:
tree = arch.tree_root(".")
except arch.errors.TreeRootError:
pass
cmd = self.fake_aba.is_command(args[0])
try:
args = cmdutil.expand_prefix_alias(args[1:], tree)
except Exception, e:
print e
return
try:
cmd.run(args)
except KeyboardInterrupt, e:
print "Interrupted"
elif options.tla_fallthrough and args[0] != "rm" and \
cmdutil.is_tla_command(args[0]):
try:
tree = None
try:
tree = arch.tree_root(".")
except arch.errors.TreeRootError:
pass
try:
args = cmdutil.expand_prefix_alias(args, tree)
except Exception, e:
print e
return
arch.util.exec_safe('tla', args, stderr=sys.stderr,
stdout=sys.stdout, expected=(0, 1))
except arch.util.ExecProblem, e:
pass
except KeyboardInterrupt, e:
print "Interrupted"
else:
try:
try:
tree = arch.tree_root(".")
except arch.errors.TreeRootError:
tree = None
args=line.split()
try:
args = cmdutil.expand_prefix_alias(args, tree)
except Exception, e:
print e
return
os.system(" ".join(args))
except KeyboardInterrupt, e:
print "Interrupted"
def completenames(self, text, line, begidx, endidx):
completions = []
iter = iter_command_names(self.fake_aba)
try:
if len(line) > 0:
arg = line.split()[-1]
else:
arg = ""
iter = cmdutil.iter_munged_completions(iter, arg, text)
except Exception, e:
print e
return list(iter)
def completedefault(self, text, line, begidx, endidx):
"""Perform completion for native commands.
:param text: The text to complete
:type text: str
:param line: The entire line to complete
:type line: str
:param begidx: The start of the text in the line
:type begidx: int
:param endidx: The end of the text in the line
:type endidx: int
"""
try:
(cmd, args, foo) = self.parseline(line)
command_obj=find_command(cmd)
if command_obj is not None:
return command_obj.complete(args.split(), text)
elif not self.fake_aba.is_command(cmd) and \
cmdutil.is_tla_command(cmd):
iter = cmdutil.iter_supported_switches(cmd)
if len(args) > 0:
arg = args.split()[-1]
else:
arg = ""
if arg.startswith("-"):
return list(cmdutil.iter_munged_completions(iter, arg,
text))
else:
return list(cmdutil.iter_munged_completions(
cmdutil.iter_file_completions(arg), arg, text))
elif cmd == "cd":
if len(args) > 0:
arg = args.split()[-1]
else:
arg = ""
iter = cmdutil.iter_dir_completions(arg)
iter = cmdutil.iter_munged_completions(iter, arg, text)
return list(iter)
elif len(args)>0:
arg = args.split()[-1]
iter = cmdutil.iter_file_completions(arg)
return list(cmdutil.iter_munged_completions(iter, arg, text))
else:
return self.completenames(text, line, begidx, endidx)
except Exception, e:
print e
def run_shell():
if len(cmdargs)!=0:
raise cmdutil.GetHelp
prompt = PromptCmd()
try:
prompt.cmdloop()
finally:
prompt.write_history()
def iter_file_completions(arg, only_dirs = False):
"""Generate an iterator that iterates through filename completions.
:param arg: The filename fragment to match
:type arg: str
:param only_dirs: If true, match only directories
:type only_dirs: bool
"""
cwd = os.getcwd()
if cwd != "/":
extras = [".", ".."]
else:
extras = []
(dir, file) = os.path.split(arg)
if dir != "":
listingdir = os.path.expanduser(dir)
else:
listingdir = cwd
for file in iter_combine([os.listdir(listingdir), extras]):
if dir != "":
userfile = dir+'/'+file
else:
userfile = file
if userfile.startswith(arg):
if os.path.isdir(listingdir+'/'+file):
userfile+='/'
yield userfile
elif not only_dirs:
yield userfile
def iter_dir_completions(arg):
"""Generate an iterator that iterates through directory name completions.
:param arg: The directory name fragment to match
:type arg: str
"""
return iter_file_completions(arg, True)
|