예제 #1
0
class Synchroniser(object):
    """
    Provides services to synchronisation file
    """

    def __init__(self,
                 configuration,
                 majic_webservice_client=None,
                 file_system_comparer=None,
                 directory_synchroniser=None):
        """
        Constructor
        :param configuration: the configuration to use for the synchronisation
        :param majic_webservice_client: the majic web service to use
        :return: nothing
        """
        super(Synchroniser, self).__init__()
        self._config = configuration

        if majic_webservice_client is None:
            self._majic_webservice_client = MajicWebserviceClient(self._config)
        else:
            self._majic_webservice_client = majic_webservice_client

        if file_system_comparer is None:
            self._file_system_comparer = FileSystemComparer(self._config)
        else:
            self._file_system_comparer = file_system_comparer

        if directory_synchroniser is None:
            self._directory_synchroniser = DirectorySynchroniser(self._config)
        else:
            self._directory_synchroniser = directory_synchroniser

    def synchronise(self):
        """
        Synchronise the files returned by the web service with those on the disc
        :return: error code to exit with
        """
        try:
            log.debug("Starting to sync")
            model_propeties = self._majic_webservice_client.get_properties_list_with_filtered_users()
            self._file_system_comparer.perform_analysis(model_propeties)
            self._file_system_comparer.add_extra_directories_to_sync()
            new_count, updated_count, deleted_count = \
                self._directory_synchroniser.synchronise_all(self._file_system_comparer)
            log.info("Finished Synchronisation:")
            log.info("  {}: Count of copied files and directories: ".format(new_count))
            log.info("  {}: Count of directory permissions updated".format(updated_count))
            log.info("  {}: Count of deleted directories".format(deleted_count))
            return 0
        except UserPrintableError as ex:
            log.error(str(ex))
            return 1
        except Exception:
            log.exception("Unknown error in synchronisation")
            log.error("An unknown error occurred so files are not synced")
            return 2
    def test_GIVEN_extra_directory_WHEN_add_extra_directories_to_sync_THEN_extra_directory_added(self):
        added_dir = "data"
        self.config = ConfigMother.test_configuration_with_values(extra_directories_to_sync=added_dir)
        expected_added_dir = [FileProperties("data", getuser(), True, True)]
        file_system_comparer = FileSystemComparer(self.config, self.mock_file_system_client)
        file_system_comparer.new_directories = []
        file_system_comparer.deleted_directories = []
        file_system_comparer.existing_non_deleted_directories = []
        file_system_comparer.changed_directories = []

        file_system_comparer.add_extra_directories_to_sync()

        assert_that_file_system_comparer(file_system_comparer, existing_non_deleted_directories=expected_added_dir)