def _flush_added_file(self, filename): mode = self._added_files.get_file_key(filename, obnamlib.REPO_FILE_MODE) assert mode is not None file_dict = self._added_files.get_file_dict(filename) if stat.S_ISDIR(mode): dir_obj = obnamlib.GADirectory() dir_obj.add_file('.') for key, value in file_dict['keys'].items(): dir_obj.set_file_key('.', key, value) self._tree.set_directory(filename, dir_obj) else: basename = os.path.basename(filename) parent_path = os.path.dirname(filename) parent_obj = self._tree.get_directory(parent_path) if parent_obj is None: parent_obj = obnamlib.GADirectory() parent_obj.add_file('.') else: parent_obj = self._make_mutable(parent_obj) parent_obj.add_file(basename) for key, value in file_dict['keys'].items(): parent_obj.set_file_key(basename, key, value) for chunk_id in file_dict['chunks']: parent_obj.append_file_chunk_id(basename, chunk_id) self._tree.set_directory(parent_path, parent_obj) self._added_files.remove_file(filename)
def test_raises_error_if_mutable_and_file_chunk_ids_are_cleared(self): dir_obj = obnamlib.GADirectory() dir_obj.add_file('README') dir_obj.set_immutable() self.assertRaises( obnamlib.GAImmutableError, dir_obj.clear_file_chunk_ids, 'README')
def test_raises_error_if_immutable_and_file_is_removed(self): dir_obj = obnamlib.GADirectory() dir_obj.add_file('README') dir_obj.set_immutable() self.assertRaises( obnamlib.GAImmutableError, dir_obj.remove_file, 'README')
def test_clears_file_chunk_ids(self): dir_obj = obnamlib.GADirectory() dir_obj.add_file('README') dir_obj.append_file_chunk_id('README', 'chunk-1') dir_obj.append_file_chunk_id('README', 'chunk-2') dir_obj.clear_file_chunk_ids('README') self.assertEqual(dir_obj.get_file_chunk_ids('README'), [])
def test_sets_file_key(self): dir_obj = obnamlib.GADirectory() dir_obj.add_file('README') dir_obj.set_file_key('README', obnamlib.REPO_FILE_MODE, 0123) self.assertEqual( dir_obj.get_file_key('README', obnamlib.REPO_FILE_MODE), 0123)
def test_raises_error_if_mutable_and_file_chunk_id_is_appended(self): dir_obj = obnamlib.GADirectory() dir_obj.add_file('README') dir_obj.set_immutable() self.assertRaises( obnamlib.GAImmutableError, dir_obj.append_file_chunk_id, 'README', 'chunk-1')
def test_raises_error_if_mutable_and_file_key_is_set(self): dir_obj = obnamlib.GADirectory() dir_obj.add_file('README') dir_obj.set_immutable() self.assertRaises( obnamlib.GAImmutableError, dir_obj.set_file_key, 'README', obnamlib.REPO_FILE_MODE, 0123)
def test_maps_file_key_to_short_name_and_back(self): key = obnamlib.REPO_FILE_SIZE dir_obj = obnamlib.GADirectory() short = dir_obj.get_short_key_name(key) key2 = dir_obj.get_key_from_short_name(short) self.assertEqual(key, key2)
def test_removes_persistent_directory(self): dir_obj = obnamlib.GADirectory() self.tree.set_directory('/foo/bar', dir_obj) self.tree.flush() tree2 = obnamlib.GATree() tree2.set_blob_store(self.blob_store) tree2.set_root_directory_id(self.tree.get_root_directory_id()) tree2.remove_directory('/foo/bar') self.assertEqual(tree2.get_directory('/foo/bar'), None)
def test_stores_objects_persistently(self): orig = obnamlib.GADirectory() self.tree.set_directory('/foo/bar', orig) self.tree.flush() tree2 = obnamlib.GATree() tree2.set_blob_store(self.blob_store) tree2.set_root_directory_id(self.tree.get_root_directory_id()) retrieved = tree2.get_directory('/foo/bar') self.assertEqual(orig.as_dict(), retrieved.as_dict())
def test_updates_subdirectory_of_persistent_directory(self): # Create original subdir. self.tree.set_directory('/foo/bar', obnamlib.GADirectory()) self.tree.flush() # Load new tree, so no cached objects. Update a subdir with a file. tree2 = obnamlib.GATree() tree2.set_blob_store(self.blob_store) tree2.set_root_directory_id(self.tree.get_root_directory_id()) new_subdir = obnamlib.GADirectory() new_subdir.add_file('README') tree2.set_directory('/foo/bar', new_subdir) tree2.flush() # Another new tree. The added file should be visible. tree3 = obnamlib.GATree() tree3.set_blob_store(self.blob_store) tree3.set_root_directory_id(tree2.get_root_directory_id()) subdir = tree3.get_directory('/foo/bar') self.assertIn('README', subdir.get_file_basenames())
def test_creates_GADirectory_from_dict(self): orig = obnamlib.GADirectory() orig.add_file('.') orig.set_file_key( '.', obnamlib.REPO_FILE_MODE, stat.S_IFDIR | 0755) orig.add_file('README') orig.set_file_key( 'README', obnamlib.REPO_FILE_MODE, stat.S_IFREG | 0644) orig.add_subdir('.git', 'git-dir-id') new = obnamlib.create_gadirectory_from_dict(orig.as_dict()) self.assertEqual(new.as_dict(), orig.as_dict())
def test_removes_nonexistent_directory_when_tree_is_not_empty(self): dir_obj = obnamlib.GADirectory() self.tree.set_directory('/foo/bar', dir_obj) self.tree.flush() tree2 = obnamlib.GATree() tree2.set_blob_store(self.blob_store) tree2.set_root_directory_id(self.tree.get_root_directory_id()) tree2.remove_directory('/some/other/path') self.assertEqual(tree2.get_directory('/some/other/path'), None) self.assertEqual(self.tree.get_root_directory_id(), tree2.get_root_directory_id())
def set_directory(self, pathname, dir_obj): self._cache.set(pathname, dir_obj) if pathname != '/': basename = os.path.basename(pathname) parent_path = os.path.dirname(pathname) parent_obj = self._cache.get(parent_path) if not parent_obj: parent_obj = self.get_directory(parent_path) if parent_obj: parent_obj = obnamlib.create_gadirectory_from_dict( parent_obj.as_dict()) else: parent_obj = obnamlib.GADirectory() parent_obj.add_file('.') if not parent_obj.is_mutable(): parent_obj = obnamlib.create_gadirectory_from_dict( parent_obj.as_dict()) parent_obj.add_subdir(basename, None) self.set_directory(parent_path, parent_obj)
def test_removes_directory(self): dir_obj = obnamlib.GADirectory() self.tree.set_directory('/foo/bar', dir_obj) self.tree.remove_directory('/foo/bar') self.assertEqual(self.tree.get_directory('/foo/bar'), None)
def test_returns_subdir_object_id(self): dir_obj = obnamlib.GADirectory() dir_obj.add_subdir('.git', 'obj-id') self.assertEqual(dir_obj.get_subdir_object_id('.git'), 'obj-id')
def test_removes_root_directory(self): dir_obj = obnamlib.GADirectory() self.tree.set_directory('/', dir_obj) self.tree.remove_directory('/') self.assertEqual(self.tree.get_directory('/'), None) self.assertEqual(self.tree.get_root_directory_id(), None)
def test_sets_subdir_and_creates_all_parents(self): dir_obj = obnamlib.GADirectory() self.tree.set_directory('/foo/bar', dir_obj) self.assertEqual(self.tree.get_directory('/foo/bar'), dir_obj) self.assertNotEqual(self.tree.get_directory('/foo'), None) self.assertNotEqual(self.tree.get_directory('/'), None)
def test_returns_None_for_subdir_object_id_if_subdir_does_not_exist(self): dir_obj = obnamlib.GADirectory() self.assertEqual(dir_obj.get_subdir_object_id('.git'), None)
def test_is_mutable_initially(self): dir_obj = obnamlib.GADirectory() self.assertTrue(dir_obj.is_mutable())
def test_sets_root_dir(self): dir_obj = obnamlib.GADirectory() self.tree.set_directory('/', dir_obj) self.assertEqual(self.tree.get_directory('/'), dir_obj)
def test_can_be_made_immutable(self): dir_obj = obnamlib.GADirectory() dir_obj.set_immutable() self.assertFalse(dir_obj.is_mutable())
def test_removes_subdir(self): dir_obj = obnamlib.GADirectory() dir_obj.add_subdir('.git', 'obj-id') dir_obj.remove_subdir('.git') self.assertEqual(dir_obj.get_subdir_basenames(), [])
def test_returns_itself_as_dictionary(self): dir_obj = obnamlib.GADirectory() self.assertEqual(type(dir_obj.as_dict()), dict)
def test_raises_error_if_mutable_and_subdir_is_removed(self): dir_obj = obnamlib.GADirectory() dir_obj.set_immutable() self.assertRaises( obnamlib.GAImmutableError, dir_obj.remove_subdir, '.git')
def test_has_no_files_initially(self): dir_obj = obnamlib.GADirectory() self.assertEqual(dir_obj.get_file_basenames(), [])
def test_adds_file(self): dir_obj = obnamlib.GADirectory() dir_obj.add_file('README') self.assertEqual(dir_obj.get_file_basenames(), ['README'])
def test_removes_nonexistent_file(self): dir_obj = obnamlib.GADirectory() dir_obj.remove_file('README') self.assertEqual(dir_obj.get_file_basenames(), [])
def test_gets_file_key_when_unset(self): dir_obj = obnamlib.GADirectory() dir_obj.add_file('README') self.assertEqual( dir_obj.get_file_key('README', obnamlib.REPO_FILE_MODE), None)
def test_removes_nonexistent_subdir(self): dir_obj = obnamlib.GADirectory() dir_obj.remove_subdir('.git') self.assertEqual(dir_obj.get_subdir_basenames(), [])