def testSubDir(self): fw = IPFileWatcher(self.fs) testPath = self.testDir testPath1 = self.testDir + "one" + os.path.sep testPath2 = self.testDir + "two" + os.path.sep self.fs.makeDirectory(testPath) events = [] def listener(*args): # (path, eventName, eventTime): events.append(args) fw.setDirsToBeWatched([testPath]) fw.addListener(listener) self.fs.writeFile(testPath1 + "test1.txt", "one") self.fs.writeFile(testPath1 + "test2.txt", "two") self.delay() for event in events: print event print events = [] self.fs.rename(testPath1, testPath2) self.delay() for event in events: print event print
def testBasic(self): fswatcher = FileWatcher(self.testDir, self.fs) fswatcher.startWatching() r = {} def listener(file, eventTime, eventName, isDir, walk=False): #sys.stdout.write("path=%s, eventTime=%s, eventName=%s, isDir=%s\n" % (file, eventTime, eventName, isDir)) r[file[len(self.testDir)+1:], eventName] = isDir if walk: r["walk"] = True fswatcher.addListener(listener) self.fs.writeFile(self.testDir + "/test.txt", "y") time.sleep(.1) self.fs.writeFile(self.testDir + "/test2.txt", "y") time.sleep(.1) self.fs.writeFile(self.testDir + "/subdir/one.txt", "y") time.sleep(.1) self.fs.writeFile(self.testDir + "/subdir/two.txt", "x") time.sleep(.1) self.fs.delete(self.testDir + "/subdir") time.sleep(.2) results = r.keys() results.sort() #for r in results: # print r if r.has_key("walk"): expected = ["Not filled in yet for linux file watcher"] else: expected = [('subdir', 'del'), ('subdir', 'mod'), ('subdir/one.txt', 'del'), ('subdir/one.txt', 'mod'), ('subdir/two.txt', 'del'), ('subdir/two.txt', 'mod'), ('test.txt', 'mod'), ('test2.txt', 'mod')] self.assertEquals(results, expected) fswatcher.close()
def testWatcherBasic(self): fw = IPFileWatcher(self.fs) paths = [self.testDir] events = [] def listener(*args): # (path, eventName, eventTime): events.append(args) fw.setDirsToBeWatched(paths) fw.addListener(listener) # assert that we have received a 'mod' event events = [] self.fs.writeFile(self.testDir + "test1.txt", "testing") self.assertTrue(self.fs.isFile(self.testDir + "test1.txt")) self.delay() self.assertEqual("mod", events[-1][1]) # assert that we have received a 'del' and 'cre' events events = [] self.fs.rename(self.testDir + "test1.txt", self.testDir + "test2.txt") self.delay() self.assertEqual("del", events[-2][1]) self.assertEqual(self.testDir + "test1.txt", events[-2][0]) self.assertEqual("cre", events[-1][1]) self.assertEqual(self.testDir + "test2.txt", events[-1][0]) # assert that we have received a 'del' event events = [] self.fs.delete(self.testDir + "test2.txt") self.delay() self.assertTrue(len(events)>0) self.assertEqual("del", events[-1][1]) # assert stopped watching events = [] fw.stopWatching() self.delay() self.fs.writeFile(self.testDir + "test1.txt", "testing") self.delay() self.assertEqual(len(events), 0) fw.close() fw = None print "done"
def testFileWatch(self): testFile = self.testDir + "/test.txt" self.fs.writeFile(testFile, "x") fswatcher = FileWatcher(testFile, self.fs) d = {} fswatcher.startWatching() def listener(file, eventTime, eventName, isDir, walk=False): #sys.stdout.write("path=%s, eventTime=%s, eventName=%s, isDir=%s\n" % (file, eventTime, eventName, isDir)) d[file] = eventName fswatcher.addListener(listener) time.sleep(.2) self.fs.writeFile(testFile, "xx") time.sleep(.2) fswatcher.close() self.assertEquals(len(d.keys()), 1)
def __init__(self, logger): os.chdir("../") self.programPath = os.getcwd() os.chdir("app") self.__fs = FileSystem(".") self.__utils = Utils() self.__config = Config(fileSystem=self.__fs) self.__db = None self.__controller = None self.__feeder = None self.__webServerShutdownMethod = None self.__logger = logger self.__configWatcher = None dbName = self.__config.watcher.get("db", "sqlite") sys.path.insert(0, "../db/%s" % dbName) Database = __import__(dbName).Database dbFileName = self.__config.watcher.get("dbFile", "queue.db") print "Starting file watcher..." #------------------------ self.__db = Database(dbFileName) self.__controller = Controller(self.__db, self.__fs, self.__config, \ FileWatcher, WatchDirectory, update=False) #self.__controller.configChanged(config) #self.__config.addReloadWatcher(self.__controller.configChanged) self.__configWatcher = FileWatcher(self.__config.configFile, self.__fs) self.__configWatcher.startWatching() def configChanged(file, eventName, **kwargs): #file=path, eventTime=eventTime, eventName=eventName, isDir=isDir, walk=False if eventName!="del" and file==self.__config.configFile: print "configChanged - reloading" self.__config.reload() self.__controller.configChanged(self.__config) self.__configWatcher.addListener(configChanged) self.__feeder = Feeder(self.__utils, self.__controller) feedservice = self.__config.watcher.get("feedservice", {}) self.host = feedservice.get("host", "localhost") self.port = feedservice.get("port", 9000) self.__webServerShutdownMethod = webServe(self.host, self.port, self.__feeder) #------------------------ print "host='%s', port=%s" % (self.host, self.port)
def testWatcherMultiPaths(self): fw = IPFileWatcher(self.fs) testPath1 = self.testDir + "one" + os.path.sep testPath2 = self.testDir + "two" + os.path.sep testPath3 = self.testDir + "three" + os.path.sep self.fs.makeDirectory(testPath1) self.fs.makeDirectory(testPath2) self.fs.makeDirectory(testPath3) events = [] def listener(*args): # (path, eventName, eventTime): events.append(args) fw.setDirsToBeWatched([testPath1, testPath2]) fw.addListener(listener) events = [] self.fs.writeFile(testPath1 + "subDir" + os.path.sep + "test.txt", "testing") self.fs.writeFile(testPath1 + "test.txt", "testing") self.delay() self.assertTrue(len(events)>0) # Test adding and removing of directories fw.setDirsToBeWatched([testPath2, testPath3]) events = [] self.fs.writeFile(testPath1 + "test2.txt", "testing") self.delay() self.assertTrue(len(events)==0) events = [] self.fs.writeFile(testPath2 + "test2.txt", "testing") self.delay() self.assertTrue(len(events)>0) events = [] self.fs.writeFile(testPath3 + "test2.txt", "testing") self.delay() self.assertTrue(len(events)>0)
def __init__(self): os.chdir("../") self.programPath = os.getcwd() os.chdir("app") self.__fs = FileSystem(".") self.__utils = Utils() self.__config = Config(fileSystem=self.__fs) self.__dbFullFileame = None self.__db = None self.__controller = None self.__feeder = None self.__webServerShutdownMethod = None self.__watcherProgDir = self.__fs.absPath(".").rstrip("/app") dbName = self.__config.watcher.get("db", "sqlite") sys.path.append("../db/%s" % dbName) Database = __import__(dbName).Database self.__dbFullFileame = self.__config.watcher.get("dbFile", "queue.db") self.__dbFullFileame = self.__fs.absPath(self.__dbFullFileame) #------------------------ stdout = sys.stdout stderr = sys.stderr # Note: must not output any data when running as a windows server. class Writer(object): def write(self, data): #stdout.write("** "+ data) # log pass if self.__config.daemon: w = Writer() sys.stdout = w sys.stderr = w #------------------------ self.__db = Database(self.__dbFullFileame) self.__controller = Controller(self.__db, self.__fs, self.__config, \ FileWatcher, WatchDirectory, update=False, \ globalIgnoreFilter=self.__globalIgnoreFilter) #self.__controller.configChanged(config) #self.__config.addReloadWatcher(self.__controller.configChanged) configFile = self.__fs.absPath(self.__config.configFile) configWatcher = FileWatcher(configFile, self.__fs) configWatcher.startWatching() def configChanged(file, eventName, **kwargs): #file=path, eventTime=eventTime, eventName=eventName, isDir=isDir, walk=False if eventName!="del" and file==self.__config.configFile: print "configChanged - reloading" self.__config.reload() self.__controller.configChanged(self.__config) configWatcher.addListener(configChanged) self.__feeder = Feeder(self.__utils, self.__controller) feedservice = self.__config.watcher.get("feedservice", {}) host = feedservice.get("host", "localhost") port = feedservice.get("port", 9000) self.__webServerShutdownMethod = webServe(host, port, self.__feeder) #------------------------ print "host='%s', port=%s" % (host, port) print "Press enter to exit..." raw_input() configWatcher.close() self.__controller.close() self.__webServerShutdownMethod() #print self.__controller._getRecordsCount() #print self.queue.getFromDate(0) sys.stdout = stdout sys.stderr = stderr
class Watcher(object): def __init__(self, logger): os.chdir("../") self.programPath = os.getcwd() os.chdir("app") self.__fs = FileSystem(".") self.__utils = Utils() self.__config = Config(fileSystem=self.__fs) self.__db = None self.__controller = None self.__feeder = None self.__webServer = None self.__logger = logger self.__configWatcher = None dbName = self.__config.watcher.get("db", "sqlite") sys.path.insert(0, "../db/%s" % dbName) Database = __import__(dbName).Database dbFileName = self.__config.watcher.get("dbFile", "queue.db") print "Starting file watcher..." #------------------------ self.__db = Database(dbFileName) self.__controller = Controller(self.__db, self.__fs, self.__config, \ FileWatcher, WatchDirectory, update=False) #self.__controller.configChanged(config) #self.__config.addReloadWatcher(self.__controller.configChanged) self.__configWatcher = FileWatcher(self.__config.configFile, self.__fs) self.__configWatcher.startWatching() def configChanged(file, eventName, **kwargs): #file=path, eventTime=eventTime, eventName=eventName, isDir=isDir, walk=False if eventName!="del" and file==self.__config.configFile: print "configChanged - reloading" self.__config.reload() self.__controller.configChanged(self.__config) self.__configWatcher.addListener(configChanged) self.__feeder = Feeder(self.__utils, self.__controller) feedservice = self.__config.watcher.get("feedservice", {}) self.host = feedservice.get("host", "localhost") self.port = feedservice.get("port", 9000) s = webServe(self.host, self.port, self.__feeder) self.__webServer = s #------------------------ @property def controller(self): return self.__controller @property def config(self): return self.__config @property def configFile(self): return self.__config.configFile @property def isServing(self): return self.__webServer.isServing def close(self): #print "FileWatcher closing" self.__configWatcher.close() self.__controller.close() self.__webServer.close() #print "FileWatcher closed" def __testListener(self, *args, **kwargs): path = kwargs.get("path") eTime = kwargs.get("eventTime") eName = kwargs.get("eventName") isDir = kwargs.get("isDir") print path, eTime, eName, isDir