From 5297812d0795d24cfe26f5eb21bac6c287d1e9a3 Mon Sep 17 00:00:00 2001 From: Daira Hopwood Date: Thu, 21 May 2015 15:43:51 +0100 Subject: [PATCH] Refactoring. Signed-off-by: Daira Hopwood --- src/allmydata/frontends/drop_upload.py | 26 ++++++++++++++++++++------ src/allmydata/test/test_drop_upload.py | 26 ++++++++++++++------------ 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/allmydata/frontends/drop_upload.py b/src/allmydata/frontends/drop_upload.py index 3f2c7aad..75fcb69a 100644 --- a/src/allmydata/frontends/drop_upload.py +++ b/src/allmydata/frontends/drop_upload.py @@ -4,6 +4,7 @@ from collections import deque from twisted.internet import defer, reactor, task from twisted.python.failure import Failure +from twisted.python import runtime from twisted.application import service from allmydata.interfaces import IDirectoryNode, NoSuchChildError, ExistingChildError @@ -15,6 +16,24 @@ from allmydata.immutable.upload import FileName from allmydata.scripts import backupdb +def get_inotify_module(): + try: + if sys.platform == "win32": + from allmydata.windows import inotify + elif runtime.platform.supportsINotify(): + from twisted.internet import inotify + else: + raise NotImplementedError("filesystem notification needed for drop-upload is not supported.\n" + "This currently requires Linux or Windows.") + return inotify + except ImportError as e: + self.log(e) + if sys.platform == "win32": + raise NotImplementedError("filesystem notification needed for drop-upload is not supported.\n" + "Windows support requires at least Vista, and has only been tested on Windows 7.") + raise + + class DropUploader(service.MultiService): name = 'drop-upload' @@ -38,12 +57,7 @@ class DropUploader(service.MultiService): self._upload_deque = deque() self.is_upload_ready = False - if inotify is None: - if sys.platform == "win32": - from allmydata.windows import inotify - else: - from twisted.internet import inotify - self._inotify = inotify + self._inotify = inotify or get_inotify_module() if not self._local_path.exists(): raise AssertionError("The '[drop_upload] local.directory' parameter was %s " diff --git a/src/allmydata/test/test_drop_upload.py b/src/allmydata/test/test_drop_upload.py index 8cfddb1e..fbf34cdf 100644 --- a/src/allmydata/test/test_drop_upload.py +++ b/src/allmydata/test/test_drop_upload.py @@ -2,7 +2,6 @@ import os, sys, stat, time from twisted.trial import unittest -from twisted.python import runtime from twisted.internet import defer from allmydata.interfaces import IDirectoryNode, NoSuchChildError @@ -14,6 +13,7 @@ from allmydata.test.no_network import GridTestMixin from allmydata.test.common_util import ReallyEqualMixin, NonASCIIPathMixin from allmydata.test.common import ShouldFailMixin +from allmydata.frontends import drop_upload from allmydata.frontends.drop_upload import DropUploader from allmydata.scripts import backupdb from allmydata.util.fileutil import abspath_expanduser_unicode @@ -147,7 +147,7 @@ class DropUploadTestMixin(GridTestMixin, ShouldFailMixin, ReallyEqualMixin, NonA d2 = defer.Deferred() self.uploader.set_uploaded_callback(d2.callback, ignore_count=0) os.rename(empty_tree_dir, new_empty_tree_dir) - self.notify_close_write(to_filepath(new_empty_tree_dir)) + self.notify(to_filepath(new_empty_tree_dir), self.inotify.IN_CLOSE_WRITE) return d2 d.addCallback(_check_move_empty_tree) d.addCallback(lambda ign: self.failUnlessReallyEqual(self._get_count('drop_upload.objects_uploaded'), 1)) @@ -161,7 +161,7 @@ class DropUploadTestMixin(GridTestMixin, ShouldFailMixin, ReallyEqualMixin, NonA d2 = defer.Deferred() self.uploader.set_uploaded_callback(d2.callback, ignore_count=1) os.rename(small_tree_dir, new_small_tree_dir) - self.notify_close_write(to_filepath(new_small_tree_dir)) + self.notify(to_filepath(new_small_tree_dir), self.inotify.IN_CLOSE_WRITE) return d2 d.addCallback(_check_move_small_tree) d.addCallback(lambda ign: self.failUnlessReallyEqual(self._get_count('drop_upload.objects_uploaded'), 3)) @@ -173,7 +173,7 @@ class DropUploadTestMixin(GridTestMixin, ShouldFailMixin, ReallyEqualMixin, NonA d2 = defer.Deferred() self.uploader.set_uploaded_callback(d2.callback, ignore_count=0) fileutil.write(abspath_expanduser_unicode(u"another", base=new_small_tree_dir), "file") - self.notify_close_write(to_filepath(abspath_expanduser_unicode(u"another", base=new_small_tree_dir))) + self.notify(to_filepath(abspath_expanduser_unicode(u"another", base=new_small_tree_dir)), self.inotify.IN_CLOSE_WRITE) return d2 d.addCallback(_check_moved_tree_is_watched) d.addCallback(lambda ign: self.failUnlessReallyEqual(self._get_count('drop_upload.objects_uploaded'), 4)) @@ -206,7 +206,7 @@ class DropUploadTestMixin(GridTestMixin, ShouldFailMixin, ReallyEqualMixin, NonA self.uploader.set_uploaded_callback(d2.callback) test_file = abspath_expanduser_unicode(u"what", base=self.local_dir) fileutil.write(test_file, "meow") - self.notify_close_write(to_filepath(test_file)) + self.notify(to_filepath(test_file), self.inotify.IN_CLOSE_WRITE) return d2 d.addCallback(create_file) d.addCallback(lambda ign: self.failUnlessReallyEqual(self._get_count('drop_upload.objects_uploaded'), 1)) @@ -296,7 +296,7 @@ class DropUploadTestMixin(GridTestMixin, ShouldFailMixin, ReallyEqualMixin, NonA if temporary and sys.platform == "win32": os.unlink(path_u) fileutil.flush_volume(path_u) - self.notify_close_write(path) + self.notify(path, self.inotify.IN_CLOSE_WRITE) def _print(res, msg): print msg @@ -329,8 +329,8 @@ class MockTest(DropUploadTestMixin, unittest.TestCase): DropUploadTestMixin.setUp(self) self.inotify = fake_inotify - def notify_close_write(self, path): - self.uploader._notifier.event(path, self.inotify.IN_CLOSE_WRITE) + def notify(self, path, mask): + self.uploader._notifier.event(path, mask) def test_errors(self): self.set_up_grid() @@ -368,11 +368,13 @@ class RealTest(DropUploadTestMixin, unittest.TestCase): def setUp(self): DropUploadTestMixin.setUp(self) - self.inotify = None + self.inotify = drop_upload.get_inotify_module() - def notify_close_write(self, path): - # Writing to the file causes the notification. + def notify(self, path, mask): + # Writing to the filesystem causes the notification. pass -if sys.platform != "win32" and not runtime.platform.supportsINotify(): +try: + drop_upload.get_inotify_module() +except NotImplementedError: RealTest.skip = "Drop-upload support can only be tested for-real on an OS that supports inotify or equivalent." -- 2.45.2