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.try_resolve() # find the type of any symlink target
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) + ' '
112 traceback.print_tb(sys.exc_traceback)
113 except Exception, e2:
114 log('Error printing traceback: %s\n' % e2)
115 log('\nError in completion: %s\n' % e)
119 bup ftp [commands...]
121 o = options.Options('bup ftp', optspec)
122 (opt, flags, extra) = o.parse(sys.argv[1:])
124 git.check_repo_or_die()
126 top = vfs.RefList(None)
136 log('* readline module not available: line editing disabled.\n')
140 readline.set_completer_delims(' \t\n\r/')
141 readline.set_completer(completer)
142 readline.parse_and_bind("tab: complete")
149 words = [word for (wordstart,word) in shquote.quotesplit(line)]
150 cmd = words[0].lower()
151 #log('execute: %r %r\n' % (cmd, parm))
154 for parm in (words[1:] or ['.']):
155 do_ls(parm, pwd.try_resolve(parm))
158 for parm in words[1:]:
159 np = np.resolve(parm)
160 if not stat.S_ISDIR(np.mode):
161 raise vfs.NotDir('%s is not a directory' % parm)
166 for parm in words[1:]:
167 write_to_file(pwd.resolve(parm).open(), sys.stdout)
169 if len(words) not in [2,3]:
171 raise Exception('Usage: get <filename> [localname]')
173 (dir,base) = os.path.split(rname)
174 lname = len(words)>2 and words[2] or base
175 inf = pwd.resolve(rname).open()
176 log('Saving %r\n' % lname)
177 write_to_file(inf, open(lname, 'wb'))
179 for parm in words[1:]:
180 (dir,base) = os.path.split(parm)
181 for n in pwd.resolve(dir).subs():
182 if fnmatch.fnmatch(n.name, base):
184 log('Saving %r\n' % n.name)
186 outf = open(n.name, 'wb')
187 write_to_file(inf, outf)
191 log(' error: %s\n' % e)
192 elif cmd == 'help' or cmd == '?':
193 log('Commands: ls cd pwd cat get mget help quit\n')
194 elif cmd == 'quit' or cmd == 'exit' or cmd == 'bye':
198 raise Exception('no such command %r' % cmd)
201 log('error: %s\n' % e)