2 import sys, os, re, stat, fnmatch
3 from bup import options, git, shquote, vfs
4 from bup.helpers import *
6 def node_name(text, n):
7 if stat.S_ISDIR(n.mode):
9 elif stat.S_ISLNK(n.mode):
15 class OptionError(Exception):
22 a,all include hidden files in the listing
24 ls_opt = options.Options('ls', ls_optspec, onabort=OptionError)
28 (opt, flags, extra) = ls_opt.parse(cmd_args)
29 except OptionError, e:
34 for path in (extra or ['.']):
35 n = pwd.try_resolve(path)
37 if stat.S_ISDIR(n.mode):
40 if opt.all or not len(name)>1 or not name.startswith('.'):
41 L.append(node_name(name, sub))
43 L.append(node_name(path, n))
44 print columnate(L, '')
47 def write_to_file(inf, outf):
48 for blob in chunkyreader(inf):
53 if os.isatty(sys.stdin.fileno()):
56 yield raw_input('bup> ')
60 for line in sys.stdin:
64 def _completer_get_subs(line):
65 (qtype, lastword) = shquote.unfinished_word(line)
66 (dir,name) = os.path.split(lastword)
67 #log('\ncompleter: %r %r %r\n' % (qtype, lastword, text))
70 subs = list(filter(lambda x: x.name.startswith(name),
72 except vfs.NoSuchFile, e:
74 return (dir, name, qtype, lastword, subs)
77 def find_readline_lib():
78 """Return the name (and possibly the full path) of the readline library
79 linked to the given readline module.
82 f = open(readline.__file__, "rb")
88 m = re.search('\0([^\0]*libreadline[^\0]*)\0', data)
94 def init_readline_vars():
95 """Work around trailing space automatically inserted by readline.
96 See http://bugs.python.org/issue5833"""
98 lib_name = find_readline_lib()
99 if lib_name is not None:
100 lib = ctypes.cdll.LoadLibrary(lib_name)
101 global rl_completion_suppress_append
102 rl_completion_suppress_append = ctypes.c_int.in_dll(lib,
103 "rl_completion_suppress_append")
106 rl_completion_suppress_append = None
109 def completer(text, state):
112 global rl_completion_suppress_append
113 if rl_completion_suppress_append is not None:
114 rl_completion_suppress_append.value = 1
116 line = readline.get_line_buffer()[:readline.get_endidx()]
117 if _last_line != line:
118 _last_res = _completer_get_subs(line)
120 (dir, name, qtype, lastword, subs) = _last_res
121 if state < len(subs):
123 sn1 = sn.try_resolve() # find the type of any symlink target
124 fullname = os.path.join(dir, sn.name)
125 if stat.S_ISDIR(sn1.mode):
126 ret = shquote.what_to_add(qtype, lastword, fullname+'/',
129 ret = shquote.what_to_add(qtype, lastword, fullname,
130 terminate=True) + ' '
136 traceback.print_tb(sys.exc_traceback)
137 except Exception, e2:
138 log('Error printing traceback: %s\n' % e2)
139 log('\nError in completion: %s\n' % e)
143 bup ftp [commands...]
145 o = options.Options('bup ftp', optspec)
146 (opt, flags, extra) = o.parse(sys.argv[1:])
148 git.check_repo_or_die()
150 top = vfs.RefList(None)
160 log('* readline module not available: line editing disabled.\n')
164 readline.set_completer_delims(' \t\n\r/')
165 readline.set_completer(completer)
166 readline.parse_and_bind("tab: complete")
173 words = [word for (wordstart,word) in shquote.quotesplit(line)]
174 cmd = words[0].lower()
175 #log('execute: %r %r\n' % (cmd, parm))
181 for parm in words[1:]:
182 np = np.resolve(parm)
183 if not stat.S_ISDIR(np.mode):
184 raise vfs.NotDir('%s is not a directory' % parm)
189 for parm in words[1:]:
190 write_to_file(pwd.resolve(parm).open(), sys.stdout)
192 if len(words) not in [2,3]:
194 raise Exception('Usage: get <filename> [localname]')
196 (dir,base) = os.path.split(rname)
197 lname = len(words)>2 and words[2] or base
198 inf = pwd.resolve(rname).open()
199 log('Saving %r\n' % lname)
200 write_to_file(inf, open(lname, 'wb'))
202 for parm in words[1:]:
203 (dir,base) = os.path.split(parm)
204 for n in pwd.resolve(dir).subs():
205 if fnmatch.fnmatch(n.name, base):
207 log('Saving %r\n' % n.name)
209 outf = open(n.name, 'wb')
210 write_to_file(inf, outf)
214 log(' error: %s\n' % e)
215 elif cmd == 'help' or cmd == '?':
216 log('Commands: ls cd pwd cat get mget help quit\n')
217 elif cmd == 'quit' or cmd == 'exit' or cmd == 'bye':
221 raise Exception('no such command %r' % cmd)
224 log('error: %s\n' % e)