diff options
author | Lars Wirzenius <liw@liw.fi> | 2015-07-08 13:37:11 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2015-07-08 13:44:44 +0300 |
commit | 03b14e640be5ddabf10cd9f966b06bc8729a690e (patch) | |
tree | 7711e9fe64ad416dc89de6b6a81640b827fe9cb3 | |
parent | 8768f2df1754a974b58d749d38048c7cfc84b972 (diff) | |
download | obnam-03b14e640be5ddabf10cd9f966b06bc8729a690e.tar.gz |
Only allow changes to a mutable GADirectory
-rw-r--r-- | obnamlib/__init__.py | 1 | ||||
-rw-r--r-- | obnamlib/fmt_ga/__init__.py | 2 | ||||
-rw-r--r-- | obnamlib/fmt_ga/dirobj.py | 17 | ||||
-rw-r--r-- | obnamlib/fmt_ga/dirobj_tests.py | 32 |
4 files changed, 51 insertions, 1 deletions
diff --git a/obnamlib/__init__.py b/obnamlib/__init__.py index 673a2312..77bf961d 100644 --- a/obnamlib/__init__.py +++ b/obnamlib/__init__.py @@ -185,6 +185,7 @@ from fmt_ga import ( GAClientList, GAClient, GADirectory, + GAImmutableError, create_gadirectory_from_dict, GATree, GAChunkStore, diff --git a/obnamlib/fmt_ga/__init__.py b/obnamlib/fmt_ga/__init__.py index d3eb21c9..dc710d5c 100644 --- a/obnamlib/fmt_ga/__init__.py +++ b/obnamlib/fmt_ga/__init__.py @@ -19,7 +19,7 @@ from .client_list import GAClientList from .chunk_store import GAChunkStore from .indexes import GAChunkIndexes -from .dirobj import GADirectory, create_gadirectory_from_dict +from .dirobj import GADirectory, GAImmutableError, create_gadirectory_from_dict from .tree import GATree from .client import GAClient from .format import RepositoryFormatGA diff --git a/obnamlib/fmt_ga/dirobj.py b/obnamlib/fmt_ga/dirobj.py index 32e997b0..989a7954 100644 --- a/obnamlib/fmt_ga/dirobj.py +++ b/obnamlib/fmt_ga/dirobj.py @@ -16,6 +16,9 @@ # =*= License: GPL-3+ =*= +import obnamlib + + class GADirectory(object): def __init__(self): @@ -35,9 +38,15 @@ class GADirectory(object): return self._dict def add_file(self, basename): + self._require_mutable() self._dict['metadata'][basename] = {} + def _require_mutable(self): + if not self._mutable: + raise GAImmutableError() + def remove_file(self, basename): + self._require_mutable() if basename in self._dict['metadata']: del self._dict['metadata'][basename] @@ -48,15 +57,18 @@ class GADirectory(object): return self._dict['metadata'][basename].get(key) def set_file_key(self, basename, key, value): + self._require_mutable() self._dict['metadata'][basename][key] = value def get_subdir_basenames(self): return self._dict['subdirs'].keys() def add_subdir(self, basename, obj_id): + self._require_mutable() self._dict['subdirs'][basename] = obj_id def remove_subdir(self, basename): + self._require_mutable() if basename in self._dict['subdirs']: del self._dict['subdirs'][basename] @@ -64,6 +76,11 @@ class GADirectory(object): return self._dict['subdirs'][basename] +class GAImmutableError(obnamlib.ObnamError): + + msg = 'Attempt to modify an immutable GADirectory' + + def create_gadirectory_from_dict(a_dict): dir_obj = GADirectory() for basename in a_dict['metadata']: diff --git a/obnamlib/fmt_ga/dirobj_tests.py b/obnamlib/fmt_ga/dirobj_tests.py index 04f634c6..43e36869 100644 --- a/obnamlib/fmt_ga/dirobj_tests.py +++ b/obnamlib/fmt_ga/dirobj_tests.py @@ -46,6 +46,11 @@ class GADirectoryTests(unittest.TestCase): dir_obj.add_file('README') self.assertEqual(dir_obj.get_file_basenames(), ['README']) + def test_raises_error_if_immutable_and_file_is_added(self): + dir_obj = obnamlib.GADirectory() + dir_obj.set_immutable() + self.assertRaises(obnamlib.GAImmutableError, dir_obj.add_file, 'README') + def test_removes_file(self): dir_obj = obnamlib.GADirectory() dir_obj.add_file('README') @@ -57,6 +62,11 @@ class GADirectoryTests(unittest.TestCase): dir_obj.remove_file('README') self.assertEqual(dir_obj.get_file_basenames(), []) + def test_raises_error_if_immutable_and_file_is_removed(self): + dir_obj = obnamlib.GADirectory() + dir_obj.set_immutable() + self.assertRaises(obnamlib.GAImmutableError, dir_obj.remove_file, 'README') + def test_gets_file_key_when_unset(self): dir_obj = obnamlib.GADirectory() dir_obj.add_file('README') @@ -72,6 +82,14 @@ class GADirectoryTests(unittest.TestCase): dir_obj.get_file_key('README', obnamlib.REPO_FILE_MODE), 0123) + def test_raises_error_if_muutable_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_has_no_subdirs_initially(self): dir_obj = obnamlib.GADirectory() self.assertEqual(dir_obj.get_subdir_basenames(), []) @@ -81,6 +99,13 @@ class GADirectoryTests(unittest.TestCase): dir_obj.add_subdir('.git', 'obj-id') self.assertEqual(dir_obj.get_subdir_basenames(), ['.git']) + def test_raises_error_if_mutable_and_subdir_is_added(self): + dir_obj = obnamlib.GADirectory() + dir_obj.set_immutable() + self.assertRaises( + obnamlib.GAImmutableError, + dir_obj.add_subdir, '.git', 'obj-id') + def test_removes_subdir(self): dir_obj = obnamlib.GADirectory() dir_obj.add_subdir('.git', 'obj-id') @@ -92,6 +117,13 @@ class GADirectoryTests(unittest.TestCase): dir_obj.remove_subdir('.git') self.assertEqual(dir_obj.get_subdir_basenames(), []) + 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_returns_subdir_object_id(self): dir_obj = obnamlib.GADirectory() dir_obj.add_subdir('.git', 'obj-id') |