Ejemplo n.º 1
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')
Ejemplo n.º 2
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')
Ejemplo n.º 3
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']))
Ejemplo n.º 4
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']))
Ejemplo n.º 5
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'])])
Ejemplo n.º 6
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'])])
Ejemplo n.º 7
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)
Ejemplo n.º 8
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])
Ejemplo n.º 9
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()
Ejemplo n.º 10
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')
Ejemplo n.º 11
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)
Ejemplo n.º 12
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')
Ejemplo n.º 13
0
    def test_set_info_on_symlinks(self):
        # lchmod is not supported on Linux, only on OSX
        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)

        orig_info = a_file.info()
        a_link.info({'mode': 0o120700}, followlinks=False)

        self.assertEqual(a_file.info().mode, orig_info.mode)
        self.assertEqual(a_link.info().mode, orig_info.mode)
        self.assertEqual(a_link.info(followlinks=False).mode, 0o120700)
Ejemplo n.º 14
0
 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')
Ejemplo n.º 15
0
 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())
Ejemplo n.º 16
0
 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')
Ejemplo n.º 17
0
 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())
Ejemplo n.º 18
0
 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')
Ejemplo n.º 19
0
 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')
Ejemplo n.º 20
0
 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')
Ejemplo n.º 21
0
 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')