Example #1
0
    def testModOnSingleDirOrFileEventExistInDb(self):
        eventTime = int(time.time())
        files = {
            "/testData/": {},
            "/testData/dir/": {},
            "/testData/dir/sub_dir/": {},
            "/testData/dir2/": {},
            "/testData/dir2/file2": {}
        }
        fs = MockFileSystem(files=files)
        dbRecords = {
            'file:///testData/dir/sub_dir/': (1245298553, 'start', True,
                                              False),
            'file:///testData/dir/': (1245298553, 'start', True, False),
            'file:///testData/dir2/': (1245298553, 'start', True, False),
            'file:///testData/dir2/file2': (1245298553, 'start', False, False),
            'file:///testData/': (1245298553, 'start', True, False)
        }
        db = MockDb(dbRecords)

        q = Queue(db=db, fs=fs)
        dirs, _ = fs.walkDirectory()

        #updating a dir
        updatingDir = "/testData/dir/sub_dir/"
        updatedEvents = \
            q.processQueue(eventDetail=("file://%s/" % fs.absPath(updatingDir), \
                                        eventTime, "mod", True), initialization=False)

        #updating a file
        updatingFile = "/testData/dir2/file2"
        updatedEvents = \
            q.processQueue(eventDetail=("file://%s" % fs.absPath(updatingFile), \
                                        eventTime, "mod", False), initialization=False)

        for event in updatedEvents:
            filePath, timeStamp, eventName, isDir, init = event
            if isDir:
                filePath = "%s/" % filePath.replace("file://", "").rstrip("/")
            else:
                filePath = "%s" % filePath.replace("file://", "")
            if filePath == updatingDir or filePath == updatingFile:
                self.assertEqual(eventName, "mod")
                self.assertEqual(int(timeStamp), eventTime)
            else:
                self.assertEqual(eventName, "start")
                self.assertEqual(int(timeStamp), 1245298553)
            self.assertTrue(filePath in files.keys())
            self.assertFalse(init)
Example #2
0
    def testIgnoreDirWhereTheProgramRun(self):
        programPath = "/home/octalina/workspace/watcher2"

        eventTime = int(time.time())
        files = {
            "/testData/": {},
            "/testData/dir/": {},
            "/testData/dir/sub_dir/": {},
            "/testData/dir2/": {},
            "/testData/dir2/file2": {}
        }
        fs = MockFileSystem(files=files)
        dbRecords = {
            'file:///testData/dir/sub_dir/': (1245298553, 'start', True,
                                              False),
            'file:///testData/dir/': (1245298553, 'start', True, False),
            'file:///testData/dir2/': (1245298553, 'start', True, False),
            'file:///testData/dir2/file2': (1245298553, 'start', False, False),
            'file:///testData/': (1245298553, 'start', True, False)
        }
        db = MockDb(dbRecords)

        q = Queue(db=db, fs=fs, programPath=programPath)
        dirs, _ = fs.walkDirectory()

        #updating a dir
        updatingDir = "/home/octalina/workspace/watcher2/app"
        updatedEvents = \
            q.processQueue(eventDetail=("file://%s/" % fs.absPath(updatingDir), \
                                        eventTime, "mod", True), initialization=False)

        #updating a file
        updatingFile = "/home/octalina/workspace/watcher2/app/watcher.py"
        updatedEvents = \
            q.processQueue(eventDetail=("file://%s" % fs.absPath(updatingFile), \
                                        eventTime, "mod", False), initialization=False)

        for event in updatedEvents:
            filePath, timeStamp, eventName, isDir, init = event
            if isDir:
                filePath = "%s/" % filePath.replace("file://", "").rstrip("/")
            else:
                filePath = "%s" % filePath.replace("file://", "")
            self.assertTrue(filePath != updatingDir)
            self.assertTrue(filePath != updatingFile)

            self.assertEqual(eventName, "start")
            self.assertEqual(int(timeStamp), 1245298553)
Example #3
0
 def testInitStartWatchWithStartEventExistInDB(self):
     eventTime = int(time.time())
     files={"/testData/": {},
            "/testData/dir/": {},
            "/testData/dir/sub_dir/": {},
            "/testData/dir2/": {},
            "/testData/dir2/file2": {}}
     fs = MockFileSystem(files=files)
     dbRecords= {'file:///testData/dir/sub_dir/': (1245298553, 'start', True, True), 
                 'file:///testData/dir/': (1245298553, 'start', True, True), 
                 'file:///testData/dir2/': (1245298553, 'start', True, True), 
                 'file:///testData/dir2/file2': (1245298553, 'start', False, True), 
                 'file:///testData/': (1245298553, 'start', True, True)}
     db = MockDb(dbRecords)
     
     q = Queue(db=db, fs=fs)
     dirs, _ = fs.walkDirectory()
     
     #when starting to watch a directory, only the watched directory will be passed to the queue
     watchingDir = "/testData/"
     updatedEvents = \
         q.processQueue(eventDetail=("file://%s/" % fs.absPath(watchingDir), \
                                     eventTime, "mod", True), initialization=True)
     for event in updatedEvents:
         filePath, timeStamp, eventName, isDir, init = event
         if isDir:
             filePath = "%s/" % filePath.replace("file://", "").rstrip("/")
             self.assertTrue(filePath in files.keys())
         else:
             filePath = "%s" % filePath.replace("file://", "").rstrip("/")
             self.assertTrue(filePath in files.keys())
         self.assertEqual(int(timeStamp), 1245298553)
         self.assertEqual(eventName, "start")
         self.assertTrue(init)
Example #4
0
 def testStopWatchEventAndNotExistInDb(self):
     eventTime = int(time.time())
     files={"/testData/": {},
            "/testData/dir/": {},
            "/testData/dir/sub_dir/": {},
            "/testData/dir2/": {},
            "/testData/dir2/file2": {}}
     fs = MockFileSystem(files=files)
     db = MockDb({})
     
     q = Queue(db=db, fs=fs)
     dirs, _ = fs.walkDirectory()
     
     #when starting to watch a directory, only the watched directory will be passed to the queue
     watchingDir = "/testData/"
     updatedEvents = \
         q.processQueue(eventDetail=("file://%s/" % fs.absPath(watchingDir), \
                                     eventTime, "stop", True), initialization=True)
     
     for event in updatedEvents:
         filePath, timeStamp, eventName, isDir, init = event
         if isDir:
             filePath = "%s/" % filePath.replace("file://", "").rstrip("/")
         else:
             filePath = "%s" % filePath.replace("file://", "").rstrip("/")
         self.assertTrue(filePath in files.keys())
         self.assertEqual(eventName, "stop")
         self.assertEqual(int(timeStamp), eventTime)
         self.assertTrue(init)
Example #5
0
 def testDeleteFile(self):
     #when a file is delete.... there will not be any modified date anymore
     files={"/testData/": {"modifiedDate": 0}}
     
     fs = MockFileSystem(files=files)
     q = Queue(fs=fs)
     
     newFile = "/testData/file"
     eventsToBeUpdated = q.processQueue(eventDetail=("file://%s" % fs.absPath(newFile), 0, "del", False))
     expectedEventsToBeUpdated = [('file:///testData/file', 0, 'del', False)]
     self.assertEqual(eventsToBeUpdated, expectedEventsToBeUpdated)
Example #6
0
 def testAddNewFileToDirectory(self):
     modifiedDate = 1245148106
     files={"/testData/": {"modifiedDate": modifiedDate}}
     
     fs = MockFileSystem(files=files)
     q = Queue(fs=fs)
     
     newFile = "/testData/file"
     eventsToBeUpdated = q.processQueue(eventDetail=("file://%s" % fs.absPath(newFile), modifiedDate, "mod", False))
     expectedEventsToBeUpdated = [('file:///testData/file', 1245148106, 'mod', False)]
     self.assertEqual(eventsToBeUpdated, expectedEventsToBeUpdated)
Example #7
0
 def testAddNewDirectoryToWatchedDir(self):
     modifiedDate = 1245148106
     files={"/testData/": {"modifiedDate": modifiedDate}}
     
     fs = MockFileSystem(files=files)
     q = Queue(fs=fs)
     
     newDir = "/testData/newDir/"
     eventsToBeUpdated = q.processQueue(eventDetail=("file://%s/" % fs.absPath(newDir), modifiedDate, "mod", True))
     expectedEventsToBeUpdated = [('file:///testData/newDir/', 1245148106, 'mod', True)]
     self.assertEqual(eventsToBeUpdated, expectedEventsToBeUpdated)
Example #8
0
    def testModOnSingleDirOrFileEventNotExistInDb(self):
        eventTime = int(time.time())
        files = {
            "/testData/": {},
            "/testData/dir/": {},
            "/testData/dir/sub_dir/": {},
            "/testData/dir2/": {},
            "/testData/dir2/file2": {}
        }
        fs = MockFileSystem(files=files)
        db = MockDb({})

        q = Queue(db=db, fs=fs)
        dirs, _ = fs.walkDirectory()

        #updating a dir
        updatingDir = "/testData/dir/sub_dir/"
        updatedEvents = \
            q.processQueue(eventDetail=("file://%s/" % fs.absPath(updatingDir), \
                                        eventTime, "mod", True), initialization=False)

        filePath, timeStamp, eventName, isDir, init = updatedEvents[0]
        filePath = "%s/" % filePath.replace("file://", "").rstrip("/")
        self.assertEqual(eventName, "mod")
        self.assertTrue(filePath in files.keys())
        self.assertEqual(int(timeStamp), eventTime)
        self.assertFalse(init)

        #updating a file
        updatingFile = "/testData/dir2/file2"
        updatedEvents = \
            q.processQueue(eventDetail=("file://%s" % fs.absPath(updatingFile), \
                                        eventTime, "mod", False), initialization=False)
        filePath, timeStamp, eventName, isDir, init = updatedEvents[1]
        filePath = "%s" % filePath.replace("file://", "")
        self.assertEqual(eventName, "mod")
        self.assertTrue(filePath in files.keys())
        self.assertFalse(isDir)
        self.assertEqual(int(timeStamp), eventTime)
        self.assertFalse(init)
Example #9
0
	def run(self):
		'''
		spusti poslouchani serveru

		'''

		# bind adresy a nastaveni socketu
		sck = self._socket

		sck.bind((self._host, self._port))

		# zacatek poslouchani
		sck.listen(5)

		# cekani na prichozi spojeni
		while not self._shutDown:
			conn, addr = sck.accept()

			# prijem zpravy
			msg = self._receive(conn)

			# uzavreni spojeni
			conn.close()

			# zpracovani zpravy a ulozeni do fronty
			request = Request.unserialize(msg)

			# pokud se jedna o pozadavek k vypnuti, pak se nic nebude zarazovat do fornty a server se vypne
			if request.method == "shutdown":
				self._shutDown = True
				continue

			Queue.push(request)

			# spusteni zpracovani fronty
			Queue.processQueue()

		sck.shutdown(socket.SHUT_WR)
		sck.close()
Example #10
0
 def testInitializingDirStructurWithFiles(self):
     modifiedDate = 1245148106
     files={"/testData/": {"modifiedDate": modifiedDate},
            "/testData/dir/": {"modifiedDate": modifiedDate},
            "/testData/dir2/": {"modifiedDate": modifiedDate},
            "/testData/dir2/file2": {"modifiedDate": modifiedDate},
            "/testData/dir/sub_dir/": {"modifiedDate": modifiedDate}}
     fs = MockFileSystem(files=files)
     q = Queue(fs=fs)
     dirs, _ = fs.walkDirectory()
     
     #when starting to watch a directory, only the watched directory will be passed to the queue
     watchingDir = "/testData/"
     eventsToBeUpdated = q.processQueue(eventDetail=("file://%s/" % fs.absPath(watchingDir), fs.modifiedDate(watchingDir), "mod", True), initialization=True)
     
     expectedEventsToBeUpdated = [('file:///testData/', 1245148106, 'mod', True), 
                                  ('file:///testData/dir/sub_dir', None, 'mod', True), 
                                  ('file:///testData/dir', None, 'mod', True), 
                                  ('file:///testData/dir2', None, 'mod', True), 
                                  ('file:///testData/dir2/file2', 1245148106, 'mod', False)]
     self.assertEqual(eventsToBeUpdated, expectedEventsToBeUpdated)