]> arthur.barton.de Git - bup.git/blob - cmd/ftp-cmd.py
cmd/ftp: Hide .dotfiles by default (-a shows them)
[bup.git] / cmd / ftp-cmd.py
1 #!/usr/bin/env python
2 import sys, os, re, stat, fnmatch
3 from bup import options, git, shquote, vfs
4 from bup.helpers import *
5
6 def node_name(text, n):
7     if stat.S_ISDIR(n.mode):
8         return '%s/' % text
9     elif stat.S_ISLNK(n.mode):
10         return '%s@' % text
11     else:
12         return '%s' % text
13
14
15 class OptionError(Exception):
16     pass
17
18
19 ls_optspec = """
20 ls [-a] [path...]
21 --
22 a,all   include hidden files in the listing
23 """
24 ls_opt = options.Options('ls', ls_optspec, onabort=OptionError)
25
26 def do_ls(cmd_args):
27     try:
28         (opt, flags, extra) = ls_opt.parse(cmd_args)
29     except OptionError, e:
30         return
31
32     L = []
33
34     for path in (extra or ['.']):
35         n = pwd.try_resolve(path)
36
37         if stat.S_ISDIR(n.mode):
38             for sub in n:
39                 name = sub.name
40                 if opt.all or not len(name)>1 or not name.startswith('.'):
41                     L.append(node_name(name, sub))
42         else:
43             L.append(node_name(path, n))
44         print columnate(L, '')
45
46
47 def write_to_file(inf, outf):
48     for blob in chunkyreader(inf):
49         outf.write(blob)
50
51
52 def inputiter():
53     if os.isatty(sys.stdin.fileno()):
54         while 1:
55             try:
56                 yield raw_input('bup> ')
57             except EOFError:
58                 break
59     else:
60         for line in sys.stdin:
61             yield line
62
63
64 def _completer_get_subs(line):
65     (qtype, lastword) = shquote.unfinished_word(line)
66     (dir,name) = os.path.split(lastword)
67     #log('\ncompleter: %r %r %r\n' % (qtype, lastword, text))
68     try:
69         n = pwd.resolve(dir)
70         subs = list(filter(lambda x: x.name.startswith(name),
71                            n.subs()))
72     except vfs.NoSuchFile, e:
73         subs = []
74     return (dir, name, qtype, lastword, subs)
75
76
77 def find_readline_lib():
78     """Return the name (and possibly the full path) of the readline library
79     linked to the given readline module.
80     """
81     import readline
82     f = open(readline.__file__, "rb")
83     try:
84         data = f.read()
85     finally:
86         f.close()
87     import re
88     m = re.search('\0([^\0]*libreadline[^\0]*)\0', data)
89     if m:
90         return m.group(1)
91     return None
92
93
94 def init_readline_vars():
95     """Work around trailing space automatically inserted by readline.
96     See http://bugs.python.org/issue5833"""
97     import ctypes
98     lib_name = find_readline_lib()
99     if lib_name is not None:
100         lib = ctypes.cdll.LoadLibrary(lib_name)
101         global rl_completion_suppress_append
102         rl_completion_suppress_append = ctypes.c_int.in_dll(lib,
103                                     "rl_completion_suppress_append")
104
105
106 rl_completion_suppress_append = None
107 _last_line = None
108 _last_res = None
109 def completer(text, state):
110     global _last_line
111     global _last_res
112     global rl_completion_suppress_append
113     if rl_completion_suppress_append is not None:
114         rl_completion_suppress_append.value = 1
115     try:
116         line = readline.get_line_buffer()[:readline.get_endidx()]
117         if _last_line != line:
118             _last_res = _completer_get_subs(line)
119             _last_line = line
120         (dir, name, qtype, lastword, subs) = _last_res
121         if state < len(subs):
122             sn = subs[state]
123             sn1 = sn.try_resolve()  # find the type of any symlink target
124             fullname = os.path.join(dir, sn.name)
125             if stat.S_ISDIR(sn1.mode):
126                 ret = shquote.what_to_add(qtype, lastword, fullname+'/',
127                                           terminate=False)
128             else:
129                 ret = shquote.what_to_add(qtype, lastword, fullname,
130                                           terminate=True) + ' '
131             return text + ret
132     except Exception, e:
133         log('\n')
134         try:
135             import traceback
136             traceback.print_tb(sys.exc_traceback)
137         except Exception, e2:
138             log('Error printing traceback: %s\n' % e2)
139         log('\nError in completion: %s\n' % e)
140
141
142 optspec = """
143 bup ftp [commands...]
144 """
145 o = options.Options('bup ftp', optspec)
146 (opt, flags, extra) = o.parse(sys.argv[1:])
147
148 git.check_repo_or_die()
149
150 top = vfs.RefList(None)
151 pwd = top
152 rv = 0
153
154 if extra:
155     lines = extra
156 else:
157     try:
158         import readline
159     except ImportError:
160         log('* readline module not available: line editing disabled.\n')
161         readline = None
162
163     if readline:
164         readline.set_completer_delims(' \t\n\r/')
165         readline.set_completer(completer)
166         readline.parse_and_bind("tab: complete")
167         init_readline_vars()
168     lines = inputiter()
169
170 for line in lines:
171     if not line.strip():
172         continue
173     words = [word for (wordstart,word) in shquote.quotesplit(line)]
174     cmd = words[0].lower()
175     #log('execute: %r %r\n' % (cmd, parm))
176     try:
177         if cmd == 'ls':
178             do_ls(words[1:])
179         elif cmd == 'cd':
180             np = pwd
181             for parm in words[1:]:
182                 np = np.resolve(parm)
183                 if not stat.S_ISDIR(np.mode):
184                     raise vfs.NotDir('%s is not a directory' % parm)
185             pwd = np
186         elif cmd == 'pwd':
187             print pwd.fullname()
188         elif cmd == 'cat':
189             for parm in words[1:]:
190                 write_to_file(pwd.resolve(parm).open(), sys.stdout)
191         elif cmd == 'get':
192             if len(words) not in [2,3]:
193                 rv = 1
194                 raise Exception('Usage: get <filename> [localname]')
195             rname = words[1]
196             (dir,base) = os.path.split(rname)
197             lname = len(words)>2 and words[2] or base
198             inf = pwd.resolve(rname).open()
199             log('Saving %r\n' % lname)
200             write_to_file(inf, open(lname, 'wb'))
201         elif cmd == 'mget':
202             for parm in words[1:]:
203                 (dir,base) = os.path.split(parm)
204                 for n in pwd.resolve(dir).subs():
205                     if fnmatch.fnmatch(n.name, base):
206                         try:
207                             log('Saving %r\n' % n.name)
208                             inf = n.open()
209                             outf = open(n.name, 'wb')
210                             write_to_file(inf, outf)
211                             outf.close()
212                         except Exception, e:
213                             rv = 1
214                             log('  error: %s\n' % e)
215         elif cmd == 'help' or cmd == '?':
216             log('Commands: ls cd pwd cat get mget help quit\n')
217         elif cmd == 'quit' or cmd == 'exit' or cmd == 'bye':
218             break
219         else:
220             rv = 1
221             raise Exception('no such command %r' % cmd)
222     except Exception, e:
223         rv = 1
224         log('error: %s\n' % e)
225         #raise
226
227 sys.exit(rv)