summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSiddharth Ravikumar <sravik@bgsu.edu>2015-03-26 16:21:14 -0400
committerSiddharth Ravikumar <sravik@bgsu.edu>2015-03-26 16:21:14 -0400
commitb4e67e3131f1b0854f7c35ef30bccb744b5c6a9b (patch)
tree810940ee5d092afd98fc443d562b2f37d6c881e2
parent53cae94179540292c9711ded5135d4f85e67ecac (diff)
fleshed out combox.events.NodDirMonitor.housekeep method.
wrote tests for it too :~( modified: combox/events.py modified: tests/events_test.py
-rw-r--r--combox/events.py50
-rw-r--r--tests/events_test.py73
2 files changed, 120 insertions, 3 deletions
diff --git a/combox/events.py b/combox/events.py
index 12d0cbe..4b91ed5 100644
--- a/combox/events.py
+++ b/combox/events.py
@@ -23,10 +23,12 @@ from os import path
from watchdog.events import LoggingEventHandler
+from combox.config import get_nodedirs
from combox.crypto import split_and_encrypt, decrypt_and_glue
from combox.file import (mk_nodedir, rm_nodedir, rm_shards,
relative_path, move_shards, move_nodedir,
- cb_path, node_path, hash_file, rm_path)
+ cb_path, node_path, hash_file, rm_path,
+ node_paths)
from combox.silo import ComboxSilo
@@ -230,6 +232,52 @@ class NodeDirMonitor(LoggingEventHandler):
"""
self.silo_update()
+ print "combox node monitor is housekeeping."
+ print "Please don't make any changes to combox directory now."
+ print "Thanks for your patience."
+
+ # Remove files from the combox directory whose shards were
+ # deleted.
+ for fpath in self.silo.keys():
+ fshards = node_paths(fpath, self.config, True)
+
+ for fshard in fshards:
+ if not path.exists(fshard):
+ # remove the file from combox directory.
+ rm_path(fpath)
+ print fpath, "was deleted on another computer. Removing it."
+ # update silo.
+ self.silo.remove(fpath)
+ break
+
+ for root, dirs, files in os.walk(get_nodedirs(self.config)[0]):
+ for f in files:
+ shard = path.join(root, f)
+
+ if not self.shardp(shard):
+ continue
+
+ file_cb_path = cb_path(shard, self.config)
+ if not self.silo.exists(file_cb_path):
+ print file_cb_path, "was created remotely. Creating it locally now..."
+ decrypt_and_glue(file_cb_path, self.config)
+ self.silo.update(file_cb_path)
+ elif self.silo.exists(file_cb_path):
+ file_content = decrypt_and_glue(file_cb_path,
+ self.config,
+ write=False)
+ file_content_hash = hash_file(file_cb_path, file_content)
+
+ if self.silo.stale(file_cb_path, file_content_hash):
+ # file modified
+ print file_cb_path, "modified remotely. Updating local copy."
+ decrypt_and_glue(file_cb_path, self.config)
+ self.silo.update(file_cb_path)
+
+
+ print "combox node monitor is done with the drudgery."
+ print "Do what you want to the combox directory."
+
def on_moved(self, event):
super(NodeDirMonitor, self).on_moved(event)
diff --git a/tests/events_test.py b/tests/events_test.py
index adaf86f..496c78e 100644
--- a/tests/events_test.py
+++ b/tests/events_test.py
@@ -31,7 +31,7 @@ from watchdog.observers import Observer
from combox.config import get_nodedirs
from combox.crypto import decrypt_and_glue, split_and_encrypt
from combox.events import ComboxDirMonitor, NodeDirMonitor
-from combox.file import (relative_path, purge_dir,
+from combox.file import (relative_path, purge_dir, hash_file,
read_file, write_file, move_shards,
rm_shards, mk_nodedir, rm_nodedir,
move_nodedir)
@@ -185,7 +185,7 @@ class TestEvents(object):
observer.join()
- def test_housekeep(self):
+ def test_CDM_housekeep(self):
"""ComboxDirMonitor's housekeep method test."""
# test file deletion and addition
@@ -373,6 +373,75 @@ class TestEvents(object):
observer.join()
+ def test_NDM_housekeep(self):
+ """Testing NodeDirMonitor's housekeep method."""
+
+ # files for testing deletion.
+ testf1 = path.join(self.FILES_DIR, 'hitchhikers.png')
+ testf2 = path.join(self.FILES_DIR, 'lorem.housekeep')
+ copyfile(self.TEST_FILE, testf1)
+ copyfile(self.lorem, testf2)
+
+ silo = ComboxSilo(self.config)
+ silo.update(testf1)
+ silo.update(testf2)
+
+ ndm = NodeDirMonitor(self.config)
+ ndm.housekeep()
+
+ assert not path.exists(testf1)
+ assert not path.exists(testf2)
+
+ self.purge_list.append(testf1)
+ self.purge_list.append(testf2)
+
+ # test shard creation
+ hmutant = "%s.mutant" % self.TEST_FILE
+ hmutant_content = read_file(self.TEST_FILE)
+
+ split_and_encrypt(hmutant, self.config,
+ hmutant_content)
+
+ ndm = NodeDirMonitor(self.config)
+ ndm.housekeep()
+
+ assert path.exists(hmutant)
+ assert hmutant_content == read_file(hmutant)
+
+ self.purge_list.append(hmutant)
+
+ # test shard modification
+ lcopy = "%s.copy" % self.lorem
+ copyfile(self.lorem, lcopy)
+ lcopy_content = read_file(lcopy)
+ split_and_encrypt(lcopy, self.config,
+ lcopy_content)
+
+ silo = ComboxSilo(self.config)
+ silo.update(lcopy)
+ shardedp(lcopy)
+
+ silo = ComboxSilo(self.config)
+ lcopy_hash = silo.db.get(lcopy)
+
+ ipsum_content = read_file(self.ipsum)
+ lcopy_content = "%s\n%s" % (lcopy_content, ipsum_content)
+
+ split_and_encrypt(lcopy, self.config,
+ lcopy_content)
+
+ ndm = NodeDirMonitor(self.config)
+ ndm.housekeep()
+
+ ## check if the lorem_file_copy's info is updated in silo
+ silo = ComboxSilo(self.config)
+
+ assert lcopy_content == read_file(lcopy)
+ assert hash_file(lcopy, lcopy_content) == silo.db.get(lcopy)
+
+ self.purge_list.append(lcopy)
+
+
def test_NDM_shardp(self):
"""Testing shardp method in NodeDirMonitor class"""
shard = 'some.shard0'