]> arthur.barton.de Git - bup.git/blobdiff - cmd/ftp-cmd.py
tvfs: accommodate python 3 and test there
[bup.git] / cmd / ftp-cmd.py
index 8bd57b6d81accff181dde968af13f865f3979348..f1e48bdbe0a7a3fb11058ce25f7b6eede4e48574 100755 (executable)
@@ -5,10 +5,11 @@ exec "$bup_python" "$0" ${1+"$@"}
 """
 # end of bup preamble
 
-from __future__ import absolute_import
+from __future__ import absolute_import, print_function
 import sys, os, stat, fnmatch
 
 from bup import options, git, shquote, ls, vfs
+from bup.io import byte_stream
 from bup.helpers import chunkyreader, handle_ctrl_c, log
 from bup.repo import LocalRepo
 
@@ -19,13 +20,13 @@ class OptionError(Exception):
     pass
 
 
-def do_ls(repo, args):
+def do_ls(repo, args, out):
     try:
         opt = ls.opts_from_cmdline(args, onabort=OptionError)
     except OptionError as e:
         log('error: %s' % e)
         return
-    return ls.within_repo(repo, opt)
+    return ls.within_repo(repo, opt, out)
 
 
 def write_to_file(inf, outf):
@@ -39,7 +40,7 @@ def inputiter():
             try:
                 yield raw_input('bup> ')
             except EOFError:
-                print ''  # Clear the line for the terminal's next prompt
+                print()  # Clear the line for the terminal's next prompt
                 break
     else:
         for line in sys.stdin:
@@ -60,51 +61,12 @@ def _completer_get_subs(repo, line):
     return dir, name, qtype, lastword, subs
 
 
-def find_readline_lib():
-    """Return the name (and possibly the full path) of the readline library
-    linked to the given readline module.
-    """
-    import readline
-    f = open(readline.__file__, "rb")
-    try:
-        data = f.read()
-    finally:
-        f.close()
-    import re
-    m = re.search('\0([^\0]*libreadline[^\0]*)\0', data)
-    if m:
-        return m.group(1)
-    return None
-
-
-def init_readline_vars():
-    """Work around trailing space automatically inserted by readline.
-    See http://bugs.python.org/issue5833"""
-    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)
-        global rl_completion_suppress_append
-        rl_completion_suppress_append = ctypes.c_int.in_dll(lib,
-                                    "rl_completion_suppress_append")
-
-
-rl_completion_suppress_append = None
 _last_line = None
 _last_res = None
 def completer(text, iteration):
     global repo
     global _last_line
     global _last_res
-    global rl_completion_suppress_append
-    if rl_completion_suppress_append is not None:
-        rl_completion_suppress_append.value = 1
     try:
         line = readline.get_line_buffer()[:readline.get_endidx()]
         if _last_line != line:
@@ -142,6 +104,8 @@ o = options.Options(optspec)
 
 git.check_repo_or_die()
 
+sys.stdout.flush()
+out = byte_stream(sys.stdout)
 repo = LocalRepo()
 pwd = vfs.resolve(repo, '/')
 rv = 0
@@ -162,7 +126,6 @@ else:
             # 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()
 
 for line in lines:
@@ -174,7 +137,8 @@ for line in lines:
     try:
         if cmd == 'ls':
             # FIXME: respect pwd (perhaps via ls accepting resolve path/parent)
-            do_ls(repo, words[1:])
+            sys.stdout.flush()  # FIXME: remove when we finish py3 support
+            do_ls(repo, words[1:], out)
         elif cmd == 'cd':
             np = pwd
             for parm in words[1:]:
@@ -190,7 +154,7 @@ for line in lines:
         elif cmd == 'pwd':
             if len(pwd) == 1:
                 sys.stdout.write('/')
-            print '/'.join(name for name, item in pwd)
+            print('/'.join(name for name, item in pwd))
         elif cmd == 'cat':
             for parm in words[1:]:
                 res = vfs.resolve(repo, parm, parent=pwd)