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