Beispiel #1
0
    def setUp(self):
        """ Initialize directory for testing diff and patch. """
        base_path = '/tmp/test_incremental_backups_tools'
        self.base_path = base_path
        os.mkdir(base_path)
        with open(os.path.join(base_path, 'file1'), 'w') as f:
            f.write('contents1')
        with open(os.path.join(base_path, 'file2'), 'w') as f:
            f.write('contents2')
        with open(os.path.join(base_path, 'file3.py'), 'w') as f:
            f.write('print "ok"')
        f = open(os.path.join(base_path, 'file3.pyc'), 'w')
        f.close()
        with open(os.path.join(base_path, '.exclude'), 'w') as f:
            f.write('excluded_dir/\n*.pyc')
        os.mkdir(os.path.join(base_path, 'excluded_dir'))
        with open(os.path.join(base_path, 'excluded_dir/excluded_file'), 'w') as f:
            f.write('excluded')
        os.mkdir(os.path.join(base_path, 'dir1'))
        os.mkdir(os.path.join(base_path, 'dir1/subdir1'))
        with open(os.path.join(base_path, 'dir1/subdir1/file_subdir1'), 'w') as f:
            f.write('inside subir1')
        f = open(os.path.join(base_path, 'dir1/subdir1/.project'), 'w')
        f.close()
        os.mkdir(os.path.join(base_path, 'dir2'))
        with open(os.path.join(base_path, 'dir2/file_dir2'), 'w') as f:
            f.write('inside dir2')

        with open(os.path.join(base_path, 'dir1/subdir1/file_subdir1'), 'w') as f:
            f.write('inside subdir1')

        shutil.copytree(base_path, base_path + '2')
        shutil.copytree(base_path, base_path + '3')
        shutil.copytree(base_path, base_path + '4')

        # We modify test_dirtools for the first time
        with open(os.path.join(base_path + '2', 'file4'), 'w') as f:
            f.write('contents4')
        os.remove(os.path.join(base_path + '2', 'file2'))
        os.mkdir(os.path.join(base_path + '2', 'dir3'))
        with open(os.path.join(base_path + '2', 'dir3/file3'), 'w') as f:
            f.write('contents3')
        with open(os.path.join(base_path + '2', 'file1'), 'w') as f:
            f.write('new things')
        shutil.rmtree(os.path.join(base_path + '2', 'dir1/subdir1'))

        self.dir = dirtools.Dir('/tmp/test_incremental_backups_tools')
        self.dir2 = dirtools.Dir('/tmp/test_incremental_backups_tools2')
        self.dir3 = dirtools.Dir('/tmp/test_incremental_backups_tools3')
        self.dir4 = dirtools.Dir('/tmp/test_incremental_backups_tools4')
Beispiel #2
0
    def testCompression(self):
        """ Check the compression, withouth pyfakefs because it doesn't support tarfile. """
        dirtools.os = os
        dirtools.open = open

        test_dir = '/tmp/test_dirtools'

        if os.path.isdir(test_dir):
            shutil.rmtree(test_dir)
        os.mkdir(test_dir)

        with open(os.path.join(test_dir, 'file1'), 'w') as f:
            f.write(os.urandom(2 ** 10))

        with open(os.path.join(test_dir, 'file2.pyc'), 'w') as f:
            f.write('excluded')
        os.mkdir(os.path.join(test_dir, 'dir1'))
        with open(os.path.join(test_dir, 'dir1/file1'), 'w') as f:
            f.write(os.urandom(2 ** 10))

        cdir = dirtools.Dir(test_dir)

        archive_path = cdir.compress_to()

        tar = tarfile.open(archive_path)

        test_dir_extract = '/tmp/test_dirtools_extract'

        if os.path.isdir(test_dir_extract):
            shutil.rmtree(test_dir_extract)
        os.mkdir(test_dir_extract)

        tar.extractall(test_dir_extract)

        extracted_dir = dirtools.Dir(test_dir_extract)

        self.assertEqual(sorted(extracted_dir.files()),
                         sorted(cdir.files()))

        self.assertEqual(sorted(extracted_dir.subdirs()),
                         sorted(cdir.subdirs()))

        self.assertEqual(extracted_dir.hash(dirtools.filehash),
                         cdir.hash(dirtools.filehash))

        shutil.rmtree(test_dir)
        shutil.rmtree(test_dir_extract)
        os.remove(archive_path)
Beispiel #3
0
    def setUp(self):
        """ Initialize a fake filesystem and dirtools. """

        # First we create a fake filesystem in order to test dirtools
        fk = fake_filesystem.FakeFilesystem()
        fk.CreateDirectory('/test_dirtools')
        fk.CreateFile('/test_dirtools/file1', contents='contents1')
        fk.CreateFile('/test_dirtools/file2', contents='contents2')
        fk.CreateFile('/test_dirtools/file3.py', contents='print "ok"')
        fk.CreateFile('/test_dirtools/file3.pyc', contents='')
        fk.CreateFile('/test_dirtools/.exclude',
                      contents='excluded_dir/\n*.pyc')

        fk.CreateDirectory('/test_dirtools/excluded_dir')
        fk.CreateFile('/test_dirtools/excluded_dir/excluded_file',
                      contents='excluded')

        fk.CreateDirectory('/test_dirtools/dir1')
        fk.CreateDirectory('/test_dirtools/dir1/subdir1')
        fk.CreateFile('/test_dirtools/dir1/subdir1/file_subdir1',
                      contents='inside subdir1')
        fk.CreateFile('/test_dirtools/dir1/subdir1/.project')

        fk.CreateDirectory('/test_dirtools/dir2')
        fk.CreateFile('/test_dirtools/dir2/file_dir2', contents='inside dir2')

        # Sort of "monkey patch" to make dirtools use the fake filesystem
        dirtools.os = fake_filesystem.FakeOsModule(fk)
        dirtools.open = fake_filesystem.FakeFileOpen(fk)

        # Dirtools initialization
        self.dir = dirtools.Dir('/test_dirtools')
        self.os = dirtools.os
        self.open = dirtools.open
Beispiel #4
0
 def testDirState(self):
     dir_state = dirtools.DirState(self.dir, index_cmp=dirtools.filehash)
     self.shutil.copytree('/test_dirtools', 'test_dirtools2')
     with self.open('/test_dirtools2/dir1/subdir1/file_subdir1', 'w') as f:
         f.write("dir state")
     with self.open('/test_dirtools2/new_file', 'w') as f:
         f.write("dir state")
     self.os.remove('/test_dirtools2/file1')
     self.shutil.rmtree('/test_dirtools2/dir2')
     dir_state2 = dirtools.DirState(dirtools.Dir('/test_dirtools2'), index_cmp=dirtools.filehash)
     diff = dir_state2 - dir_state
     self.assertEqual(diff, {'deleted': ['file1', 'dir2/file_dir2'], 'updated': ['dir1/subdir1/file_subdir1'], 'deleted_dirs': ['dir2'], 'created': ['new_file']})
     self.assertEqual(diff, dirtools.compute_diff(dir_state2.state, dir_state.state))
Beispiel #5
0
    def testDirectoryFilter(self):
        """ Check that directory_filter causes directories to be excluded based on rule """
        def project_filter(root, dirs, files):
            if ".project" in files:
                return False
            else:
                return True

        dir_filter = dirtools.Dir('/test_dirtools',
                                  directory_filter=project_filter)

        self.assertEqual(
            sorted(dir_filter.files()),
            sorted(
                ["file1", "file2", "file3.py", ".exclude", "dir2/file_dir2"]))