def test_fileModifiedAndMovedOnBothDevices(self): mySystemModifications = { "dirs_created": [], "dirs_deleted": [], "dirs_modified": [], "dirs_moved": [], "files_created": [], "files_deleted": [], "files_modified": ['moved_file'], "files_moved": [('moved_file', 'moved_file_destination')], } otherSystemModifications = { "dirs_created": [], "dirs_deleted": [], "dirs_modified": [], "dirs_moved": [], "files_created": [], "files_deleted": [], "files_modified": ['moved_file'], "files_moved": [('moved_file', 'moved_file_destination')], } synchronizer = SynchronizationManager(mySystemModifications, otherSystemModifications) mySystemFileProcesses, otherSystemFileProcesses = synchronizer.getFileProcesses( ) expectedMySystemFileProcesses, expectedOtherSystemFileProcesses = [], [] self.assertCountEqual(mySystemFileProcesses, expectedMySystemFileProcesses) self.assertCountEqual(otherSystemFileProcesses, expectedOtherSystemFileProcesses)
def connectionLost(self, reason): if reason.check(ConnectionDone): SynchronizationManager.synchronize(self.fileProcesses, self.contents, self.factory.fileSystemManager) self.factory.fileSystemManager.syncBackupState() reactor.stop()
def test_fileMovedToDifferentDestinationOnOtherDevice(self): mySystemModifications = { "dirs_created": [], "dirs_deleted": [], "dirs_modified": [], "dirs_moved": [], "files_created": [], "files_deleted": [], "files_modified": [], "files_moved": [('moved_file_source', 'my_moved_file_destination')], } otherSystemModifications = { "dirs_created": [], "dirs_deleted": [], "dirs_modified": [], "dirs_moved": [], "files_created": [], "files_deleted": [], "files_modified": [], "files_moved": [('moved_file_source', 'other_moved_file_destination')], } synchronizer = SynchronizationManager(mySystemModifications, otherSystemModifications) mySystemFileProcesses, otherSystemFileProcesses = synchronizer.getFileProcesses( ) expectedMySystemFileProcesses = [('create', 'other_moved_file_destination')] expectedOtherSystemFileProcesses = [('create', 'my_moved_file_destination')] self.assertCountEqual(mySystemFileProcesses, expectedMySystemFileProcesses) self.assertCountEqual(otherSystemFileProcesses, expectedOtherSystemFileProcesses)
def test_directoryDeletedOnMyDevice(self): mySystemModifications = { "dirs_created": [], "dirs_deleted": ['deleted_directory'], "dirs_modified": [], "dirs_moved": [], "files_created": [], "files_deleted": [], "files_modified": [], "files_moved": [], } otherSystemModifications = { "dirs_created": [], "dirs_deleted": [], "dirs_modified": [], "dirs_moved": [], "files_created": [], "files_deleted": [], "files_modified": [], "files_moved": [], } synchronizer = SynchronizationManager(mySystemModifications, otherSystemModifications) mySystemFileProcesses, otherSystemFileProcesses = synchronizer.getFileProcesses( ) expectedMySystemFileProcesses = [] expectedOtherSystemFileProcesses = [('deletedDirectory', 'deleted_directory')] self.assertCountEqual(mySystemFileProcesses, expectedMySystemFileProcesses) self.assertCountEqual(otherSystemFileProcesses, expectedOtherSystemFileProcesses)
def test_fileCreatedAndModifiedOnOtherDeviceAndMovedOnMyDevice(self): mySystemModifications = { "dirs_created": [], "dirs_deleted": [], "dirs_modified": [], "dirs_moved": [], "files_created": [], "files_deleted": [], "files_modified": [], "files_moved": [("modified_file", "created_file")], } otherSystemModifications = { "dirs_created": [], "dirs_deleted": [], "dirs_modified": [], "dirs_moved": [], "files_created": ['created_file'], "files_deleted": [], "files_modified": ['modified_file'], "files_moved": [], } synchronizer = SynchronizationManager(mySystemModifications, otherSystemModifications) mySystemFileProcesses, otherSystemFileProcesses = synchronizer.getFileProcesses( ) expectedMySystemFileProcesses = [("create", "modified_file"), ("create", "created_file")] expectedOtherSystemFileProcesses = [("create", "modified_file"), ("create", "created_file")] self.assertCountEqual(mySystemFileProcesses, expectedMySystemFileProcesses) self.assertCountEqual(otherSystemFileProcesses, expectedOtherSystemFileProcesses)
def handleModifications(self, otherSystemModifications): mySystemModifications = self.factory.fileSystemManager.getDifferencesSinceLastSync() self.synchroniser = SynchronizationManager(mySystemModifications, otherSystemModifications) mySystemFileProcesses, otherSystemFileProcesses = self.synchroniser.getFileProcesses() self.mySystemRequiredFiles = self.synchroniser.getRequiredFiles(mySystemFileProcesses) self.otherSystemRequiredFiles = self.synchroniser.getRequiredFiles(otherSystemFileProcesses) self.fileProcesses = mySystemFileProcesses response = { 'type' : 'operations', #needs-attention 'operations': otherSystemFileProcesses, 'files' : self.otherSystemRequiredFiles } return response
class SynchronizationProtocol(Protocol): def __init__(self, factory): self.factory = factory self.fileProcesses = [] self.contents = {} def dataReceived(self, contents): response = { 'type' : 'error', 'message' : 'Unknown error' } try: message = json.loads(contents.decode()) if message['type'] == 'modifications': response = self.handleModifications(message['modifications']) elif message['type'] == 'data': response = self.handleData(message['data']) elif message['type'] == 'end': self.transport.loseConnection() else: response = { 'type' : 'error', 'message' : 'Message type not recognized' } except json.JSONDecodeError: response = { 'type' : 'error', 'message' : 'Message format not recognized' } finally: self.transport.write(json.dumps(response).encode()) def connectionLost(self, reason): if reason.check(ConnectionDone): SynchronizationManager.synchronize(self.fileProcesses, self.contents, self.factory.fileSystemManager) self.factory.fileSystemManager.syncBackupState() def handleModifications(self, otherSystemModifications): mySystemModifications = self.factory.fileSystemManager.getDifferencesSinceLastSync() self.synchroniser = SynchronizationManager(mySystemModifications, otherSystemModifications) mySystemFileProcesses, otherSystemFileProcesses = self.synchroniser.getFileProcesses() self.mySystemRequiredFiles = self.synchroniser.getRequiredFiles(mySystemFileProcesses) self.otherSystemRequiredFiles = self.synchroniser.getRequiredFiles(otherSystemFileProcesses) self.fileProcesses = mySystemFileProcesses response = { 'type' : 'operations', #needs-attention 'operations': otherSystemFileProcesses, 'files' : self.otherSystemRequiredFiles } return response def handleData(self, data): mySystemData, otherSystemData = self.synchroniser.getData( data, self.mySystemRequiredFiles, self.otherSystemRequiredFiles, self.factory.fileSystemManager) self.contents = mySystemData response = { 'type' : 'data', 'data' : otherSystemData } return response