Revision 99c11993 snf-pithos-app/pithos/api/swiss_army/__init__.py

b/snf-pithos-app/pithos/api/swiss_army/__init__.py
64 64
        for i in range(len(accounts)):
65 65
            account = accounts[i]
66 66
            matcher = re.compile(account, re.IGNORECASE)
67
            duplicate = filter(matcher.match, accounts[i + 1:])
67
            duplicate = filter(matcher.match, (i for i in accounts[i + 1:] \
68
                if len(i) == len(account)))
68 69
            if duplicate:
69 70
                duplicate.insert(0, account)
70 71
                duplicates.append(duplicate)
......
111 112

  
112 113
    def move_object(self, src_account, src_container, src_name,
113 114
                    dest_account, dry=True, silent=False):
115
        if src_account not in self.existing_accounts():
116
            raise NameError('%s does not exist' % src_account)
117
        if dest_account not in self.existing_accounts():
118
            raise NameError('%s does not exist' % dest_account)
114 119

  
115 120
        trans = self.backend.wrapper.conn.begin()
116 121
        try:
117
            self._move_object(src_account, src_container, src_name,
118
                    dest_account)
122
            self._copy_object(src_account, src_container, src_name,
123
                              dest_account, move=True)
119 124

  
120 125
            if dry:
121 126
                if not silent:
......
129 134
            trans.rollback()
130 135
            raise
131 136

  
132
    def _move_object(self, src_account, src_container, src_name,
133
                    dest_account):
137
    def _copy_object(self, src_account, src_container, src_name,
138
                    dest_account, move=False):
134 139
        path = os.path.join(src_container, src_name)
135 140
        fullpath = os.path.join(src_account, path)
136 141
        dest_container = src_container
......
173 178
                                     meta={}, replace_meta=False,
174 179
                                     permissions=permissions)
175 180

  
176
        self.backend.delete_object(src_account, src_account, src_container,
177
                                   src_name)
181
        if move:
182
            self.backend.delete_object(src_account, src_account,
183
                                       src_container, src_name)
178 184

  
179 185
        dest_path, dest_node = self.backend._lookup_object(dest_account,
180 186
                                                           dest_container,
......
190 196
            fullpath = '/'.join([dest_account, dest_container, dest_name])
191 197
            self.backend.permissions.public_set(fullpath)
192 198

  
193
    def _merge_account(self, src_account, dest_account):
199
    def _merge_account(self, src_account, dest_account, delete_src=False):
194 200
            # TODO: handle exceptions
195 201
            # copy all source objects
196 202
            for path in self.list_all_objects(src_account):
......
198 204
                    '/%s' % path)
199 205

  
200 206
                # give read permissions to the dest_account
201
                permissions  = self.backend.get_object_permissions(
207
                permissions = self.backend.get_object_permissions(
202 208
                    src_account, src_account, src_container, src_name)
203 209
                if permissions:
204 210
                    permissions = permissions[2]
......
210 216
                                                       src_name,
211 217
                                                       permissions)
212 218

  
213
                self._move_object(src_account, src_container, src_name,
214
                                 dest_account)
219
                self._copy_object(src_account, src_container, src_name,
220
                                 dest_account, move=delete_src)
215 221

  
216 222
            # move groups also
217 223
            groups = self.backend.get_account_groups(src_account, src_account)
218 224
            (v.replace(src_account, dest_account) for v in groups.values())
219 225
            self.backend.update_account_groups(dest_account, dest_account,
220 226
                                               groups)
221
            self._delete_account(src_account)
227
            if delete_src:
228
                self._delete_account(src_account)
222 229

  
223 230
    def merge_account(self, src_account, dest_account, only_stats=True,
224
                      dry=True, silent=False):
231
                      dry=True, silent=False, delete_src=False):
232
        if src_account not in self.existing_accounts():
233
            raise NameError('%s does not exist' % src_account)
234
        if dest_account not in self.existing_accounts():
235
            raise NameError('%s does not exist' % dest_account)
236

  
225 237
        if only_stats:
226 238
            print "The following %s's entries will be moved to %s:" \
227 239
                % (src_account, dest_account)
......
233 245

  
234 246
        trans = self.backend.wrapper.conn.begin()
235 247
        try:
236
            self._merge_account(src_account, dest_account)
248
            self._merge_account(src_account, dest_account, delete_src)
237 249

  
238 250
            if dry:
239 251
                if not silent:
......
242 254
            else:
243 255
                trans.commit()
244 256
                if not silent:
245
                    msg = "%s merged into %s and deleted."
257
                    msg = "%s merged into %s."
246 258
                    print msg % (src_account, dest_account)
247 259
        except:
248 260
            trans.rollback()
......
262 274
        self.backend.delete_account(account, account)
263 275

  
264 276
    def delete_account(self, account, only_stats=True, dry=True, silent=False):
277
        if account not in self.existing_accounts():
278
            raise NameError('%s does not exist' % account)
265 279
        if only_stats:
266 280
            print "The following %s's entries will be removed:" % account
267 281
            print "Objects: %r" % self.list_all_objects(account)

Also available in: Unified diff