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