2 import sys, os, re, stat, fnmatch
3 from bup import options, git, shquote, vfs
4 from bup.helpers import *
9 log('* readline module not available: line editing disabled.\n')
13 def node_name(text, n):
14 if stat.S_ISDIR(n.mode):
16 elif stat.S_ISLNK(n.mode):
24 if stat.S_ISDIR(n.mode):
26 l.append(node_name(sub.name, sub))
28 l.append(node_name(path, n))
29 print columnate(l, '')
32 def write_to_file(inf, outf):
33 for blob in chunkyreader(inf):
38 if os.isatty(sys.stdin.fileno()):
41 yield raw_input('bup> ')
45 for line in sys.stdin:
49 def _completer_get_subs(line):
50 (qtype, lastword) = shquote.unfinished_word(line)
51 (dir,name) = os.path.split(lastword)
52 #log('\ncompleter: %r %r %r\n' % (qtype, lastword, text))
54 subs = list(filter(lambda x: x.name.startswith(name),
56 return (dir, name, qtype, lastword, subs)
59 def find_readline_lib():
60 """Return the name (and possibly the full path) of the readline library
61 linked to the given readline module.
64 f = open(readline.__file__, "rb")
70 m = re.search('\0([^\0]*libreadline[^\0]*)\0', data)
76 def init_readline_vars():
77 """Work around trailing space automatically inserted by readline.
78 See http://bugs.python.org/issue5833"""
80 lib_name = find_readline_lib()
81 if lib_name is not None:
82 lib = ctypes.cdll.LoadLibrary(lib_name)
83 global rl_completion_suppress_append
84 rl_completion_suppress_append = ctypes.c_int.in_dll(lib,
85 "rl_completion_suppress_append")
88 rl_completion_suppress_append = None
91 def completer(text, state):
94 global rl_completion_suppress_append
95 if rl_completion_suppress_append is not None:
96 rl_completion_suppress_append.value = 1
98 line = readline.get_line_buffer()[:readline.get_endidx()]
99 if _last_line != line:
100 _last_res = _completer_get_subs(line)
102 (dir, name, qtype, lastword, subs) = _last_res
103 if state < len(subs):
105 sn1 = sn.resolve('') # deref symlinks
106 fullname = os.path.join(dir, sn.name)
107 if stat.S_ISDIR(sn1.mode):
108 ret = shquote.what_to_add(qtype, lastword, fullname+'/',
111 ret = shquote.what_to_add(qtype, lastword, fullname,
112 terminate=True) + ' '
115 log('\nerror in completion: %s\n' % e)
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)
134 readline.set_completer_delims(' \t\n\r/')
135 readline.set_completer(completer)
136 readline.parse_and_bind("tab: complete")
143 words = [word for (wordstart,word) in shquote.quotesplit(line)]
144 cmd = words[0].lower()
145 #log('execute: %r %r\n' % (cmd, parm))
148 for parm in (words[1:] or ['.']):
149 do_ls(parm, pwd.resolve(parm))
151 for parm in words[1:]:
152 pwd = pwd.resolve(parm)
156 for parm in words[1:]:
157 write_to_file(pwd.resolve(parm).open(), sys.stdout)
159 if len(words) not in [2,3]:
161 raise Exception('Usage: get <filename> [localname]')
163 (dir,base) = os.path.split(rname)
164 lname = len(words)>2 and words[2] or base
165 inf = pwd.resolve(rname).open()
166 log('Saving %r\n' % lname)
167 write_to_file(inf, open(lname, 'wb'))
169 for parm in words[1:]:
170 (dir,base) = os.path.split(parm)
171 for n in pwd.resolve(dir).subs():
172 if fnmatch.fnmatch(n.name, base):
174 log('Saving %r\n' % n.name)
176 outf = open(n.name, 'wb')
177 write_to_file(inf, outf)
181 log(' error: %s\n' % e)
182 elif cmd == 'help' or cmd == '?':
183 log('Commands: ls cd pwd cat get mget help quit\n')
184 elif cmd == 'quit' or cmd == 'exit' or cmd == 'bye':
188 raise Exception('no such command %r' % cmd)
191 log('error: %s\n' % e)