def test_synchronize_false(self): """Do not synchronize anything if the timestamp is older""" db = Worker('/tmp/pacha_test/pacha_test.db') db.insert('/tmp/pacha_test',None, None, timestamp=9999999999) watch = daemon.SingleRepository('/tmp/pacha_test') watch.synchronize() repo = [i for i in db.get_repo('/tmp/pacha_test')] self.assertFalse(os.path.isdir('/tmp/remote_pacha/hosts/%s/pacha_test' % host.hostname())) self.assertEqual(repo[0][4], u'9999999999')
def test_synchronize_true(self): """When we find a modified file we need to synchronize""" db = Worker('/tmp/pacha_test/pacha_test.db') db.insert('/tmp/pacha_test',None, None, timestamp=1) watch = daemon.SingleRepository('/tmp/pacha_test') watch.synchronize() repo = [i for i in db.get_repo('/tmp/pacha_test')] self.assertTrue(os.path.isdir('/tmp/remote_pacha/hosts/%s/pacha_test' % host.hostname())) self.assertNotEqual(repo[0][4], 1)
def test_store_timestamp(self): """Make sure we are storing (updating) the timestamp""" db = Worker('/tmp/pacha_test/pacha_test.db') db.insert('/tmp',None, None, timestamp=9997446874) db.closedb() watch = daemon.SingleRepository('/tmp') watch.store_timestamp(111) dbase = Worker('/tmp/pacha_test/pacha_test.db') repo = [i for i in dbase.get_repo('/tmp')] actual = repo[0][4] expected = u'111' self.assertEqual(actual, expected)
class SingleRepository(object): """ Compares the modified time of a file or directory via lstat and the timestamp stored in the database """ def __init__(self, path): self.dir_path = os.path.normpath(path) self.path = os.path.normpath(path) if not os.path.isdir(path): self.dir_path = os.path.dirname(path) self.db = Worker(DB_FILE) def is_modified(self): """checks whether the directory or the file is modified """ daemon_log.debug('looking for modified files in %s' % self.path) repo = self.db.get_repo(self.path) saved_tstamp = int([i[4] for i in repo][0]) stat = os.lstat(self.path) modified = int(stat.st_mtime) if saved_tstamp < modified: return modified return False def synchronize(self): timestamp = self.is_modified() if timestamp: daemon_log.info('found modified files in %s' % self.path) sync = Sync(path=self.path) sync.sync() # Since we got a modified file, update the DB: self.store_timestamp(timestamp) def store_timestamp(self, new_tstamp): """If we find a newly modified repository the database needs to be updated""" daemon_log.debug('adding new modified timestamp to path %s' % self.path) self.db.update_timestamp(self.path, new_tstamp)