2019-04-04 13:16:02 +02:00
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
from typing import Any
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
from typing_extensions import override
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
"""
|
2019-04-04 13:16:02 +02:00
|
|
|
Example usage for testing purposes. For testing data see the mattermost_fixtures
|
|
|
|
in zerver/tests/.
|
|
|
|
|
|
|
|
./manage.py convert_mattermost_data mattermost_fixtures --output mm_export
|
|
|
|
./manage.py import --destroy-rebuild-database mattermost mm_export/gryffindor
|
|
|
|
|
|
|
|
Test out the realm:
|
2023-03-04 02:17:54 +01:00
|
|
|
./tools/run-dev
|
2019-04-04 13:16:02 +02:00
|
|
|
go to browser and use your dev url
|
2021-02-12 08:20:45 +01:00
|
|
|
"""
|
2019-04-04 13:16:02 +02:00
|
|
|
|
2024-05-24 16:49:56 +02:00
|
|
|
from django.core.management.base import CommandError, CommandParser
|
2019-04-04 13:16:02 +02:00
|
|
|
|
|
|
|
from zerver.data_import.mattermost import do_convert_data
|
2024-05-24 16:49:56 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2019-04-04 13:16:02 +02:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2024-05-24 16:49:56 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
2019-04-04 13:16:02 +02:00
|
|
|
help = """Convert the mattermost data into Zulip data format."""
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2019-04-04 13:16:02 +02:00
|
|
|
def add_arguments(self, parser: CommandParser) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
dir_help = (
|
|
|
|
"Directory containing exported JSON file and exported_emoji (optional) directory."
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"mattermost_data_dir", metavar="<mattermost data directory>", help=dir_help
|
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 converted data to."
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"--mask",
|
|
|
|
dest="masking_content",
|
2021-02-12 08:19:30 +01:00
|
|
|
action="store_true",
|
2021-02-12 08:20:45 +01:00
|
|
|
help="Mask the content for privacy during QA.",
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-04-04 13:16:02 +02:00
|
|
|
|
|
|
|
parser.formatter_class = argparse.RawTextHelpFormatter
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2019-04-04 13:16:02 +02:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
|
|
|
output_dir = options["output_dir"]
|
|
|
|
if output_dir is None:
|
2019-05-02 20:13:41 +02:00
|
|
|
raise CommandError("You need to specify --output <output directory>")
|
2019-04-04 13:16:02 +02:00
|
|
|
|
|
|
|
if os.path.exists(output_dir) and not os.path.isdir(output_dir):
|
2019-05-02 20:13:41 +02:00
|
|
|
raise CommandError(output_dir + " is not a directory")
|
2019-04-04 13:16:02 +02:00
|
|
|
|
|
|
|
os.makedirs(output_dir, exist_ok=True)
|
|
|
|
|
|
|
|
if os.listdir(output_dir):
|
2021-02-12 08:20:45 +01:00
|
|
|
raise CommandError("Output directory should be empty!")
|
2019-04-04 13:16:02 +02:00
|
|
|
output_dir = os.path.realpath(output_dir)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
data_dir = options["mattermost_data_dir"]
|
2019-04-04 13:16:02 +02:00
|
|
|
if not os.path.exists(data_dir):
|
2020-06-10 06:41:04 +02:00
|
|
|
raise CommandError(f"Directory not found: '{data_dir}'")
|
2019-04-04 13:16:02 +02:00
|
|
|
data_dir = os.path.realpath(data_dir)
|
|
|
|
|
2020-10-23 02:43:28 +02:00
|
|
|
print("Converting data ...")
|
2019-04-04 13:16:02 +02:00
|
|
|
do_convert_data(
|
|
|
|
mattermost_data_dir=data_dir,
|
|
|
|
output_dir=output_dir,
|
2021-02-12 08:20:45 +01:00
|
|
|
masking_content=options.get("masking_content", False),
|
2019-04-04 13:16:02 +02:00
|
|
|
)
|