~rye/ubuntuone-client/unique-check-is-unique

« back to all changes in this revision

Viewing changes to ubuntuone/syncdaemon/sync.py

  • Committer: Tarmac
  • Author(s): facundo at com
  • Date: 2010-09-09 13:05:46 UTC
  • mfrom: (676.2.8 move-limbo)
  • Revision ID: tarmac-20100909130546-11g5qu3beajtesgh
Move operations live in limbo until confirmed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
empty_hash = ""
37
37
 
38
38
class FSKey(object):
39
 
    """This encapsulates the problem of getting the metadata with different
40
 
    keys."""
 
39
    """Encapsulate the problem of getting the metadata with different keys."""
41
40
    __slots__ = ('fs', 'keys', 'mdid', '_changes')
42
41
 
43
42
    def __init__(self, fs, **keys):
44
 
        """create"""
45
43
        self.fs = fs
46
44
        self.keys = keys
47
45
        self.mdid = None
98
96
        self._changes.update(kwargs)
99
97
 
100
98
    def sync(self):
101
 
        """sync the changes back to FSM"""
 
99
        """Sync the changes back to FSM."""
102
100
        if self._changes and self.has_metadata():
103
101
            self.fs.set_by_mdid(self.get_mdid(), **self._changes)
104
102
            self._changes = {}
135
133
            return 'NA'
136
134
 
137
135
    def open_file(self):
138
 
        """get the file object for reading"""
 
136
        """Get the file object for reading."""
139
137
        mdid = self.get_mdid()
140
138
        try:
141
139
            fo = self.fs.open_file(mdid)
148
146
        return fo
149
147
 
150
148
    def upload_finished(self, server_hash):
151
 
        """signal that we have uploaded the file"""
 
149
        """Signal that we have uploaded the file."""
152
150
        mdid = self.get_mdid()
153
151
        self.fs.upload_finished(mdid, server_hash)
154
152
 
155
153
    def delete_file(self):
156
 
        """delete the file and metadata"""
 
154
        """Delete the file and metadata."""
157
155
        path = self["path"]
158
156
        self.fs.delete_file(path)
159
157
        self.mdid = None
160
158
        self._changes = {}
161
159
 
162
160
    def delete_to_trash(self):
163
 
        """Move the node to trash"""
 
161
        """Move the node to trash."""
164
162
        self.fs.delete_to_trash(self.get_mdid(), self["parent_id"])
165
163
 
166
164
    def remove_from_trash(self, share_id, node_id):
167
 
        """Remove the node from trash"""
 
165
        """Remove the node from trash."""
168
166
        self.fs.remove_from_trash(share_id, node_id)
169
167
 
170
168
    def delete_metadata(self):
171
 
        """delete the metadata"""
 
169
        """Delete the metadata."""
172
170
        path = self["path"]
173
171
        self.fs.delete_metadata(path)
174
172
        self.mdid = None
175
173
        self._changes = {}
176
174
 
177
175
    def move_file(self, new_share_id, new_parent_id, new_name):
178
 
        """get the stuff we need to move the file."""
 
176
        """Get the stuff we need to move the file."""
179
177
        source_path = self['path']
180
178
        parent_path = self.fs.get_by_node_id(new_share_id, new_parent_id).path
181
179
        dest_path = os.path.join(
184
182
        self.fs.move_file(new_share_id, source_path, dest_path)
185
183
 
186
184
    def moved(self, new_share_id, path_to):
187
 
        """change the metadata of a moved file."""
 
185
        """Change the metadata of a moved file."""
188
186
        self.fs.moved(new_share_id, self['path'], path_to)
189
187
        if "path" in self.keys:
190
188
            self.keys["path"] = path_to
191
189
 
192
190
    def remove_partial(self):
193
 
        """remove a partial file"""
 
191
        """Remove a partial file."""
194
192
        # pylint: disable-msg=W0704
195
193
        try:
196
194
            self.fs.remove_partial(self["node_id"], self["share_id"])
199
197
            pass
200
198
 
201
199
    def move_to_conflict(self):
202
 
        """Move file to conflict"""
 
200
        """Move file to conflict."""
203
201
        self.fs.move_to_conflict(self.get_mdid())
204
202
 
205
203
    def refresh_stat(self):
206
 
        """refresh the stat"""
 
204
        """Refresh the stat."""
207
205
        path = self["path"]
208
206
        # pylint: disable-msg=W0704
209
207
        try:
213
211
            pass
214
212
 
215
213
    def safe_get(self, key, default='^_^'):
216
 
        """ safe version of self.get, to be used in the FileLogger. """
 
214
        """Safe version of self.get, to be used in the FileLogger."""
217
215
        # catch all errors as we are here to help logging
218
216
        # pylint: disable-msg=W0703
219
217
        try:
509
507
        new_name = os.path.basename(path_to)
510
508
        new_parent = FSKey(self.m.fs, path=new_path)
511
509
        new_parent_id = new_parent['node_id']
 
510
        share_id = self.key['share_id']
 
511
        node_id = self.key['node_id']
512
512
 
513
 
        self.m.action_q.move(share_id=self.key['share_id'],
514
 
            node_id=self.key['node_id'], old_parent_id=old_parent_id,
515
 
            new_parent_id=new_parent_id, new_name=new_name)
516
 
        self.key.moved(self.key['share_id'], path_to)
 
513
        self.m.action_q.move(share_id, node_id, old_parent_id,
 
514
                             new_parent_id, new_name)
 
515
        self.m.fs.add_to_move_limbo(share_id, node_id, old_parent_id,
 
516
                                    new_parent_id, new_name)
 
517
        self.key.moved(share_id, path_to)
517
518
 
518
519
        # we only hash it if we're a file, not a directory
519
520
        if not self.key.is_dir():
738
739
        """Remove the node from trash."""
739
740
        self.key.remove_from_trash(share_id, node_id)
740
741
 
 
742
    def clean_move_limbo(self, event, params, share_id, node_id):
 
743
        """Remove the node from move limbo."""
 
744
        self.m.fs.remove_from_move_limbo(share_id, node_id)
 
745
 
741
746
    def server_moved(self, event, params, share_id, node_id,
742
747
                     new_share_id, new_parent_id, new_name):
743
748
        """file was moved on the server"""
1044
1049
        key = FSKey(self.m.fs, share_id=share_id, node_id=node_id)
1045
1050
        log = FileLogger(self.logger, key)
1046
1051
        ssmr = SyncStateMachineRunner(self.fsm, self.m, key, log)
 
1052
        ssmr.on_event("AQ_MOVE_OK", {}, share_id, node_id)
1047
1053
        ssmr.update_generation(share_id, node_id, new_generation)
1048
1054
 
 
1055
    def handle_AQ_MOVE_ERROR(self, share_id, node_id, parent_id, error):
 
1056
        """On AQ_MOVE_ERROR."""
 
1057
        key = FSKey(self.m.fs, share_id=share_id, node_id=node_id)
 
1058
        log = FileLogger(self.logger, key)
 
1059
        ssmr = SyncStateMachineRunner(self.fsm, self.m, key, log)
 
1060
        ssmr.on_event("AQ_MOVE_ERROR", {}, share_id, node_id)
 
1061
 
1049
1062
    def handle_AQ_DELTA_OK(self, volume_id, delta_content, end_generation,
1050
1063
                           full, free_bytes):
1051
1064
        """We got a new delta. Apply item by item of the delta.