2 import sys, os, stat, fnmatch
3 from bup import options, git, shquote, vfs
4 from bup.helpers import *
9 def node_name(opt, text, n):
12 prefix += "%s " % n.hash.encode('hex')
13 if stat.S_ISDIR(n.mode):
14 return '%s%s/' % (prefix, text)
15 elif stat.S_ISLNK(n.mode):
16 return '%s%s@' % (prefix, text)
18 return '%s%s' % (prefix, text)
21 class OptionError(Exception):
28 s,hash show hash for each file
29 a,all include hidden files in the listing
31 ls_opt = options.Options(ls_optspec, onabort=OptionError)
35 (opt, flags, extra) = ls_opt.parse(cmd_args)
36 except OptionError, e:
41 for path in (extra or ['.']):
42 n = pwd.try_resolve(path)
44 if stat.S_ISDIR(n.mode):
47 if opt.all or not len(name)>1 or not name.startswith('.'):
49 L.append(node_name(opt, name, sub))
51 print node_name(opt, name, sub)
54 L.append(node_name(opt, path, n))
56 print node_name(opt, path, n)
57 sys.stdout.write(columnate(L, ''))
60 def write_to_file(inf, outf):
61 for blob in chunkyreader(inf):
66 if os.isatty(sys.stdin.fileno()):
69 yield raw_input('bup> ')
71 print '' # Clear the line for the terminal's next prompt
74 for line in sys.stdin:
78 def _completer_get_subs(line):
79 (qtype, lastword) = shquote.unfinished_word(line)
80 (dir,name) = os.path.split(lastword)
81 #log('\ncompleter: %r %r %r\n' % (qtype, lastword, text))
84 subs = list(filter(lambda x: x.name.startswith(name),
86 except vfs.NoSuchFile, e:
88 return (dir, name, qtype, lastword, subs)
91 def find_readline_lib():
92 """Return the name (and possibly the full path) of the readline library
93 linked to the given readline module.
96 f = open(readline.__file__, "rb")
102 m = re.search('\0([^\0]*libreadline[^\0]*)\0', data)
108 def init_readline_vars():
109 """Work around trailing space automatically inserted by readline.
110 See http://bugs.python.org/issue5833"""
114 # python before 2.5 didn't have the ctypes module; but those
115 # old systems probably also didn't have this readline bug, so
118 lib_name = find_readline_lib()
119 if lib_name is not None:
120 lib = ctypes.cdll.LoadLibrary(lib_name)
121 global rl_completion_suppress_append
122 rl_completion_suppress_append = ctypes.c_int.in_dll(lib,
123 "rl_completion_suppress_append")
126 rl_completion_suppress_append = None
129 def completer(text, state):
132 global rl_completion_suppress_append
133 if rl_completion_suppress_append is not None:
134 rl_completion_suppress_append.value = 1
136 line = readline.get_line_buffer()[:readline.get_endidx()]
137 if _last_line != line:
138 _last_res = _completer_get_subs(line)
140 (dir, name, qtype, lastword, subs) = _last_res
141 if state < len(subs):
143 sn1 = sn.try_resolve() # find the type of any symlink target
144 fullname = os.path.join(dir, sn.name)
145 if stat.S_ISDIR(sn1.mode):
146 ret = shquote.what_to_add(qtype, lastword, fullname+'/',
149 ret = shquote.what_to_add(qtype, lastword, fullname,
150 terminate=True) + ' '
156 traceback.print_tb(sys.exc_traceback)
157 except Exception, e2:
158 log('Error printing traceback: %s\n' % e2)
159 log('\nError in completion: %s\n' % e)
163 bup ftp [commands...]
165 o = options.Options(optspec)
166 (opt, flags, extra) = o.parse(sys.argv[1:])
168 git.check_repo_or_die()
170 top = vfs.RefList(None)
180 log('* readline module not available: line editing disabled.\n')
184 readline.set_completer_delims(' \t\n\r/')
185 readline.set_completer(completer)
186 readline.parse_and_bind("tab: complete")
193 words = [word for (wordstart,word) in shquote.quotesplit(line)]
194 cmd = words[0].lower()
195 #log('execute: %r %r\n' % (cmd, parm))
201 for parm in words[1:]:
202 np = np.resolve(parm)
203 if not stat.S_ISDIR(np.mode):
204 raise vfs.NotDir('%s is not a directory' % parm)
209 for parm in words[1:]:
210 write_to_file(pwd.resolve(parm).open(), sys.stdout)
212 if len(words) not in [2,3]:
214 raise Exception('Usage: get <filename> [localname]')
216 (dir,base) = os.path.split(rname)
217 lname = len(words)>2 and words[2] or base
218 inf = pwd.resolve(rname).open()
219 log('Saving %r\n' % lname)
220 write_to_file(inf, open(lname, 'wb'))
222 for parm in words[1:]:
223 (dir,base) = os.path.split(parm)
224 for n in pwd.resolve(dir).subs():
225 if fnmatch.fnmatch(n.name, base):
227 log('Saving %r\n' % n.name)
229 outf = open(n.name, 'wb')
230 write_to_file(inf, outf)
234 log(' error: %s\n' % e)
235 elif cmd == 'help' or cmd == '?':
236 log('Commands: ls cd pwd cat get mget help quit\n')
237 elif cmd == 'quit' or cmd == 'exit' or cmd == 'bye':
241 raise Exception('no such command %r' % cmd)
244 log('error: %s\n' % e)