]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/t/tmetadata.py
Replace remaining print statements with print function
[bup.git] / lib / bup / t / tmetadata.py
index 4aa87cc1b17f8ff6c9eeebe6cc35b361f3708923..6b0b1e48bc6355b11236abe4895bed6695ce46c0 100644 (file)
@@ -1,9 +1,16 @@
+
+from __future__ import absolute_import, print_function
 import errno, glob, grp, pwd, stat, tempfile, subprocess
-import bup.helpers as helpers
-from bup import git, metadata, vfs
-from bup.helpers import clear_errors, detect_fakeroot, is_superuser, realpath
+
 from wvtest import *
+
+from bup import git, metadata
+from bup import vfs
+from bup.helpers import clear_errors, detect_fakeroot, is_superuser, resolve_parent
+from bup.repo import LocalRepo
 from bup.xstat import utime, lutime
+from buptest import no_lingering_errors, test_tempdir
+import bup.helpers as helpers
 
 
 top_dir = '../../..'
@@ -15,16 +22,16 @@ start_dir = os.getcwd()
 def ex(*cmd):
     try:
         cmd_str = ' '.join(cmd)
-        print >> sys.stderr, cmd_str
+        print(cmd_str, file=sys.stderr)
         rc = subprocess.call(cmd)
         if rc < 0:
-            print >> sys.stderr, 'terminated by signal', - rc
+            print('terminated by signal', - rc, file=sys.stderr)
             sys.exit(1)
         elif rc > 0:
-            print >> sys.stderr, 'returned exit status', rc
+            print('returned exit status', rc, file=sys.stderr)
             sys.exit(1)
     except OSError as e:
-        print >> sys.stderr, 'subprocess call failed:', e
+        print('subprocess call failed:', e, file=sys.stderr)
         sys.exit(1)
 
 
@@ -52,104 +59,111 @@ def cleanup_testfs():
 
 @wvtest
 def test_clean_up_archive_path():
-    cleanup = metadata._clean_up_path_for_archive
-    WVPASSEQ(cleanup('foo'), 'foo')
-    WVPASSEQ(cleanup('/foo'), 'foo')
-    WVPASSEQ(cleanup('///foo'), 'foo')
-    WVPASSEQ(cleanup('/foo/bar'), 'foo/bar')
-    WVPASSEQ(cleanup('foo/./bar'), 'foo/bar')
-    WVPASSEQ(cleanup('/foo/./bar'), 'foo/bar')
-    WVPASSEQ(cleanup('/foo/./bar/././baz'), 'foo/bar/baz')
-    WVPASSEQ(cleanup('/foo/./bar///././baz'), 'foo/bar/baz')
-    WVPASSEQ(cleanup('//./foo/./bar///././baz/.///'), 'foo/bar/baz/')
-    WVPASSEQ(cleanup('./foo/./.bar'), 'foo/.bar')
-    WVPASSEQ(cleanup('./foo/.'), 'foo')
-    WVPASSEQ(cleanup('./foo/..'), '.')
-    WVPASSEQ(cleanup('//./..//.../..//.'), '.')
-    WVPASSEQ(cleanup('//./..//..././/.'), '...')
-    WVPASSEQ(cleanup('/////.'), '.')
-    WVPASSEQ(cleanup('/../'), '.')
-    WVPASSEQ(cleanup(''), '.')
+    with no_lingering_errors():
+        cleanup = metadata._clean_up_path_for_archive
+        WVPASSEQ(cleanup('foo'), 'foo')
+        WVPASSEQ(cleanup('/foo'), 'foo')
+        WVPASSEQ(cleanup('///foo'), 'foo')
+        WVPASSEQ(cleanup('/foo/bar'), 'foo/bar')
+        WVPASSEQ(cleanup('foo/./bar'), 'foo/bar')
+        WVPASSEQ(cleanup('/foo/./bar'), 'foo/bar')
+        WVPASSEQ(cleanup('/foo/./bar/././baz'), 'foo/bar/baz')
+        WVPASSEQ(cleanup('/foo/./bar///././baz'), 'foo/bar/baz')
+        WVPASSEQ(cleanup('//./foo/./bar///././baz/.///'), 'foo/bar/baz/')
+        WVPASSEQ(cleanup('./foo/./.bar'), 'foo/.bar')
+        WVPASSEQ(cleanup('./foo/.'), 'foo')
+        WVPASSEQ(cleanup('./foo/..'), '.')
+        WVPASSEQ(cleanup('//./..//.../..//.'), '.')
+        WVPASSEQ(cleanup('//./..//..././/.'), '...')
+        WVPASSEQ(cleanup('/////.'), '.')
+        WVPASSEQ(cleanup('/../'), '.')
+        WVPASSEQ(cleanup(''), '.')
 
 
 @wvtest
 def test_risky_path():
-    risky = metadata._risky_path
-    WVPASS(risky('/foo'))
-    WVPASS(risky('///foo'))
-    WVPASS(risky('/../foo'))
-    WVPASS(risky('../foo'))
-    WVPASS(risky('foo/..'))
-    WVPASS(risky('foo/../'))
-    WVPASS(risky('foo/../bar'))
-    WVFAIL(risky('foo'))
-    WVFAIL(risky('foo/'))
-    WVFAIL(risky('foo///'))
-    WVFAIL(risky('./foo'))
-    WVFAIL(risky('foo/.'))
-    WVFAIL(risky('./foo/.'))
-    WVFAIL(risky('foo/bar'))
-    WVFAIL(risky('foo/./bar'))
+    with no_lingering_errors():
+        risky = metadata._risky_path
+        WVPASS(risky('/foo'))
+        WVPASS(risky('///foo'))
+        WVPASS(risky('/../foo'))
+        WVPASS(risky('../foo'))
+        WVPASS(risky('foo/..'))
+        WVPASS(risky('foo/../'))
+        WVPASS(risky('foo/../bar'))
+        WVFAIL(risky('foo'))
+        WVFAIL(risky('foo/'))
+        WVFAIL(risky('foo///'))
+        WVFAIL(risky('./foo'))
+        WVFAIL(risky('foo/.'))
+        WVFAIL(risky('./foo/.'))
+        WVFAIL(risky('foo/bar'))
+        WVFAIL(risky('foo/./bar'))
 
 
 @wvtest
 def test_clean_up_extract_path():
-    cleanup = metadata._clean_up_extract_path
-    WVPASSEQ(cleanup('/foo'), 'foo')
-    WVPASSEQ(cleanup('///foo'), 'foo')
-    WVFAIL(cleanup('/../foo'))
-    WVFAIL(cleanup('../foo'))
-    WVFAIL(cleanup('foo/..'))
-    WVFAIL(cleanup('foo/../'))
-    WVFAIL(cleanup('foo/../bar'))
-    WVPASSEQ(cleanup('foo'), 'foo')
-    WVPASSEQ(cleanup('foo/'), 'foo/')
-    WVPASSEQ(cleanup('foo///'), 'foo///')
-    WVPASSEQ(cleanup('./foo'), './foo')
-    WVPASSEQ(cleanup('foo/.'), 'foo/.')
-    WVPASSEQ(cleanup('./foo/.'), './foo/.')
-    WVPASSEQ(cleanup('foo/bar'), 'foo/bar')
-    WVPASSEQ(cleanup('foo/./bar'), 'foo/./bar')
-    WVPASSEQ(cleanup('/'), '.')
-    WVPASSEQ(cleanup('./'), './')
-    WVPASSEQ(cleanup('///foo/bar'), 'foo/bar')
-    WVPASSEQ(cleanup('///foo/bar'), 'foo/bar')
+    with no_lingering_errors():
+        cleanup = metadata._clean_up_extract_path
+        WVPASSEQ(cleanup('/foo'), 'foo')
+        WVPASSEQ(cleanup('///foo'), 'foo')
+        WVFAIL(cleanup('/../foo'))
+        WVFAIL(cleanup('../foo'))
+        WVFAIL(cleanup('foo/..'))
+        WVFAIL(cleanup('foo/../'))
+        WVFAIL(cleanup('foo/../bar'))
+        WVPASSEQ(cleanup('foo'), 'foo')
+        WVPASSEQ(cleanup('foo/'), 'foo/')
+        WVPASSEQ(cleanup('foo///'), 'foo///')
+        WVPASSEQ(cleanup('./foo'), './foo')
+        WVPASSEQ(cleanup('foo/.'), 'foo/.')
+        WVPASSEQ(cleanup('./foo/.'), './foo/.')
+        WVPASSEQ(cleanup('foo/bar'), 'foo/bar')
+        WVPASSEQ(cleanup('foo/./bar'), 'foo/./bar')
+        WVPASSEQ(cleanup('/'), '.')
+        WVPASSEQ(cleanup('./'), './')
+        WVPASSEQ(cleanup('///foo/bar'), 'foo/bar')
+        WVPASSEQ(cleanup('///foo/bar'), 'foo/bar')
 
 
 @wvtest
 def test_metadata_method():
-    initial_failures = wvfailure_count()
-    tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tmetadata-')
-    bup_dir = tmpdir + '/bup'
-    data_path = tmpdir + '/foo'
-    os.mkdir(data_path)
-    ex('touch', data_path + '/file')
-    ex('ln', '-s', 'file', data_path + '/symlink')
-    test_time1 = 13 * 1000000000
-    test_time2 = 42 * 1000000000
-    utime(data_path + '/file', (0, test_time1))
-    lutime(data_path + '/symlink', (0, 0))
-    utime(data_path, (0, test_time2))
-    ex(bup_path, '-d', bup_dir, 'init')
-    ex(bup_path, '-d', bup_dir, 'index', '-v', data_path)
-    ex(bup_path, '-d', bup_dir, 'save', '-tvvn', 'test', data_path)
-    git.check_repo_or_die(bup_dir)
-    top = vfs.RefList(None)
-    n = top.lresolve('/test/latest' + realpath(data_path))
-    m = n.metadata()
-    WVPASS(m.mtime == test_time2)
-    WVPASS(len(n.subs()) == 2)
-    WVPASS(n.name == 'foo')
-    WVPASS(set([x.name for x in n.subs()]) == set(['file', 'symlink']))
-    for sub in n:
-        if sub.name == 'file':
-            m = sub.metadata()
-            WVPASS(m.mtime == test_time1)
-        elif sub.name == 'symlink':
-            m = sub.metadata()
-            WVPASS(m.mtime == 0)
-    if wvfailure_count() == initial_failures:
-        subprocess.call(['rm', '-rf', tmpdir])
+    with no_lingering_errors():
+        with test_tempdir('bup-tmetadata-') as tmpdir:
+            bup_dir = tmpdir + '/bup'
+            data_path = tmpdir + '/foo'
+            os.mkdir(data_path)
+            ex('touch', data_path + '/file')
+            ex('ln', '-s', 'file', data_path + '/symlink')
+            test_time1 = 13 * 1000000000
+            test_time2 = 42 * 1000000000
+            utime(data_path + '/file', (0, test_time1))
+            lutime(data_path + '/symlink', (0, 0))
+            utime(data_path, (0, test_time2))
+            ex(bup_path, '-d', bup_dir, 'init')
+            ex(bup_path, '-d', bup_dir, 'index', '-v', data_path)
+            ex(bup_path, '-d', bup_dir, 'save', '-tvvn', 'test', data_path)
+            git.check_repo_or_die(bup_dir)
+            repo = LocalRepo()
+            resolved = vfs.lresolve(repo,
+                                    '/test/latest' + resolve_parent(data_path))
+            leaf_name, leaf_item = resolved[-1]
+            m = leaf_item.meta
+            WVPASS(m.mtime == test_time2)
+            WVPASS(leaf_name == 'foo')
+            contents = tuple(vfs.contents(repo, leaf_item))
+            WVPASS(len(contents) == 3)
+            WVPASSEQ(frozenset(name for name, item in contents),
+                     frozenset(('.', 'file', 'symlink')))
+            for name, item in contents:
+                if name == 'file':
+                    m = item.meta
+                    WVPASS(m.mtime == test_time1)
+                elif name == 'symlink':
+                    m = item.meta
+                    WVPASSEQ(m.symlink_target, 'file')
+                    WVPASSEQ(m.size, 4)
+                    WVPASSEQ(m.mtime, 0)
 
 
 def _first_err():
@@ -160,25 +174,22 @@ def _first_err():
 
 @wvtest
 def test_from_path_error():
-    initial_failures = wvfailure_count()
     if is_superuser() or detect_fakeroot():
         return
-    tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tmetadata-')
-    path = tmpdir + '/foo'
-    os.mkdir(path)
-    m = metadata.from_path(path, archive_path=path, save_symlinks=True)
-    WVPASSEQ(m.path, path)
-    os.chmod(path, 000)
-    metadata.from_path(path, archive_path=path, save_symlinks=True)
-    if metadata.get_linux_file_attr:
-        print >> sys.stderr, 'saved_errors:', helpers.saved_errors
-        WVPASS(len(helpers.saved_errors) == 1)
-        errmsg = _first_err()
-        WVPASS(errmsg.startswith('read Linux attr'))
-        clear_errors()
-    if wvfailure_count() == initial_failures:
-        subprocess.call(['chmod', '-R', 'u+rwX', tmpdir])
-        subprocess.call(['rm', '-rf', tmpdir])
+    with no_lingering_errors():
+        with test_tempdir('bup-tmetadata-') as tmpdir:
+            path = tmpdir + '/foo'
+            os.mkdir(path)
+            m = metadata.from_path(path, archive_path=path, save_symlinks=True)
+            WVPASSEQ(m.path, path)
+            os.chmod(path, 0o000)
+            metadata.from_path(path, archive_path=path, save_symlinks=True)
+            if metadata.get_linux_file_attr:
+                print('saved_errors:', helpers.saved_errors, file=sys.stderr)
+                WVPASS(len(helpers.saved_errors) == 1)
+                errmsg = _first_err()
+                WVPASS(errmsg.startswith('read Linux attr'))
+                clear_errors()
 
 
 def _linux_attr_supported(path):
@@ -197,69 +208,64 @@ def _linux_attr_supported(path):
 
 @wvtest
 def test_apply_to_path_restricted_access():
-    initial_failures = wvfailure_count()
     if is_superuser() or detect_fakeroot():
         return
     if sys.platform.startswith('cygwin'):
         return # chmod 000 isn't effective.
-    tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tmetadata-')
-    parent = tmpdir + '/foo'
-    path = parent + '/bar'
-    os.mkdir(parent)
-    os.mkdir(path)
-    clear_errors()
-    m = metadata.from_path(path, archive_path=path, save_symlinks=True)
-    WVPASSEQ(m.path, path)
-    os.chmod(parent, 000)
-    m.apply_to_path(path)
-    print >> sys.stderr, 'saved_errors:', helpers.saved_errors
-    expected_errors = ['utime: ']
-    if m.linux_attr and _linux_attr_supported(tmpdir):
-        expected_errors.append('Linux chattr: ')
-    if metadata.xattr and m.linux_xattr:
-        expected_errors.append("xattr.set '")
-    WVPASS(len(helpers.saved_errors) == len(expected_errors))
-    for i in xrange(len(expected_errors)):
-        WVPASS(str(helpers.saved_errors[i]).startswith(expected_errors[i]))
-    clear_errors()
-    if wvfailure_count() == initial_failures:
-        subprocess.call(['chmod', '-R', 'u+rwX', tmpdir])
-        subprocess.call(['rm', '-rf', tmpdir])
+    with no_lingering_errors():
+        with test_tempdir('bup-tmetadata-') as tmpdir:
+            parent = tmpdir + '/foo'
+            path = parent + '/bar'
+            os.mkdir(parent)
+            os.mkdir(path)
+            clear_errors()
+            m = metadata.from_path(path, archive_path=path, save_symlinks=True)
+            WVPASSEQ(m.path, path)
+            os.chmod(parent, 0o000)
+            m.apply_to_path(path)
+            print('saved_errors:', helpers.saved_errors, file=sys.stderr)
+            expected_errors = ['utime: ']
+            if m.linux_attr and _linux_attr_supported(tmpdir):
+                expected_errors.append('Linux chattr: ')
+            if metadata.xattr and m.linux_xattr:
+                expected_errors.append("xattr.set '")
+            WVPASS(len(helpers.saved_errors) == len(expected_errors))
+            for i in xrange(len(expected_errors)):
+                WVPASS(str(helpers.saved_errors[i]).startswith(expected_errors[i]))
+            clear_errors()
 
 
 @wvtest
 def test_restore_over_existing_target():
-    initial_failures = wvfailure_count()
-    tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tmetadata-')
-    path = tmpdir + '/foo'
-    os.mkdir(path)
-    dir_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
-    os.rmdir(path)
-    open(path, 'w').close()
-    file_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
-    # Restore dir over file.
-    WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
-    WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
-    # Restore dir over dir.
-    WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
-    WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
-    # Restore file over dir.
-    WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
-    WVPASS(stat.S_ISREG(os.stat(path).st_mode))
-    # Restore file over file.
-    WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
-    WVPASS(stat.S_ISREG(os.stat(path).st_mode))
-    # Restore file over non-empty dir.
-    os.remove(path)
-    os.mkdir(path)
-    open(path + '/bar', 'w').close()
-    WVEXCEPT(Exception, file_m.create_path, path, create_symlinks=True)
-    # Restore dir over non-empty dir.
-    os.remove(path + '/bar')
-    os.mkdir(path + '/bar')
-    WVEXCEPT(Exception, dir_m.create_path, path, create_symlinks=True)
-    if wvfailure_count() == initial_failures:
-        subprocess.call(['rm', '-rf', tmpdir])
+    with no_lingering_errors():
+        with test_tempdir('bup-tmetadata-') as tmpdir:
+            path = tmpdir + '/foo'
+            os.mkdir(path)
+            dir_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
+            os.rmdir(path)
+            open(path, 'w').close()
+            file_m = metadata.from_path(path, archive_path=path, save_symlinks=True)
+            # Restore dir over file.
+            WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
+            WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
+            # Restore dir over dir.
+            WVPASSEQ(dir_m.create_path(path, create_symlinks=True), None)
+            WVPASS(stat.S_ISDIR(os.stat(path).st_mode))
+            # Restore file over dir.
+            WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
+            WVPASS(stat.S_ISREG(os.stat(path).st_mode))
+            # Restore file over file.
+            WVPASSEQ(file_m.create_path(path, create_symlinks=True), None)
+            WVPASS(stat.S_ISREG(os.stat(path).st_mode))
+            # Restore file over non-empty dir.
+            os.remove(path)
+            os.mkdir(path)
+            open(path + '/bar', 'w').close()
+            WVEXCEPT(Exception, file_m.create_path, path, create_symlinks=True)
+            # Restore dir over non-empty dir.
+            os.remove(path + '/bar')
+            os.mkdir(path + '/bar')
+            WVEXCEPT(Exception, dir_m.create_path, path, create_symlinks=True)
 
 
 from bup.metadata import posix1e