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