Cleanup empty dirs

This commit is contained in:
HelloZeroNet 2016-03-26 00:22:27 +01:00
parent 045a57491a
commit 6d222b6ed7
2 changed files with 20 additions and 2 deletions

View file

@ -27,7 +27,7 @@ class ContentManager(object):
# Load content.json to self.content # Load content.json to self.content
# Return: Changed files ["index.html", "data/messages.json"], Deleted files ["old.jpg"] # Return: Changed files ["index.html", "data/messages.json"], Deleted files ["old.jpg"]
def loadContent(self, content_inner_path="content.json", add_bad_files=True, delete_removed_files=True, load_includes=True, force=False): def loadContent(self, content_inner_path="content.json", add_bad_files=True, delete_removed_files=True, load_includes=True, force=False):
content_inner_path = content_inner_path.strip("/") # Remove / from begning content_inner_path = content_inner_path.strip("/") # Remove / from beginning
old_content = self.contents.get(content_inner_path) old_content = self.contents.get(content_inner_path)
content_path = self.site.storage.getPath(content_inner_path) content_path = self.site.storage.getPath(content_inner_path)
content_dir = helper.getDirname(self.site.storage.getPath(content_inner_path)) content_dir = helper.getDirname(self.site.storage.getPath(content_inner_path))
@ -61,7 +61,7 @@ class ContentManager(object):
for relative_path, info in new_content.get("files", {}).iteritems(): for relative_path, info in new_content.get("files", {}).iteritems():
if "sha512" in info: if "sha512" in info:
hash_type = "sha512" hash_type = "sha512"
else: # Backward compatiblity else: # Backward compatibility
hash_type = "sha1" hash_type = "sha1"
new_hash = info[hash_type] new_hash = info[hash_type]
@ -112,6 +112,20 @@ class ContentManager(object):
except Exception, err: except Exception, err:
self.log.debug("Error deleting file %s: %s" % (file_inner_path, err)) self.log.debug("Error deleting file %s: %s" % (file_inner_path, err))
# Cleanup empty dirs
tree = {root: [dirs, files] for root, dirs, files in os.walk(self.site.storage.getPath(content_inner_dir))}
for root in sorted(tree, key=len, reverse=True):
dirs, files = tree[root]
if dirs == [] and files == []:
root_inner_path = self.site.storage.getInnerPath(root.replace("\\", "/"))
self.log.debug("Empty directory: %s, cleaning up." % root_inner_path)
try:
self.site.storage.deleteDir(root_inner_path)
# Remove from tree dict to reflect changed state
tree[os.path.dirname(root)][0].remove(os.path.basename(root))
except Exception, err:
self.log.debug("Error deleting empty directory %s: %s" % (root_inner_path, err))
# Load includes # Load includes
if load_includes and "includes" in new_content: if load_includes and "includes" in new_content:
for relative_path, info in new_content["includes"].items(): for relative_path, info in new_content["includes"].items():

View file

@ -163,6 +163,10 @@ class SiteStorage:
file_path = self.getPath(inner_path) file_path = self.getPath(inner_path)
os.unlink(file_path) os.unlink(file_path)
def deleteDir(self, inner_path):
dir_path = self.getPath(inner_path)
os.rmdir(dir_path)
# List files from a directory # List files from a directory
def list(self, dir_inner_path): def list(self, dir_inner_path):
directory = self.getPath(dir_inner_path) directory = self.getPath(dir_inner_path)