summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--combox/events.py52
-rw-r--r--tests/events_test.py24
2 files changed, 75 insertions, 1 deletions
diff --git a/combox/events.py b/combox/events.py
index 00fba84..f4bbcc0 100644
--- a/combox/events.py
+++ b/combox/events.py
@@ -46,6 +46,58 @@ class ComboxDirMonitor(FileSystemEventHandler):
datefmt='%Y-%m-%d %H:%M:%S')
+ def housekeep(self):
+ """Recursively traverses combox directory, discovers changes and updates silo and node directories.
+
+ Information about files that have been removed from combox
+ directory is purged from the silo. Also the corresponding
+ shards are removed from the node directories.
+
+ The untracked files are encrypted and split between the node
+ directories and information about these files are stashed in
+ the silo.
+
+ Information about modified files in the combox directory are
+ updated and the file's shards are updated.
+
+ """
+ print "combox is housekeeping."
+ print "Please don't make any changes to combox directory now."
+ print "Thanks for your patience."
+
+ # Remove information about files that were deleted.
+ for fpath in self.silo.keys():
+ if not path.exists(fpath):
+ # remove this file's info from silo.
+ print fpath, "was deleted. Removing it from DB."
+ self.silo.remove(fpath)
+ # also purge the file's shards in node directories.
+ rm_shards(fpath, self.config)
+
+ # Add/update information about files that were created/modded.
+ # Also do split_and_encrypt on files that were created/modded.
+ for root, dirs, files in os.walk(self.config['combox_dir']):
+
+ for f in files:
+ fpath = path.join(root, f)
+
+ if (self.silo.exists(fpath) and
+ self.silo.stale(fpath)):
+ # file was modified
+ print fpath, "was modified. Updating DB and shards..."
+ split_and_encrypt(fpath, self.config)
+ self.silo.update(fpath)
+ elif not self.silo.exists(fpath):
+ # new file
+ print 'Adding new file', fpath, '...'
+ split_and_encrypt(fpath, self.config)
+ self.silo.update(fpath)
+
+
+ print "combox is done with the drudgery."
+ print "Do what you want to the combox directory."
+
+
def on_moved(self, event):
super(ComboxDirMonitor, self).on_moved(event)
diff --git a/tests/events_test.py b/tests/events_test.py
index f9579b0..5c726d9 100644
--- a/tests/events_test.py
+++ b/tests/events_test.py
@@ -32,7 +32,9 @@ from combox.config import get_nodedirs
from combox.crypto import decrypt_and_glue
from combox.events import ComboxDirMonitor
from combox.file import (relative_path, purge_dir,
- read_file, write_file)
+ read_file, write_file,
+ rm_shards)
+
from combox.silo import ComboxSilo
@@ -238,3 +240,23 @@ def test_CDM():
observer.stop()
observer.join()
+
+
+def test_housekeep():
+ """ComboxDirMonitor's housekeep method test."""
+
+ lorem = path.join(FILES_DIR, 'lorem.txt')
+ lorem_moved = path.join(FILES_DIR, 'lorem.moved.txt')
+ os.rename(lorem, lorem_moved)
+
+ cdm = ComboxDirMonitor(config)
+ cdm.housekeep()
+
+ silo = ComboxSilo(config)
+ assert not silo.exists(lorem)
+ assert silo.exists(lorem_moved)
+ shardedp(lorem_moved)
+
+ os.rename(lorem_moved, lorem)
+ rm_shards(lorem_moved, config)
+ silo.remove(lorem_moved)