def process(self, event): flags_ = flags.from_mask(event.mask) if flags.ISDIR in flags_: if flags.DELETE in flags_: path = os.path.join(self.TRACKING_DIRS, event.directory.path) if os.path.exists(path): os.system('rm -r {}'.format(path)) else: # TODO: add proper exception logger_srv.error('Do not support "{}" type of operations ' .format(flags_)) raise NotImplementedError else: path = os.path.join(self.TRACKING_DIRS, event.directory.path, event.name) if flags.MODIFY in flags_: fc.patch(path, event.diff) elif flags.DELETE in flags_: if os.path.exists(path): os.system('rm {}'.format(path)) else: # TODO: add proper exception logger_srv.error('Do not support "{}" type of operations ' .format(flags_)) raise NotImplementedError
def test_on_modify(self): os.system('echo hello > tmp/hello.txt') events = self.process_dummy_events() self.assertEqual(len(events), 2) os.system('mv tmp/hello.txt tmp/hello_check.txt') os.system('touch tmp/hello.txt tracking_dirs/tmp/hello.txt') diff = fc.diff('tmp/hello.txt', 'tmp/.ds/hello.txt') event = events[1] event.diff = diff self.ep.process(event) self.assertTrue( fc.is_equal('tracking_dirs/tmp/hello.txt', 'tmp/hello_check.txt'))
def test_replace_and_delete_in_files_for_patch(self): os.system('echo hello >' + self.file1) os.system('echo awful >>' + self.file1) os.system('echo world >>' + self.file1) os.system('echo haaaa!! >> ' + self.file1) os.system('echo hello > ' + self.file2) os.system('echo great >> ' + self.file2) os.system('echo my >> ' + self.file2) os.system('echo world >> ' + self.file2) diff = fc.diff(self.file1, self.file2) self.assertFalse(fc.is_equal(self.file1, self.file2)) self.assertEqual(len(diff), 3) fc.patch(self.file1, diff) self.assertTrue(fc.is_equal(self.file1, self.file2))
def test_on_modify_file_in_directory(self): # Add file to new directory os.system('touch tmp/hello/hello.txt') events = self.m.get_events() self.assertEqual(len(events), 1) flags_ = self.m.get_flags(events[0]) self.assertIn(flags.CREATE, flags_) self.ep.process(events[0]) self.assertTrue(os.path.exists('tmp/.ds/hello/hello.txt')) self.assertTrue( fc.is_equal('tmp/hello/hello.txt', 'tmp/.ds/hello/hello.txt')) # Modify file in new directory os.system('echo hello >> tmp/hello/hello.txt') events = self.m.get_events() self.assertEqual(len(events), 1) flags_ = self.m.get_flags(events[0]) self.assertIn(flags.MODIFY, flags_) self.ep.process(events[0]) self.assertTrue(os.path.exists('tmp/.ds/hello/hello.txt')) self.assertTrue( fc.is_equal('tmp/hello/hello.txt', 'tmp/.ds/hello/hello.txt')) # Delete file in new directory os.system('rm tmp/hello/hello.txt') events = self.m.get_events() self.assertEqual(len(events), 1) flags_ = self.m.get_flags(events[0]) self.assertIn(flags.DELETE, flags_) self.ep.process(events[0]) self.assertFalse(os.path.exists('tmp/hello/hello.txt')) self.assertFalse(os.path.exists('tmp/.ds/hello/hello.txt'))
def process(self, event): flags_ = flags.from_mask(event.mask) # Generate path to local copy for new object to have chance # get changes on object modify event later. ds_path_to_new_object = \ os.path.join(self.ds_path, event.directory.path[len(self.directory)+1:]) if flags.ISDIR in flags_: if flags.CREATE in flags_: copy_tree(event.directory.path, ds_path_to_new_object) self.sender.send_directory(event.directory.path) elif flags.DELETE in flags_: os.system('rm -r {}'.format(ds_path_to_new_object)) self.sender.send_event(event) else: logger_cl.error('Can not handle "{}" event'.format(flags_)) # TODO: add proper exception raise NotImplementedError else: file_path = os.path.join(event.directory.path, event.name) ds_file_path = os.path.join(ds_path_to_new_object, event.name) if flags.CREATE in flags_: if not os.path.exists(ds_path_to_new_object): os.mkdir(ds_path_to_new_object) os.system('cp {} {}'.format(file_path, ds_file_path)) self.sender.send_file(event.directory.path, event.name) elif flags.MODIFY in flags_: diff = fc.diff(ds_file_path, file_path) fc.patch(ds_file_path, diff) event.diff = diff self.sender.send_event(event) elif flags.DELETE in flags_: if os.path.exists(ds_file_path): os.system('rm -r {}'.format(ds_file_path)) self.sender.send_event(event) else: # TODO: add proper exception logger_cl.error('Can not handle "{}" event'.format(flags_)) raise NotImplementedError
def test_on_modify(self): os.system('echo hello >> tmp/hello.txt') events = self.m.get_events() self.assertEqual(len(events), 1) flags_ = self.m.get_flags(events[0]) self.assertIn(flags.MODIFY, flags_) self.ep.process(events[0]) self.assertTrue(os.path.exists('tmp/.ds/hello.txt')) self.assertTrue(fc.is_equal('tmp/hello.txt', 'tmp/.ds/hello.txt'))
def test_compare_empty_and_non_empty_file_get_diff_patch_first_file(self): os.system('echo >' + self.file1) os.system('echo hello > ' + self.file2) diff = fc.diff(self.file1, self.file2) self.assertFalse(fc.is_equal(self.file1, self.file2)) self.assertEqual(len(diff), 1) os.system('echo world >> ' + self.file2) diff = fc.diff(self.file1, self.file2) self.assertFalse(fc.is_equal(self.file1, self.file2)) self.assertEqual(len(diff), 2) fc.patch(self.file1, diff) self.assertTrue(fc.is_equal(self.file1, self.file2))
def test_compare_two_empty_files(self): os.system('echo > ' + self.file1) os.system('echo >' + self.file2) self.assertTrue(fc.is_equal(self.file1, self.file2)) self.assertEqual(len(fc.diff(self.file1, self.file2)), 0)
def test_on_create(self): self.assertEqual(len(self.events), 1) self.assertIn(flags.CREATE, self.flags_) self.assertTrue(os.path.exists('tmp/.ds/hello.txt')) self.assertTrue(fc.is_equal('tmp/hello.txt', 'tmp/.ds/hello.txt'))