Skip to content

Commit 1c470b8

Browse files
authored
Merge pull request #235 from joshuagl/joshuagl/abstract-file-remove
Add remove() method to StorageBackendInterface
2 parents 01a0c95 + 807a775 commit 1c470b8

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

securesystemslib/storage.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,25 @@ def put(self, fileobj, filepath):
9393
raise NotImplementedError # pragma: no cover
9494

9595

96+
@abc.abstractmethod
97+
def remove(self, filepath):
98+
"""
99+
<Purpose>
100+
Remove the file at 'filepath' from the storage.
101+
102+
<Arguments>
103+
filepath:
104+
The full path to the file.
105+
106+
<Exceptions>
107+
securesystemslib.exceptions.StorageError, if the file can not be removed.
108+
109+
<Returns>
110+
None
111+
"""
112+
raise NotImplementedError # pragma: no cover
113+
114+
96115
@abc.abstractmethod
97116
def getsize(self, filepath):
98117
"""
@@ -214,6 +233,14 @@ def put(self, fileobj, filepath):
214233
"Can't write file %s" % filepath)
215234

216235

236+
def remove(self, filepath):
237+
try:
238+
os.remove(filepath)
239+
except (FileNotFoundError, PermissionError, OSError): # pragma: no cover
240+
raise securesystemslib.exceptions.StorageError(
241+
"Can't remove file %s" % filepath)
242+
243+
217244
def getsize(self, filepath):
218245
try:
219246
return os.path.getsize(filepath)

tests/test_storage.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ def test_files(self):
8181
with open(put_path, 'rb') as put_file:
8282
self.assertEqual(put_file.read(), self.fileobj.read())
8383

84+
self.assertTrue(os.path.exists(put_path))
85+
self.storage_backend.remove(put_path)
86+
self.assertFalse(os.path.exists(put_path))
87+
8488

8589
def test_folders(self):
8690
leaves = ['test1', 'test2', 'test3']

0 commit comments

Comments
 (0)