aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Spaeth <Sebastian@SSpaeth.de>2012-12-12 16:29:14 +0100
committerChristopher Allan Webber <cwebber@dustycloud.org>2013-01-22 14:05:34 -0600
commitb34d7e1d9b53f393aa673cc431a26bc640164c39 (patch)
tree59d417e1d883bbf6ee52a40ee8cfc6570b4a1a8e
parent79f7cfd900c115fd4c1e4fe8e50f6aad6a56160f (diff)
downloadmediagoblin-b34d7e1d9b53f393aa673cc431a26bc640164c39.tar.lz
mediagoblin-b34d7e1d9b53f393aa673cc431a26bc640164c39.tar.xz
mediagoblin-b34d7e1d9b53f393aa673cc431a26bc640164c39.zip
Implement delete_dir in the FileStorage
plus options for deleting only empty directories and deleting them recursively. Not sure how cloudfile storage is or should be handled here. Are things such as a "directory" even a concept there?
-rw-r--r--mediagoblin/storage/__init__.py16
-rw-r--r--mediagoblin/storage/filestorage.py26
2 files changed, 39 insertions, 3 deletions
diff --git a/mediagoblin/storage/__init__.py b/mediagoblin/storage/__init__.py
index 2db4c37d..77e4212e 100644
--- a/mediagoblin/storage/__init__.py
+++ b/mediagoblin/storage/__init__.py
@@ -101,14 +101,28 @@ class StorageInterface(object):
def delete_file(self, filepath):
"""
- Delete or dereference the file at filepath.
+ Delete or dereference the file (not directory) at filepath.
+ TODO: is the below comment correct? AFAIK, we won't clean up empty directories...
This might need to delete directories, buckets, whatever, for
cleanliness. (Be sure to avoid race conditions on that though)
"""
# Subclasses should override this method.
self.__raise_not_implemented()
+ def delete_dir(self, dirpath, recursive=False):
+ """Delete the directory at dirpath
+
+ :param recursive: Usually, a directory must not contain any
+ files for the delete to succeed. If True, containing files
+ and subdirectories within dirpath will be recursively
+ deleted.
+
+ :returns: True in case of success, False otherwise.
+ """
+ # Subclasses should override this method.
+ self.__raise_not_implemented()
+
def file_url(self, filepath):
"""
Get the URL for this file. This assumes our storage has been
diff --git a/mediagoblin/storage/filestorage.py b/mediagoblin/storage/filestorage.py
index 00d6335e..c86315f1 100644
--- a/mediagoblin/storage/filestorage.py
+++ b/mediagoblin/storage/filestorage.py
@@ -62,10 +62,32 @@ class BasicFileStorage(StorageInterface):
return open(self._resolve_filepath(filepath), mode)
def delete_file(self, filepath):
- # TODO: Also delete unused directories if empty (safely, with
- # checks to avoid race conditions).
+ """Delete file at filepath
+
+ Raises OSError in case filepath is a directory."""
+ #TODO: log error
os.remove(self._resolve_filepath(filepath))
+ def delete_dir(self, dirpath, recursive=False):
+ """returns True on succes, False on failure"""
+
+ dirpath = self._resolve_filepath(dirpath)
+
+ # Shortcut the default and simple case of nonempty=F, recursive=F
+ if recursive:
+ try:
+ shutil.rmtree(dirpath)
+ except OSError as e:
+ #TODO: log something here
+ return False
+ else: # recursively delete everything
+ try:
+ os.rmdir(dirpath)
+ except OSError as e:
+ #TODO: log something here
+ return False
+ return True
+
def file_url(self, filepath):
if not self.base_url:
raise NoWebServing(