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
|
2023-10-12 19:43:45 +02:00
|
|
|
from typing_extensions import override
|
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"""
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
parser.add_argument("--path", help="Path to find messages.json archives")
|
|
|
|
parser.add_argument("--thread", help="Thread ID")
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"--consent-message-id",
|
2021-02-12 08:19:30 +01:00
|
|
|
type=int,
|
2021-02-12 08:20:45 +01:00
|
|
|
help="ID of the message advertising users to react with thumbs up",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2016-04-05 00:27:37 +02:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
logging.info("Starting UserMessage batch thread %s", options["thread"])
|
|
|
|
files = set(glob.glob(os.path.join(options["path"], "messages-*.json.partial")))
|
2016-04-05 00:27:37 +02:00
|
|
|
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
|
2021-02-12 08:20:45 +01: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
|