2017-12-08 11:28:48 +01:00
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
from typing import Any
|
|
|
|
|
2018-04-15 16:21:02 +02:00
|
|
|
from django.core.management.base import BaseCommand, CommandParser, CommandError
|
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
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = """Convert the Slack data into Zulip data format."""
|
|
|
|
|
|
|
|
def add_arguments(self, parser: CommandParser) -> None:
|
|
|
|
parser.add_argument('slack_data_zip', nargs='+',
|
|
|
|
metavar='<slack data zip>',
|
|
|
|
help="Zipped slack data")
|
|
|
|
|
2018-02-01 00:56:57 +01:00
|
|
|
parser.add_argument('--token', metavar='<slack_token>',
|
|
|
|
type=str, help='Slack legacy token of the organsation')
|
|
|
|
|
2017-12-08 11:28:48 +01:00
|
|
|
parser.add_argument('--output', dest='output_dir',
|
|
|
|
action="store", default=None,
|
|
|
|
help='Directory to write exported data to.')
|
2018-04-15 16:21:02 +02:00
|
|
|
|
|
|
|
parser.add_argument('--threads',
|
|
|
|
dest='threads',
|
|
|
|
action="store",
|
|
|
|
default=6,
|
|
|
|
help='Threads to use in exporting UserMessage objects in parallel')
|
|
|
|
|
2017-12-08 11:28:48 +01: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-slack-data-")
|
2017-12-08 11:28:48 +01:00
|
|
|
else:
|
|
|
|
output_dir = os.path.realpath(output_dir)
|
|
|
|
|
2018-02-01 00:56:57 +01:00
|
|
|
token = options['token']
|
|
|
|
if token is None:
|
|
|
|
print("Enter slack legacy token!")
|
|
|
|
exit(1)
|
|
|
|
|
2018-04-15 16:21:02 +02:00
|
|
|
num_threads = int(options['threads'])
|
|
|
|
if num_threads < 1:
|
|
|
|
raise CommandError('You must have at least one thread.')
|
|
|
|
|
2017-12-08 11:28:48 +01:00
|
|
|
for path in options['slack_data_zip']:
|
|
|
|
if not os.path.exists(path):
|
|
|
|
print("Slack data directory not found: '%s'" % (path,))
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
print("Converting Data ...")
|
2018-04-15 16:21:02 +02:00
|
|
|
do_convert_data(path, output_dir, token, threads=num_threads)
|