Skip to content

Commit 1957223

Browse files
authored
Merge pull request #180 from karlproject/fix-arc2box
Fix arc2box
2 parents 2a28267 + 6037381 commit 1957223

File tree

10 files changed

+155
-250
lines changed

10 files changed

+155
-250
lines changed

CHANGES.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
karl package Changelog
22
======================
33

4+
- Added an arc2box option to just refresh box authentication. We'll
5+
create a cron job to run this weekly to keep the auth token alive.
6+
7+
- When normalizing file and directory names for box, don't lower case
8+
or convert dots to dashes.
9+
410
4.38.5 (2017-08-03)
511
===================
612

buildout.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ login_cookie_max_age = 36000
129129
mail_white_list =
130130
static_rev =
131131
redis = redislog = false
132+
sentry =
132133
statsd_uri =
133134
statsd_uri = statsd://localhost:8125
134135
statsd =
@@ -205,6 +206,7 @@ text =
205206
mailin_trace_file = ${:var}/mailin_trace
206207

207208
${:redis}
209+
${:sentry}
208210

209211
${:statsd_uri}
210212
framestats = ${:framestats}

karl/box/archive.py

Lines changed: 44 additions & 42 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,46 +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-
transaction.abort()
274-
transaction.begin()
275-
community.archive_copied = copied
276-
community.archive_last_copied = joined
277-
transaction.commit()
278-
raise
279-
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)
280277

281278
path = reversed([o.__name__ for o in lineage(community) if o.__name__])
282-
copied = []
283-
if getattr(community, 'archive_copied', None) is not None:
284-
copied = community.archive_copied
285-
log.info("Resuming copy of: %s", resource_path(community))
286-
folder = box.root().get_or_make('Karl Archive', *path)
287-
if folder and not copied:
288-
transaction.abort()
289-
raise ValueError(
290-
'Cannot archive community, folder already exists: %s' % (
291-
'/' + '/'.join(path)))
292-
293-
realize_archive(archive(community), folder, tuple(path), copied)
279+
folder = box.get_or_make('Karl Archive', *path)
280+
281+
realize_archive(archive(community), folder)
294282
community.archive_status = 'reviewing'
295283
if getattr(community, 'archive_copied', None) is not None:
296284
del community.archive_copied
@@ -386,6 +374,10 @@ def worker():
386374
parser = OptionParser(usage, description=__doc__)
387375
parser.add_option('-C', '--config', dest='config', default=None,
388376
help="Specify a paster config file. Defaults to $CWD/etc/karl.ini")
377+
parser.add_option('-r', '--refresh-authentication', action='store_true',
378+
help="Refresh box authentication")
379+
parser.add_option('--archive-community',
380+
help="Manually archive a commmunity to box.")
389381

390382
options, args = parser.parse_args()
391383
if args:
@@ -397,19 +389,26 @@ def worker():
397389
root, closer = open_root(config)
398390

399391
registry = get_current_registry()
392+
sentry_dsn = registry.settings.get('sentry_dsn')
400393
queue = RedisArchiveQueue.from_settings(registry.settings)
394+
395+
if options.refresh_authentication:
396+
BoxClient(find_box(root), registry.settings).refresh()
397+
return
398+
401399
closer()
400+
root._p_jar.close()
402401

403-
transaction.commit()
404-
transaction.manager.explicit = True
405-
root._p_jar.explicit_transactions = True
406-
# Cause the connection to rollback the current trans wo starting a new one.
407-
transaction.begin(); transaction.abort()
402+
if options.archive_community:
403+
path = '/communities/' + options.archive_community
404+
operation = queue.COPY_QUEUE_KEY
405+
root, closer = open_root(config)
406+
community = find_resource(root, path)
407+
else:
408+
log.info("Waiting for work.")
409+
operation, community = next(work_queue(queue, config))
408410

409-
log.info("Waiting for work.")
410-
operation, community = next(work_queue(queue, config))
411411
log.info("Got work.")
412-
transaction.begin()
413412
with persistent_log(community) as plog:
414413
try:
415414
if operation == queue.COPY_QUEUE_KEY:
@@ -430,6 +429,9 @@ def worker():
430429
transaction.begin()
431430
community.archive_status = 'exception'
432431
transaction.commit()
432+
if sentry_dsn:
433+
import raven
434+
raven.Client(sentry_dsn).captureException()
433435
raise
434436
finally:
435437
# Persist log in its own transaction so that even if there is an

0 commit comments

Comments
 (0)