From 378c63382465bb77f2bf92288c209029d7436078 Mon Sep 17 00:00:00 2001 From: Siddharth Ravikumar Date: Wed, 21 Jan 2015 19:07:25 -0500 Subject: new module: combox/silo.py - Contains the ComboxSilo class which provides an interface to access the db that stores information about files in the combox directory. Tests for this module is at tests/silo_test.py --- combox/silo.py | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/silo_test.py | 87 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 186 insertions(+) create mode 100644 combox/silo.py create mode 100644 tests/silo_test.py diff --git a/combox/silo.py b/combox/silo.py new file mode 100644 index 0000000..cbb2065 --- /dev/null +++ b/combox/silo.py @@ -0,0 +1,99 @@ +# Copyright (C) 2015 Combox author(s). See AUTHORS. +# +# This file is part of Combox. +# +# Combox is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Combox is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Combox (see COPYING). If not, see +# . + +import pickledb + +from os import path + +from combox.file import hash_file + +class ComboxSilo(object): + """The Combox silo. + + Helps keep track of files in combox directory. + """ + + + def __init__(self, config): + """config: a dictinary which contains combox configuration. + + """ + self.config = config + + silo = path.join(config['silo_dir'], 'silo.db') + self.db = pickledb.load(silo, True) + + + def update(self, filep): + """Update filep's info in db + + filep and the hash of its content is written to the db. + + filep: path to the file in combox directory. + + """ + + fhash = hash_file(filep) + + return self.db.set(filep, fhash) + + + def remove(self, filep): + """Removes filep from db. + + filep: path to the file in combox directory. + + """ + + return self.db.rem(filep) + + + def exists(self, filep): + """Checks if filep's info is stored in db. + + Returns True if filep's info is in db; False otherwise. + + filep: path to the file in combox directory. + + """ + + if self.db.get(filep) is None: + return False + else: + return True + + + def stale(self, filep): + """Returns True if filep's hash is different from the hash stored in db. + + Returns None, if filep's info is not yet stored in db. + Returns False, if filep's hash has not changed it. + + filep: path to the file in combox directory. + + """ + + fhash = hash_file(filep) + fhash_in_db = self.db.get(filep) + + if fhash_in_db is None: + return None + elif fhash == fhash_in_db: + return False + else: + return True diff --git a/tests/silo_test.py b/tests/silo_test.py new file mode 100644 index 0000000..0f1558f --- /dev/null +++ b/tests/silo_test.py @@ -0,0 +1,87 @@ +# Copyright (C) 2015 Combox author(s). See AUTHORS. +# +# This file is part of Combox. +# +# Combox is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Combox is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Combox (see COPYING). If not, see +# . + +import yaml + +from shutil import copyfile +from os import path, remove + +from combox.silo import ComboxSilo +from combox.file import read_file, write_file + +CONFIG_DIR = path.join('tests', 'test-config') + +config_file = path.join(CONFIG_DIR, 'config.yaml') +try: + config = yaml.load(file(config_file, 'r')) +except yaml.YAMLError, exc: + raise AssertionError("Error in configuration file:", exc) + + +FILES_DIR = path.abspath(config['combox_dir']) +LOREM = path.join(FILES_DIR,'lorem.txt') +LOREM_IPSUM = path.join(FILES_DIR,'lorem-ipsum.txt') +IPSUM = path.join(FILES_DIR,'ipsum.txt') + + +def test_csilo(): + """ + Tests the ComboxSilo class. + """ + csilo = ComboxSilo(config) + + + # Test - update + csilo.update(LOREM) + lorem_content = read_file(LOREM) + lorem_hash = csilo.db.get(LOREM) + assert lorem_hash + + csilo.update(IPSUM) + ispum_content = read_file(IPSUM) + ipsum_hash = csilo.db.get(IPSUM) + assert ipsum_hash + + lorem_ipsum_content = "%s\n%s" % (LOREM, IPSUM) + write_file(LOREM_IPSUM, lorem_ipsum_content) + + csilo.update(LOREM_IPSUM) + lorem_ipsum_hash = csilo.db.get(LOREM_IPSUM) + assert lorem_ipsum_hash + + assert lorem_ipsum_hash != lorem_hash + assert lorem_ipsum_hash != ipsum_hash + + # Test - stale + lorem_ipsum_content = "%s\n%s" % (lorem_ipsum_content, IPSUM) + write_file(LOREM_IPSUM, lorem_ipsum_content) + + assert csilo.stale(LOREM_IPSUM) + csilo.update(LOREM_IPSUM) + assert csilo.stale(LOREM_IPSUM) is False + + lorem_ipsum_hash_new = csilo.db.get(LOREM_IPSUM) + assert lorem_ipsum_hash_new + assert lorem_ipsum_hash_new != lorem_ipsum_hash + + # Test - remove + remove(LOREM_IPSUM) + csilo.remove(LOREM_IPSUM) + + # Test - exists + assert not csilo.exists(LOREM_IPSUM) -- cgit v1.2.3