Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add remove() method to StorageBackendInterface #235

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions securesystemslib/storage.py
Expand Up @@ -93,6 +93,25 @@ def put(self, fileobj, filepath):
raise NotImplementedError # pragma: no cover


@abc.abstractmethod
def remove(self, filepath):
"""
<Purpose>
Remove the file at 'filepath' from the storage.

<Arguments>
filepath:
The full path to the file.

<Exceptions>
securesystemslib.exceptions.StorageError, if the file can not be removed.

<Returns>
None
"""
raise NotImplementedError # pragma: no cover


@abc.abstractmethod
def getsize(self, filepath):
"""
Expand Down Expand Up @@ -214,6 +233,14 @@ def put(self, fileobj, filepath):
"Can't write file %s" % filepath)


def remove(self, filepath):
try:
os.remove(filepath)
except (FileNotFoundError, PermissionError, OSError): # pragma: no cover
raise securesystemslib.exceptions.StorageError(
"Can't remove file %s" % filepath)


def getsize(self, filepath):
try:
return os.path.getsize(filepath)
Expand Down
4 changes: 4 additions & 0 deletions tests/test_storage.py
Expand Up @@ -81,6 +81,10 @@ def test_files(self):
with open(put_path, 'rb') as put_file:
self.assertEqual(put_file.read(), self.fileobj.read())

self.assertTrue(os.path.exists(put_path))
self.storage_backend.remove(put_path)
self.assertFalse(os.path.exists(put_path))


def test_folders(self):
leaves = ['test1', 'test2', 'test3']
Expand Down