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):
17 if stat.S_ISDIR(n.mode):
19 l.append(node_name(sub.name, sub))
21 l.append(node_name(path, n))
22 print columnate(l, '')
25 def write_to_file(inf, outf):
26 for blob in chunkyreader(inf):
31 if os.isatty(sys.stdin.fileno()):
34 yield raw_input('bup> ')
38 for line in sys.stdin:
42 def _completer_get_subs(line):
43 (qtype, lastword) = shquote.unfinished_word(line)
44 (dir,name) = os.path.split(lastword)
45 #log('\ncompleter: %r %r %r\n' % (qtype, lastword, text))
47 subs = list(filter(lambda x: x.name.startswith(name),
49 return (dir, name, qtype, lastword, subs)
52 def find_readline_lib():
53 """Return the name (and possibly the full path) of the readline library
54 linked to the given readline module.
57 f = open(readline.__file__, "rb")
63 m = re.search('\0([^\0]*libreadline[^\0]*)\0', data)
69 def init_readline_vars():
70 """Work around trailing space automatically inserted by readline.
71 See http://bugs.python.org/issue5833"""
73 lib_name = find_readline_lib()
74 if lib_name is not None:
75 lib = ctypes.cdll.LoadLibrary(lib_name)
76 global rl_completion_suppress_append
77 rl_completion_suppress_append = ctypes.c_int.in_dll(lib,
78 "rl_completion_suppress_append")
81 rl_completion_suppress_append = None
84 def completer(text, state):
87 global rl_completion_suppress_append
88 if rl_completion_suppress_append is not None:
89 rl_completion_suppress_append.value = 1
91 line = readline.get_line_buffer()[:readline.get_endidx()]
92 if _last_line != line:
93 _last_res = _completer_get_subs(line)
95 (dir, name, qtype, lastword, subs) = _last_res
98 sn1 = sn.resolve('') # deref symlinks
99 fullname = os.path.join(dir, sn.name)
100 if stat.S_ISDIR(sn1.mode):
101 ret = shquote.what_to_add(qtype, lastword, fullname+'/',
104 ret = shquote.what_to_add(qtype, lastword, fullname,
105 terminate=True) + ' '
108 log('\nerror in completion: %s\n' % e)
112 bup ftp [commands...]
114 o = options.Options('bup ftp', optspec)
115 (opt, flags, extra) = o.parse(sys.argv[1:])
117 git.check_repo_or_die()
119 top = vfs.RefList(None)
129 log('* readline module not available: line editing disabled.\n')
133 readline.set_completer_delims(' \t\n\r/')
134 readline.set_completer(completer)
135 readline.parse_and_bind("tab: complete")
142 words = [word for (wordstart,word) in shquote.quotesplit(line)]
143 cmd = words[0].lower()
144 #log('execute: %r %r\n' % (cmd, parm))
147 for parm in (words[1:] or ['.']):
148 do_ls(parm, pwd.resolve(parm))
150 for parm in words[1:]:
151 pwd = pwd.resolve(parm)
155 for parm in words[1:]:
156 write_to_file(pwd.resolve(parm).open(), sys.stdout)
158 if len(words) not in [2,3]:
160 raise Exception('Usage: get <filename> [localname]')
162 (dir,base) = os.path.split(rname)
163 lname = len(words)>2 and words[2] or base
164 inf = pwd.resolve(rname).open()
165 log('Saving %r\n' % lname)
166 write_to_file(inf, open(lname, 'wb'))
168 for parm in words[1:]:
169 (dir,base) = os.path.split(parm)
170 for n in pwd.resolve(dir).subs():
171 if fnmatch.fnmatch(n.name, base):
173 log('Saving %r\n' % n.name)
175 outf = open(n.name, 'wb')
176 write_to_file(inf, outf)
180 log(' error: %s\n' % e)
181 elif cmd == 'help' or cmd == '?':
182 log('Commands: ls cd pwd cat get mget help quit\n')
183 elif cmd == 'quit' or cmd == 'exit' or cmd == 'bye':
187 raise Exception('no such command %r' % cmd)
190 log('error: %s\n' % e)