Esempio n. 1
0
def handle_conflict(key, action_1, client_1, action_2, client_2):
    print(
        "\n"
        'Conflict for "{}". Which version would you like to keep?\n'
        "   (1) {}{} updated at {} ({})\n"
        "   (2) {}{} updated at {} ({})\n"
        "   (d) View difference (requires the diff command)\n"
        "   (X) Skip this file\n".format(
            key,
            client_1.get_uri(),
            key,
            action_1.get_remote_datetime(),
            action_1.state,
            client_2.get_uri(),
            key,
            action_2.get_remote_datetime(),
            action_2.state,
        ),
        file=sys.stderr,
    )
    while True:
        choice = utils.get_input("Choice (default=skip): ")
        print("", file=sys.stderr)

        if choice == "d":
            show_diff(client_1, client_2, key)
        else:
            break

    if choice == "1":
        return Resolution.get_resolution(key, action_1, client_2, client_1)
    elif choice == "2":
        return Resolution.get_resolution(key, action_2, client_1, client_2)
Esempio n. 2
0
File: sync.py Progetto: epayet/S4
    def sync(self, conflict_choice=None, keys=None, dry_run=False):
        self.client_1.lock()
        self.client_2.lock()
        try:
            resolutions, unhandled_events = self.get_sync_states(keys)

            self.logger.debug(
                'There are %s unhandled events for the user to solve', len(unhandled_events)
            )
            self.logger.debug(
                'There are %s automatically resolved calls', len(resolutions)
            )
            for key in sorted(unhandled_events.keys()):
                action_1, action_2 = unhandled_events[key]
                if conflict_choice == '1':
                    resolutions[key] = Resolution.get_resolution(
                        key, action_1, self.client_2, self.client_1
                    )
                elif conflict_choice == '2':
                    resolutions[key] = Resolution.get_resolution(
                        key, action_2, self.client_1, self.client_2
                    )
                if self.conflict_handler is not None:
                    resolution = self.conflict_handler(
                        key, action_1, self.client_1, action_2, self.client_2
                    )
                    if resolution is not None:
                        resolutions[key] = resolution
                    else:
                        self.logger.info('Ignoring sync conflict for %s', key)
                else:
                    self.logger.info('Unable to resolve conflict for %s', key)

            self.run_resolutions(resolutions, dry_run)

        except KeyboardInterrupt:
            self.logger.warning('Session interrupted by Keyboard Interrupt. Aborting....')
        finally:
            self.client_1.unlock()
            self.client_2.unlock()
Esempio n. 3
0
 def test_get_resolution_updated(self, state, action, s3_client,
                                 local_client):
     sync_state = SyncState(state, 1234, 4567)
     resolution = Resolution.get_resolution('foo/bar', sync_state,
                                            s3_client, local_client)
     assert resolution.action == action
     assert resolution.to_client == s3_client
     assert resolution.key == 'foo/bar'
     if state != SyncState.DELETED:
         assert resolution.timestamp == 1234
         assert resolution.from_client == local_client
     else:
         assert resolution.timestamp == 4567
         assert resolution.from_client is None
Esempio n. 4
0
    def sync(self, conflict_choice=None, keys=None, dry_run=False):
        self.client_1.lock()
        self.client_2.lock()
        try:
            resolutions, unhandled_events = self.get_sync_states(keys)

            self.logger.debug(
                "There are %s unhandled events for the user to solve",
                len(unhandled_events),
            )
            self.logger.debug("There are %s automatically resolved calls",
                              len(resolutions))
            for key in sorted(unhandled_events.keys()):
                action_1, action_2 = unhandled_events[key]
                if conflict_choice == "1":
                    resolutions[key] = Resolution.get_resolution(
                        key, action_1, self.client_2, self.client_1)
                elif conflict_choice == "2":
                    resolutions[key] = Resolution.get_resolution(
                        key, action_2, self.client_1, self.client_2)
                if self.conflict_handler is not None:
                    resolution = self.conflict_handler(key, action_1,
                                                       self.client_1, action_2,
                                                       self.client_2)
                    if resolution is not None:
                        resolutions[key] = resolution
                    else:
                        self.logger.info("Ignoring sync conflict for %s", key)
                else:
                    self.logger.info("Unable to resolve conflict for %s", key)

            self.run_resolutions(resolutions, dry_run)

        finally:
            self.client_1.unlock()
            self.client_2.unlock()
Esempio n. 5
0
 def test_invalid_sync_state(self):
     with pytest.raises(ValueError) as exc:
         Resolution.get_resolution('', SyncState('Invalid', None, None),
                                   None, None)
     assert exc.value