Skip to content

Commit 758ebe3

Browse files
author
Jim Fulton
committed
Reimplemented box API access using the box sdk.
1 parent 9835b28 commit 758ebe3

File tree

5 files changed

+81
-280
lines changed

5 files changed

+81
-280
lines changed

karl/box/archive.py

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from .client import find_box, BoxClient
1818
from .log import persistent_log
1919
from .queue import RedisArchiveQueue
20+
from .slugify import slugify
2021

2122

2223
log = logging.getLogger(__name__)
@@ -251,43 +252,33 @@ def copy_community_to_box(community):
251252
log.info("Connecting to Box.")
252253
box = BoxClient(find_box(community), get_current_registry().settings)
253254

254-
def realize_archive(archive, folder, path, copied):
255+
def realize_archive(archive, folder, path=''):
256+
contents = box.contents(folder)
255257
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
261259
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
265264
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)
268268
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)
278277

279278
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)
291282
community.archive_status = 'reviewing'
292283
if getattr(community, 'archive_copied', None) is not None:
293284
del community.archive_copied
@@ -402,7 +393,7 @@ def worker():
402393
queue = RedisArchiveQueue.from_settings(registry.settings)
403394

404395
if options.refresh_authentication:
405-
BoxClient(find_box(root), registry.settings).refresh(commit=True)
396+
BoxClient(find_box(root), registry.settings).refresh()
406397
return
407398

408399
closer()

0 commit comments

Comments
 (0)