2 import sys, os, stat, fnmatch
3 from bup import options, git, shquote, vfs
4 from bup.helpers import *
9 def node_name(text, n):
10 if stat.S_ISDIR(n.mode):
12 elif stat.S_ISLNK(n.mode):
18 class OptionError(Exception):
25 a,all include hidden files in the listing
27 ls_opt = options.Options(ls_optspec, onabort=OptionError)
31 (opt, flags, extra) = ls_opt.parse(cmd_args)
32 except OptionError, e:
37 for path in (extra or ['.']):
38 n = pwd.try_resolve(path)
40 if stat.S_ISDIR(n.mode):
43 if opt.all or not len(name)>1 or not name.startswith('.'):
44 L.append(node_name(name, sub))
46 L.append(node_name(path, n))
47 sys.stdout.write(columnate(L, ''))
50 def write_to_file(inf, outf):
51 for blob in chunkyreader(inf):
56 if os.isatty(sys.stdin.fileno()):
59 yield raw_input('bup> ')
61 print '' # Clear the line for the terminal's next prompt
64 for line in sys.stdin:
68 def _completer_get_subs(line):
69 (qtype, lastword) = shquote.unfinished_word(line)
70 (dir,name) = os.path.split(lastword)
71 #log('\ncompleter: %r %r %r\n' % (qtype, lastword, text))
74 subs = list(filter(lambda x: x.name.startswith(name),
76 except vfs.NoSuchFile, e:
78 return (dir, name, qtype, lastword, subs)
81 def find_readline_lib():
82 """Return the name (and possibly the full path) of the readline library
83 linked to the given readline module.
86 f = open(readline.__file__, "rb")
92 m = re.search('\0([^\0]*libreadline[^\0]*)\0', data)
98 def init_readline_vars():
99 """Work around trailing space automatically inserted by readline.
100 See http://bugs.python.org/issue5833"""
104 # python before 2.5 didn't have the ctypes module; but those
105 # old systems probably also didn't have this readline bug, so
108 lib_name = find_readline_lib()
109 if lib_name is not None:
110 lib = ctypes.cdll.LoadLibrary(lib_name)
111 global rl_completion_suppress_append
112 rl_completion_suppress_append = ctypes.c_int.in_dll(lib,
113 "rl_completion_suppress_append")
116 rl_completion_suppress_append = None
119 def completer(text, state):
122 global rl_completion_suppress_append
123 if rl_completion_suppress_append is not None:
124 rl_completion_suppress_append.value = 1
126 line = readline.get_line_buffer()[:readline.get_endidx()]
127 if _last_line != line:
128 _last_res = _completer_get_subs(line)
130 (dir, name, qtype, lastword, subs) = _last_res
131 if state < len(subs):
133 sn1 = sn.try_resolve() # find the type of any symlink target
134 fullname = os.path.join(dir, sn.name)
135 if stat.S_ISDIR(sn1.mode):
136 ret = shquote.what_to_add(qtype, lastword, fullname+'/',
139 ret = shquote.what_to_add(qtype, lastword, fullname,
140 terminate=True) + ' '
146 traceback.print_tb(sys.exc_traceback)
147 except Exception, e2:
148 log('Error printing traceback: %s\n' % e2)
149 log('\nError in completion: %s\n' % e)
153 bup ftp [commands...]
155 o = options.Options(optspec)
156 (opt, flags, extra) = o.parse(sys.argv[1:])
158 git.check_repo_or_die()
160 top = vfs.RefList(None)
170 log('* readline module not available: line editing disabled.\n')
174 readline.set_completer_delims(' \t\n\r/')
175 readline.set_completer(completer)
176 readline.parse_and_bind("tab: complete")
183 words = [word for (wordstart,word) in shquote.quotesplit(line)]
184 cmd = words[0].lower()
185 #log('execute: %r %r\n' % (cmd, parm))
191 for parm in words[1:]:
192 np = np.resolve(parm)
193 if not stat.S_ISDIR(np.mode):
194 raise vfs.NotDir('%s is not a directory' % parm)
199 for parm in words[1:]:
200 write_to_file(pwd.resolve(parm).open(), sys.stdout)
202 if len(words) not in [2,3]:
204 raise Exception('Usage: get <filename> [localname]')
206 (dir,base) = os.path.split(rname)
207 lname = len(words)>2 and words[2] or base
208 inf = pwd.resolve(rname).open()
209 log('Saving %r\n' % lname)
210 write_to_file(inf, open(lname, 'wb'))
212 for parm in words[1:]:
213 (dir,base) = os.path.split(parm)
214 for n in pwd.resolve(dir).subs():
215 if fnmatch.fnmatch(n.name, base):
217 log('Saving %r\n' % n.name)
219 outf = open(n.name, 'wb')
220 write_to_file(inf, outf)
224 log(' error: %s\n' % e)
225 elif cmd == 'help' or cmd == '?':
226 log('Commands: ls cd pwd cat get mget help quit\n')
227 elif cmd == 'quit' or cmd == 'exit' or cmd == 'bye':
231 raise Exception('no such command %r' % cmd)
234 log('error: %s\n' % e)