2018-05-30 10:33:01 +02: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
|
2018-05-30 10:33:01 +02:00
|
|
|
|
2018-08-01 00:27:06 +02:00
|
|
|
from zerver.data_import.gitter import do_convert_data
|
2018-05-30 10:33:01 +02:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2018-05-30 10:33:01 +02:00
|
|
|
class Command(BaseCommand):
|
|
|
|
help = """Convert the Gitter data into Zulip data format."""
|
|
|
|
|
|
|
|
def add_arguments(self, parser: CommandParser) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument(
|
|
|
|
'gitter_data', nargs='+', metavar='<gitter data>', help="Gitter data in json format"
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
'--output', dest='output_dir', help='Directory to write exported data to.'
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
'--threads',
|
|
|
|
default=settings.DEFAULT_DATA_EXPORT_IMPORT_PARALLELISM,
|
|
|
|
help='Threads to download avatars and attachments faster',
|
|
|
|
)
|
2018-05-30 10:33:01 +02:00
|
|
|
|
|
|
|
parser.formatter_class = argparse.RawTextHelpFormatter
|
|
|
|
|
|
|
|
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-gitter-data-")
|
2018-05-30 10:33:01 +02:00
|
|
|
else:
|
|
|
|
output_dir = os.path.realpath(output_dir)
|
|
|
|
|
|
|
|
num_threads = int(options['threads'])
|
|
|
|
if num_threads < 1:
|
|
|
|
raise CommandError('You must have at least one thread.')
|
|
|
|
|
|
|
|
for path in options['gitter_data']:
|
|
|
|
if not os.path.exists(path):
|
2020-06-10 06:41:04 +02:00
|
|
|
raise CommandError(f"Gitter data file not found: '{path}'")
|
2018-05-30 10:33:01 +02:00
|
|
|
# TODO add json check
|
2020-10-23 02:43:28 +02:00
|
|
|
print("Converting data ...")
|
2018-05-30 10:33:01 +02:00
|
|
|
do_convert_data(path, output_dir, num_threads)
|