Exemplo n.º 1
0
 def tearDown(self):
     for d in (self.src_dir, self.dst_dir):
         try:
             if d:
                 shutil.rmtree(d)
         except:
             pass
Exemplo n.º 2
0
    def test_copytree_simple(self):
        def read_data(path):
            f = open(path)
            data = f.read()
            f.close()
            return data

        src_dir = tempfile.mkdtemp()
        dst_dir = os.path.join(tempfile.mkdtemp(), 'destination')
        self._write_data(os.path.join(src_dir, 'test.txt'), '123')
        os.mkdir(os.path.join(src_dir, 'test_dir'))
        self._write_data(os.path.join(src_dir, 'test_dir', 'test.txt'), '456')

        try:
            shutil.copytree(src_dir, dst_dir)
            self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test.txt')))
            self.assertTrue(os.path.isdir(os.path.join(dst_dir, 'test_dir')))
            self.assertTrue(
                os.path.isfile(os.path.join(dst_dir, 'test_dir', 'test.txt')))
            actual = read_data(os.path.join(dst_dir, 'test.txt'))
            self.assertEqual(actual, '123')
            actual = read_data(os.path.join(dst_dir, 'test_dir', 'test.txt'))
            self.assertEqual(actual, '456')
        finally:
            for path in (
                    os.path.join(src_dir, 'test.txt'),
                    os.path.join(dst_dir, 'test.txt'),
                    os.path.join(src_dir, 'test_dir', 'test.txt'),
                    os.path.join(dst_dir, 'test_dir', 'test.txt'),
            ):
                if os.path.exists(path):
                    os.remove(path)
            for path in (src_dir, os.path.dirname(dst_dir)):
                if os.path.exists(path):
                    shutil.rmtree(path)
Exemplo n.º 3
0
 def tearDown(self):
     for d in (self.src_dir, self.dst_dir):
         try:
             if d:
                 shutil.rmtree(d)
         except:
             pass
Exemplo n.º 4
0
    def test_dont_copy_file_onto_link_to_itself(self):
        # Temporarily disable test on Windows.
        if os.name == 'nt':
            return
        # bug 851123.
        TESTFN = self.TESTFN
        os.mkdir(TESTFN)
        src = os.path.join(TESTFN, 'cheese')
        dst = os.path.join(TESTFN, 'shop')
        try:
            f = open(src, 'w')
            try:
                f.write('cheddar')
            finally:
                f.close()

            os.link(src, dst)
            self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
            f = open(src, 'r')
            try:
                self.assertEqual(f.read(), 'cheddar')
            finally:
                f.close()
            os.remove(dst)
        finally:
            shutil.rmtree(TESTFN, ignore_errors=True)
Exemplo n.º 5
0
 def test_dont_copy_file_onto_symlink_to_itself(self):
     # bug 851123.
     TESTFN = self.TESTFN
     os.mkdir(TESTFN)
     src = os.path.join(TESTFN, 'cheese')
     dst = os.path.join(TESTFN, 'shop')
     try:
         f = open(src, 'w')
         try:
             f.write('cheddar')
         finally:
             f.close()
         # Using `src` here would mean we end up with a symlink pointing
         # to TESTFN/TESTFN/cheese, while it should point at
         # TESTFN/cheese.
         os.symlink('cheese', dst)
         self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
         f = open(src, 'r')
         try:
             self.assertEqual(f.read(), 'cheddar')
         finally:
             f.close()
         os.remove(dst)
     finally:
         shutil.rmtree(TESTFN, ignore_errors=True)
Exemplo n.º 6
0
    def test_dont_copy_file_onto_link_to_itself(self):
        # Temporarily disable test on Windows.
        if os.name == 'nt':
            return
        # bug 851123.
        TESTFN = self.TESTFN
        os.mkdir(TESTFN)
        src = os.path.join(TESTFN, 'cheese')
        dst = os.path.join(TESTFN, 'shop')
        try:
            f = open(src, 'w')
            try:
                f.write('cheddar')
            finally:
                f.close()

            os.link(src, dst)
            self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
            f = open(src, 'r')
            try:
                self.assertEqual(f.read(), 'cheddar')
            finally:
                f.close()
            os.remove(dst)
        finally:
            shutil.rmtree(TESTFN, ignore_errors=True)
Exemplo n.º 7
0
 def test_dont_copy_file_onto_symlink_to_itself(self):
     # bug 851123.
     TESTFN = self.TESTFN
     os.mkdir(TESTFN)
     src = os.path.join(TESTFN, 'cheese')
     dst = os.path.join(TESTFN, 'shop')
     try:
         f = open(src, 'w')
         try:
             f.write('cheddar')
         finally:
             f.close()
         # Using `src` here would mean we end up with a symlink pointing
         # to TESTFN/TESTFN/cheese, while it should point at
         # TESTFN/cheese.
         os.symlink('cheese', dst)
         self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
         f = open(src, 'r')
         try:
             self.assertEqual(f.read(), 'cheddar')
         finally:
             f.close()
         os.remove(dst)
     finally:
         shutil.rmtree(TESTFN, ignore_errors=True)
Exemplo n.º 8
0
        def test_on_error(self):
            self.errorState = 0
            TESTFN = self.TESTFN
            os.mkdir(TESTFN)
            self.childpath = os.path.join(TESTFN, 'a')
            f = open(self.childpath, 'w')
            f.close()
            old_dir_mode = os.stat(TESTFN).st_mode
            old_child_mode = os.stat(self.childpath).st_mode
            # Make unwritable.
            os.chmod(self.childpath, stat.S_IREAD)
            os.chmod(TESTFN, stat.S_IREAD)

            shutil.rmtree(TESTFN, onerror=self.check_args_to_onerror)
            # Test whether onerror has actually been called.
            self.assertEqual(
                self.errorState, 2,
                "Expected call to onerror function did not happen.")

            # Make writable again.
            os.chmod(TESTFN, old_dir_mode)
            os.chmod(self.childpath, old_child_mode)

            # Clean up.
            shutil.rmtree(TESTFN)
Exemplo n.º 9
0
 def test_move_dir(self):
     # Move a dir to another location on the same filesystem.
     dst_dir = tempfile.mktemp()
     try:
         self._check_move_dir(self.src_dir, dst_dir, dst_dir)
     finally:
         try:
             shutil.rmtree(dst_dir)
         except:
             pass
Exemplo n.º 10
0
 def test_move_dir(self):
     # Move a dir to another location on the same filesystem.
     dst_dir = tempfile.mktemp()
     try:
         self._check_move_dir(self.src_dir, dst_dir, dst_dir)
     finally:
         try:
             shutil.rmtree(dst_dir)
         except:
             pass
Exemplo n.º 11
0
 def test_destinsrc_false_positive(self):
     TESTFN = self.TESTFN
     os.mkdir(TESTFN)
     try:
         for src, dst in [('srcdir', 'src/dest'), ('srcdir', 'srcdir.new')]:
             src = os.path.join(TESTFN, src)
             dst = os.path.join(TESTFN, dst)
             self.assertFalse(shutil._destinsrc(src, dst),
                         msg='_destinsrc() wrongly concluded that '
                         'dst (%s) is in src (%s)' % (dst, src))
     finally:
         shutil.rmtree(TESTFN, ignore_errors=True)
Exemplo n.º 12
0
 def test_rmtree_on_symlink(self):
     # bug 1669.
     TESTFN = self.TESTFN
     os.mkdir(TESTFN)
     try:
         src = os.path.join(TESTFN, 'cheese')
         dst = os.path.join(TESTFN, 'shop')
         os.mkdir(src)
         os.symlink(src, dst)
         self.assertRaises(OSError, shutil.rmtree, dst)
     finally:
         shutil.rmtree(TESTFN, ignore_errors=True)
Exemplo n.º 13
0
 def test_destinsrc_false_positive(self):
     TESTFN = self.TESTFN
     os.mkdir(TESTFN)
     try:
         for src, dst in [('srcdir', 'src/dest'), ('srcdir', 'srcdir.new')]:
             src = os.path.join(TESTFN, src)
             dst = os.path.join(TESTFN, dst)
             self.assertFalse(shutil._destinsrc(src, dst),
                              msg='_destinsrc() wrongly concluded that '
                              'dst (%s) is in src (%s)' % (dst, src))
     finally:
         shutil.rmtree(TESTFN, ignore_errors=True)
Exemplo n.º 14
0
 def test_rmtree_on_symlink(self):
     # bug 1669.
     TESTFN = self.TESTFN
     os.mkdir(TESTFN)
     try:
         src = os.path.join(TESTFN, 'cheese')
         dst = os.path.join(TESTFN, 'shop')
         os.mkdir(src)
         os.symlink(src, dst)
         self.assertRaises(OSError, shutil.rmtree, dst)
     finally:
         shutil.rmtree(TESTFN, ignore_errors=True)
Exemplo n.º 15
0
 def setUp(self):
     filename = "foo"
     self.src_dir = tempfile.mkdtemp()
     self.dst_dir = tempfile.mkdtemp()
     self.src_file = os.path.join(self.src_dir, filename)
     self.dst_file = os.path.join(self.dst_dir, filename)
     d = tempfile.mkdtemp()
     self.TESTFN = '%s-%d' % (d, os.getpid())
     shutil.rmtree(d)
     #self.TESTFN2 = '%s-2' % self.TESTFN
     f = open(self.src_file, "wb")
     try:
         f.write(b"spam")
     finally:
         f.close()
Exemplo n.º 16
0
 def setUp(self):
     filename = "foo"
     self.src_dir = tempfile.mkdtemp()
     self.dst_dir = tempfile.mkdtemp()
     self.src_file = os.path.join(self.src_dir, filename)
     self.dst_file = os.path.join(self.dst_dir, filename)
     d = tempfile.mkdtemp()
     self.TESTFN = '%s-%d' % (d, os.getpid())
     shutil.rmtree(d)
     #self.TESTFN2 = '%s-2' % self.TESTFN
     f = open(self.src_file, "wb")
     try:
         f.write(b"spam")
     finally:
         f.close()
Exemplo n.º 17
0
 def test_copytree_named_pipe(self):
     TESTFN = self.TESTFN
     TESTFN2 = self.TESTFN2
     os.mkdir(TESTFN)
     try:
         subdir = os.path.join(TESTFN, "subdir")
         os.mkdir(subdir)
         pipe = os.path.join(subdir, "mypipe")
         os.mkfifo(pipe)
         try:
             shutil.copytree(TESTFN, TESTFN2)
         except shutil.Error as e:
             errors = e.args[0]
             self.assertEqual(len(errors), 1)
             src, dst, error_msg = errors[0]
             self.assertEqual("`%s` is a named pipe" % pipe, error_msg)
         else:
             self.fail("shutil.Error should have been raised")
     finally:
         shutil.rmtree(TESTFN, ignore_errors=True)
         shutil.rmtree(TESTFN2, ignore_errors=True)
Exemplo n.º 18
0
 def test_copytree_named_pipe(self):
     TESTFN = self.TESTFN
     TESTFN2 = self.TESTFN2
     os.mkdir(TESTFN)
     try:
         subdir = os.path.join(TESTFN, "subdir")
         os.mkdir(subdir)
         pipe = os.path.join(subdir, "mypipe")
         os.mkfifo(pipe)
         try:
             shutil.copytree(TESTFN, TESTFN2)
         except shutil.Error as e:
             errors = e.args[0]
             self.assertEqual(len(errors), 1)
             src, dst, error_msg = errors[0]
             self.assertEqual("`%s` is a named pipe" % pipe, error_msg)
         else:
             self.fail("shutil.Error should have been raised")
     finally:
         shutil.rmtree(TESTFN, ignore_errors=True)
         shutil.rmtree(TESTFN2, ignore_errors=True)
Exemplo n.º 19
0
    def test_copytree_simple(self):

        def read_data(path):
            f = open(path)
            data = f.read()
            f.close()
            return data

        src_dir = tempfile.mkdtemp()
        dst_dir = os.path.join(tempfile.mkdtemp(), 'destination')
        self._write_data(os.path.join(src_dir, 'test.txt'), '123')
        os.mkdir(os.path.join(src_dir, 'test_dir'))
        self._write_data(os.path.join(src_dir, 'test_dir', 'test.txt'), '456')

        try:
            shutil.copytree(src_dir, dst_dir)
            self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test.txt')))
            self.assertTrue(os.path.isdir(os.path.join(dst_dir, 'test_dir')))
            self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test_dir',
                                                        'test.txt')))
            actual = read_data(os.path.join(dst_dir, 'test.txt'))
            self.assertEqual(actual, '123')
            actual = read_data(os.path.join(dst_dir, 'test_dir', 'test.txt'))
            self.assertEqual(actual, '456')
        finally:
            for path in (
                    os.path.join(src_dir, 'test.txt'),
                    os.path.join(dst_dir, 'test.txt'),
                    os.path.join(src_dir, 'test_dir', 'test.txt'),
                    os.path.join(dst_dir, 'test_dir', 'test.txt'),
                ):
                if os.path.exists(path):
                    os.remove(path)
            for path in (src_dir,
                    os.path.dirname(dst_dir)
                ):
                if os.path.exists(path):
                    shutil.rmtree(path)
Exemplo n.º 20
0
        def test_on_error(self):
            self.errorState = 0
            TESTFN = self.TESTFN
            os.mkdir(TESTFN)
            self.childpath = os.path.join(TESTFN, 'a')
            f = open(self.childpath, 'w')
            f.close()
            old_dir_mode = os.stat(TESTFN).st_mode
            old_child_mode = os.stat(self.childpath).st_mode
            # Make unwritable.
            os.chmod(self.childpath, stat.S_IREAD)
            os.chmod(TESTFN, stat.S_IREAD)

            shutil.rmtree(TESTFN, onerror=self.check_args_to_onerror)
            # Test whether onerror has actually been called.
            self.assertEqual(self.errorState, 2,
                             "Expected call to onerror function did not happen.")

            # Make writable again.
            os.chmod(TESTFN, old_dir_mode)
            os.chmod(self.childpath, old_child_mode)

            # Clean up.
            shutil.rmtree(TESTFN)
Exemplo n.º 21
0
 def _cleanup_testfn(self):
     path = self.TESTFN
     if os.path.isfile(path):
         os.remove(path)
     elif os.path.isdir(path):
         shutil.rmtree(path)
Exemplo n.º 22
0
 def _cleanup_testfn(self):
     path = self.TESTFN
     if os.path.isfile(path):
         os.remove(path)
     elif os.path.isdir(path):
         shutil.rmtree(path)
Exemplo n.º 23
0
    def test_copytree_with_exclude(self):

        def read_data(path):
            f = open(path)
            data = f.read()
            f.close()
            return data

        # creating data
        join = os.path.join
        exists = os.path.exists
        src_dir = tempfile.mkdtemp()
        try:
            dst_dir = join(tempfile.mkdtemp(), 'destination')
            self._write_data(join(src_dir, 'test.txt'), '123')
            self._write_data(join(src_dir, 'test.tmp'), '123')
            os.mkdir(join(src_dir, 'test_dir'))
            self._write_data(join(src_dir, 'test_dir', 'test.txt'), '456')
            os.mkdir(join(src_dir, 'test_dir2'))
            self._write_data(join(src_dir, 'test_dir2', 'test.txt'), '456')
            os.mkdir(join(src_dir, 'test_dir2', 'subdir'))
            os.mkdir(join(src_dir, 'test_dir2', 'subdir2'))
            self._write_data(join(src_dir, 'test_dir2', 'subdir', 'test.txt'),
                             '456')
            self._write_data(join(src_dir, 'test_dir2', 'subdir2', 'test.py'),
                             '456')


            # testing glob-like patterns
            try:
                patterns = shutil.ignore_patterns('*.tmp', 'test_dir2')
                shutil.copytree(src_dir, dst_dir, ignore=patterns)
                # checking the result: some elements should not be copied
                self.assertTrue(exists(join(dst_dir, 'test.txt')))
                self.assertTrue(not exists(join(dst_dir, 'test.tmp')))
                self.assertTrue(not exists(join(dst_dir, 'test_dir2')))
            finally:
                if os.path.exists(dst_dir):
                    shutil.rmtree(dst_dir)
            try:
                patterns = shutil.ignore_patterns('*.tmp', 'subdir*')
                shutil.copytree(src_dir, dst_dir, ignore=patterns)
                # checking the result: some elements should not be copied
                self.assertTrue(not exists(join(dst_dir, 'test.tmp')))
                self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir2')))
                self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir')))
            finally:
                if os.path.exists(dst_dir):
                    shutil.rmtree(dst_dir)

            # testing callable-style
            try:
                def _filter(src, names):
                    res = []
                    for name in names:
                        path = os.path.join(src, name)

                        if (os.path.isdir(path) and
                            path.split()[-1] == 'subdir'):
                            res.append(name)
                        elif os.path.splitext(path)[-1] in ('.py'):
                            res.append(name)
                    return res

                shutil.copytree(src_dir, dst_dir, ignore=_filter)

                # checking the result: some elements should not be copied
                self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir2',
                                        'test.py')))
                self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir')))

            finally:
                if os.path.exists(dst_dir):
                    shutil.rmtree(dst_dir)
        finally:
            shutil.rmtree(src_dir)
            shutil.rmtree(os.path.dirname(dst_dir))
Exemplo n.º 24
0
 def tearDown(self):
     super(TestShutil, self).tearDown()
     while self.tempdirs:
         d = self.tempdirs.pop()
         shutil.rmtree(d, os.name in ('nt', 'cygwin'))
Exemplo n.º 25
0
    def test_copytree_with_exclude(self):
        def read_data(path):
            f = open(path)
            data = f.read()
            f.close()
            return data

        # creating data
        join = os.path.join
        exists = os.path.exists
        src_dir = tempfile.mkdtemp()
        try:
            dst_dir = join(tempfile.mkdtemp(), 'destination')
            self._write_data(join(src_dir, 'test.txt'), '123')
            self._write_data(join(src_dir, 'test.tmp'), '123')
            os.mkdir(join(src_dir, 'test_dir'))
            self._write_data(join(src_dir, 'test_dir', 'test.txt'), '456')
            os.mkdir(join(src_dir, 'test_dir2'))
            self._write_data(join(src_dir, 'test_dir2', 'test.txt'), '456')
            os.mkdir(join(src_dir, 'test_dir2', 'subdir'))
            os.mkdir(join(src_dir, 'test_dir2', 'subdir2'))
            self._write_data(join(src_dir, 'test_dir2', 'subdir', 'test.txt'),
                             '456')
            self._write_data(join(src_dir, 'test_dir2', 'subdir2', 'test.py'),
                             '456')

            # testing glob-like patterns
            try:
                patterns = shutil.ignore_patterns('*.tmp', 'test_dir2')
                shutil.copytree(src_dir, dst_dir, ignore=patterns)
                # checking the result: some elements should not be copied
                self.assertTrue(exists(join(dst_dir, 'test.txt')))
                self.assertTrue(not exists(join(dst_dir, 'test.tmp')))
                self.assertTrue(not exists(join(dst_dir, 'test_dir2')))
            finally:
                if os.path.exists(dst_dir):
                    shutil.rmtree(dst_dir)
            try:
                patterns = shutil.ignore_patterns('*.tmp', 'subdir*')
                shutil.copytree(src_dir, dst_dir, ignore=patterns)
                # checking the result: some elements should not be copied
                self.assertTrue(not exists(join(dst_dir, 'test.tmp')))
                self.assertTrue(
                    not exists(join(dst_dir, 'test_dir2', 'subdir2')))
                self.assertTrue(
                    not exists(join(dst_dir, 'test_dir2', 'subdir')))
            finally:
                if os.path.exists(dst_dir):
                    shutil.rmtree(dst_dir)

            # testing callable-style
            try:

                def _filter(src, names):
                    res = []
                    for name in names:
                        path = os.path.join(src, name)

                        if (os.path.isdir(path)
                                and path.split()[-1] == 'subdir'):
                            res.append(name)
                        elif os.path.splitext(path)[-1] in ('.py'):
                            res.append(name)
                    return res

                shutil.copytree(src_dir, dst_dir, ignore=_filter)

                # checking the result: some elements should not be copied
                self.assertTrue(not exists(
                    join(dst_dir, 'test_dir2', 'subdir2', 'test.py')))
                self.assertTrue(
                    not exists(join(dst_dir, 'test_dir2', 'subdir')))

            finally:
                if os.path.exists(dst_dir):
                    shutil.rmtree(dst_dir)
        finally:
            shutil.rmtree(src_dir)
            shutil.rmtree(os.path.dirname(dst_dir))
Exemplo n.º 26
0
 def tearDown(self):
     super(TestShutil, self).tearDown()
     while self.tempdirs:
         d = self.tempdirs.pop()
         shutil.rmtree(d, os.name in ('nt', 'cygwin'))