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