3 bup_python="$(dirname "$0")/bup-python" || exit $?
4 exec "$bup_python" "$0" ${1+"$@"}
7 import sys, os, stat, fnmatch
8 from bup import options, git, shquote, vfs, ls
9 from bup.helpers import *
14 class OptionError(Exception):
18 # Check out lib/bup/ls.py for the opt spec
21 ls.do_ls(cmd_args, pwd, onabort=OptionError)
22 except OptionError, e:
26 def write_to_file(inf, outf):
27 for blob in chunkyreader(inf):
32 if os.isatty(sys.stdin.fileno()):
35 yield raw_input('bup> ')
37 print '' # Clear the line for the terminal's next prompt
40 for line in sys.stdin:
44 def _completer_get_subs(line):
45 (qtype, lastword) = shquote.unfinished_word(line)
46 (dir,name) = os.path.split(lastword)
47 #log('\ncompleter: %r %r %r\n' % (qtype, lastword, text))
50 subs = list(filter(lambda x: x.name.startswith(name),
52 except vfs.NoSuchFile, e:
54 return (dir, name, qtype, lastword, subs)
57 def find_readline_lib():
58 """Return the name (and possibly the full path) of the readline library
59 linked to the given readline module.
62 f = open(readline.__file__, "rb")
68 m = re.search('\0([^\0]*libreadline[^\0]*)\0', data)
74 def init_readline_vars():
75 """Work around trailing space automatically inserted by readline.
76 See http://bugs.python.org/issue5833"""
80 # python before 2.5 didn't have the ctypes module; but those
81 # old systems probably also didn't have this readline bug, so
84 lib_name = find_readline_lib()
85 if lib_name is not None:
86 lib = ctypes.cdll.LoadLibrary(lib_name)
87 global rl_completion_suppress_append
88 rl_completion_suppress_append = ctypes.c_int.in_dll(lib,
89 "rl_completion_suppress_append")
92 rl_completion_suppress_append = None
95 def completer(text, state):
98 global rl_completion_suppress_append
99 if rl_completion_suppress_append is not None:
100 rl_completion_suppress_append.value = 1
102 line = readline.get_line_buffer()[:readline.get_endidx()]
103 if _last_line != line:
104 _last_res = _completer_get_subs(line)
106 (dir, name, qtype, lastword, subs) = _last_res
107 if state < len(subs):
109 sn1 = sn.try_resolve() # find the type of any symlink target
110 fullname = os.path.join(dir, sn.name)
111 if stat.S_ISDIR(sn1.mode):
112 ret = shquote.what_to_add(qtype, lastword, fullname+'/',
115 ret = shquote.what_to_add(qtype, lastword, fullname,
116 terminate=True) + ' '
122 traceback.print_tb(sys.exc_traceback)
123 except Exception, e2:
124 log('Error printing traceback: %s\n' % e2)
125 log('\nError in completion: %s\n' % e)
129 bup ftp [commands...]
131 o = options.Options(optspec)
132 (opt, flags, extra) = o.parse(sys.argv[1:])
134 git.check_repo_or_die()
136 top = vfs.RefList(None)
146 log('* readline module not available: line editing disabled.\n')
150 readline.set_completer_delims(' \t\n\r/')
151 readline.set_completer(completer)
152 if sys.platform.startswith('darwin'):
153 # MacOS uses a slighly incompatible clone of libreadline
154 readline.parse_and_bind('bind ^I rl_complete')
155 readline.parse_and_bind('tab: complete')
162 words = [word for (wordstart,word) in shquote.quotesplit(line)]
163 cmd = words[0].lower()
164 #log('execute: %r %r\n' % (cmd, parm))
170 for parm in words[1:]:
171 np = np.resolve(parm)
172 if not stat.S_ISDIR(np.mode):
173 raise vfs.NotDir('%s is not a directory' % parm)
178 for parm in words[1:]:
179 write_to_file(pwd.resolve(parm).open(), sys.stdout)
181 if len(words) not in [2,3]:
183 raise Exception('Usage: get <filename> [localname]')
185 (dir,base) = os.path.split(rname)
186 lname = len(words)>2 and words[2] or base
187 inf = pwd.resolve(rname).open()
188 log('Saving %r\n' % lname)
189 write_to_file(inf, open(lname, 'wb'))
191 for parm in words[1:]:
192 (dir,base) = os.path.split(parm)
193 for n in pwd.resolve(dir).subs():
194 if fnmatch.fnmatch(n.name, base):
196 log('Saving %r\n' % n.name)
198 outf = open(n.name, 'wb')
199 write_to_file(inf, outf)
203 log(' error: %s\n' % e)
204 elif cmd == 'help' or cmd == '?':
205 log('Commands: ls cd pwd cat get mget help quit\n')
206 elif cmd == 'quit' or cmd == 'exit' or cmd == 'bye':
210 raise Exception('no such command %r' % cmd)
213 log('error: %s\n' % e)