Example #1
0
 def test_split_windows(self):
     path = URI(r'C:\some\path\on\windows', sep='\\')
     pth, tail = path.split()
     self.assertEqual(pth.uri, 'file:///C/some/path/on')
     self.assertEqual(pth.path, r'C:\some\path\on')
     self.assertEqual(pth, URI(r'C:\some\path\on', sep='\\'))
     self.assertEqual(tail, 'windows')
Example #2
0
class TestWritingZip(ZipTestCase):
    def setUp(self):
        super(TestWritingZip, self).setUp()
        self.zip_uri = 'file://./file.zip'
        self.zip_path = URI(self.zip_uri)


    def tearDown(self):
        # reset the connection registry, otherwise the zip file cannot
        # be deleted on windows since it is still opened by the
        # backend
        CONNECTION_REGISTRY.cleanup(force=True)
        if self.zip_path.exists():
            self.zip_path.remove()


    def test_write_file_to_non_existing_zip(self):
        foo = URI('zip://((%s))/foo.txt' % self.zip_uri)
        with foo.open('wb') as fd:
            fd.write(b'bar')


    def test_write_file_to_non_existing_zip_2(self):
        foo = URI('zip://((%s))/deeper/foo.txt' % self.zip_uri)
        with foo.open('wb') as fd:
            fd.write(b'bar')


    def test_write_two_files(self):
        foo = URI('zip://((%s))/foo.txt' % self.zip_uri)
        with foo.open('wb') as fd:
            fd.write(b'bar')
        bar = URI('zip://((%s))/bar.txt' % self.zip_uri)
        with bar.open('wb') as fd:
            fd.write(b'foo')
Example #3
0
 def test_dirname(self):
     pth = URI("/this/is/a/path")
     self.assertEqual(pth.dirname(), URI("/this/is/a"))
     self.assertEqual(pth.dirname(level=2), URI("/this/is"))
     self.assertEqual(pth.dirname(level=3), URI("/this"))
     self.assertEqual(pth.dirname(level=4), URI("/"))
     self.assertEqual(pth.dirname(level=5), URI("/"))
Example #4
0
class TestAdvancedZip(ZipTestCase):

    def setUp(self):
        super(TestAdvancedZip, self).setUp()
        self.zip_path = URI('memory:///file.zip')
        zip_handle = self.zip_path.open('wb')
        try:
            self.fp_zip = ZipFile(zip_handle, 'w')
            self.fp_zip.writestr('/dir1/foo.txt', 'bar')
            self.fp_zip.writestr('/dir1/bar.txt', 'bar')
            self.fp_zip.writestr('/bar.txt', 'bar')
            self.fp_zip.close()
        finally:
            zip_handle.close()

    def tearDown(self):
        self.zip_path.remove()


    def test_walk(self):
        root = URI('zip://((%s))/' % self.zip_path.uri)
        self.assertEqual(len(root.listdir()), 2)
        rlist = []
        for base, dirs, files in root.walk():
            rlist.append((base, dirs, files))
        self.assertEqual(rlist,
                         [(root, ['dir1'], ['bar.txt']),
                          ((root / 'dir1'), [], ['bar.txt', 'foo.txt'])])
Example #5
0
 def tearDown(self):
     p = URI(self.foo_dir)
     if p.isdir():
         p.remove(recursive=True)
     b = URI(self.bar_dir)
     if b.isdir():
         b.remove(recursive=True)
Example #6
0
 def tearDown(self):
     p = URI("test.txt")
     if p.exists():
         p.remove()
     l = URI("test_link")
     if l.islink():
         l.remove()
Example #7
0
 def test_dirname(self):
     pth = URI("/this/is/a/path")
     self.assertEqual(pth.dirname(), URI("/this/is/a"))
     self.assertEqual(pth.dirname(level=2), URI("/this/is"))
     self.assertEqual(pth.dirname(level=3), URI("/this"))
     self.assertEqual(pth.dirname(level=4), URI("/"))
     self.assertEqual(pth.dirname(level=5), URI("/"))
Example #8
0
class TestAdvancedZip(ZipTestCase):
    def setUp(self):
        super(TestAdvancedZip, self).setUp()
        self.zip_path = URI('memory:///file.zip')
        zip_handle = self.zip_path.open('wb')
        try:
            self.fp_zip = ZipFile(zip_handle, 'w')
            self.fp_zip.writestr('/dir1/foo.txt', 'bar')
            self.fp_zip.writestr('/dir1/bar.txt', 'bar')
            self.fp_zip.writestr('/bar.txt', 'bar')
            self.fp_zip.close()
        finally:
            zip_handle.close()

    def tearDown(self):
        self.zip_path.remove()

    def test_walk(self):
        root = URI('zip://((%s))/' % self.zip_path.uri)
        self.assertEqual(len(root.listdir()), 2)
        rlist = []
        for base, dirs, files in root.walk():
            rlist.append((base, dirs, files))
        self.assertEqual(rlist,
                         [(root, ['dir1'], ['bar.txt']),
                          ((root / 'dir1'), [], ['bar.txt', 'foo.txt'])])
Example #9
0
class TestWritingZip(ZipTestCase):
    def setUp(self):
        super(TestWritingZip, self).setUp()
        self.zip_uri = 'file://./file.zip'
        self.zip_path = URI(self.zip_uri)

    def tearDown(self):
        # reset the connection registry, otherwise the zip file cannot
        # be deleted on windows since it is still opened by the
        # backend
        CONNECTION_REGISTRY.cleanup(force=True)
        if self.zip_path.exists():
            self.zip_path.remove()

    def test_write_file_to_non_existing_zip(self):
        foo = URI('zip://((%s))/foo.txt' % self.zip_uri)
        with foo.open('wb') as fd:
            fd.write(b'bar')

    def test_write_file_to_non_existing_zip_2(self):
        foo = URI('zip://((%s))/deeper/foo.txt' % self.zip_uri)
        with foo.open('wb') as fd:
            fd.write(b'bar')

    def test_write_two_files(self):
        foo = URI('zip://((%s))/foo.txt' % self.zip_uri)
        with foo.open('wb') as fd:
            fd.write(b'bar')
        bar = URI('zip://((%s))/bar.txt' % self.zip_uri)
        with bar.open('wb') as fd:
            fd.write(b'foo')
Example #10
0
 def test_write_two_files(self):
     foo = URI('zip://((%s))/foo.txt' % self.zip_uri)
     with foo.open('wb') as fd:
         fd.write(b'bar')
     bar = URI('zip://((%s))/bar.txt' % self.zip_uri)
     with bar.open('wb') as fd:
         fd.write(b'foo')
Example #11
0
 def test_split_windows(self):
     path = URI(r'C:\some\path\on\windows', sep='\\')
     pth, tail = path.split()
     self.assertEqual(pth.uri, 'file:///C/some/path/on')
     self.assertEqual(pth.path, r'C:\some\path\on')
     self.assertEqual(pth, URI(r'C:\some\path\on', sep='\\'))
     self.assertEqual(tail, 'windows')
Example #12
0
    def test_dont_mix_unicode_and_bytes(self):
        p = URI("Vögel")
        p2 = p / "no_ünicode"
        self.assertTrue(type(p2.uri) is str)

        p3 = URI("Vögel")
        p4 = p3 / "ünicode"
        self.assertTrue(type(p4.uri) is str)
Example #13
0
 def test_walk(self):
     root = URI('zip://((%s))/' % self.zip_path.uri)
     self.assertEqual(len(root.listdir()), 2)
     rlist = []
     for base, dirs, files in root.walk():
         rlist.append((base, dirs, files))
     self.assertEqual(rlist,
                      [(root, ['dir1'], ['bar.txt']),
                       ((root / 'dir1'), [], ['bar.txt', 'foo.txt'])])
Example #14
0
 def test_walk(self):
     root = URI('zip://((%s))/' % self.zip_path.uri)
     self.assertEqual(len(root.listdir()), 2)
     rlist = []
     for base, dirs, files in root.walk():
         rlist.append((base, dirs, files))
     self.assertEqual(rlist,
                      [(root, ['dir1'], ['bar.txt']),
                       ((root / 'dir1'), [], ['bar.txt', 'foo.txt'])])
Example #15
0
 def setUp(self):
     super(TestReadingZip, self).setUp()
     self.zip_path = URI('memory:///file.zip')
     zip_handle = self.zip_path.open('wb')
     try:
         self.fp_zip = ZipFile(zip_handle, 'w')
         self.fp_zip.writestr('/foo.txt', 'bar')
         self.fp_zip.close()
     finally:
         zip_handle.close()
Example #16
0
    def test_globbing_by_suffix(self):
        base = URI("memory:///")
        a = base / "a.foo"
        b = base / "b.bar"

        for f in [a, b]:
            with f.open("w") as outf:
                outf.write("foo")

        self.assertEqual([a], base.glob("a.*"))
Example #17
0
    def test_globbing_by_suffix(self):
        base = URI("memory:///")
        a = base / "a.foo"
        b = base / "b.bar"

        for f in [a, b]:
            with f.open("w") as outf:
                outf.write("foo")

        self.assertEqual([a], base.glob("a.*"))
Example #18
0
    def test_creation(self):
        local_path = URI('/tmp/this')
        self.assertEqual(local_path.path.split(os.sep), ['', 'tmp', 'this'])
        self.assertEqual(local_path.scheme, 'file')

        path = URI('localpath', sep='/')
        self.assertEqual(path.path, './localpath', path.path)

        path = URI('trailing/slash/', sep='/')
        self.assertEqual(path.path, './trailing/slash/')
Example #19
0
    def test_split(self):
        local_path = URI('/some/long/dir/structure')
        pth, tail = local_path.split()
        self.assertEqual(pth, URI('/some/long/dir'))
        self.assertEqual(tail, 'structure')

        local_path = URI('somedir')
        pth, tail = local_path.split()
        self.assertEqual(pth, URI('.'))
        self.assertEqual(tail, 'somedir')
Example #20
0
    def test_globbing_by_prefix_in_subdir(self):
        base = URI("memory:///") / "dir"
        base.mkdir()
        a = base / "a.foo"
        b = base / "b.bar"

        for f in [a, b]:
            with f.open("w") as outf:
                outf.write("foo")

        self.assertEqual([a], base.glob("*.foo"))
Example #21
0
    def test_globbing_by_prefix_in_subdir(self):
        base = URI("memory:///") / "dir"
        base.mkdir()
        a = base / "a.foo"
        b = base / "b.bar"

        for f in [a, b]:
            with f.open("w") as outf:
                outf.write("foo")

        self.assertEqual([a], base.glob("*.foo"))
Example #22
0
 def test_setting_mode(self):
     # setting the permission flags are not supported on windows
     if sys.platform != "win32":
         p = URI("test.txt")
         mode = p.info().mode
         new_mode = mode | stat.S_IXUSR
         p.info(dict(mode=new_mode))
         self.assertEqual(
             p.info().mode,
             new_mode,
             )
Example #23
0
class TestReadingZip(ZipTestCase):

    def setUp(self):
        super(TestReadingZip, self).setUp()
        self.zip_path = URI('memory:///file.zip')
        zip_handle = self.zip_path.open('wb')
        try:
            self.fp_zip = ZipFile(zip_handle, 'w')
            self.fp_zip.writestr('/foo.txt', 'bar')
            self.fp_zip.close()
        finally:
            zip_handle.close()

    def tearDown(self):
        if self.zip_path.exists():
            self.zip_path.remove()


    def test_read_a_file(self):
        p = URI('zip://((memory:///file.zip))/foo.txt')
        with p.open('rb') as fd:
            self.assertEqual(fd.read(), b'bar')

    def test_write_a_file(self):
        p = URI('zip://((memory:///file.zip))/bar.txt')
        with p.open('wb') as fd:
            fd.write(b'foo')
        with p.open() as fd:
            self.assertEqual(fd.read(), b'foo')

    def test_exists(self):
        p = URI('zip://((memory:///file.zip))/foo.txt')
        with p.open('wb') as fd:
            fd.write(b'foo')
        self.assertTrue(p.exists())

    def test_isfile(self):
        p = URI('zip://((memory:///file.zip))/foo.txt')
        with p.open('wb') as fd:
            fd.write(b'foo')
        self.assertTrue(p.isfile())

    def test_isdir(self):
        dir_path = URI('zip://((memory:///file.zip))/somedir')
        p = dir_path / 'foo.txt'
        with p.open('wb') as fd:
            fd.write(b'foo')
        self.assertTrue(dir_path.isdir())

    def test_path(self):
        dir_path = URI('zip://((memory:///file.zip))/somedir')
        self.assertEqual(dir_path.path, '/somedir')
        new_path = dir_path / 'other'
        self.assertEqual(new_path.path, '/somedir/other')
Example #24
0
    def test_backend(self):
        foo_path = URI(self.baseurl) / 'foo'
        bar_path = URI(foo_path.path + '?arg1=value1')
        foo_2_path = foo_path / 'some_dir'
        self.assertTrue(
            foo_path.get_connection() is foo_2_path.get_connection())
        self.assertTrue(
            bar_path.get_connection() is not foo_path.get_connection())

        foo_path_connection = foo_path.get_connection()
        foo_path.query['arg'] = 'value'
        self.assertTrue(foo_path_connection is not foo_path.get_connection())
Example #25
0
    def test_copy_empty_dirs_recursive(self):
        root = URI(self.baseurl)
        root.makedirs()

        gaz_path = root / 'gaz'
        gaz_path.makedirs()

        moo_path = root / 'moo'

        gaz_path.copy(moo_path, recursive=True)

        self.assertTrue((moo_path).isdir())
Example #26
0
    def test_copy_empty_dirs_recursive(self):
        root = URI(self.baseurl)
        root.makedirs()

        gaz_path = root / 'gaz'
        gaz_path.makedirs()

        moo_path = root / 'moo'

        gaz_path.copy(moo_path, recursive=True)

        self.assertTrue((moo_path).isdir())
Example #27
0
class TestReadingZip(ZipTestCase):
    def setUp(self):
        super(TestReadingZip, self).setUp()
        self.zip_path = URI('memory:///file.zip')
        zip_handle = self.zip_path.open('wb')
        try:
            self.fp_zip = ZipFile(zip_handle, 'w')
            self.fp_zip.writestr('/foo.txt', 'bar')
            self.fp_zip.close()
        finally:
            zip_handle.close()

    def tearDown(self):
        if self.zip_path.exists():
            self.zip_path.remove()

    def test_read_a_file(self):
        p = URI('zip://((memory:///file.zip))/foo.txt')
        with p.open('rb') as fd:
            self.assertEqual(fd.read(), b'bar')

    def test_write_a_file(self):
        p = URI('zip://((memory:///file.zip))/bar.txt')
        with p.open('wb') as fd:
            fd.write(b'foo')
        with p.open() as fd:
            self.assertEqual(fd.read(), b'foo')

    def test_exists(self):
        p = URI('zip://((memory:///file.zip))/foo.txt')
        with p.open('wb') as fd:
            fd.write(b'foo')
        self.assertTrue(p.exists())

    def test_isfile(self):
        p = URI('zip://((memory:///file.zip))/foo.txt')
        with p.open('wb') as fd:
            fd.write(b'foo')
        self.assertTrue(p.isfile())

    def test_isdir(self):
        dir_path = URI('zip://((memory:///file.zip))/somedir')
        p = dir_path / 'foo.txt'
        with p.open('wb') as fd:
            fd.write(b'foo')
        self.assertTrue(dir_path.isdir())

    def test_path(self):
        dir_path = URI('zip://((memory:///file.zip))/somedir')
        self.assertEqual(dir_path.path, '/somedir')
        new_path = dir_path / 'other'
        self.assertEqual(new_path.path, '/somedir/other')
Example #28
0
 def tearDown(self):
     p = URI(self.foo_dir)
     if p.isdir():
         p.remove(recursive=True)
     b = URI(self.bar_dir)
     if b.isdir():
         b.remove(recursive=True)
Example #29
0
    def test_remove_recursive_with_readonly_file(self):
        foo_path = URI(self.baseurl) / 'foo'
        bar_path = foo_path / 'bar'
        bar_path.makedirs()

        gaz_path = bar_path / 'ghaz.txt'
        create_file(gaz_path)

        mode = gaz_path.info().mode
        gaz_path.info(set_info=dict(mode=mode & ~stat.S_IWUSR))

        foo_path.remove(recursive=True)

        self.assertTrue(not foo_path.exists())
Example #30
0
    def test_remove_recursive_with_readonly_file(self):
        foo_path = URI(self.baseurl) / 'foo'
        bar_path = foo_path / 'bar'
        bar_path.makedirs()

        gaz_path = bar_path / 'ghaz.txt'
        create_file(gaz_path)

        mode = gaz_path.info().mode
        gaz_path.info(set_info=dict(mode=mode & ~stat.S_IWUSR))

        foo_path.remove(recursive=True)

        self.assertTrue(not foo_path.exists())
Example #31
0
 def setUp(self):
     self.starttime = datetime.datetime.now()
     p = URI("test.txt")
     with p.open("w") as fs:
         fs.write('test')
     a_link = URI("test_link")
     if p.connection.supports_symlinks() and not a_link.islink():
         p.symlink(a_link)
Example #32
0
    def test_copy_dirsymlink_to_dirsymlink_preservelinks(self):
        root = URI(self.baseurl)
        bar_path = root / 'foo' / 'bar'
        bar_path.makedirs()

        gaz_path = bar_path / 'gaz.txt'
        create_file(gaz_path, content='foobar')

        moo_path = root / 'moo'
        moo_path.makedirs()

        tee_path = root / 'helloworld'
        bar_path.symlink(tee_path)

        tee2_path = root / 'helloworld2'
        moo_path.symlink(tee2_path)

        tee_path.copy(tee2_path, recursive=True, followlinks=False)

        helloworld_path = tee2_path / 'helloworld'

        self.assertTrue(tee2_path.islink())  # still a link?
        self.assertTrue(tee_path.islink())  # still a link?

        self.assertTrue(helloworld_path.islink())
        self.assertTrue(helloworld_path.isdir())
        self.assertTrue((helloworld_path / 'gaz.txt').isfile())
        self.assertEqual(helloworld_path.readlink(), bar_path)
        self.assertEqual(helloworld_path.readlink(), tee_path.readlink())
Example #33
0
    def test_symlink_file(self):
        root = URI(self.baseurl)
        bar_path = root / 'foo' / 'bar'
        bar_path.makedirs()

        gaz_path = bar_path / 'gaz.txt'
        create_file(gaz_path, content='foobar')

        tee_path = root / 'helloworld'

        self.assertTrue(not tee_path.exists())
        self.assertTrue(not tee_path.islink())

        gaz_path.symlink(tee_path)

        self.assertTrue(tee_path.exists())
        self.assertTrue(tee_path.islink())
        # a symlink to a file is a file
        self.assertTrue(tee_path.isfile())

        link = tee_path.readlink()
        self.assertEqual(link, gaz_path)

        # check that gaz.txt is accessible through the symlink
        self.assertEqual(load_file(tee_path), 'foobar')
Example #34
0
    def test_copy_filesymlink_to_dirsymlink_followlinks(self):
        root = URI(self.baseurl)
        bar_path = root / 'foo' / 'bar'
        bar_path.makedirs()

        gaz_path = bar_path / 'gaz.txt'
        create_file(gaz_path, content='foobar')

        moo_path = root / 'moo'
        moo_path.makedirs()

        tee_path = root / 'helloworld'
        gaz_path.symlink(tee_path)

        tee2_path = root / 'helloworld2'
        moo_path.symlink(tee2_path)

        tee_path.copy(tee2_path, followlinks=True)

        helloworld_path = tee2_path / 'helloworld'

        self.assertTrue(tee2_path.islink())  # still a link?
        self.assertTrue(tee_path.islink())  # still a link?

        self.assertTrue(not helloworld_path.islink())
        self.assertTrue(helloworld_path.isfile())
        self.assertEqual(load_file(helloworld_path), 'foobar')
        self.assertTrue((moo_path / 'helloworld').isfile())
Example #35
0
    def _setup_hierarchy(self):
        """Walk the following tree:
           foo/
             test.py
             bar/
               file1a.txt
               file1b.png
             gaz/
             file2a.jpg
             file2b.html
        """

        path = URI(self.baseurl)
        foo_path = path / 'foo'
        bar_path = foo_path / 'bar'
        bar_path.makedirs()
        gaz_path = foo_path / 'gaz'
        gaz_path.makedirs()

        create_file(bar_path / 'file1a.txt')
        create_file(bar_path / 'file1b.png')
        create_file(foo_path / 'file2a.jpg')
        create_file(foo_path / 'file2b.html')
        create_file(foo_path / 'test.py')

        return foo_path
Example #36
0
    def test_walk_very_deep_hierarchies(self):
        root = URI(self.baseurl)
        foo_path = root / 'foo'
        expected = []

        def expected_root_str(path):
            return 'ROOT: .%s' % path.path[len(foo_path.path):].replace(
                '\\', '/')

        d_path = foo_path
        for i in range(0, 49):
            nm = 'f%d' % i
            expected.append('DIR: %s' % nm)
            expected.append(expected_root_str(d_path))
            d_path = d_path / nm
        d_path.makedirs()

        expected.append(expected_root_str(d_path))

        expected.reverse()

        actual = self._walk(foo_path, topdown=False, followlinks=False)

        self.assertEqual(expected, actual)
        # expect the right amount of output.  For 64 level with 2 lines per
        # level (1 x ROOT:, 1x DIR:) + 1 for the iinermost ('f63')
        self.assertEqual(len(actual), 99)
Example #37
0
 def setUp(self):
     super(CommonFileSystemTest, self).setUp()
     self.local_setup()
     self.foo_path = URI(self.baseurl) / 'foo'
     self.existing_dir = ujoin(self.baseurl, 'foo')
     self.existing_file = ujoin(self.baseurl, 'foo', 'foo.txt')
     self.non_existing_file = ujoin(self.baseurl, 'bar.txt')
Example #38
0
    def test_remove_recursive_with_symlinks(self):
        root = URI(self.baseurl)
        hw_path = root / 'doo' / 'helloworld'
        hw_path.makedirs()
        humpty_path = hw_path / 'humpty.txt'
        create_file(humpty_path)

        raz_path = root / 'raz'
        mam_path = raz_path / 'mam'
        mam_path.makedirs()
        create_file(mam_path / 'x')

        foo_path = root / 'foo'
        bar_path = foo_path / 'bar'
        bar_path.makedirs()

        gaz_path = bar_path / 'gaz.txt'
        create_file(gaz_path, content='foobar')

        # a symlink to a dir outside of foo
        tee_path = bar_path / 'tee'
        mam_path.symlink(tee_path)

        # a symlink to a file outside of foo
        moo_path = bar_path / 'moo.txt'
        humpty_path.symlink(moo_path)

        foo_path.remove(recursive=True)

        self.assertTrue(not foo_path.isdir())

        # followlinks: the pointed to files should not been deleted
        self.assertTrue(humpty_path.isfile())
        self.assertTrue(mam_path.isdir())
        self.assertTrue(raz_path.isdir())
Example #39
0
    def test_copy_filesymlink_to_filesymlink_followlinks(self):
        root = URI(self.baseurl)
        bar_path = root / 'foo' / 'bar'
        bar_path.makedirs()

        gaz_path = bar_path / 'gaz.txt'
        create_file(gaz_path, content='foobar')

        gaz2_path = bar_path / 'gaz2.txt'
        create_file(gaz2_path, content='foobar2')

        tee_path = root / 'helloworld'
        gaz_path.symlink(tee_path)

        tee2_path = root / 'helloworld2'
        gaz2_path.symlink(tee2_path)

        tee_path.copy(tee2_path, followlinks=True)

        # when following links copying to a symlink->file modifies the
        # referenced file!
        self.assertTrue(tee2_path.islink())
        self.assertEqual(load_file(tee2_path), 'foobar')
        self.assertEqual(load_file(gaz2_path), 'foobar')
        self.assertTrue((bar_path / 'gaz2.txt').isfile())
        self.assertTrue((bar_path / 'gaz.txt').isfile())
Example #40
0
 def test_unipath_windows(self):
     path = URI('C:\\some?extra=arg', sep='\\')
     self.assertEqual(path.path, 'C:\\some')
     self.assertEqual(path.unipath, '/C/some')
     self.assertEqual(path.uri, 'file:///C/some?extra=arg')
     new_path = path / 'other'
     self.assertEqual(new_path.unipath, '/C/some/other')
     self.assertEqual(new_path.uri, 'file:///C/some/other?extra=arg')
Example #41
0
    def test_selfpointing_symlink(self):
        root = URI(self.baseurl)

        tee_path = root / 'helloworld'
        tee_path.symlink(tee_path)

        self.assertTrue(tee_path.islink())
        self.assertEqual(tee_path.readlink(), tee_path)
Example #42
0
 def test_open_unknown_file_fails(self):
     """Check that both backends fail with a proper exception when trying to
     open a path for loading, which does not exist.
     """
     root = URI(self.baseurl)
     notexisting_path = root / 'ma' / 'moo'
     self.assertRaises(IOError, load_file, notexisting_path)
     self.assertRaises(IOError, create_file, notexisting_path)
Example #43
0
    def test_exists_doesnt_fail_on_deadlink(self):
        root = URI(self.baseurl)

        notexisting_path = root / 'foo' / 'bar'
        tee_path = root / 'helloworld'
        notexisting_path.symlink(tee_path)

        self.assertTrue(not tee_path.exists())
Example #44
0
    def test_isexec_fails_on_deadlink(self):
        root = URI(self.baseurl)

        notexisting_path = root / 'foo' / 'bar'
        tee_path = root / 'helloworld'
        notexisting_path.symlink(tee_path)

        self.assertRaises(OSError, tee_path.isexec)
Example #45
0
    def test_backend(self):
        foo_path = URI(self.baseurl) / 'foo'
        bar_path = URI(foo_path.path + '?arg1=value1')
        foo_2_path = foo_path / 'some_dir'
        self.assertTrue(foo_path.get_connection() is foo_2_path.get_connection())
        self.assertTrue(bar_path.get_connection() is not foo_path.get_connection())

        foo_path_connection = foo_path.get_connection()
        foo_path.query['arg'] = 'value'
        self.assertTrue(foo_path_connection is not foo_path.get_connection())
Example #46
0
    def test_copy_recursive_with_preservelinks(self):
        src_path = URI(self.baseurl) / 'folder'
        base_path = src_path / 'gaz'
        foo_path = base_path / 'foo'
        bar_path = foo_path / 'tmp'
        bar_path.makedirs()

        mee_path = foo_path / 'mee.txt'
        create_file(mee_path)
        mee2_path = bar_path / 'mee2.txt'
        create_file(mee2_path)

        dest_path = URI(self.baseurl) / 'helloworld'

        src_path.copy(dest_path, recursive=True, followlinks=False)

        self.assertTrue((dest_path / 'gaz' / 'foo' / 'mee.txt').isfile())
        self.assertTrue((dest_path / 'gaz' / 'foo' / 'tmp').isdir())
Example #47
0
 def test_listdir(self):
     base_path = URI('zip://((%s))/' % self.zip_path.uri)
     self.assertEqual(base_path.listdir(), [])
     p1 = URI('zip://((%s))/foo.txt' % self.zip_path.uri)
     with p1.open('wb') as fd:
         fd.write(b'foo')
     self.assertEqual(base_path.listdir(), ['foo.txt'])
     p2 = URI('zip://((%s))/dir/foo.txt' % self.zip_path.uri)
     with p2.open('w') as fd:
         fd.write(b'foo')
     self.assertEqual(set(base_path.listdir()), set(['foo.txt', 'dir']))
Example #48
0
 def setUp(self):
     super(TestReadingZip, self).setUp()
     self.zip_path = URI('memory:///file.zip')
     zip_handle = self.zip_path.open('wb')
     try:
         self.fp_zip = ZipFile(zip_handle, 'w')
         self.fp_zip.writestr('/foo.txt', 'bar')
         self.fp_zip.close()
     finally:
         zip_handle.close()
Example #49
0
class TestListDir(ZipTestCase):
    def setUp(self):
        super(TestListDir, self).setUp()
        self.zip_path = URI('memory:///file.zip')

    def tearDown(self):
        if self.zip_path.exists():
            self.zip_path.remove()

    def test_listdir(self):
        base_path = URI('zip://((%s))/' % self.zip_path.uri)
        self.assertEqual(base_path.listdir(), [])
        p1 = URI('zip://((%s))/foo.txt' % self.zip_path.uri)
        with p1.open('wb') as fd:
            fd.write(b'foo')
        self.assertEqual(base_path.listdir(), ['foo.txt'])
        p2 = URI('zip://((%s))/dir/foo.txt' % self.zip_path.uri)
        with p2.open('w') as fd:
            fd.write(b'foo')
        self.assertEqual(set(base_path.listdir()), set(['foo.txt', 'dir']))
Example #50
0
    def test_file(self):
        path = URI(self.baseurl) / 'testfile.txt'
        create_file(path, content='hallo')
        content = load_file(path)

        self.assertEqual(content, 'hallo')
        self.assertTrue(path.exists())
        self.assertTrue(path.isfile())
        path.remove()
        self.assertTrue(not path.exists())

        path = URI(self.existing_file)
        self.assertTrue(path.exists())
        self.assertTrue(path.isfile())

        content = load_file(path)
        self.assertTrue(content)
Example #51
0
 def test_info_mtime(self):
     p = URI("test.txt")
     now = datetime.datetime.now()
     size = p.info().size
     with p.open('a') as fs:
         fs.write(' again')
     self.assertTrue(p.info().mtime >= p.info().ctime)
     self.assertTrue( p.info().size > size)
     # due to now's millisecond resolution, we must ignore milliseconds
     self.assertTrue(p.info().mtime.timetuple()[:6] >= now.timetuple()[:6])
Example #52
0
    def test_manipulation_api(self):
        p = self.root_path / "test.txt"
        p._manipulate(lock=True)
        mfile = p.open("w")
        assert not mfile.lock.acquire(False)
        p._manipulate(unlock=True)
        try:
            assert mfile.lock.acquire(False)
        finally:
            mfile.lock.release()

        with p.open("w") as outf:
            outf.write("foo")

        old_mtime = p.mtime()
        new_mtime = old_mtime + 100
        p._manipulate(mtime=new_mtime)

        self.assertEqual(p.mtime(), new_mtime)

        error_file = self.root_path / "error"

        with error_file.open("wb") as outf:
            outf.write(b"foobarbaz")

        error_dir = self.root_path / "error.dir"
        error_dir.mkdir()

        def next_op_callback(_path, _func):
            raise OSError(13, "Permission denied")

        for error in (error_file, error_dir):
            error._manipulate(next_op_callback=next_op_callback)
            clone = URI(error)
            try:
                clone.remove()
            except OSError as e:
                self.assertEqual(e.errno, 13)
            else:
                assert False, "Shouldn't be here"
Example #53
0
 def test_locking(self):
     try:
         p = URI("lock.txt")
         content = "I'm something written into a locked file"
         with p.lock() as inf:
             inf.write(content)
         self.assertEqual(p.open().read(), content)
     finally:
         if p.exists():
             p.remove()
Example #54
0
    def test_info_on_symlinks(self):
        a_file = URI("test.txt")
        a_link = URI("test_link")
        with a_file.open('w') as f:
            f.write("a" * 800)

        if not a_file.connection.supports_symlinks():
            return

        self.assertEqual(a_file.info().size, 800)
        self.assertEqual(a_link.info().size, 800)
        self.assertNotEqual(a_link.info(followlinks=False).size, 800)
Example #55
0
 def setUp(self):
     super(TestRemovalOfFilesAndDirs, self).setUp()
     self.root_path = URI('memory:///')
Example #56
0
 def setUp(self):
     super(MemoryFSTests, self).setUp()
     self.temp_path = URI(tempfile.mktemp())
     self.temp_path.mkdir()
     self.root = URI("memory:///")
Example #57
0
class MemoryFSTests(CleanupMemoryBeforeTestMixin, TestCase):

    def setUp(self):
        super(MemoryFSTests, self).setUp()
        self.temp_path = URI(tempfile.mktemp())
        self.temp_path.mkdir()
        self.root = URI("memory:///")


    def tearDown(self):
        if self.temp_path.isdir():
            self.temp_path.remove(recursive=True)
        super(MemoryFSTests, self).tearDown()


    def test_all(self):
        root = self.root
        assert root.isdir()

        subdir = root / "foo"

        subdir.mkdir()

        assert subdir.isdir()
        assert not subdir.isfile()

        out = subdir / "bar"

        with out.open("w") as outf:
            outf.write("foobar")

        assert not out.isdir()
        assert out.isfile()

        with out.open() as inf:
            content = inf.read()
            self.assertEqual(content, "foobar")


        assert subdir == root / "foo"

        time.sleep(.5)
        assert out.info().mtime < time.time()

        connection = subdir.connection
        out = StringIO()
        connection.dump(out)
        print((out.getvalue()))


    def test_write_text_read_binary(self):
        test_file = self.root / 'foo'
        with test_file.open('w') as text_proxy:
            text_proxy.write("spam & eggs")
        with test_file.open('rb') as binary_proxy:
            self.assertEqual(binary_proxy.read(), b"spam & eggs")


    def test_write_binary_read_text(self):
        test_file = self.root / 'foo'
        with test_file.open('wb') as binary_proxy:
            binary_proxy.write(b"spam & eggs")
        with test_file.open('r') as text_proxy:
            self.assertEqual(text_proxy.read(), "spam & eggs")


    def test_info_on_symlinks(self):
        root = self.root
        a_file = root / "a_file"
        a_link = root / "a_link"
        with a_file.open('w') as f:
            f.write("a" * 800)
        a_file.symlink(a_link)

        self.assertEqual(a_file.info().size, 800)
        self.assertEqual(a_link.info().size, 800)
        self.assertNotEqual(a_link.info(followlinks=False).size, 800)

        orig_info = a_file.info()
        new_info = a_file.info()
        new_info.mtime = new_info.mtime + 100
        a_link.info(new_info, followlinks=False)

        self.assertEqual(a_file.info().mtime, orig_info.mtime)
        self.assertEqual(a_link.info().mtime, orig_info.mtime)
        self.assertEqual(a_link.info(followlinks=False).mtime, new_info.mtime)


    def test_listdir_empty_root(self):
        root = self.root
        files = root.listdir()
        assert not files


    def test_listdir_empty_dir(self):
        root = self.root
        foo = root / 'foo'
        foo.mkdir()
        rootfiles = root.listdir()
        assert 'foo' in rootfiles
        foofiles = foo.listdir()
        assert not foofiles


    def test_walk(self):
        root = self.root
        foo = root / 'foo'
        foo.mkdir()
        bar = root / 'bar'
        bar.mkdir()
        foofile =  foo / 'foocontent.txt'
        with foofile.open('w') as fd:
            fd.write('foocontent')
        results = []
        for root, dirs, files in root.walk():
            results.append((root, dirs, files))
        assert len(results) == 3


    def test_next(self):
        root = self.root
        subdir = root / "foo"
        with subdir.open("w") as outf:
            outf.write("foo\nbar")

        with subdir.open() as inf:
            content = next(inf)
            self.assertEqual(content, "foo\n")
            content = next(inf)
            self.assertEqual(content, "bar")

        with subdir.open() as inf:
            for line in inf:
                self.assertIn(line, ["foo\n", "bar"])

    def test_exists_on_root(self):
        root = self.root
        assert root.exists()


    def test_root_of_non_existing_dir_exists(self):
        dir_path = URI("memory:///foo")
        assert dir_path.dirname().exists()


    def test_directory_cant_be_overwritten_by_file(self):
        base = URI("memory:///")
        d = base / "foo"
        d.mkdir()
        assert d.exists()

        try:
            with d.open("w") as outf:
                outf.write("foo")
        except IOError as e:
            self.assertEqual(e.errno, errno.EISDIR)
        else:
            assert False, "You shouldn't be able to ovewrite a directory like this"


    def test_copy_into_fs(self):
        root = self.root
        for item in ["foo", "bar"]:
            with (root/item).open("wb") as fd:
                fd.write(item.encode('utf-8'))
        root.copy(self.temp_path, recursive=True)
        content = self.temp_path.listdir()
        self.assertEqual(set(content), set(["foo", "bar"]))


    def test_cleanup_removes_lingering_locks(self):
        lockfile = self.root / "lockfile"
        with lockfile.open("w") as outf:
            outf.write(" ")

        lockfile._manipulate(mtime=lockfile.mtime() + 3, lock=True)
        CONNECTION_REGISTRY.cleanup(force=True)

        with lockfile.lock(fail_on_lock=True):
            pass
Example #58
0
class TestRemovalOfFilesAndDirs(CleanupMemoryBeforeTestMixin, TestCase):

    def setUp(self):
        super(TestRemovalOfFilesAndDirs, self).setUp()
        self.root_path = URI('memory:///')

    def test_first(self):
        self.assertEqual(self.root_path.listdir(),[])

    def test_removedir(self):
        dir_path = self.root_path / 'foo'
        self.assertTrue(not dir_path.exists())
        dir_path.mkdir()
        self.assertTrue(dir_path.exists())
        self.assertTrue(dir_path.isdir())
        dir_path.remove()
        self.assertTrue(not dir_path.exists())

    def test_remove_not_existing_dir(self):
        dir_path = self.root_path / 'foo'
        self.assertRaises(FileDoesNotExistError, dir_path.remove, ())

    def test_removefile(self):
        file_path = self.root_path / 'foo.txt'
        self.assertTrue(not file_path.exists())
        with file_path.open('w') as fd:
            fd.write('bar')
        self.assertTrue(file_path.isfile())
        file_path.remove()
        self.assertTrue(not file_path.exists())


    def test_removefile_not_existing(self):
        file_path = self.root_path / 'foo.txt'
        self.assertRaises(FileDoesNotExistError, file_path.remove, ())


    def test_remove_recursive(self):
        dir_path = self.root_path / 'foo'
        file_path = dir_path / 'bar.txt'
        dir_path.mkdir()
        with file_path.open('w') as fd:
            fd.write('info')
        self.assertTrue(dir_path.exists())
        self.assertTrue(file_path.exists())
        dir_path.remove(recursive=True)
        self.assertTrue(not dir_path.exists())
        self.assertTrue(not file_path.exists())


    def test_locking(self):
        p = self.root_path / "test.txt"
        try:
            content = "I'm something written into a locked file"
            with p.lock() as inf:
                inf.write(content)
            self.assertEqual(p.open().read(), content)
        finally:
            if p.exists():
                p.remove()

        mfile = p.open("w")

        lock_a = mfile.lock
        mfile.close()

        mfile = p.open("w")
        assert lock_a is mfile.lock

        # artificially lock the file
        mfile.lock.acquire()

        try:
            with p.lock(fail_on_lock=True):
                assert False, "we shouldn't be allowed here!"
        except LockFileObtainException:
            pass
        finally:
            mfile.lock.release()


    def test_manipulation_api(self):
        p = self.root_path / "test.txt"
        p._manipulate(lock=True)
        mfile = p.open("w")
        assert not mfile.lock.acquire(False)
        p._manipulate(unlock=True)
        try:
            assert mfile.lock.acquire(False)
        finally:
            mfile.lock.release()

        with p.open("w") as outf:
            outf.write("foo")

        old_mtime = p.mtime()
        new_mtime = old_mtime + 100
        p._manipulate(mtime=new_mtime)

        self.assertEqual(p.mtime(), new_mtime)

        error_file = self.root_path / "error"

        with error_file.open("wb") as outf:
            outf.write(b"foobarbaz")

        error_dir = self.root_path / "error.dir"
        error_dir.mkdir()

        def next_op_callback(_path, _func):
            raise OSError(13, "Permission denied")

        for error in (error_file, error_dir):
            error._manipulate(next_op_callback=next_op_callback)
            clone = URI(error)
            try:
                clone.remove()
            except OSError as e:
                self.assertEqual(e.errno, 13)
            else:
                assert False, "Shouldn't be here"


    def test_reading_from_write_only_files_not_working(self):
        p = self.root_path / "test.txt"
        with p.open("w") as outf:
            self.assertRaises(IOError, outf.read)



    def test_lockfile_cleanup(self):
        p = self.root_path / "test.txt"
        if p.exists():
            p.remove()

        with p.lock(cleanup=True):
            assert p.exists()

        assert not p.exists()


    def test_file_name_comparison(self):
        a = self.root_path / "a"
        b = self.root_path / "b"
        assert a == a
        assert b == b
        assert a != b
        assert b != a
        assert not a != a
        assert not b != b


    def test_double_dir_creation_fails(self):
        a = self.root_path / "a"
        a.mkdir()
        self.assertRaises(OSError, a.mkdir)


    def test_setting_mode(self):
        p = self.root_path / "test.txt"
        if p.exists():
            p.remove()

        create_file(p, content="foo")

        mode = p.info().mode
        new_mode = mode | stat.S_IXUSR
        p.info(set_info=dict(mode=new_mode))
        self.assertEqual(p.info().mode,
                         new_mode)


    def test_removing_non_empty_dirs(self):
        p = self.root_path / "test-dir"
        assert not p.exists()
        p.mkdir()

        create_file(p / "some-file.txt", content="foobar")

        self.assertRaises(OSError, p.remove)

        (p / "some-file.txt").remove()
        p.remove()

        assert not p.exists()
        p.mkdir()

        create_file(p / "some-file.txt", content="foobar")

        p.remove(recursive=True)