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
|
|
|
|
|
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
|
2024-05-24 16:49:56 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2016-04-05 00:27:37 +02:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2024-05-24 16:49:56 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
2016-04-05 00:27:37 +02:00
|
|
|
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(
|
2024-09-24 06:45:34 +02:00
|
|
|
"--export-full-with-consent",
|
|
|
|
action="store_true",
|
|
|
|
help="Whether to export private data of users who consented",
|
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:
|
2024-09-24 06:45:34 +02:00
|
|
|
export_usermessages_batch(
|
|
|
|
locked_path, output_path, options["export_full_with_consent"]
|
|
|
|
)
|
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
|