2016-04-05 00:27:37 +02:00
|
|
|
|
|
|
|
import glob
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import shutil
|
2017-11-16 00:43:27 +01:00
|
|
|
from argparse import ArgumentParser
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand
|
2016-04-05 00:27:37 +02:00
|
|
|
|
|
|
|
from zerver.lib.export import export_usermessages_batch
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = """UserMessage fetching helper for export.py"""
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2016-04-05 00:27:37 +02:00
|
|
|
parser.add_argument('--path',
|
|
|
|
dest='path',
|
|
|
|
action="store",
|
|
|
|
default=None,
|
|
|
|
help='Path to find messages.json archives')
|
|
|
|
parser.add_argument('--thread',
|
|
|
|
dest='thread',
|
|
|
|
action="store",
|
|
|
|
default=None,
|
|
|
|
help='Thread ID')
|
2019-05-10 14:28:38 +02:00
|
|
|
parser.add_argument('--consent-message-id',
|
|
|
|
dest="consent_message_id",
|
|
|
|
action="store",
|
|
|
|
default=None,
|
|
|
|
type=int,
|
|
|
|
help='ID of the message advertising users to react with thumbs up')
|
2016-04-05 00:27:37 +02:00
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2016-04-05 00:27:37 +02:00
|
|
|
logging.info("Starting UserMessage batch thread %s" % (options['thread'],))
|
|
|
|
files = set(glob.glob(os.path.join(options['path'], 'messages-*.json.partial')))
|
|
|
|
for partial_path in files:
|
|
|
|
locked_path = partial_path.replace(".json.partial", ".json.locked")
|
|
|
|
output_path = partial_path.replace(".json.partial", ".json")
|
|
|
|
try:
|
|
|
|
shutil.move(partial_path, locked_path)
|
|
|
|
except Exception:
|
|
|
|
# Already claimed by another process
|
|
|
|
continue
|
|
|
|
logging.info("Thread %s processing %s" % (options['thread'], output_path))
|
|
|
|
try:
|
2019-05-10 14:28:38 +02:00
|
|
|
export_usermessages_batch(locked_path, output_path, options["consent_message_id"])
|
2016-04-05 00:27:37 +02:00
|
|
|
except Exception:
|
|
|
|
# Put the item back in the free pool when we fail
|
|
|
|
shutil.move(locked_path, partial_path)
|
|
|
|
raise
|