]> arthur.barton.de Git - bup.git/commitdiff
Port ftp to vfs2 and add test-ftp
authorRob Browning <rlb@defaultvalue.org>
Sun, 26 Nov 2017 22:08:19 +0000 (16:08 -0600)
committerRob Browning <rlb@defaultvalue.org>
Sun, 24 Dec 2017 20:30:05 +0000 (14:30 -0600)
Just test for the existing behavior for now.

Signed-off-by: Rob Browning <rlb@defaultvalue.org>
Tested-by: Rob Browning <rlb@defaultvalue.org>
Makefile
cmd/ftp-cmd.py
t/test-ftp [new file with mode: 0755]

index 66475695e97fb6d1929df2a52401a8f6297700f1..1059304c8317ec1a60822505e986c9a1ede54016 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -148,6 +148,7 @@ runtests-python: all t/tmp
            | tee -a t/tmp/test-log/$$$$.log
 
 cmdline_tests := \
+  t/test-ftp \
   t/test-save-restore \
   t/test-packsizelimit \
   t/test-prune-older \
index 524d5b0de47e55066be5a59d95cdf9fdd7073137..5aced05f47b4a0e0552042af83bbaaa3729a6eec 100755 (executable)
@@ -7,9 +7,9 @@ exec "$bup_python" "$0" ${1+"$@"}
 
 import sys, os, stat, fnmatch
 
-from bup import options, git, shquote, vfs, ls
+from bup import options, git, shquote, ls, vfs2 as vfs
 from bup.helpers import chunkyreader, handle_ctrl_c, log
-
+from bup.repo import LocalRepo
 
 handle_ctrl_c()
 
@@ -18,12 +18,13 @@ class OptionError(Exception):
     pass
 
 
-# Check out lib/bup/ls.py for the opt spec
-def do_ls(cmd_args):
+def do_ls(repo, args):
     try:
-        ls.do_ls(cmd_args, pwd, onabort=OptionError)
+        opt = ls.opts_from_cmdline(args, onabort=OptionError)
     except OptionError as e:
+        log('error: %s' % e)
         return
+    return ls.within_repo(repo, opt)
 
 
 def write_to_file(inf, outf):
@@ -44,17 +45,18 @@ def inputiter():
             yield line
 
 
-def _completer_get_subs(line):
+def _completer_get_subs(repo, line):
     (qtype, lastword) = shquote.unfinished_word(line)
     (dir,name) = os.path.split(lastword)
-    #log('\ncompleter: %r %r %r\n' % (qtype, lastword, text))
-    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)
+    dir_path = vfs.resolve(repo, dir or '/')
+    _, dir_item = dir_path[-1]
+    if not dir_item:
+        subs = tuple()
+    else:
+        subs = tuple(dir_path + (entry,)
+                     for entry in vfs.contents(repo, dir_item)
+                     if (entry[0] != '.' and entry[0].startswith(name)))
+    return dir, name, qtype, lastword, subs
 
 
 def find_readline_lib():
@@ -95,7 +97,8 @@ def init_readline_vars():
 rl_completion_suppress_append = None
 _last_line = None
 _last_res = None
-def completer(text, state):
+def completer(text, iteration):
+    global repo
     global _last_line
     global _last_res
     global rl_completion_suppress_append
@@ -104,14 +107,16 @@ def completer(text, state):
     try:
         line = readline.get_line_buffer()[:readline.get_endidx()]
         if _last_line != line:
-            _last_res = _completer_get_subs(line)
+            _last_res = _completer_get_subs(repo, line)
             _last_line = line
         (dir, name, qtype, lastword, subs) = _last_res
-        if state < len(subs):
-            sn = subs[state]
-            sn1 = sn.try_resolve()  # find the type of any symlink target
-            fullname = os.path.join(dir, sn.name)
-            if stat.S_ISDIR(sn1.mode):
+        if iteration < len(subs):
+            path = subs[iteration]
+            leaf_name, leaf_item = path[-1]
+            res = vfs.try_resolve(repo, leaf_name, parent=path[:-1])
+            leaf_name, leaf_item = res[-1]
+            fullname = os.path.join(*(name for name, item in res))
+            if stat.S_ISDIR(vfs.item_mode(leaf_item)):
                 ret = shquote.what_to_add(qtype, lastword, fullname+'/',
                                           terminate=False)
             else:
@@ -136,8 +141,8 @@ o = options.Options(optspec)
 
 git.check_repo_or_die()
 
-top = vfs.RefList(None)
-pwd = top
+repo = LocalRepo()
+pwd = vfs.resolve(repo, '/')
 rv = 0
 
 if extra:
@@ -167,19 +172,33 @@ for line in lines:
     #log('execute: %r %r\n' % (cmd, parm))
     try:
         if cmd == 'ls':
-            do_ls(words[1:])
+            # FIXME: respect pwd (perhaps via ls accepting resolve path/parent)
+            do_ls(repo, words[1:])
         elif cmd == 'cd':
             np = pwd
             for parm in words[1:]:
-                np = np.resolve(parm)
-                if not stat.S_ISDIR(np.mode):
-                    raise vfs.NotDir('%s is not a directory' % parm)
+                res = vfs.resolve(repo, parm, parent=np)
+                _, leaf_item = res[-1]
+                if not leaf_item:
+                    raise Exception('%r does not exist'
+                                    % '/'.join(name for name, item in res))
+                if not stat.S_ISDIR(vfs.item_mode(leaf_item)):
+                    raise Exception('%r is not a directory' % parm)
+                np = res
             pwd = np
         elif cmd == 'pwd':
-            print pwd.fullname()
+            if len(pwd) == 1:
+                sys.stdout.write('/')
+            print '/'.join(name for name, item in pwd)
         elif cmd == 'cat':
             for parm in words[1:]:
-                write_to_file(pwd.resolve(parm).open(), sys.stdout)
+                res = vfs.resolve(repo, parm, parent=pwd)
+                _, leaf_item = res[-1]
+                if not leaf_item:
+                    raise Exception('%r does not exist' %
+                                    '/'.join(name for name, item in res))
+                with vfs.fopen(repo, leaf_item) as srcfile:
+                    write_to_file(srcfile, sys.stdout)
         elif cmd == 'get':
             if len(words) not in [2,3]:
                 rv = 1
@@ -187,26 +206,43 @@ for line in lines:
             rname = words[1]
             (dir,base) = os.path.split(rname)
             lname = len(words)>2 and words[2] or base
-            inf = pwd.resolve(rname).open()
-            log('Saving %r\n' % lname)
-            write_to_file(inf, open(lname, 'wb'))
+            res = vfs.resolve(repo, rname, parent=pwd)
+            _, leaf_item = res[-1]
+            if not leaf_item:
+                raise Exception('%r does not exist' %
+                                '/'.join(name for name, item in res))
+            with vfs.fopen(repo, leaf_item) as srcfile:
+                with open(lname, 'wb') as destfile:
+                    log('Saving %r\n' % lname)
+                    write_to_file(srcfile, destfile)
         elif cmd == 'mget':
             for parm in words[1:]:
                 (dir,base) = os.path.split(parm)
-                for n in pwd.resolve(dir).subs():
-                    if fnmatch.fnmatch(n.name, base):
-                        try:
-                            log('Saving %r\n' % n.name)
-                            inf = n.open()
-                            outf = open(n.name, 'wb')
-                            write_to_file(inf, outf)
-                            outf.close()
-                        except Exception as e:
-                            rv = 1
-                            log('  error: %s\n' % e)
+
+                res = vfs.resolve(repo, dir, parent=pwd)
+                _, dir_item = res[-1]
+                if not dir_item:
+                    raise Exception('%r does not exist' % dir)
+                for name, item in vfs.contents(repo, dir_item):
+                    if name == '.':
+                        continue
+                    if fnmatch.fnmatch(name, base):
+                        if stat.S_ISLNK(vfs.item_mode(item)):
+                            deref = vfs.resolve(repo, name, parent=res)
+                            deref_name, deref_item = deref[-1]
+                            if not deref_item:
+                                raise Exception('%r does not exist' %
+                                                '/'.join(name for name, item
+                                                         in deref))
+                            item = deref_item
+                        with vfs.fopen(repo, item) as srcfile:
+                            with open(name, 'wb') as destfile:
+                                log('Saving %r\n' % name)
+                                write_to_file(srcfile, destfile)
         elif cmd == 'help' or cmd == '?':
+            # FIXME: move to stdout
             log('Commands: ls cd pwd cat get mget help quit\n')
-        elif cmd == 'quit' or cmd == 'exit' or cmd == 'bye':
+        elif cmd in ('quit', 'exit', 'bye'):
             break
         else:
             rv = 1
@@ -214,6 +250,6 @@ for line in lines:
     except Exception as e:
         rv = 1
         log('error: %s\n' % e)
-        #raise
+        raise
 
 sys.exit(rv)
diff --git a/t/test-ftp b/t/test-ftp
new file mode 100755 (executable)
index 0000000..62c81c1
--- /dev/null
@@ -0,0 +1,131 @@
+#!/bin/sh
+"""": # -*-python-*-
+bup_python="$(dirname "$0")/../cmd/bup-python" || exit $?
+exec "$bup_python" "$0" ${1+"$@"}
+"""
+# end of bup preamble
+
+from __future__ import print_function
+from os import environ, chdir, mkdir, symlink, unlink
+from os.path import abspath, dirname
+from time import localtime, strftime
+import os, sys
+
+script_home = abspath(dirname(sys.argv[0] or '.'))
+sys.path[:0] = [abspath(script_home + '/../lib'), abspath(script_home + '/..')]
+top = os.getcwd()
+bup_cmd = top + '/bup'
+
+from buptest import exc, exo, logcmd, test_tempdir
+from wvtest import wvfail, wvpass, wvpasseq, wvpassne, wvstart
+
+from bup.helpers import unlink as unlink_if_exists
+
+def bup(*args, **kwargs):
+    return exo((bup_cmd,) + args, **kwargs)[0]
+
+def bupc(*args, **kwargs):
+    return exc((bup_cmd,) + args, **kwargs)
+
+def jl(*lines):
+    return ''.join(line + '\n' for line in lines)
+
+environ['GIT_AUTHOR_NAME'] = 'bup test'
+environ['GIT_COMMITTER_NAME'] = 'bup test'
+environ['GIT_AUTHOR_EMAIL'] = 'bup@a425bc70a02811e49bdf73ee56450e6f'
+environ['GIT_COMMITTER_EMAIL'] = 'bup@a425bc70a02811e49bdf73ee56450e6f'
+
+with test_tempdir('ftp-') as tmpdir:
+    environ['BUP_DIR'] = tmpdir + '/repo'
+    environ['GIT_DIR'] = tmpdir + '/repo'
+
+    chdir(tmpdir)
+    mkdir('src')
+    chdir('src')
+    mkdir('dir')
+    with open('file-1', 'wb') as f:
+        print('excitement!', file=f)
+    with open('dir/file-2', 'wb') as f:
+        print('more excitement!', file=f)
+    symlink('file-1', 'file-symlink')
+    symlink('dir', 'dir-symlink')
+    symlink('not-there', 'bad-symlink')
+
+    chdir(tmpdir)    
+    bupc('init')
+    bupc('index', 'src')
+    bupc('save', '-n', 'src', '--strip', 'src')
+    save_utc = int(exo(('git', 'show', '-s', '--format=%at', 'src')).out.strip())
+    save_name = strftime('%Y-%m-%d-%H%M%S', localtime(save_utc))
+    
+    wvstart('help')
+    wvpasseq('Commands: ls cd pwd cat get mget help quit\n',
+             exo((bup_cmd, 'ftp'), input='help\n').err)
+
+    wvstart('pwd/cd')
+    wvpasseq('/\n', bup('ftp', input='pwd\n'))
+    wvpasseq('', bup('ftp', input='cd src\n'))
+    wvpasseq('/src\n', bup('ftp', input=jl('cd src', 'pwd')))
+    wvpasseq('/src\n/\n', bup('ftp', input=jl('cd src', 'pwd', 'cd ..', 'pwd')))
+    wvpasseq('/src\n/\n', bup('ftp', input=jl('cd src', 'pwd',
+                                              'cd ..', 'cd ..', 'pwd')))
+    wvpasseq('/src/latest/dir\n',
+             bup('ftp', input=jl('cd src/latest/dir-symlink', 'pwd')))
+    wvpasseq('/src/latest/dir\n',
+             bup('ftp', input=jl('cd src latest dir-symlink', 'pwd')))
+    wvpassne(0, bupc('ftp',
+                     input=jl('cd src/latest/bad-symlink', 'pwd'),
+                     check=False).rc)
+    wvpassne(0, bupc('ftp',
+                     input=jl('cd src/latest/not-there', 'pwd'),
+                     check=False).rc)
+
+    wvstart('ls')
+    # FIXME: elaborate
+    wvpasseq('src\n', bup('ftp', input='ls\n'))
+    wvpasseq(save_name + '\nlatest\n',
+             bup('ftp', input='ls src\n'))
+
+    wvstart('cat')
+    wvpasseq('excitement!\n',
+             bup('ftp', input='cat src/latest/file-1\n'))
+    wvpasseq('excitement!\nmore excitement!\n',
+             bup('ftp', input='cat src/latest/file-1 src/latest/dir/file-2\n'))
+    
+    wvstart('get')
+    bupc('ftp', input=jl('get src/latest/file-1 dest'))
+    with open('dest', 'rb') as f:
+        wvpasseq('excitement!\n', f.read())
+    unlink('dest')
+    bupc('ftp', input=jl('get src/latest/file-symlink dest'))
+    with open('dest', 'rb') as f:
+        wvpasseq('excitement!\n', f.read())
+    unlink('dest')
+    wvpassne(0, bupc('ftp',
+                     input=jl('get src/latest/bad-symlink dest'),
+                     check=False).rc)
+    wvpassne(0, bupc('ftp',
+                     input=jl('get src/latest/not-there'),
+                     check=False).rc)
+    
+    wvstart('mget')
+    unlink_if_exists('file-1')
+    bupc('ftp', input=jl('mget src/latest/file-1'))
+    with open('file-1', 'rb') as f:
+        wvpasseq('excitement!\n', f.read())
+    unlink_if_exists('file-1')
+    unlink_if_exists('file-2')
+    bupc('ftp', input=jl('mget src/latest/file-1 src/latest/dir/file-2'))
+    with open('file-1', 'rb') as f:
+        wvpasseq('excitement!\n', f.read())
+    with open('file-2', 'rb') as f:
+        wvpasseq('more excitement!\n', f.read())
+    unlink_if_exists('file-symlink')
+    bupc('ftp', input=jl('mget src/latest/file-symlink'))
+    with open('file-symlink', 'rb') as f:
+        wvpasseq('excitement!\n', f.read())
+    wvpassne(0, bupc('ftp',
+                     input=jl('mget src/latest/bad-symlink dest'),
+                     check=False).rc)
+    # bup mget currently always does pattern matching
+    bupc('ftp', input='mget src/latest/not-there\n')