From 05f129b1bfb76bf4cfa5a648a9512db02ebc84a9 Mon Sep 17 00:00:00 2001 From: Siddharth Ravikumar Date: Wed, 21 Jan 2015 22:25:41 -0500 Subject: combox/events.py: now when files are -- created, moved, removed, modified -- their info is kept track in simple db file (using PickleDB). also updated tests/events_test.py to test if the newly added code to that updates the db works properly. --- combox/events.py | 17 +++++++++++------ tests/events_test.py | 32 +++++++++++++++++++++++++++----- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/combox/events.py b/combox/events.py index 782ab75..00fba84 100644 --- a/combox/events.py +++ b/combox/events.py @@ -26,6 +26,7 @@ from watchdog.events import FileSystemEventHandler from combox.crypto import split_and_encrypt from combox.file import (mk_nodedir, rm_nodedir, rm_shards, relative_path, move_shards, move_nodedir) +from combox.silo import ComboxSilo class ComboxDirMonitor(FileSystemEventHandler): @@ -38,6 +39,7 @@ class ComboxDirMonitor(FileSystemEventHandler): config: a dictinary which contains combox configuration. """ self.config = config + self.silo = ComboxSilo(self.config) logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s', @@ -50,12 +52,12 @@ class ComboxDirMonitor(FileSystemEventHandler): if event.is_directory: # creates a corresponding directory at the node dirs. move_nodedir(event.src_path, event.dest_path, self.config) - # TODO: code for updating files under the renamed - # directory in YAML silo. else: # file moved move_shards(event.src_path, event.dest_path, self.config) - # TODO: code for updating file info in YAML silo. + # update file info in silo. + self.silo.remove(event.src_path) + self.silo.update(event.dest_path) type_ = 'directory' if event.is_directory else 'file' logging.info("Moved %s: from %s to %s", type_, event.src_path, @@ -71,7 +73,8 @@ class ComboxDirMonitor(FileSystemEventHandler): else: # file was created split_and_encrypt(event.src_path, self.config) - # TODO: code for storing file info in YAML silo. + # store file info in silo. + self.silo.update(event.src_path) type_ = 'directory' if event.is_directory else 'file' logging.info("Created %s: %s", type_, event.src_path) @@ -87,7 +90,8 @@ class ComboxDirMonitor(FileSystemEventHandler): # remove the corresponding file shards in the node # directories. rm_shards(event.src_path, self.config) - # TODO: code for removing file info from YAML silo. + # remove file info from silo. + self.silo.remove(event.src_path) type_ = 'directory' if event.is_directory else 'file' logging.info("Deleted %s: %s", type_, event.src_path) @@ -102,7 +106,8 @@ class ComboxDirMonitor(FileSystemEventHandler): else: # file was modified split_and_encrypt(event.src_path, self.config) - # TODO: code for updating file info in YAML silo. + # update file info in silo. + self.silo.update(event.src_path) type_ = 'directory' if event.is_directory else 'file' logging.info("Modified %s: %s", type_, event.src_path) diff --git a/tests/events_test.py b/tests/events_test.py index 3eadc87..4ede3f1 100644 --- a/tests/events_test.py +++ b/tests/events_test.py @@ -33,6 +33,7 @@ from combox.crypto import decrypt_and_glue from combox.events import ComboxDirMonitor from combox.file import (relative_path, purge_dir, read_file, write_file) +from combox.silo import ComboxSilo CONFIG_DIR = path.join('tests', 'test-config') @@ -139,11 +140,17 @@ def test_CEH(): time.sleep(1) ## check if the shards were created. shardedp(TEST_FILE_COPY_0) + ## check if the new file's info is in silo + silo = ComboxSilo(config) + assert silo.exists(TEST_FILE_COPY_0) # Test - File deletion. remove(TEST_FILE_COPY_0) time.sleep(1) path_deletedp(TEST_FILE_COPY_0) + ## check if the new file's info is removed from silo + silo = ComboxSilo(config) + assert not silo.exists(TEST_FILE_COPY_0) # Test - directory creation TEST_DIR_0 = path.join(FILES_DIR, 'foo') @@ -168,17 +175,22 @@ def test_CEH(): TEST_DIR_1_NEW = path.join(path.dirname(TEST_DIR_1), 'snafu') TEST_FILE_COPY_1_NEW = path.join(TEST_DIR_1_NEW, path.basename(TEST_FILE)) + os.rename(TEST_DIR_1, TEST_DIR_1_NEW) time.sleep(1) renamedp(TEST_DIR_1, TEST_DIR_1_NEW) renamedp(TEST_FILE_COPY_1, TEST_FILE_COPY_1_NEW) + ## check if the new file's info is updated in silo + silo = ComboxSilo(config) + assert not silo.exists(TEST_FILE_COPY_1) + assert silo.exists(TEST_FILE_COPY_1_NEW) # Test directory & file deletion purge_dir(TEST_DIR_0) # remove the directory itself. os.rmdir(TEST_DIR_0) time.sleep(2) - path_deletedp(TEST_FILE_COPY_1) + path_deletedp(TEST_FILE_COPY_1_NEW) path_deletedp(TEST_DIR_1, True) path_deletedp(TEST_DIR_0, True) @@ -190,15 +202,22 @@ def test_CEH(): copyfile(lorem_file, lorem_file_copy) time.sleep(1) shardedp(lorem_file_copy) + ## check if the lorem_file_copy's info is stored in silo + silo = ComboxSilo(config) + lorem_file_copy_hash = silo.db.get(lorem_file_copy) ipsum_file = path.join(FILES_DIR, 'ipsum.txt') ipsum_content = read_file(ipsum_file) - lorem_content = read_file(lorem_file_copy) - lorem_content = "%s\n%s" % (lorem_content, ipsum_content) + lorem_copy_content = read_file(lorem_file_copy) + lorem_copy_content = "%s\n%s" % (lorem_copy_content, ipsum_content) # write lorem's new content to lorem_file_copy - write_file(lorem_file_copy, lorem_content) + write_file(lorem_file_copy, lorem_copy_content) time.sleep(1) + ## check if the lorem_file_copy's info is updated in silo + silo = ComboxSilo(config) + assert lorem_file_copy_hash != silo.db.get(lorem_file_copy) + # decrypt_and_glue will decrypt the file shards, glues them and # writes it to the respective file @@ -206,13 +225,16 @@ def test_CEH(): time.sleep(1) lorem_content_from_disk = read_file(lorem_file_copy) - assert lorem_content == lorem_content_from_disk + assert lorem_copy_content == lorem_content_from_disk # remove lorem_file_copy and confirm that its shards are deleted # in the node directories. remove(lorem_file_copy) time.sleep(1) path_deletedp(lorem_file_copy) + ## check if the lorem_file_copy's info is deleted from silo + silo = ComboxSilo(config) + assert not silo.exists(lorem_file_copy) observer.stop() observer.join() -- cgit v1.2.3