2017-12-08 11:28:48 +01:00
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
from typing import Any
|
|
|
|
|
2020-06-21 13:18:08 +02:00
|
|
|
from django.conf import settings
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.core.management.base import BaseCommand, CommandError, CommandParser
|
2023-10-12 19:43:45 +02:00
|
|
|
from typing_extensions import override
|
2017-12-08 11:28:48 +01:00
|
|
|
|
2018-08-01 00:18:04 +02:00
|
|
|
from zerver.data_import.slack import do_convert_data
|
2017-12-08 11:28:48 +01:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2017-12-08 11:28:48 +01:00
|
|
|
class Command(BaseCommand):
|
|
|
|
help = """Convert the Slack data into Zulip data format."""
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2017-12-08 11:28:48 +01:00
|
|
|
def add_arguments(self, parser: CommandParser) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument(
|
2021-05-28 03:16:04 +02:00
|
|
|
"slack_data_path",
|
|
|
|
nargs="+",
|
|
|
|
metavar="<Slack data path>",
|
|
|
|
help="Zipped Slack data or directory",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
2023-06-21 19:46:27 +02:00
|
|
|
"--token", metavar="<slack_token>", help="Bot user OAuth token, starting xoxb-"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"--output", dest="output_dir", help="Directory to write exported data to."
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"--threads",
|
2021-02-12 08:19:30 +01:00
|
|
|
default=settings.DEFAULT_DATA_EXPORT_IMPORT_PARALLELISM,
|
2021-02-12 08:20:45 +01:00
|
|
|
help="Threads to use in exporting UserMessage objects in parallel",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2018-04-15 16:21:02 +02:00
|
|
|
|
2021-08-13 05:48:16 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"--no-convert-slack-threads",
|
|
|
|
action="store_true",
|
|
|
|
help="If specified, do not convert Slack threads to separate Zulip topics",
|
|
|
|
)
|
|
|
|
|
2017-12-08 11:28:48 +01:00
|
|
|
parser.formatter_class = argparse.RawTextHelpFormatter
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2017-12-08 11:28:48 +01:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
|
|
|
output_dir = options["output_dir"]
|
|
|
|
if output_dir is None:
|
2019-01-15 03:02:06 +01:00
|
|
|
output_dir = tempfile.mkdtemp(prefix="converted-slack-data-")
|
2017-12-08 11:28:48 +01:00
|
|
|
else:
|
|
|
|
output_dir = os.path.realpath(output_dir)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
token = options["token"]
|
2018-02-01 00:56:57 +01:00
|
|
|
if token is None:
|
2020-10-23 02:43:28 +02:00
|
|
|
raise CommandError("Enter Slack legacy token!")
|
2018-02-01 00:56:57 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
num_threads = int(options["threads"])
|
2018-04-15 16:21:02 +02:00
|
|
|
if num_threads < 1:
|
2021-02-12 08:20:45 +01:00
|
|
|
raise CommandError("You must have at least one thread.")
|
2018-04-15 16:21:02 +02:00
|
|
|
|
2021-05-28 03:16:04 +02:00
|
|
|
for path in options["slack_data_path"]:
|
2017-12-08 11:28:48 +01:00
|
|
|
if not os.path.exists(path):
|
2020-06-10 06:41:04 +02:00
|
|
|
raise CommandError(f"Slack data directory not found: '{path}'")
|
2017-12-08 11:28:48 +01:00
|
|
|
|
2020-10-23 02:43:28 +02:00
|
|
|
print("Converting data ...")
|
2021-08-13 05:48:16 +02:00
|
|
|
convert_slack_threads = not options["no_convert_slack_threads"]
|
|
|
|
do_convert_data(
|
|
|
|
path,
|
|
|
|
output_dir,
|
|
|
|
token,
|
|
|
|
threads=num_threads,
|
|
|
|
convert_slack_threads=convert_slack_threads,
|
|
|
|
)
|