|
17 | 17 | from .client import find_box, BoxClient
|
18 | 18 | from .log import persistent_log
|
19 | 19 | from .queue import RedisArchiveQueue
|
| 20 | +from .slugify import slugify |
20 | 21 |
|
21 | 22 |
|
22 | 23 | log = logging.getLogger(__name__)
|
@@ -251,43 +252,33 @@ def copy_community_to_box(community):
|
251 | 252 | log.info("Connecting to Box.")
|
252 | 253 | box = BoxClient(find_box(community), get_current_registry().settings)
|
253 | 254 |
|
254 |
| - def realize_archive(archive, folder, path, copied): |
| 255 | + def realize_archive(archive, folder, path=''): |
| 256 | + contents = box.contents(folder) |
255 | 257 | for name, item in archive.items():
|
256 |
| - subpath = path + (name,) |
257 |
| - joined = '/' + '/'.join(subpath) |
258 |
| - if joined in copied: |
259 |
| - log.info("Skipping existing file %s", joined) |
260 |
| - continue |
| 258 | + subpath = path + '/' + name |
261 | 259 | if isinstance(item, ArchiveFolder):
|
262 |
| - log.info("Creating folder %s", joined) |
263 |
| - if name in folder: |
264 |
| - subfolder = folder[name] |
| 260 | + if name in contents: |
| 261 | + log.info("Exists folder %s", subpath) |
| 262 | + subfolder = contents[name] |
| 263 | + assert subfolder.type == 'folder', subpath |
265 | 264 | else:
|
266 |
| - subfolder = folder.mkdir(name) |
267 |
| - realize_archive(item, subfolder, subpath, copied) |
| 265 | + log.info("Creating folder %s", subpath) |
| 266 | + subfolder = folder.create_subfolder(name) |
| 267 | + realize_archive(item, subfolder, subpath) |
268 | 268 | else:
|
269 |
| - log.info("Uploading (%d) %s", len(copied), joined) |
270 |
| - try: |
271 |
| - folder.upload(name, item.open()) |
272 |
| - except: |
273 |
| - community.archive_copied = copied |
274 |
| - community.archive_last_copied = joined |
275 |
| - transaction.commit() |
276 |
| - raise |
277 |
| - copied.append(joined) |
| 269 | + name = slugify(name) |
| 270 | + subpath = "%s (%s)" % (subpath, name) |
| 271 | + if name in contents: |
| 272 | + log.info("Exists file %s", subpath) |
| 273 | + assert contents[name].type == 'file', subpath |
| 274 | + else: |
| 275 | + log.info("Uploading file %s", subpath) |
| 276 | + folder.upload_stream(item.open(), name) |
278 | 277 |
|
279 | 278 | path = reversed([o.__name__ for o in lineage(community) if o.__name__])
|
280 |
| - copied = [] |
281 |
| - if getattr(community, 'archive_copied', None) is not None: |
282 |
| - copied = community.archive_copied |
283 |
| - log.info("Resuming copy of: %s", resource_path(community)) |
284 |
| - folder = box.root().get_or_make('Karl Archive', *path) |
285 |
| - if folder and not copied: |
286 |
| - raise ValueError( |
287 |
| - 'Cannot archive community, folder already exists: %s' % ( |
288 |
| - '/' + '/'.join(path))) |
289 |
| - |
290 |
| - realize_archive(archive(community), folder, tuple(path), copied) |
| 279 | + folder = box.get_or_make('Karl Archive', *path) |
| 280 | + |
| 281 | + realize_archive(archive(community), folder) |
291 | 282 | community.archive_status = 'reviewing'
|
292 | 283 | if getattr(community, 'archive_copied', None) is not None:
|
293 | 284 | del community.archive_copied
|
@@ -402,7 +393,7 @@ def worker():
|
402 | 393 | queue = RedisArchiveQueue.from_settings(registry.settings)
|
403 | 394 |
|
404 | 395 | if options.refresh_authentication:
|
405 |
| - BoxClient(find_box(root), registry.settings).refresh(commit=True) |
| 396 | + BoxClient(find_box(root), registry.settings).refresh() |
406 | 397 | return
|
407 | 398 |
|
408 | 399 | closer()
|
|
0 commit comments