-#!/usr/bin/env python
-import sys, os, re, stat, fnmatch
-from bup import options, git, shquote, vfs
-from bup.helpers import *
-
-def node_name(text, n):
- if stat.S_ISDIR(n.mode):
- return '%s/' % text
- elif stat.S_ISLNK(n.mode):
- return '%s@' % text
- else:
- return '%s' % text
+#!/bin/sh
+"""": # -*-python-*-
+bup_python="$(dirname "$0")/bup-python" || exit $?
+exec "$bup_python" "$0" ${1+"$@"}
+"""
+# end of bup preamble
+import sys, os, stat, fnmatch
+
+from bup import options, git, shquote, vfs, ls
+from bup.helpers import chunkyreader, handle_ctrl_c, log
+
+
+handle_ctrl_c()
+
+
+class OptionError(Exception):
+ pass
+
+
+# Check out lib/bup/ls.py for the opt spec
+def do_ls(cmd_args):
+ try:
+ ls.do_ls(cmd_args, pwd, onabort=OptionError)
+ except OptionError as e:
+ return
-def do_ls(path, n):
- l = []
- if stat.S_ISDIR(n.mode):
- for sub in n:
- l.append(node_name(sub.name, sub))
- else:
- l.append(node_name(path, n))
- print columnate(l, '')
-
def write_to_file(inf, outf):
for blob in chunkyreader(inf):
outf.write(blob)
-
+
def inputiter():
if os.isatty(sys.stdin.fileno()):
try:
yield raw_input('bup> ')
except EOFError:
+ print '' # Clear the line for the terminal's next prompt
break
else:
for line in sys.stdin:
(qtype, lastword) = shquote.unfinished_word(line)
(dir,name) = os.path.split(lastword)
#log('\ncompleter: %r %r %r\n' % (qtype, lastword, text))
- n = pwd.resolve(dir)
- subs = list(filter(lambda x: x.name.startswith(name),
- n.subs()))
+ try:
+ n = pwd.resolve(dir)
+ subs = list(filter(lambda x: x.name.startswith(name),
+ n.subs()))
+ except vfs.NoSuchFile as e:
+ subs = []
return (dir, name, qtype, lastword, subs)
def init_readline_vars():
"""Work around trailing space automatically inserted by readline.
See http://bugs.python.org/issue5833"""
- import ctypes
+ try:
+ import ctypes
+ except ImportError:
+ # python before 2.5 didn't have the ctypes module; but those
+ # old systems probably also didn't have this readline bug, so
+ # just ignore it.
+ return
lib_name = find_readline_lib()
if lib_name is not None:
lib = ctypes.cdll.LoadLibrary(lib_name)
(dir, name, qtype, lastword, subs) = _last_res
if state < len(subs):
sn = subs[state]
- sn1 = sn.resolve('') # deref symlinks
+ sn1 = sn.try_resolve() # find the type of any symlink target
fullname = os.path.join(dir, sn.name)
if stat.S_ISDIR(sn1.mode):
ret = shquote.what_to_add(qtype, lastword, fullname+'/',
ret = shquote.what_to_add(qtype, lastword, fullname,
terminate=True) + ' '
return text + ret
- except Exception, e:
- log('\nerror in completion: %s\n' % e)
+ except Exception as e:
+ log('\n')
+ try:
+ import traceback
+ traceback.print_tb(sys.exc_traceback)
+ except Exception as e2:
+ log('Error printing traceback: %s\n' % e2)
+ log('\nError in completion: %s\n' % e)
optspec = """
bup ftp [commands...]
"""
-o = options.Options('bup ftp', optspec)
+o = options.Options(optspec)
(opt, flags, extra) = o.parse(sys.argv[1:])
git.check_repo_or_die()
if readline:
readline.set_completer_delims(' \t\n\r/')
readline.set_completer(completer)
- readline.parse_and_bind("tab: complete")
+ if sys.platform.startswith('darwin'):
+ # MacOS uses a slightly incompatible clone of libreadline
+ readline.parse_and_bind('bind ^I rl_complete')
+ readline.parse_and_bind('tab: complete')
init_readline_vars()
lines = inputiter()
#log('execute: %r %r\n' % (cmd, parm))
try:
if cmd == 'ls':
- for parm in (words[1:] or ['.']):
- do_ls(parm, pwd.resolve(parm))
+ do_ls(words[1:])
elif cmd == 'cd':
+ np = pwd
for parm in words[1:]:
- pwd = pwd.resolve(parm)
+ np = np.resolve(parm)
+ if not stat.S_ISDIR(np.mode):
+ raise vfs.NotDir('%s is not a directory' % parm)
+ pwd = np
elif cmd == 'pwd':
print pwd.fullname()
elif cmd == 'cat':
outf = open(n.name, 'wb')
write_to_file(inf, outf)
outf.close()
- except Exception, e:
+ except Exception as e:
rv = 1
log(' error: %s\n' % e)
elif cmd == 'help' or cmd == '?':
else:
rv = 1
raise Exception('no such command %r' % cmd)
- except Exception, e:
+ except Exception as e:
rv = 1
log('error: %s\n' % e)
#raise