def test_is_modified_false(self): """Return false when the directory timestamp is older than the tracked one""" db = Worker('/tmp/pacha_test/pacha_test.db') db.insert('/tmp',None, None, timestamp=9997446874) watch = daemon.SingleRepository('/tmp') self.assertFalse(watch.is_modified())
def test_is_modified_true(self): """Return true when a directory timestamp is newer than the tracked one""" db = Worker('/tmp/pacha_test/pacha_test.db') db.insert('/tmp',None, None, timestamp=1) watch = daemon.SingleRepository('/tmp') self.assertTrue(watch.is_modified())
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 start(config=None, foreground=False, run_once=False): if config == None: config=ConfigMapper(DB_FILE).stored_config() if not foreground: log_path = config['log_path'] log_enable = config['log_enable'] if not log_enable or log_path is None: daemon = supay.Daemon(name='pacha', log=False, pid_dir=PID_DIR) if log_enable and log_enable: daemon = supay.Daemon(name='pacha', catch_all_log=log_path, pid_dir=PID_DIR) daemon.start() daemon_log.debug('Daemon started') while True: try: db = Worker(DB_FILE) daemon_log.debug('reading repos from database') repos = [i for i in db.get_repos()] db.closedb() freq = frecuency(config['frequency']) #### *** DVCS Support HERE *** ### # try: # master = config['master'] # if master == 'True': # hg.update(config['hosts_path']) # daemon_log.debug('machine set to master') # except AttributeError, error: # pass for repo in repos: daemon_log.debug('looping over repos in db') repo_path = repo[1] if os.path.exists(repo_path): # catches a path no longer there stat_check = SingleRepository(repo_path) stat_check.synchronize() else: pass daemon_log.warning('path %s does not exist' % repo_path) if run_once: raise KeyboardInterrupt daemon_log.debug('daemon going to sleep for %s seconds' % freq) time.sleep(freq) except KeyboardInterrupt: print "Exiting from foreground daemon" sys.exit(0) except Exception, error: daemon_log.error('Fatal exception - daemon killed') daemon_log.error(error) sys.exit(1)
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)
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)
def insert(self, path, own, grp, permissions, ftype): """For every file, sends the info to the database""" db = Worker(DB_FILE) db.insert_meta(path, own, grp, permissions, ftype) rwx_log.debug("inserting path to database: %s" % path)
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)