2016-04-05 00:27:37 +02:00
|
|
|
import glob
|
|
|
|
import logging
|
|
|
|
import os
|
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
|
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2016-04-05 00:27:37 +02:00
|
|
|
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:
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument('--path', help='Path to find messages.json archives')
|
|
|
|
parser.add_argument('--thread', help='Thread ID')
|
|
|
|
parser.add_argument(
|
|
|
|
'--consent-message-id',
|
|
|
|
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:
|
2020-05-02 08:44:14 +02:00
|
|
|
logging.info("Starting UserMessage batch thread %s", options['thread'])
|
2016-04-05 00:27:37 +02:00
|
|
|
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:
|
2020-10-09 04:06:13 +02:00
|
|
|
os.rename(partial_path, locked_path)
|
|
|
|
except FileNotFoundError:
|
2016-04-05 00:27:37 +02:00
|
|
|
# Already claimed by another process
|
|
|
|
continue
|
2020-05-02 08:44:14 +02:00
|
|
|
logging.info("Thread %s processing %s", options['thread'], output_path)
|
2016-04-05 00:27:37 +02:00
|
|
|
try:
|
2019-05-10 14:28:38 +02:00
|
|
|
export_usermessages_batch(locked_path, output_path, options["consent_message_id"])
|
2020-10-09 03:00:21 +02:00
|
|
|
except BaseException:
|
2016-04-05 00:27:37 +02:00
|
|
|
# Put the item back in the free pool when we fail
|
2020-10-09 04:06:13 +02:00
|
|
|
os.rename(locked_path, partial_path)
|
2016-04-05 00:27:37 +02:00
|
|
|
raise
|