2 import sys, os, re, 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', 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 print 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> ')
63 for line in sys.stdin:
67 def _completer_get_subs(line):
68 (qtype, lastword) = shquote.unfinished_word(line)
69 (dir,name) = os.path.split(lastword)
70 #log('\ncompleter: %r %r %r\n' % (qtype, lastword, text))
73 subs = list(filter(lambda x: x.name.startswith(name),
75 except vfs.NoSuchFile, e:
77 return (dir, name, qtype, lastword, subs)
80 def find_readline_lib():
81 """Return the name (and possibly the full path) of the readline library
82 linked to the given readline module.
85 f = open(readline.__file__, "rb")
91 m = re.search('\0([^\0]*libreadline[^\0]*)\0', data)
97 def init_readline_vars():
98 """Work around trailing space automatically inserted by readline.
99 See http://bugs.python.org/issue5833"""
101 lib_name = find_readline_lib()
102 if lib_name is not None:
103 lib = ctypes.cdll.LoadLibrary(lib_name)
104 global rl_completion_suppress_append
105 rl_completion_suppress_append = ctypes.c_int.in_dll(lib,
106 "rl_completion_suppress_append")
109 rl_completion_suppress_append = None
112 def completer(text, state):
115 global rl_completion_suppress_append
116 if rl_completion_suppress_append is not None:
117 rl_completion_suppress_append.value = 1
119 line = readline.get_line_buffer()[:readline.get_endidx()]
120 if _last_line != line:
121 _last_res = _completer_get_subs(line)
123 (dir, name, qtype, lastword, subs) = _last_res
124 if state < len(subs):
126 sn1 = sn.try_resolve() # find the type of any symlink target
127 fullname = os.path.join(dir, sn.name)
128 if stat.S_ISDIR(sn1.mode):
129 ret = shquote.what_to_add(qtype, lastword, fullname+'/',
132 ret = shquote.what_to_add(qtype, lastword, fullname,
133 terminate=True) + ' '
139 traceback.print_tb(sys.exc_traceback)
140 except Exception, e2:
141 log('Error printing traceback: %s\n' % e2)
142 log('\nError in completion: %s\n' % e)
146 bup ftp [commands...]
148 o = options.Options('bup ftp', optspec)
149 (opt, flags, extra) = o.parse(sys.argv[1:])
151 git.check_repo_or_die()
153 top = vfs.RefList(None)
163 log('* readline module not available: line editing disabled.\n')
167 readline.set_completer_delims(' \t\n\r/')
168 readline.set_completer(completer)
169 readline.parse_and_bind("tab: complete")
176 words = [word for (wordstart,word) in shquote.quotesplit(line)]
177 cmd = words[0].lower()
178 #log('execute: %r %r\n' % (cmd, parm))
184 for parm in words[1:]:
185 np = np.resolve(parm)
186 if not stat.S_ISDIR(np.mode):
187 raise vfs.NotDir('%s is not a directory' % parm)
192 for parm in words[1:]:
193 write_to_file(pwd.resolve(parm).open(), sys.stdout)
195 if len(words) not in [2,3]:
197 raise Exception('Usage: get <filename> [localname]')
199 (dir,base) = os.path.split(rname)
200 lname = len(words)>2 and words[2] or base
201 inf = pwd.resolve(rname).open()
202 log('Saving %r\n' % lname)
203 write_to_file(inf, open(lname, 'wb'))
205 for parm in words[1:]:
206 (dir,base) = os.path.split(parm)
207 for n in pwd.resolve(dir).subs():
208 if fnmatch.fnmatch(n.name, base):
210 log('Saving %r\n' % n.name)
212 outf = open(n.name, 'wb')
213 write_to_file(inf, outf)
217 log(' error: %s\n' % e)
218 elif cmd == 'help' or cmd == '?':
219 log('Commands: ls cd pwd cat get mget help quit\n')
220 elif cmd == 'quit' or cmd == 'exit' or cmd == 'bye':
224 raise Exception('no such command %r' % cmd)
227 log('error: %s\n' % e)